@@ -53,9 +53,16 @@ def mock_session_env(monkeypatch_session):
53
53
monkeypatch_session .setenv ("RUNNING_IN_PRODUCTION" , "False" )
54
54
# Azure Subscription
55
55
monkeypatch_session .setenv ("AZURE_SUBSCRIPTION_ID" , "test-storage-subid" )
56
- # OpenAI
57
- monkeypatch_session .setenv ("AZURE_OPENAI_CHATGPT_MODEL" , "gpt-35-turbo" )
58
- monkeypatch_session .setenv ("OPENAI_API_KEY" , "fakekey" )
56
+ # Azure OpenAI
57
+ monkeypatch_session .setenv ("OPENAI_CHAT_HOST" , "azure" )
58
+ monkeypatch_session .setenv ("OPENAI_EMBED_HOST" , "azure" )
59
+ monkeypatch_session .setenv ("AZURE_OPENAI_VERSION" , "2024-03-01-preview" )
60
+ monkeypatch_session .setenv ("AZURE_OPENAI_CHAT_DEPLOYMENT" , "gpt-35-turbo" )
61
+ monkeypatch_session .setenv ("AZURE_OPENAI_CHAT_MODEL" , "gpt-35-turbo" )
62
+ monkeypatch_session .setenv ("AZURE_OPENAI_EMBED_DEPLOYMENT" , "text-embedding-ada-002" )
63
+ monkeypatch_session .setenv ("AZURE_OPENAI_EMBED_MODEL" , "text-embedding-ada-002" )
64
+ monkeypatch_session .setenv ("AZURE_OPENAI_EMBED_MODEL_DIMENSIONS" , "1536" )
65
+ monkeypatch_session .setenv ("AZURE_OPENAI_KEY" , "fakekey" )
59
66
# Allowed Origin
60
67
monkeypatch_session .setenv ("ALLOWED_ORIGIN" , "https://frontend.com" )
61
68
@@ -82,16 +89,8 @@ async def app(mock_session_env):
82
89
return app
83
90
84
91
85
- @pytest .fixture (scope = "function" )
86
- def mock_default_azure_credential (mock_session_env ):
87
- """Mock the Azure credential for testing."""
88
- with mock .patch ("azure.identity.DefaultAzureCredential" ) as mock_default_azure_credential :
89
- mock_default_azure_credential .return_value = MockAzureCredential ()
90
- yield mock_default_azure_credential
91
-
92
-
93
- @pytest .fixture (autouse = True )
94
- def mock_openai_embedding (monkeypatch ):
92
+ @pytest .fixture (scope = "session" )
93
+ def mock_openai_embedding (monkeypatch_session ):
95
94
async def mock_acreate (* args , ** kwargs ):
96
95
return CreateEmbeddingResponse (
97
96
object = "list" ,
@@ -106,14 +105,13 @@ async def mock_acreate(*args, **kwargs):
106
105
usage = Usage (prompt_tokens = 8 , total_tokens = 8 ),
107
106
)
108
107
109
- def patch ():
110
- monkeypatch .setattr (openai .resources .AsyncEmbeddings , "create" , mock_acreate )
108
+ monkeypatch_session .setattr (openai .resources .AsyncEmbeddings , "create" , mock_acreate )
111
109
112
- return patch
110
+ yield
113
111
114
112
115
- @pytest .fixture
116
- def mock_openai_chatcompletion (monkeypatch ):
113
+ @pytest .fixture ( scope = "session" )
114
+ def mock_openai_chatcompletion (monkeypatch_session ):
117
115
class AsyncChatCompletionIterator :
118
116
def __init__ (self , answer : str ):
119
117
chunk_id = "test-id"
@@ -215,19 +213,22 @@ async def mock_acreate(*args, **kwargs):
215
213
model = "test-model" ,
216
214
)
217
215
218
- def patch ():
219
- monkeypatch .setattr (openai .resources .chat .completions .AsyncCompletions , "create" , mock_acreate )
216
+ monkeypatch_session .setattr (openai .resources .chat .completions .AsyncCompletions , "create" , mock_acreate )
220
217
221
- return patch
218
+ yield
219
+
220
+
221
+ @pytest .fixture (scope = "function" )
222
+ def mock_default_azure_credential (mock_session_env ):
223
+ """Mock the Azure credential for testing."""
224
+ with mock .patch ("azure.identity.DefaultAzureCredential" ) as mock_default_azure_credential :
225
+ mock_default_azure_credential .return_value = MockAzureCredential ()
226
+ yield
222
227
223
228
224
229
@pytest_asyncio .fixture (scope = "function" )
225
- async def test_client (
226
- monkeypatch , app , mock_default_azure_credential , mock_openai_embedding , mock_openai_chatcompletion
227
- ):
230
+ async def test_client (app , mock_default_azure_credential , mock_openai_embedding , mock_openai_chatcompletion ):
228
231
"""Create a test client."""
229
- mock_openai_embedding ()
230
- mock_openai_chatcompletion ()
231
232
with TestClient (app ) as test_client :
232
233
yield test_client
233
234
0 commit comments