Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add async test #115

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/integrations/openai_tracker/test_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import time
import uuid

import pytest

from greptimeai.openai_patcher import _collector

from ..database.db import get_trace_data, truncate_tables
from ..openai_tracker import async_client


@pytest.fixture
def _truncate_tables():
truncate_tables()
yield


@pytest.mark.asyncio
async def test_chat_completion(_truncate_tables):
user_id = str(uuid.uuid4())
model = "gpt-3.5-turbo"
resp = await async_client.chat.completions.create(
messages=[
{
"role": "user",
"content": "1+1=",
}
],
model=model,
user=user_id,
seed=1,
)
assert resp.choices[0].message.content == "2"

_collector._collector._force_flush()

trace = get_trace_data(user_id)
retry = 0
while retry < 3 and not trace:
retry += 1
time.sleep(2)
trace = get_trace_data(user_id)

assert trace is not None

assert "openai" == trace.get("resource_attributes", {}).get("service.name")
assert "openai_completion" == trace.get("span_name")

assert ["client.chat.completions.create[async]", "end"] == [
event.get("name") for event in trace.get("span_events", [])
]

assert resp.model == trace.get("model")
assert resp.model.startswith(model)

assert resp.usage
assert resp.usage.prompt_tokens == trace.get("prompt_tokens")
assert resp.usage.completion_tokens == trace.get("completion_tokens")
Loading