-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_server_openai.py
44 lines (34 loc) · 1.14 KB
/
test_server_openai.py
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
import pytest
from openai import OpenAI
from fastapi.testclient import TestClient
from server_openai import app
test_api = TestClient(app)
@pytest.fixture
def openai_client():
return OpenAI(
base_url="http://testserver/my-custom-path/openai/v1",
http_client=test_api,
)
def test_chat_completion_invoke(openai_client):
chat_completion = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": 'Say "This is a test"',
}
],
)
assert "This is a test" in chat_completion.choices[0].message.content
def test_chat_completion_stream(openai_client):
chunks = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": 'Say "This is a test"'}],
stream=True,
)
every_content = []
for chunk in chunks:
if chunk.choices and isinstance(chunk.choices[0].delta.content, str):
every_content.append(chunk.choices[0].delta.content)
stream_output = "".join(every_content)
assert "This is a test" in stream_output