-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecorator_trace_llm.py
More file actions
59 lines (46 loc) · 1.74 KB
/
decorator_trace_llm.py
File metadata and controls
59 lines (46 loc) · 1.74 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
"""
Langfuse API Level 2: Decorator-based (@observe())
This approach uses decorators to automatically create traces and spans.
Nested function calls automatically create nested spans.
"""
from langfuse import observe, Langfuse
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
langfuse = Langfuse()
@observe() # Creates a trace automatically
def calculator(expression: str) -> str:
"""Single calculation - becomes a span when called from another @observe function."""
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": "You are a very accurate calculator. You output only the result of the calculation.",
},
{"role": "user", "content": expression},
],
)
# Update current trace/span with additional context
langfuse.update_current_span(
metadata={"project": "decorator_example", "tags": ["calculator", "math"]},
)
return completion.choices[0].message.content
@observe() # Nested function = nested span
def process_calculations(expressions: list[str]) -> list[str]:
"""Process multiple calculations - each calculator() call becomes a child span."""
results = []
for expr in expressions:
result = calculator(expr) # Each call becomes a child span
results.append(f"{expr} = {result}")
return results
if __name__ == "__main__":
# Run it - this creates a trace with nested spans
expressions = ["123 + 456", "789 * 2", "100 / 4"]
results = process_calculations(expressions)
print("Results:")
for r in results:
print(f" {r}")
# Ensure data is sent to Langfuse
langfuse.flush()