-
Notifications
You must be signed in to change notification settings - Fork 961
Expand file tree
/
Copy pathconftest.py
More file actions
378 lines (337 loc) · 15.5 KB
/
conftest.py
File metadata and controls
378 lines (337 loc) · 15.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import json
import os
from pathlib import Path
from unittest import mock
import openai
import openai.resources
import pytest
import pytest_asyncio
from fastapi.testclient import TestClient
from openai.types import CreateEmbeddingResponse, Embedding
from openai.types.chat import ChatCompletion, ChatCompletionChunk
from openai.types.chat.chat_completion import (
ChatCompletionMessage,
Choice,
)
from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function
from openai.types.create_embedding_response import Usage
from sqlalchemy.ext.asyncio import async_sessionmaker
from fastapi_app import create_app
from fastapi_app.openai_clients import create_openai_embed_client
from fastapi_app.postgres_engine import create_postgres_engine_from_env
from fastapi_app.setup_postgres_database import create_db_schema
from fastapi_app.setup_postgres_seeddata import seed_data
from tests.data import test_data
from tests.mocks import MockAzureCredential
# Always use localhost for testing
POSTGRES_HOST = "localhost"
POSTGRES_USERNAME = os.getenv("POSTGRES_USERNAME", "admin")
POSTGRES_DATABASE = os.getenv("POSTGRES_DATABASE", "postgres")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", "postgres")
POSTGRES_SSL = "prefer"
POSTGRESQL_DATABASE_URL = (
f"postgresql+asyncpg://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}/{POSTGRES_DATABASE}"
)
@pytest.fixture(scope="session")
def monkeypatch_session():
with pytest.MonkeyPatch.context() as monkeypatch_session:
yield monkeypatch_session
@pytest.fixture(scope="session")
def mock_session_env(monkeypatch_session):
"""Mock the environment variables for testing."""
# Note that this does *not* clear existing env variables by default-
# we used to specify clear=True but this caused issues with Playwright tests
# https://github.com/microsoft/playwright-python/issues/2506
with mock.patch.dict(os.environ):
# Database
monkeypatch_session.setenv("POSTGRES_HOST", POSTGRES_HOST)
monkeypatch_session.setenv("POSTGRES_USERNAME", POSTGRES_USERNAME)
monkeypatch_session.setenv("POSTGRES_DATABASE", POSTGRES_DATABASE)
monkeypatch_session.setenv("POSTGRES_PASSWORD", POSTGRES_PASSWORD)
monkeypatch_session.setenv("POSTGRES_SSL", POSTGRES_SSL)
monkeypatch_session.setenv("RUNNING_IN_PRODUCTION", "False")
# Azure Subscription
monkeypatch_session.setenv("AZURE_SUBSCRIPTION_ID", "test-storage-subid")
# Azure OpenAI
monkeypatch_session.setenv("OPENAI_CHAT_HOST", "azure")
monkeypatch_session.setenv("OPENAI_EMBED_HOST", "azure")
monkeypatch_session.setenv("AZURE_OPENAI_ENDPOINT", "https://api.openai.com")
monkeypatch_session.setenv("AZURE_OPENAI_CHAT_DEPLOYMENT", "gpt-4o-mini")
monkeypatch_session.setenv("AZURE_OPENAI_CHAT_MODEL", "gpt-4o-mini")
monkeypatch_session.setenv("AZURE_OPENAI_EMBED_DEPLOYMENT", "text-embedding-3-large")
monkeypatch_session.setenv("AZURE_OPENAI_EMBED_MODEL", "text-embedding-3-large")
monkeypatch_session.setenv("AZURE_OPENAI_EMBED_DIMENSIONS", "1024")
monkeypatch_session.setenv("AZURE_OPENAI_EMBEDDING_COLUMN", "embedding_3l")
monkeypatch_session.setenv("AZURE_OPENAI_KEY", "fakekey")
yield
@pytest.fixture(scope="session")
def mock_session_env_openai(monkeypatch_session):
"""Mock the environment variables for testing."""
# Note that this does *not* clear existing env variables by default-
# we used to specify clear=True but this caused issues with Playwright tests
# https://github.com/microsoft/playwright-python/issues/2506
with mock.patch.dict(os.environ):
# Database
monkeypatch_session.setenv("POSTGRES_HOST", POSTGRES_HOST)
monkeypatch_session.setenv("POSTGRES_USERNAME", POSTGRES_USERNAME)
monkeypatch_session.setenv("POSTGRES_DATABASE", POSTGRES_DATABASE)
monkeypatch_session.setenv("POSTGRES_PASSWORD", POSTGRES_PASSWORD)
monkeypatch_session.setenv("POSTGRES_SSL", POSTGRES_SSL)
monkeypatch_session.setenv("RUNNING_IN_PRODUCTION", "False")
# Azure Subscription
monkeypatch_session.setenv("AZURE_SUBSCRIPTION_ID", "test-storage-subid")
# OpenAI.com OpenAI
monkeypatch_session.setenv("OPENAI_CHAT_HOST", "openai")
monkeypatch_session.setenv("OPENAI_EMBED_HOST", "openai")
monkeypatch_session.setenv("OPENAICOM_KEY", "fakekey")
monkeypatch_session.setenv("OPENAICOM_CHAT_MODEL", "gpt-3.5-turbo")
monkeypatch_session.setenv("OPENAICOM_EMBED_MODEL", "text-embedding-3-large")
monkeypatch_session.setenv("OPENAICOM_EMBED_DIMENSIONS", "1024")
monkeypatch_session.setenv("OPENAICOM_EMBEDDING_COLUMN", "embedding_3l")
yield
@pytest.fixture(scope="session")
def mock_session_env_ollama(monkeypatch_session):
"""Mock the environment variables for testing."""
# Note that this does *not* clear existing env variables by default-
# we used to specify clear=True but this caused issues with Playwright tests
# https://github.com/microsoft/playwright-python/issues/2506
with mock.patch.dict(os.environ):
# Database
monkeypatch_session.setenv("POSTGRES_HOST", POSTGRES_HOST)
monkeypatch_session.setenv("POSTGRES_USERNAME", POSTGRES_USERNAME)
monkeypatch_session.setenv("POSTGRES_DATABASE", POSTGRES_DATABASE)
monkeypatch_session.setenv("POSTGRES_PASSWORD", POSTGRES_PASSWORD)
monkeypatch_session.setenv("POSTGRES_SSL", POSTGRES_SSL)
monkeypatch_session.setenv("RUNNING_IN_PRODUCTION", "False")
# Azure Subscription
monkeypatch_session.setenv("AZURE_SUBSCRIPTION_ID", "test-storage-subid")
# Ollama OpenAI
monkeypatch_session.setenv("OPENAI_CHAT_HOST", "ollama")
monkeypatch_session.setenv("OPENAI_EMBED_HOST", "ollama")
monkeypatch_session.setenv("OLLAMA_ENDPOINT", "http://host.docker.internal:11434/v1")
monkeypatch_session.setenv("OLLAMA_CHAT_MODEL", "llama3.1")
monkeypatch_session.setenv("OLLAMA_EMBED_MODEL", "nomic-embed-text")
monkeypatch_session.setenv("OLLAMA_EMBEDDING_COLUMN", "embedding_nomic")
yield
async def create_and_seed_db():
"""Create and seed the database."""
engine = await create_postgres_engine_from_env()
await create_db_schema(engine)
await seed_data(engine)
await engine.dispose()
@pytest_asyncio.fixture(scope="session")
async def app(mock_session_env):
"""Create a FastAPI app."""
if not Path("src/backend/static/").exists():
pytest.skip("Please generate frontend files first!")
app = create_app(testing=True)
await create_and_seed_db()
return app
@pytest.fixture(scope="session")
def mock_openai_embedding(monkeypatch_session):
async def mock_acreate(*args, **kwargs):
return CreateEmbeddingResponse(
object="list",
data=[
Embedding(
embedding=test_data.embeddings,
index=0,
object="embedding",
)
],
model="text-embedding-3-large",
usage=Usage(prompt_tokens=8, total_tokens=8),
)
monkeypatch_session.setattr(openai.resources.AsyncEmbeddings, "create", mock_acreate)
yield
@pytest.fixture(scope="session")
def mock_openai_chatcompletion(monkeypatch_session):
class AsyncChatCompletionIterator:
def __init__(self, answer: str):
chunk_id = "test-id"
model = "gpt-4o-mini"
self.responses = [
{"object": "chat.completion.chunk", "choices": [], "id": chunk_id, "model": model, "created": 1},
{
"object": "chat.completion.chunk",
"choices": [{"delta": {"role": "assistant"}, "index": 0, "finish_reason": None}],
"id": chunk_id,
"model": model,
"created": 1,
},
]
# Split at << to simulate chunked responses
if answer.find("<<") > -1:
parts = answer.split("<<")
self.responses.append(
{
"object": "chat.completion.chunk",
"choices": [
{
"delta": {"role": "assistant", "content": parts[0] + "<<"},
"index": 0,
"finish_reason": None,
}
],
"id": chunk_id,
"model": model,
"created": 1,
}
)
self.responses.append(
{
"object": "chat.completion.chunk",
"choices": [
{"delta": {"role": "assistant", "content": parts[1]}, "index": 0, "finish_reason": None}
],
"id": chunk_id,
"model": model,
"created": 1,
}
)
self.responses.append(
{
"object": "chat.completion.chunk",
"choices": [{"delta": {"role": None, "content": None}, "index": 0, "finish_reason": "stop"}],
"id": chunk_id,
"model": model,
"created": 1,
}
)
else:
self.responses.append(
{
"object": "chat.completion.chunk",
"choices": [{"delta": {"content": answer}, "index": 0, "finish_reason": None}],
"id": chunk_id,
"model": model,
"created": 1,
}
)
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
return None
def __aiter__(self):
return self
async def __anext__(self):
if self.responses:
return ChatCompletionChunk.model_validate(self.responses.pop(0))
else:
raise StopAsyncIteration
async def mock_acreate(*args, **kwargs):
messages = kwargs["messages"]
last_question = messages[-1]["content"]
last_role = messages[-1]["role"]
if last_role == "tool":
items = json.loads(last_question)["items"]
arguments = {"query": "capital of France", "items": items, "filters": []}
return ChatCompletion(
object="chat.completion",
choices=[
Choice(
message=ChatCompletionMessage(
role="assistant",
tool_calls=[
ChatCompletionMessageToolCall(
id="call_abc123final",
type="function",
function=Function(
name="final_result",
arguments=json.dumps(arguments),
),
)
],
),
finish_reason="stop",
index=0,
)
],
id="test-123final",
created=0,
model="test-model",
)
if last_question == "Find search results for user query: What is the capital of France?":
return ChatCompletion(
object="chat.completion",
choices=[
Choice(
message=ChatCompletionMessage(
role="assistant",
tool_calls=[
ChatCompletionMessageToolCall(
id="call_abc123",
type="function",
function=Function(
name="search_database", arguments='{"search_query":"climbing gear outside"}'
),
)
],
),
finish_reason="stop",
index=0,
)
],
id="test-123",
created=0,
model="test-model",
)
elif last_question == "Find search results for user query: Are interest rates high?":
answer = "interest rates"
elif isinstance(last_question, list) and last_question[2].get("image_url"):
answer = "From the provided sources, the impact of interest rates and GDP growth on "
"financial markets can be observed through the line graph. [Financial Market Analysis Report 2023-7.png]"
else:
answer = "The capital of France is Paris. [Benefit_Options-2.pdf]."
if messages[0]["content"].find("Generate 3 very brief follow-up questions") > -1:
answer = "The capital of France is Paris. [Benefit_Options-2.pdf]. <<What is the capital of Spain?>>"
if "stream" in kwargs and kwargs["stream"] is True:
return AsyncChatCompletionIterator(answer)
else:
return ChatCompletion(
object="chat.completion",
choices=[
Choice(
message=ChatCompletionMessage(role="assistant", content=answer), finish_reason="stop", index=0
)
],
id="test-123",
created=0,
model="test-model",
)
monkeypatch_session.setattr(openai.resources.chat.completions.AsyncCompletions, "create", mock_acreate)
yield
@pytest.fixture(scope="function")
def mock_azure_credential(mock_session_env):
"""Mock the Azure credential for testing."""
with mock.patch("azure.identity.AzureDeveloperCliCredential") as mock_azure_credential:
mock_azure_credential.return_value = MockAzureCredential()
yield mock_azure_credential
@pytest_asyncio.fixture(scope="function")
async def test_client(app, mock_azure_credential, mock_openai_embedding, mock_openai_chatcompletion):
"""Create a test client."""
with TestClient(app) as test_client:
yield test_client
@pytest_asyncio.fixture(scope="function")
async def db_session(mock_session_env, mock_azure_credential):
"""Create a new database session with a rollback at the end of the test."""
engine = await create_postgres_engine_from_env()
async_sesion = async_sessionmaker(autocommit=False, autoflush=False, bind=engine)
session = async_sesion()
await session.begin()
yield session
await session.rollback()
await session.close()
await engine.dispose()
@pytest_asyncio.fixture(scope="function")
async def postgres_searcher(mock_session_env, mock_azure_credential, db_session, mock_openai_embedding):
from fastapi_app.postgres_searcher import PostgresSearcher
openai_embed_client = await create_openai_embed_client(mock_azure_credential)
yield PostgresSearcher(
db_session=db_session,
openai_embed_client=openai_embed_client,
embed_deployment="text-embedding-3-large",
embed_model="text-embedding-3-large",
embed_dimensions=1024,
embedding_column="embedding_3l",
)