Skip to content

Commit 89fcebc

Browse files
committed
Add langchain standard integration tests
1 parent 667cdfb commit 89fcebc

File tree

5 files changed

+1103
-189
lines changed

5 files changed

+1103
-189
lines changed

langchain/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dev = [
3434
"mypy>=1.13.0",
3535
"pytest>=8.3.3",
3636
"ruff>=0.9.0,<0.10",
37+
"langchain-tests>=0.3.20",
3738
]
3839

3940
[tool.ruff.lint]

langchain/tests/conftest.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import json
2+
import logging
3+
import os
4+
import time
5+
from collections.abc import Iterator
6+
from pathlib import Path
7+
from typing import Literal
8+
9+
import pytest
10+
import urllib3
11+
import vectorize_client as v
12+
from vectorize_client import ApiClient, RetrieveDocumentsRequest
13+
14+
15+
@pytest.fixture(scope="session")
16+
def api_token() -> str:
17+
token = os.getenv("VECTORIZE_TOKEN")
18+
if not token:
19+
msg = "Please set the VECTORIZE_TOKEN environment variable"
20+
raise ValueError(msg)
21+
return token
22+
23+
24+
@pytest.fixture(scope="session")
25+
def org_id() -> str:
26+
org = os.getenv("VECTORIZE_ORG")
27+
if not org:
28+
msg = "Please set the VECTORIZE_ORG environment variable"
29+
raise ValueError(msg)
30+
return org
31+
32+
33+
@pytest.fixture(scope="session")
34+
def environment() -> Literal["prod", "dev", "local", "staging"]:
35+
env = os.getenv("VECTORIZE_ENV", "prod")
36+
if env not in ["prod", "dev", "local", "staging"]:
37+
msg = "Invalid VECTORIZE_ENV environment variable."
38+
raise ValueError(msg)
39+
return env
40+
41+
42+
@pytest.fixture(scope="session")
43+
def api_client(api_token: str, environment: str) -> Iterator[ApiClient]:
44+
header_name = None
45+
header_value = None
46+
if environment == "prod":
47+
host = "https://api.vectorize.io/v1"
48+
elif environment == "dev":
49+
host = "https://api-dev.vectorize.io/v1"
50+
elif environment == "local":
51+
host = "http://localhost:3000/api"
52+
header_name = "x-lambda-api-key"
53+
header_value = api_token
54+
else:
55+
host = "https://api-staging.vectorize.io/v1"
56+
57+
with v.ApiClient(
58+
v.Configuration(host=host, access_token=api_token, debug=True),
59+
header_name,
60+
header_value,
61+
) as api:
62+
yield api
63+
64+
65+
@pytest.fixture(scope="session")
66+
def pipeline_id(api_client: v.ApiClient, org_id: str) -> Iterator[str]:
67+
pipelines = v.PipelinesApi(api_client)
68+
69+
connectors_api = v.ConnectorsApi(api_client)
70+
response = connectors_api.create_source_connector(
71+
org_id,
72+
[
73+
v.CreateSourceConnector(
74+
name="from api", type=v.SourceConnectorType.FILE_UPLOAD
75+
)
76+
],
77+
)
78+
source_connector_id = response.connectors[0].id
79+
logging.info("Created source connector %s", source_connector_id)
80+
81+
uploads_api = v.UploadsApi(api_client)
82+
upload_response = uploads_api.start_file_upload_to_connector(
83+
org_id,
84+
source_connector_id,
85+
v.StartFileUploadToConnectorRequest(
86+
name="research.pdf",
87+
content_type="application/pdf",
88+
metadata=json.dumps({"created-from-api": True}),
89+
),
90+
)
91+
92+
http = urllib3.PoolManager()
93+
this_dir = Path(__file__).parent
94+
file_path = this_dir / "research.pdf"
95+
96+
with file_path.open("rb") as f:
97+
http_response = http.request(
98+
"PUT",
99+
upload_response.upload_url,
100+
body=f,
101+
headers={
102+
"Content-Type": "application/pdf",
103+
"Content-Length": str(file_path.stat().st_size),
104+
},
105+
)
106+
if http_response.status != 200:
107+
msg = "Upload failed:"
108+
raise ValueError(msg)
109+
else:
110+
logging.info("Upload successful")
111+
112+
ai_platforms = connectors_api.get_ai_platform_connectors(org_id)
113+
builtin_ai_platform = next(
114+
c.id for c in ai_platforms.ai_platform_connectors if c.type == "VECTORIZE"
115+
)
116+
logging.info("Using AI platform %s", builtin_ai_platform)
117+
118+
vector_databases = connectors_api.get_destination_connectors(org_id)
119+
builtin_vector_db = next(
120+
c.id for c in vector_databases.destination_connectors if c.type == "VECTORIZE"
121+
)
122+
logging.info("Using destination connector %s", builtin_vector_db)
123+
124+
pipeline_response = pipelines.create_pipeline(
125+
org_id,
126+
v.PipelineConfigurationSchema(
127+
source_connectors=[
128+
v.SourceConnectorSchema(
129+
id=source_connector_id,
130+
type=v.SourceConnectorType.FILE_UPLOAD,
131+
config={},
132+
)
133+
],
134+
destination_connector=v.DestinationConnectorSchema(
135+
id=builtin_vector_db,
136+
type=v.DestinationConnectorType.VECTORIZE,
137+
config={},
138+
),
139+
ai_platform=v.AIPlatformSchema(
140+
id=builtin_ai_platform,
141+
type=v.AIPlatformType.VECTORIZE,
142+
config=v.AIPlatformConfigSchema(),
143+
),
144+
pipeline_name="Test pipeline",
145+
schedule=v.ScheduleSchema(type=v.ScheduleSchemaType.MANUAL),
146+
),
147+
)
148+
pipeline_id = pipeline_response.data.id
149+
logging.info("Created pipeline %s", pipeline_id)
150+
151+
# Wait for the pipeline to be created
152+
request = RetrieveDocumentsRequest(
153+
question="query",
154+
num_results=2,
155+
)
156+
start = time.time()
157+
while True:
158+
response = pipelines.retrieve_documents(org_id, pipeline_id, request)
159+
docs = response.documents
160+
if len(docs) == 2:
161+
break
162+
if time.formtime() - start > 180:
163+
msg = "Docs not retrieved in time"
164+
raise RuntimeError(msg)
165+
time.sleep(1)
166+
167+
yield pipeline_id
168+
169+
try:
170+
pipelines.delete_pipeline(org_id, pipeline_id)
171+
except Exception:
172+
logging.exception("Failed to delete pipeline %s", pipeline_id)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Literal
2+
3+
import pytest
4+
from langchain_tests.integration_tests import RetrieversIntegrationTests
5+
6+
from langchain_vectorize import VectorizeRetriever
7+
8+
9+
class TestVectorizeRetrieverIntegration(RetrieversIntegrationTests):
10+
@classmethod
11+
@pytest.fixture(autouse=True, scope="class")
12+
def setup(
13+
cls,
14+
environment: Literal["prod", "dev", "local", "staging"],
15+
api_token: str,
16+
org_id: str,
17+
pipeline_id: str,
18+
) -> None:
19+
cls.environment = environment
20+
cls.api_token = api_token
21+
cls.org_id = org_id
22+
cls.pipeline_id = pipeline_id
23+
24+
@property
25+
def retriever_constructor(self) -> type[VectorizeRetriever]:
26+
return VectorizeRetriever
27+
28+
@property
29+
def retriever_constructor_params(self) -> dict:
30+
return {
31+
"environment": self.environment,
32+
"api_token": self.api_token,
33+
"organization": self.org_id,
34+
"pipeline_id": self.pipeline_id,
35+
}
36+
37+
@property
38+
def retriever_query_example(self) -> str:
39+
return "What are you?"
40+
41+
@pytest.mark.xfail(reason="VectorizeRetriever does not support k parameter in constructor")
42+
def test_k_constructor_param(self) -> None:
43+
raise NotImplementedError
44+
45+
@pytest.mark.xfail(reason="VectorizeRetriever does not support k parameter in invoke")
46+
def test_invoke_with_k_kwarg(self) -> None:
47+
raise NotImplementedError

0 commit comments

Comments
 (0)