15
15
os .environ ["OPENLAYER_API_KEY" ] = "your-api-key-here"
16
16
os .environ ["OPENLAYER_INFERENCE_PIPELINE_ID" ] = "your-pipeline-id-here"
17
17
18
- import openlayer
18
+ from openlayer . lib import trace , trace_async , update_current_trace , update_current_span
19
19
20
20
21
21
class UserSession :
@@ -34,15 +34,15 @@ class ChatApplication:
34
34
def __init__ (self ):
35
35
self .active_sessions : Dict [str , UserSession ] = {}
36
36
37
- @openlayer . trace ()
37
+ @trace ()
38
38
def handle_user_request (self , request_text : str , session_token : str ) -> str :
39
39
"""Main request handler that dynamically sets trace metadata."""
40
40
41
41
# Get user session (this info isn't available as function arguments)
42
42
user_session = self .get_user_session (session_token )
43
43
44
44
# Set trace-level metadata with user context
45
- openlayer . update_current_trace (
45
+ update_current_trace (
46
46
name = f"chat_request_{ user_session .user_id } " ,
47
47
user_id = user_session .user_id ,
48
48
tags = ["chat" , "user_request" , user_session .preferences .get ("tier" , "free" )],
@@ -62,7 +62,7 @@ def handle_user_request(self, request_text: str, session_token: str) -> str:
62
62
final_response = self .postprocess_response (response , user_session )
63
63
64
64
# Update trace with final output
65
- openlayer . update_current_trace (
65
+ update_current_trace (
66
66
output = {"response" : final_response , "processing_time" : "0.5s" },
67
67
metadata = {
68
68
"response_length" : len (final_response ),
@@ -73,12 +73,12 @@ def handle_user_request(self, request_text: str, session_token: str) -> str:
73
73
user_session .interaction_count += 1
74
74
return final_response
75
75
76
- @openlayer . trace ()
76
+ @trace ()
77
77
def preprocess_request (self , text : str , user_session : UserSession ) -> str :
78
78
"""Preprocess user request with step-level metadata."""
79
79
80
80
# Update current step with preprocessing context
81
- openlayer . update_current_span (
81
+ update_current_span (
82
82
metadata = {
83
83
"preprocessing_type" : "standard" ,
84
84
"user_preferences_applied" : True ,
@@ -97,14 +97,14 @@ def preprocess_request(self, text: str, user_session: UserSession) -> str:
97
97
98
98
return processed
99
99
100
- @openlayer . trace ()
100
+ @trace ()
101
101
def generate_response (self , processed_text : str , user_session : UserSession ) -> str :
102
102
"""Generate AI response with model metadata."""
103
103
104
104
# Set model-specific metadata
105
105
model_version = "gpt-4" if user_session .preferences .get ("tier" ) == "premium" else "gpt-3.5-turbo"
106
106
107
- openlayer . update_current_span (
107
+ update_current_span (
108
108
metadata = {
109
109
"model_used" : model_version ,
110
110
"temperature" : 0.7 ,
@@ -127,11 +127,11 @@ def generate_response(self, processed_text: str, user_session: UserSession) -> s
127
127
128
128
return response
129
129
130
- @openlayer . trace ()
130
+ @trace ()
131
131
def postprocess_response (self , response : str , user_session : UserSession ) -> str :
132
132
"""Postprocess response with personalization metadata."""
133
133
134
- openlayer . update_current_span (
134
+ update_current_span (
135
135
metadata = {
136
136
"personalization_applied" : True ,
137
137
"content_filtering" : user_session .preferences .get ("content_filter" , "moderate" ),
@@ -170,12 +170,12 @@ def make_formal(self, text: str) -> str:
170
170
return text .replace ("can't" , "cannot" ).replace ("won't" , "will not" )
171
171
172
172
173
- @openlayer . trace ()
173
+ @trace ()
174
174
def batch_processing_example ():
175
175
"""Example showing batch processing with trace metadata updates."""
176
176
177
177
# Set trace metadata for batch job
178
- openlayer . update_current_trace (
178
+ update_current_trace (
179
179
name = "batch_user_requests" ,
180
180
tags = ["batch" , "processing" , "multiple_users" ],
181
181
metadata = {
@@ -199,15 +199,15 @@ def batch_processing_example():
199
199
results .append (result )
200
200
201
201
# Update batch progress
202
- openlayer . update_current_trace (
202
+ update_current_trace (
203
203
metadata = {
204
204
"requests_processed" : i + 1 ,
205
205
"progress_percentage" : ((i + 1 ) / len (test_requests )) * 100
206
206
}
207
207
)
208
208
209
209
# Update final batch metadata
210
- openlayer . update_current_trace (
210
+ update_current_trace (
211
211
output = {"batch_results" : results , "total_processed" : len (results )},
212
212
metadata = {
213
213
"processing_complete" : True ,
@@ -219,18 +219,18 @@ def batch_processing_example():
219
219
return results
220
220
221
221
222
- @openlayer . trace ()
222
+ @trace ()
223
223
def error_handling_example ():
224
224
"""Example showing error handling with trace metadata."""
225
225
226
- openlayer . update_current_trace (
226
+ update_current_trace (
227
227
name = "error_handling_demo" ,
228
228
metadata = {"expected_behavior" : "demonstrate error tracing" }
229
229
)
230
230
231
231
try :
232
232
# Simulate some processing
233
- openlayer . update_current_span (
233
+ update_current_span (
234
234
metadata = {"processing_step" : "initial_validation" }
235
235
)
236
236
@@ -239,7 +239,7 @@ def error_handling_example():
239
239
240
240
except ValueError as e :
241
241
# Update trace with error information
242
- openlayer . update_current_trace (
242
+ update_current_trace (
243
243
metadata = {
244
244
"error_occurred" : True ,
245
245
"error_type" : type (e ).__name__ ,
@@ -252,11 +252,11 @@ def error_handling_example():
252
252
return f"Error handled: { str (e )} "
253
253
254
254
255
- @openlayer . trace_async ()
255
+ @trace_async ()
256
256
async def async_example ():
257
257
"""Example showing async trace metadata updates."""
258
258
259
- openlayer . update_current_trace (
259
+ update_current_trace (
260
260
name = "async_processing" ,
261
261
metadata = {"execution_mode" : "async" },
262
262
tags = ["async" , "demo" ]
@@ -265,12 +265,12 @@ async def async_example():
265
265
# Simulate async processing steps
266
266
import asyncio
267
267
268
- openlayer . update_current_span (
268
+ update_current_span (
269
269
metadata = {"step" : "async_sleep_simulation" }
270
270
)
271
271
await asyncio .sleep (0.1 )
272
272
273
- openlayer . update_current_trace (
273
+ update_current_trace (
274
274
metadata = {"async_complete" : True },
275
275
output = "Async processing completed"
276
276
)
0 commit comments