-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_openai_basic.py
More file actions
43 lines (31 loc) · 1.07 KB
/
Copy pathraw_openai_basic.py
File metadata and controls
43 lines (31 loc) · 1.07 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
"""Smallest possible ``@protect`` usage with raw OpenAI.
The first ``@protect`` call lazily creates the runtime and patches
httpx so the OpenAI SDK fires ``track_llm`` events automatically.
NullRun's URL-keyed extractor reads the OpenAI response body and
pulls out ``prompt_tokens`` / ``completion_tokens``.
Run:
pip install nullrun openai
export NULLRUN_API_KEY=nr_live_...
export OPENAI_API_KEY=sk-...
python examples/raw_openai_basic.py
"""
from __future__ import annotations
from _env import load_env
load_env() # populate os.environ from examples/.env (no-op if absent)
from openai import OpenAI
import nullrun
from nullrun import protect, shutdown
client = OpenAI()
@protect
def answer(prompt: str) -> str:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content or ""
if __name__ == "__main__":
try:
with nullrun.handle():
print(answer("In one sentence, what does NullRun do?"))
finally:
shutdown()