-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_retrievers.py
More file actions
222 lines (192 loc) · 6.4 KB
/
test_retrievers.py
File metadata and controls
222 lines (192 loc) · 6.4 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import json
import logging
import os
import time
from collections.abc import Iterator
from pathlib import Path
from typing import Literal
import pytest
import urllib3
import vectorize_client as v
from vectorize_client import ApiClient
from langchain_vectorize.retrievers import VectorizeRetriever
logger = logging.getLogger(__name__)
@pytest.fixture(scope="session")
def api_token() -> str:
token = os.getenv("VECTORIZE_TOKEN")
if not token:
msg = "Please set the VECTORIZE_TOKEN environment variable"
raise ValueError(msg)
return token
@pytest.fixture(scope="session")
def org_id() -> str:
org = os.getenv("VECTORIZE_ORG")
if not org:
msg = "Please set the VECTORIZE_ORG environment variable"
raise ValueError(msg)
return org
@pytest.fixture(scope="session")
def environment() -> Literal["prod", "dev", "local", "staging"]:
env = os.getenv("VECTORIZE_ENV", "prod")
if env not in {"prod", "dev", "local", "staging"}:
msg = "Invalid VECTORIZE_ENV environment variable."
raise ValueError(msg)
return env
@pytest.fixture(scope="session")
def api_client(api_token: str, environment: str) -> Iterator[ApiClient]:
header_name = None
header_value = None
if environment == "prod":
host = "https://api.vectorize.io/v1"
elif environment == "dev":
host = "https://api-dev.vectorize.io/v1"
elif environment == "local":
host = "http://localhost:3000/api"
header_name = "x-lambda-api-key"
header_value = api_token
else:
host = "https://api-staging.vectorize.io/v1"
with v.ApiClient(
v.Configuration(host=host, access_token=api_token, debug=True),
header_name,
header_value,
) as api:
yield api
@pytest.fixture(scope="session")
def pipeline_id(api_client: v.ApiClient, org_id: str) -> Iterator[str]:
pipelines = v.PipelinesApi(api_client)
connectors_api = v.SourceConnectorsApi(api_client)
response = connectors_api.create_source_connector(
org_id,
v.CreateSourceConnectorRequest(
v.FileUpload(name="from api", type="FILE_UPLOAD")
),
)
source_connector_id = response.connector.id
logger.info("Created source connector %s", source_connector_id)
uploads_api = v.UploadsApi(api_client)
upload_response = uploads_api.start_file_upload_to_connector(
org_id,
source_connector_id,
v.StartFileUploadToConnectorRequest(
name="research.pdf",
content_type="application/pdf",
metadata=json.dumps({"created-from-api": True}),
),
)
http = urllib3.PoolManager()
this_dir = Path(__file__).parent
file_path = this_dir / "research.pdf"
with file_path.open("rb") as f:
http_response = http.request(
"PUT",
upload_response.upload_url,
body=f,
headers={
"Content-Type": "application/pdf",
"Content-Length": str(file_path.stat().st_size),
},
)
if http_response.status != 200:
msg = "Upload failed:"
raise ValueError(msg)
else:
logger.info("Upload successful")
ai_platforms = v.AIPlatformConnectorsApi(api_client).get_ai_platform_connectors(
org_id
)
builtin_ai_platform = next(
c.id for c in ai_platforms.ai_platform_connectors if c.type == "VECTORIZE"
)
logger.info("Using AI platform %s", builtin_ai_platform)
vector_databases = v.DestinationConnectorsApi(
api_client
).get_destination_connectors(org_id)
builtin_vector_db = next(
c.id for c in vector_databases.destination_connectors if c.type == "VECTORIZE"
)
logger.info("Using destination connector %s", builtin_vector_db)
pipeline_response = pipelines.create_pipeline(
org_id,
v.PipelineConfigurationSchema(
source_connectors=[
v.PipelineSourceConnectorSchema(
id=source_connector_id,
type=v.SourceConnectorType.FILE_UPLOAD,
config={},
)
],
destination_connector=v.PipelineDestinationConnectorSchema(
id=builtin_vector_db,
type="VECTORIZE",
config={},
),
ai_platform_connector=v.PipelineAIPlatformConnectorSchema(
id=builtin_ai_platform,
type="VECTORIZE",
config={},
),
pipeline_name="Test pipeline",
schedule=v.ScheduleSchema(type="manual"),
),
)
pipeline_id = pipeline_response.data.id
logger.info("Created pipeline %s", pipeline_id)
yield pipeline_id
try:
pipelines.delete_pipeline(org_id, pipeline_id)
except Exception:
logger.exception("Failed to delete pipeline %s", pipeline_id)
def test_retrieve_init_args(
environment: Literal["prod", "dev", "local", "staging"],
api_token: str,
org_id: str,
pipeline_id: str,
) -> None:
retriever = VectorizeRetriever(
environment=environment,
api_token=api_token,
organization=org_id,
pipeline_id=pipeline_id,
num_results=2,
)
start = time.time()
while True:
try:
docs = retriever.invoke(input="What are you?")
if len(docs) == 2:
break
except Exception as e:
if "503" in str(e):
continue
raise RuntimeError(e) from e
if time.time() - start > 180:
msg = "Docs not retrieved in time"
raise RuntimeError(msg)
time.sleep(1)
def test_retrieve_invoke_args(
environment: Literal["prod", "dev", "local", "staging"],
api_token: str,
org_id: str,
pipeline_id: str,
) -> None:
retriever = VectorizeRetriever(environment=environment, api_token=api_token)
start = time.time()
while True:
try:
docs = retriever.invoke(
input="What are you?",
organization=org_id,
pipeline_id=pipeline_id,
num_results=2,
)
if len(docs) == 2:
break
except Exception as e:
if "503" in str(e):
continue
raise RuntimeError(e) from e
if time.time() - start > 180:
msg = "Docs not retrieved in time"
raise RuntimeError(msg)
time.sleep(1)