Repository navigation
Expand file tree
/
Copy pathllm_api_example.py
More file actions
63 lines (51 loc) · 1.9 KB
/
Copy pathllm_api_example.py
File metadata and controls
63 lines (51 loc) · 1.9 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# /// script
# requires-python = ">=3.10"
# dependencies = ["wildedge-sdk"]
#
# [tool.uv.sources]
# wildedge-sdk = { path = "..", editable = true }
# ///
"""Track LLM calls made over plain HTTP with wildedge.llm_api().
No openai or anthropic client library involved: requests go through stdlib
urllib against OpenRouter's OpenAI-compatible endpoint, and llm_api() records
the same inference events the integrations emit (tokens, TTFT, stop reason),
correlated into the surrounding trace and spans.
Run with: uv run llm_api_example.py
Requires: OPENROUTER_API_KEY environment variable. Set WILDEDGE_DSN to send events.
"""
import json
import os
import urllib.request
import uuid
import wildedge
wildedge.init(app_version="1.0.0") # uses WILDEDGE_DSN if set; otherwise no-op
URL = "https://openrouter.ai/api/v1/chat/completions"
MODEL = "openai/gpt-4o-mini"
def chat(prompt: str) -> dict:
request = urllib.request.Request(
URL,
data=json.dumps(
{"model": MODEL, "messages": [{"role": "user", "content": prompt}]}
).encode(),
headers={
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(request, timeout=120) as response:
return json.load(response)
PROMPTS = [
"What is on-device AI in one sentence?",
"Name three edge inference runtimes.",
]
with wildedge.trace(agent_id="llm-api-example", run_id=str(uuid.uuid4())):
for step, prompt in enumerate(PROMPTS):
with (
wildedge.span(kind="agent_step", name="ask", step_index=step),
wildedge.llm_api(model=MODEL, provider="openrouter", prompt=prompt) as call,
):
data = chat(prompt)
call.response(data)
print(f"Q: {prompt}\nA: {data['choices'][0]['message']['content']}\n")
wildedge.flush()
print("Done. Events flushed to WildEdge.")