forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_test.py
More file actions
123 lines (88 loc) · 4.09 KB
/
Copy pathrag_test.py
File metadata and controls
123 lines (88 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# flake8: noqa ANN001, ANN201
import os
from pathlib import Path
import pytest
import rag
import vertexai
# TODO(https://github.com/GoogleCloudPlatform/python-docs-samples/issues/11557): Remove once Allowlist is removed
pytest.skip(allow_module_level=True)
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
LOCATION = "us-central1"
GCS_FILE = "gs://cloud-samples-data/generative-ai/pdf/earnings_statement.pdf"
vertexai.init(project=PROJECT_ID, location=LOCATION)
@pytest.fixture(scope="module", name="test_file")
def test_file_fixture():
file_path = Path("./hello.txt")
file_path.write_text("Hello World", encoding="utf-8")
yield file_path.absolute().as_posix()
file_path.unlink() # Delete the file after tests
@pytest.fixture(scope="module", name="test_corpus")
def test_corpus_fixture():
"""Creates a corpus for testing and deletes it after tests are complete."""
corpus = rag.create_corpus(PROJECT_ID, "test_corpus")
yield corpus
rag.delete_corpus(PROJECT_ID, corpus.name)
@pytest.fixture(scope="module", name="uploaded_file")
def uploaded_file_fixture(test_corpus, test_file):
"""Uploads a file to the corpus and deletes it after the test."""
rag_file = rag.upload_file(PROJECT_ID, test_corpus.name, test_file)
# TODO: Remove when upload_file service response changes to the correct format.
rag_file.name = rag_file.name.replace("RagFile", "ragFiles")
yield rag_file
def test_create_corpus():
corpus = rag.create_corpus(PROJECT_ID, "test_create_corpus")
assert corpus.display_name == "test_create_corpus"
rag.delete_corpus(PROJECT_ID, corpus.name)
def test_get_corpus(test_corpus):
retrieved_corpus = rag.get_corpus(PROJECT_ID, test_corpus.name)
assert retrieved_corpus.name == test_corpus.name
def test_list_corpora(test_corpus):
corpora = rag.list_corpora(PROJECT_ID)
assert any(c.name == test_corpus.name for c in corpora)
def test_upload_file(test_corpus, test_file):
rag_file = rag.upload_file(PROJECT_ID, test_corpus.name, test_file)
assert rag_file
# TODO: Remove when upload_file service response changes to the correct format.
rag_file.name = rag_file.name.replace("RagFile", "ragFiles")
rag.delete_file(PROJECT_ID, rag_file.name)
def test_import_files(test_corpus):
response = rag.import_files(PROJECT_ID, test_corpus.name, [GCS_FILE])
assert response.imported_rag_files_count > 0
rag.delete_file(PROJECT_ID, response.imported_rag_files[0].name)
@pytest.mark.asyncio
async def test_import_files_async(test_corpus):
result = await rag.import_files_async(PROJECT_ID, test_corpus.name, [GCS_FILE])
assert result.imported_rag_files_count > 0
rag.delete_file(PROJECT_ID, result.imported_rag_files[0].name)
def test_get_file(uploaded_file):
retrieved_file = rag.get_file(PROJECT_ID, uploaded_file.name)
assert retrieved_file.name == uploaded_file.name
def test_list_files(test_corpus, uploaded_file):
files = rag.list_files(PROJECT_ID, test_corpus.name)
assert any(f.name == uploaded_file.name for f in files)
def test_retrieval_query(test_corpus):
response = rag.retrieval_query(PROJECT_ID, test_corpus.name)
assert response
assert response.contexts
def test_generate_content_with_rag(test_corpus):
response = rag.generate_content_with_rag(PROJECT_ID, test_corpus.name)
assert response
assert response.text
def test_quickstart():
corpus, response = rag.quickstart(PROJECT_ID, "test_corpus_quickstart", [GCS_FILE])
assert response
assert response.text
rag.delete_corpus(PROJECT_ID, corpus.name)