-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpromote_trace_columns.py
More file actions
171 lines (130 loc) · 6.03 KB
/
promote_trace_columns.py
File metadata and controls
171 lines (130 loc) · 6.03 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Example: Promoting inputs and outputs to top-level trace columns.
The `promote` parameter on @trace() lets you surface function inputs *and*
output fields as top-level columns in the trace data, so you can create
Openlayer tests against them (e.g. "agent_tool_call_count < 10").
Keys are resolved from **inputs first**, then from the **output** (dict,
Pydantic model, or dataclass). Use a list to keep original names, or a dict
to alias them and avoid collisions between parent and child steps.
"""
import dataclasses
import os
from typing import Any, Dict, List
from pydantic import BaseModel
os.environ["OPENLAYER_API_KEY"] = "your-api-key-here"
os.environ["OPENLAYER_INFERENCE_PIPELINE_ID"] = "your-pipeline-id-here"
from openlayer.lib import trace
from openlayer.lib.tracing import tracer
# ---------------------------------------------------------------------------
# 1. Promote from a Pydantic model output
# ---------------------------------------------------------------------------
class AgentResult(BaseModel):
answer: str
tool_call_count: int
tool_names: List[str]
@trace(promote={
"user_query": "agent_input_query", # from input
"tool_call_count": "agent_tool_calls", # from output
"tool_names": "agent_tools", # from output
})
def run_agent(user_query: str) -> AgentResult:
"""Simulates an agent that uses tools to answer a question.
The trace data will include three top-level columns:
- agent_input_query (from the `user_query` input)
- agent_tool_calls (from the Pydantic output's `tool_call_count`)
- agent_tools (from the Pydantic output's `tool_names`)
"""
# ... agent logic would go here ...
return AgentResult(
answer="Paris is the capital of France.",
tool_call_count=2,
tool_names=["web_search", "summarize"],
)
# ---------------------------------------------------------------------------
# 2. Promote from a dict output (list form -- no aliasing)
# ---------------------------------------------------------------------------
@trace(promote=["score", "confidence"])
def evaluate(text: str) -> Dict[str, Any]:
"""Evaluates text quality. `score` and `confidence` become top-level columns."""
return {"score": 0.95, "confidence": 0.87, "explanation": "Well-structured."}
# ---------------------------------------------------------------------------
# 3. Promote from a dataclass output
# ---------------------------------------------------------------------------
@dataclasses.dataclass
class RetrievalResult:
documents: List[str]
doc_count: int
avg_relevance: float
@trace(promote={"doc_count": "retrieval_doc_count", "avg_relevance": "retrieval_relevance"})
def retrieve(query: str) -> RetrievalResult:
"""Retrieves relevant documents. Promotes doc_count and avg_relevance."""
return RetrievalResult(
documents=["doc_a", "doc_b", "doc_c"],
doc_count=3,
avg_relevance=0.82,
)
# ---------------------------------------------------------------------------
# 4. Nested traces -- child steps promote to the same top-level row
# ---------------------------------------------------------------------------
class ToolResult(BaseModel):
tool_call_count: int
tool_names: List[str]
result: str
@trace(promote={"tool_call_count": "child_tool_calls", "tool_names": "child_tools"})
def inner_agent_step(task: str) -> ToolResult:
"""A child step whose output fields are promoted to the parent trace.
Even though this is a nested step, `promote` writes to the shared Trace
object, so `child_tool_calls` and `child_tools` become top-level columns.
"""
return ToolResult(
tool_call_count=5,
tool_names=["search", "calculator", "code_exec", "summarize", "translate"],
result=f"Completed: {task}",
)
@trace(promote={"user_query": "input_query"})
def orchestrator(user_query: str) -> str:
"""Parent function that delegates to a child step.
After execution the trace will have top-level columns from *both* levels:
- input_query (parent input)
- child_tool_calls (child output)
- child_tools (child output)
"""
step1 = inner_agent_step("look up facts")
step2 = inner_agent_step("summarize findings")
return f"{step1.result} | {step2.result}"
# ---------------------------------------------------------------------------
def main():
print("=== 1. Promote from Pydantic output ===")
result = run_agent("What is the capital of France?")
print(f" Answer: {result.answer}")
print(f" Tool calls: {result.tool_call_count}")
print()
print("=== 2. Promote from dict output (list form) ===")
scores = evaluate("The quick brown fox.")
print(f" Score: {scores['score']}, Confidence: {scores['confidence']}")
print()
print("=== 3. Promote from dataclass output ===")
docs = retrieve("machine learning basics")
print(f" Retrieved {docs.doc_count} docs, avg relevance: {docs.avg_relevance}")
print()
print("=== 4. Nested traces -- child promote to parent row ===")
captured_trace = None
@trace(promote={"user_query": "input_query"})
def traced_orchestrator(user_query: str) -> str:
nonlocal captured_trace
step1 = inner_agent_step("look up facts")
step2 = inner_agent_step("summarize findings")
captured_trace = tracer.get_current_trace()
return f"{step1.result} | {step2.result}"
out = traced_orchestrator("hello world")
print(f" Result: {out}")
print(f" Trace metadata: {captured_trace.metadata}")
assert captured_trace.metadata["input_query"] == "hello world", "parent input promoted"
assert captured_trace.metadata["child_tool_calls"] == 5, "child output promoted"
assert "search" in captured_trace.metadata["child_tools"], "child list output promoted"
print(" All promoted columns verified!")
print()
print("Check your Openlayer dashboard -- promoted fields appear as top-level")
print("columns you can write tests against (e.g. child_tool_calls < 10).")
if __name__ == "__main__":
main()