forked from deepset-ai/haystack-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserve_quickstart.py
More file actions
388 lines (287 loc) · 12.5 KB
/
Copy pathobserve_quickstart.py
File metadata and controls
388 lines (287 loc) · 12.5 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""
observe() Quickstart — Python
Zero-config instrumentation for OpenAI and Anthropic clients.
Uses mock clients so it runs without API keys.
Run:
python examples/observe_quickstart.py
"""
import asyncio
from tealtiger.observe import observe, freeze, unfreeze, FrozenAgentError
from tealtiger.observe.freeze_registry import FreezeRegistry
# ---------------------------------------------------------------------------
# Mock clients — stand-ins for real openai / anthropic SDK instances.
# Replace with the real SDK clients and your API keys in production.
#
# Duck-type compatible with the observe() provider detector:
# - Mock OpenAI: has chat.completions.create() method and base_url attribute
# - Mock Anthropic: has messages.create() method
# ---------------------------------------------------------------------------
class _MockCompletions:
"""Simulates openai.resources.chat.Completions."""
def create(self, **kwargs):
"""Sync chat completion — returns an OpenAI-shaped response."""
return _make_openai_response(kwargs.get("model", "gpt-4o-mini"))
async def acreate(self, **kwargs):
"""Async chat completion — same shape, awaitable."""
return _make_openai_response(kwargs.get("model", "gpt-4o-mini"))
class _MockChat:
"""Simulates openai.resources.Chat (has .completions)."""
def __init__(self):
self.completions = _MockCompletions()
class MockOpenAI:
"""Mock OpenAI client — duck-type compatible with provider detector.
Detector checks: has `chat.completions.create` and `base_url` attribute.
"""
def __init__(self):
self.chat = _MockChat()
self.base_url = "https://api.openai.com/v1"
class _MockMessages:
"""Simulates anthropic.resources.Messages."""
def create(self, **kwargs):
"""Sync message creation — returns an Anthropic-shaped response."""
return _make_anthropic_response(kwargs.get("model", "claude-3-5-sonnet-20241022"))
async def acreate(self, **kwargs):
"""Async message creation — same shape, awaitable."""
return _make_anthropic_response(kwargs.get("model", "claude-3-5-sonnet-20241022"))
class MockAnthropic:
"""Mock Anthropic client — duck-type compatible with provider detector.
Detector checks: has `messages.create` method.
"""
def __init__(self):
self.messages = _MockMessages()
# ---------------------------------------------------------------------------
# Response factories — build mock provider responses with usage metadata
# ---------------------------------------------------------------------------
class _Namespace:
"""Generic namespace that converts kwargs to attributes."""
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
def __repr__(self):
attrs = ", ".join(f"{k}={v!r}" for k, v in self.__dict__.items())
return f"_Namespace({attrs})"
def _make_openai_response(model: str):
"""Create a mock OpenAI ChatCompletion response object."""
return _Namespace(
id="chatcmpl-mock-001",
object="chat.completion",
model=model,
choices=[
_Namespace(
index=0,
message=_Namespace(
role="assistant",
content="[mock] OpenAI response via TealTiger observe()",
tool_calls=None,
),
finish_reason="stop",
)
],
usage=_Namespace(
prompt_tokens=12,
completion_tokens=10,
total_tokens=22,
),
)
def _make_anthropic_response(model: str):
"""Create a mock Anthropic message response object."""
return _Namespace(
id="msg-mock-001",
type="message",
role="assistant",
model=model,
content=[
_Namespace(type="text", text="[mock] Anthropic response via TealTiger observe()")
],
stop_reason="end_turn",
usage=_Namespace(
input_tokens=14,
output_tokens=11,
),
)
# ---------------------------------------------------------------------------
# 1. Zero-config observe() — one line instruments the client
# ---------------------------------------------------------------------------
def example_zero_config():
"""Demonstrates the simplest usage: observe() with no options."""
print("\n--- 1. Zero-config observe() ---")
# observe() wraps the client and auto-enables cost tracking,
# audit logging, and PII detection with sensible defaults.
openai = observe(MockOpenAI())
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is TealTiger?"}],
)
print(f"Response: {response.choices[0].message.content}")
print(f"Agent ID: {openai.get_agent_id()}")
print(f"Cost: {openai.get_cost()}")
# ---------------------------------------------------------------------------
# 2. Named agent with session tracking
# ---------------------------------------------------------------------------
def example_named_agent():
"""Demonstrates observe() with explicit agentId and sessionId."""
print("\n--- 2. Named agent + session ID ---")
# agent_id groups all calls under one logical agent in the audit log.
# session_id ties calls within a single user conversation together.
anthropic = observe(
MockAnthropic(),
agent_id="support-bot",
session_id="sess-abc-123",
)
response = anthropic.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=200,
messages=[{"role": "user", "content": "Summarise the TealTiger governance model."}],
)
print(f"Response: {response.content[0].text}")
print(f"Agent ID: {anthropic.get_agent_id()}")
print(f"Session ID: {anthropic.get_session_id()}")
# ---------------------------------------------------------------------------
# 3. freeze() / unfreeze() — emergency circuit-breaker (kill switch)
# ---------------------------------------------------------------------------
def example_circuit_breaker():
"""Demonstrates the freeze/unfreeze kill switch."""
print("\n--- 3. freeze() / unfreeze() ---")
openai = observe(MockOpenAI(), agent_id="billing-agent")
# Freeze the agent immediately — all subsequent calls will be blocked
# until unfreeze() is called. Use this when you detect anomalous behaviour
# or want to halt an agent during an incident.
freeze("billing-agent")
print("Agent frozen. Calls will be blocked.")
try:
openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Process payment for order #99"}],
)
except FrozenAgentError as err:
# Expected: FrozenAgentError
print(f"Blocked as expected: {err}")
# Unfreeze when the incident is resolved.
unfreeze("billing-agent")
print("Agent unfrozen. Resuming normal operation.")
response = openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Process payment for order #99"}],
)
print(f"Response after unfreeze: {response.choices[0].message.content}")
# ---------------------------------------------------------------------------
# 4. Both providers side-by-side (multi-provider setup)
# ---------------------------------------------------------------------------
def example_multi_provider():
"""Demonstrates using the same agentId across multiple providers."""
print("\n--- 4. Multi-provider under one agentId ---")
# The same agent_id can be applied to different provider clients.
# All calls appear together in the governance audit log.
openai = observe(MockOpenAI(), agent_id="research-agent")
anthropic = observe(MockAnthropic(), agent_id="research-agent")
oai_res = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain tool use in OpenAI."}],
)
ant_res = anthropic.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=100,
messages=[{"role": "user", "content": "Explain tool use in Anthropic."}],
)
print(f"OpenAI: {oai_res.choices[0].message.content}")
print(f"Anthropic: {ant_res.content[0].text}")
# ---------------------------------------------------------------------------
# 5. Async usage patterns
# ---------------------------------------------------------------------------
class _MockAsyncCompletions:
"""Async-native mock completions (coroutine function detected by observe)."""
async def create(self, **kwargs):
return _make_openai_response(kwargs.get("model", "gpt-4o-mini"))
class _MockAsyncChat:
def __init__(self):
self.completions = _MockAsyncCompletions()
class MockAsyncOpenAI:
"""Mock async OpenAI client — same duck-type shape."""
def __init__(self):
self.chat = _MockAsyncChat()
self.base_url = "https://api.openai.com/v1"
class _MockAsyncMessages:
"""Async-native mock messages."""
async def create(self, **kwargs):
return _make_anthropic_response(kwargs.get("model", "claude-3-5-sonnet-20241022"))
class MockAsyncAnthropic:
"""Mock async Anthropic client — same duck-type shape."""
def __init__(self):
self.messages = _MockAsyncMessages()
async def async_main():
"""Async equivalents of the sync examples above."""
print("\n\n========== ASYNC EXAMPLES ==========")
# Reset FreezeRegistry to avoid cross-contamination from sync examples
FreezeRegistry.get_instance()._reset()
# --- Async zero-config ---
print("\n--- Async: Zero-config observe() ---")
openai = observe(MockAsyncOpenAI())
response = await openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is TealTiger? (async)"}],
)
print(f"Response: {response.choices[0].message.content}")
# --- Async named agent ---
print("\n--- Async: Named agent + session ID ---")
anthropic = observe(
MockAsyncAnthropic(),
agent_id="async-support-bot",
session_id="sess-async-001",
)
response = await anthropic.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=200,
messages=[{"role": "user", "content": "Explain observe() in async mode."}],
)
print(f"Response: {response.content[0].text}")
# --- Async freeze/unfreeze ---
print("\n--- Async: freeze() / unfreeze() ---")
openai = observe(MockAsyncOpenAI(), agent_id="async-billing-agent")
freeze("async-billing-agent")
try:
await openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Process async payment"}],
)
except FrozenAgentError as err:
print(f"Blocked as expected: {err}")
unfreeze("async-billing-agent")
response = await openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Process async payment"}],
)
print(f"Response after unfreeze: {response.choices[0].message.content}")
# --- Async multi-provider ---
print("\n--- Async: Multi-provider ---")
openai = observe(MockAsyncOpenAI(), agent_id="async-research-agent")
anthropic = observe(MockAsyncAnthropic(), agent_id="async-research-agent")
oai_res, ant_res = await asyncio.gather(
openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Async OpenAI call"}],
),
anthropic.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=100,
messages=[{"role": "user", "content": "Async Anthropic call"}],
),
)
print(f"OpenAI: {oai_res.choices[0].message.content}")
print(f"Anthropic: {ant_res.content[0].text}")
print("\nDone (async). In production, swap mock clients for real SDK instances.")
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def main():
"""Run all sync examples."""
# Reset FreezeRegistry singleton to avoid cross-contamination between runs
FreezeRegistry.get_instance()._reset()
print("========== SYNC EXAMPLES ==========")
example_zero_config()
example_named_agent()
example_circuit_breaker()
example_multi_provider()
print("\nDone (sync). In production, swap mock clients for real SDK instances.")
if __name__ == "__main__":
main()
asyncio.run(async_main())