9
from dotenv import load_dotenv
15
async def config_update(session):
16
url = "http://0.0.0.0:4000/config/update"
17
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
20
"success_callback": ["langfuse"],
22
"environment_variables": {
23
"LANGFUSE_PUBLIC_KEY": os.environ["LANGFUSE_PUBLIC_KEY"],
24
"LANGFUSE_SECRET_KEY": os.environ["LANGFUSE_SECRET_KEY"],
28
async with session.post(url, headers=headers, json=data) as response:
29
status = response.status
30
response_text = await response.text()
36
raise Exception(f"Request did not return a 200 status code: {status}")
37
return await response.json()
40
async def chat_completion(session, key, model="azure-gpt-3.5", request_metadata=None):
41
url = "http://0.0.0.0:4000/chat/completions"
43
"Authorization": f"Bearer {key}",
44
"Content-Type": "application/json",
49
{"role": "system", "content": "You are a helpful assistant."},
50
{"role": "user", "content": "Hello!"},
52
"metadata": request_metadata,
55
print("data sent in test=", data)
57
async with session.post(url, headers=headers, json=data) as response:
58
status = response.status
59
response_text = await response.text()
65
raise Exception(f"Request did not return a 200 status code: {status}")
69
async def test_team_logging():
71
1. Add Langfuse as a callback with /config/update
72
2. Call /chat/completions
73
3. Assert the logs are sent to Langfuse
76
async with aiohttp.ClientSession() as session:
79
await config_update(session)
84
_trace_id = f"trace-{uuid.uuid4()}"
86
"trace_id": _trace_id,
89
await chat_completion(
92
model="fake-openai-endpoint",
93
request_metadata=_request_metadata,
99
langfuse_client = langfuse.Langfuse(
100
public_key=os.getenv("LANGFUSE_PUBLIC_KEY"),
101
secret_key=os.getenv("LANGFUSE_SECRET_KEY"),
104
await asyncio.sleep(10)
106
print(f"searching for trace_id={_trace_id} on langfuse")
108
generations = langfuse_client.get_generations(trace_id=_trace_id).data
111
assert len(generations) == 1
113
except Exception as e:
114
pytest.fail("Team 2 logging failed: " + str(e))