forked from opper-ai/opper-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.py
More file actions
189 lines (135 loc) · 5.1 KB
/
tracing.py
File metadata and controls
189 lines (135 loc) · 5.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Demonstrate how to capture execution traces.
Each method demonstrates how to capture execution traces using different methods.
It also demonstrates how to add metrics and manual generations to the traces.
Generations are added automatically when using `opper.call` or when functions created
with `opper.functions.create` or `@fn` are called.
"""
import asyncio
import time
from opperai import AsyncOpper, Opper, trace
opper = Opper()
def trace_with_context_manager():
"""trace using context manager"""
with opper.traces.start(name="context manager") as span:
span.save_metric("score", 100.0, "context manager")
def trace_manually():
"""trace manually using `start_span`"""
span = opper.traces.start_span("manually created span")
span.save_metric("score", 100.0, "manually created span")
span.end()
@trace
def trace_with_decorator():
"""trace function using the `@trace` decorator"""
opper.traces.current_span.save_metric("score", 100.0, "decorator")
def manual_generation():
"""manually add a generation to the current span"""
with opper.traces.start(
name="manual generation",
input="What is the meaning of life?",
) as span:
time.sleep(0.1)
span.save_generation(
duration_ms=100,
input="What is the meaning of life?",
response="42",
model="openai/gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the meaning of life?"},
{"role": "assistant", "content": "42"},
],
)
@trace
def trace_with_call():
result, response = opper.call(name="python/sdk/bare-minimum", input="Hello, world!")
response.span.save_metric("score", 100.0, "call")
return result
def get_span_and_save_metric():
# Create span during runtime
span = opper.traces.start_span("async_get_span_and_save_metric")
span.end()
span_id = span.uuid
# Later fetch the span using its uuid and save a metric
metric_span = opper.spans.get_span(span_id=span_id)
metric_span.save_metric("my_metric", 1, "my metric comment")
@trace
def run_sync():
print("running synchronous tracing")
trace_with_context_manager()
trace_manually()
trace_with_decorator()
manual_generation()
trace_with_call()
get_span_and_save_metric()
opper.traces.current_span.save_metric(
"total_score", 100.0, "metric on the root span"
)
aopper = AsyncOpper()
async def async_trace_with_context_manager():
"""trace using context manager"""
async with aopper.traces.start(name="async context manager") as span:
await span.save_metric("score", 100.0, "async context manager")
async def async_trace_manually():
"""trace manually using `start_span`"""
span = await aopper.traces.start_span("manually created span")
await span.save_metric("score", 100.0, "manually created span")
await span.end()
@trace
async def async_trace_with_decorator():
"""trace function using the `@trace` decorator"""
await aopper.traces.current_span.save_metric("score", 100.0, "decorator")
async def async_manual_generation():
"""manually add a generation to the current span"""
async with aopper.traces.start(
name="async manual generation",
input="What is the meaning of life?",
) as span:
time.sleep(0.1)
await span.save_generation(
duration_ms=100,
input="What is the meaning of life?",
response="42",
model="openai/gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the meaning of life?"},
{"role": "assistant", "content": "42"},
],
)
@trace
async def async_trace_with_call():
result, response = await aopper.call(
name="python/sdk/bare-minimum", input="Hello, world!"
)
await response.span.save_metric("score", 100.0, "call")
return result
async def async_get_span_and_save_metric():
# Create span during runtime
span = await aopper.traces.start_span("async_get_span_and_save_metric")
await span.end()
span_id = span.uuid
# Later fetch the span using its uuid and save a metric
metric_span = aopper.spans.get_span(span_id=span_id)
await metric_span.save_metric("my_metric", 1, "my metric comment")
@trace
async def run_async():
print("running asynchronous tracing")
await asyncio.gather(
async_trace_with_context_manager(),
async_trace_manually(),
async_trace_with_decorator(),
async_manual_generation(),
async_trace_with_call(),
async_get_span_and_save_metric(),
)
await aopper.traces.current_span.save_metric("total_score", 100.0, "chain")
if __name__ == "__main__":
import sys
if len(sys.argv) == 2:
if sys.argv[1] == "async":
asyncio.run(run_async())
elif sys.argv[1] == "sync":
run_sync()
else:
run_sync()
asyncio.run(run_async())