Skip to content

Commit 9f747a4

Browse files
committed
Add test cases for openai (only run with token set)
1 parent 4074ec1 commit 9f747a4

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

aikido_zen/sinks/tests/openai_test.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import os
2+
3+
import pytest
4+
import aikido_zen.sinks.openai
5+
6+
from aikido_zen.thread.thread_cache import get_cache
7+
8+
skip_no_api_key = pytest.mark.skipif(
9+
"OPENAI_API_KEY" not in os.environ,
10+
reason="OPENAI_API_KEY environment variable not set",
11+
)
12+
13+
14+
@pytest.fixture(autouse=True)
15+
def setup():
16+
get_cache().reset()
17+
yield
18+
get_cache().reset()
19+
20+
21+
@pytest.fixture
22+
def client():
23+
import openai
24+
25+
return openai.OpenAI()
26+
27+
28+
def get_ai_stats():
29+
return get_cache().ai_stats.get_stats()
30+
31+
32+
@skip_no_api_key
33+
def test_openai_responses_create_with_vision(client):
34+
prompt = "What is in this image?"
35+
img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/2023_06_08_Raccoon1.jpg/1599px-2023_06_08_Raccoon1.jpg"
36+
37+
response = client.responses.create(
38+
model="gpt-4o-mini",
39+
input=[
40+
{
41+
"role": "user",
42+
"content": [
43+
{"type": "input_text", "text": prompt},
44+
{"type": "input_image", "image_url": f"{img_url}"},
45+
],
46+
}
47+
],
48+
max_output_tokens=25,
49+
)
50+
print(response)
51+
52+
assert get_ai_stats()[0]["model"] == "gpt-4o-mini-2024-07-18"
53+
assert get_ai_stats()[0]["calls"] == 1
54+
assert get_ai_stats()[0]["provider"] == "openai"
55+
assert get_ai_stats()[0]["tokens"]["input"] == 36848
56+
assert get_ai_stats()[0]["tokens"]["output"] == 25
57+
assert get_ai_stats()[0]["tokens"]["total"] == 36873
58+
59+
60+
@skip_no_api_key
61+
def test_openai_chat_complete(client):
62+
completion = client.chat.completions.create(
63+
model="gpt-4o",
64+
max_tokens=15,
65+
messages=[
66+
{"role": "developer", "content": "Talk like a pirate."},
67+
{
68+
"role": "user",
69+
"content": "Who is the best French painter? Answer in one short sentence.",
70+
},
71+
],
72+
)
73+
answer = completion.choices[0].message.content
74+
print(answer)
75+
76+
assert get_ai_stats()[0]["model"] == "gpt-4o-2024-08-06"
77+
assert get_ai_stats()[0]["calls"] == 1
78+
assert get_ai_stats()[0]["provider"] == "openai"
79+
assert get_ai_stats()[0]["tokens"]["input"] == 29
80+
assert get_ai_stats()[0]["tokens"]["output"] == 15
81+
assert get_ai_stats()[0]["tokens"]["total"] == 44
82+
83+
84+
@skip_no_api_key
85+
def test_openai_responses_create(client):
86+
response = client.responses.create(
87+
model="gpt-4o",
88+
instructions="You are a coding assistant that talks like a pirate.",
89+
input="How do I check if a Python object is an instance of a class?",
90+
max_output_tokens=18,
91+
)
92+
print(response.output_text)
93+
94+
assert get_ai_stats()[0]["model"] == "gpt-4o-2024-08-06"
95+
assert get_ai_stats()[0]["calls"] == 1
96+
assert get_ai_stats()[0]["provider"] == "openai"
97+
assert get_ai_stats()[0]["tokens"]["input"] == 37
98+
assert get_ai_stats()[0]["tokens"]["output"] == 18
99+
assert get_ai_stats()[0]["tokens"]["total"] == 55

0 commit comments

Comments
 (0)