-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
458 lines (361 loc) · 16.1 KB
/
examples.py
File metadata and controls
458 lines (361 loc) · 16.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
AiAssist Python Client - Full Example Usage
Demonstrates all features of the SDK:
- Chat completions (OpenAI-compatible)
- Streaming responses
- Workspace management
- Shadow mode detection
- Human-in-the-loop handling
- Typing previews
- Conversation lifecycle
Works with all 12 providers:
OpenAI, Anthropic, Groq, Gemini, Mistral, xAI Grok,
Together AI, OpenRouter, DeepSeek, Fireworks, Perplexity, PIN
"""
import asyncio
from aiassist import (
AiAssistClient,
SyncAiAssistClient,
AiAssistError,
AuthenticationError,
RateLimitError,
)
# =============================================================================
# BASIC CHAT COMPLETION
# =============================================================================
async def basic_chat():
"""Simple chat completion - works like OpenAI SDK."""
async with AiAssistClient(
api_key="aai_your_api_key",
base_url="https://api.aiassist.net"
) as client:
response = await client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is Python?"}
],
model="gpt-4o", # Or any model from your configured providers
temperature=0.7,
max_tokens=500
)
print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")
# =============================================================================
# MULTI-PROVIDER EXAMPLES
# =============================================================================
async def multi_provider_chat():
"""Use different providers by specifying different models."""
async with AiAssistClient(api_key="aai_xxx") as client:
# OpenAI
openai_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from OpenAI"}],
model="gpt-4o"
)
# Anthropic
anthropic_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Anthropic"}],
model="claude-3-5-sonnet-20241022"
)
# Groq (ultra-fast)
groq_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Groq"}],
model="llama-3.3-70b-versatile"
)
# Google Gemini
gemini_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Gemini"}],
model="gemini-1.5-pro"
)
# Mistral
mistral_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Mistral"}],
model="mistral-large-latest"
)
# xAI Grok
grok_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Grok"}],
model="grok-2"
)
# Together AI
together_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Together"}],
model="meta-llama/Llama-3.3-70B-Instruct-Turbo"
)
# OpenRouter (any model)
openrouter_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from OpenRouter"}],
model="openrouter/auto"
)
# DeepSeek
deepseek_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from DeepSeek"}],
model="deepseek-chat"
)
# Fireworks
fireworks_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Fireworks"}],
model="accounts/fireworks/models/llama-v3p3-70b-instruct"
)
# Perplexity
perplexity_response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello from Perplexity"}],
model="llama-3.1-sonar-large-128k-online"
)
# =============================================================================
# STREAMING RESPONSES
# =============================================================================
async def streaming_chat():
"""Stream responses token by token."""
async with AiAssistClient(api_key="aai_xxx") as client:
stream = await client.chat.completions.create(
messages=[
{"role": "user", "content": "Write a haiku about coding."}
],
model="gpt-4o",
stream=True
)
async for chunk in stream:
if chunk.choices and chunk.choices[0].get("delta", {}).get("content"):
print(chunk.choices[0]["delta"]["content"], end="", flush=True)
print() # Newline at end
# =============================================================================
# WORKSPACE MANAGEMENT (Managed Conversations)
# =============================================================================
async def workspace_conversation():
"""Create and manage a workspace conversation."""
async with AiAssistClient(api_key="aai_xxx") as client:
# Create workspace with system prompt and context
result = await client.workspaces.create(
initial_message="Hi, I need help with my order.",
client_id="customer_12345", # Your user ID
system_prompt="You are a helpful customer support agent.",
context={
"customer_tier": "premium",
"recent_orders": ["ORD-001", "ORD-002"],
"account_status": "active"
},
metadata={
"source": "mobile_app",
"session_id": "sess_abc123"
}
)
workspace = result.workspace
print(f"Created workspace: {workspace.id}")
print(f"Mode: {workspace.mode}") # ai, shadow, or takeover
# Print initial messages (including AI response if in AI mode)
for msg in result.messages:
print(f"[{msg.role}]: {msg.content}")
async def workspace_send_message():
"""Send messages and handle different modes."""
async with AiAssistClient(api_key="aai_xxx") as client:
workspace_id = "ws_existing_id"
# Send a message
response = await client.workspaces.send_message(
workspace_id,
"What's the status of my recent order?"
)
# Check the mode
print(f"Mode: {response.mode}")
if response.mode == "ai":
# Fully autonomous - AI handled it
for msg in response.responses:
print(f"AI: {msg.content}")
elif response.mode == "shadow":
# Shadow mode - AI drafted, awaiting manager approval
if response.pending_approval:
print("Response is pending manager approval")
for msg in response.responses:
print(f"[DRAFT] AI: {msg.content}")
else:
# Already approved
for msg in response.responses:
print(f"AI (approved): {msg.content}")
elif response.mode == "takeover":
# Human takeover - waiting for human agent
print("Conversation handed to human agent")
# =============================================================================
# SHADOW MODE HANDLING (Enterprise)
# =============================================================================
async def shadow_mode_workflow():
"""Complete shadow mode workflow with pending approval detection."""
async with AiAssistClient(api_key="aai_xxx") as client:
# Get or create workspace
result = await client.workspaces.get_by_client_id("customer_456")
if not result.exists:
create_result = await client.workspaces.create(
client_id="customer_456",
system_prompt="You are a sales agent. Be helpful but professional."
)
workspace_id = create_result.workspace.id
else:
workspace_id = result.workspace.id
# Send message
response = await client.workspaces.send_message(
workspace_id,
"I want to cancel my subscription"
)
# Handle based on mode and approval status
if response.pending_approval:
# Show draft to user with "pending" indicator
print("Your message is being reviewed...")
draft = response.responses[0].content if response.responses else ""
# Optionally show draft preview to end user
elif response.mode == "takeover":
# Human is handling
print("A team member will respond shortly...")
else:
# Normal AI response
for msg in response.responses:
print(f"Agent: {msg.content}")
# =============================================================================
# TYPING PREVIEWS
# =============================================================================
async def typing_preview():
"""Send typing previews for real-time feedback."""
async with AiAssistClient(api_key="aai_xxx") as client:
workspace_id = "ws_xxx"
# As user types, send previews (debounce in your UI)
await client.workspaces.send_typing_preview(workspace_id, "I need help")
await asyncio.sleep(0.5)
await client.workspaces.send_typing_preview(workspace_id, "I need help with my")
await asyncio.sleep(0.5)
await client.workspaces.send_typing_preview(workspace_id, "I need help with my order")
# Then send the actual message
response = await client.workspaces.send_message(
workspace_id,
"I need help with my order"
)
# =============================================================================
# CONVERSATION LIFECYCLE
# =============================================================================
async def conversation_lifecycle():
"""Full conversation lifecycle management."""
async with AiAssistClient(api_key="aai_xxx") as client:
# Create
result = await client.workspaces.create(
client_id="user_789",
initial_message="Hello!"
)
workspace_id = result.workspace.id
# Converse
await client.workspaces.send_message(workspace_id, "First question")
await client.workspaces.send_message(workspace_id, "Follow up")
# Get all messages
messages = await client.workspaces.get_messages(workspace_id)
for msg in messages:
print(f"[{msg.created_at}] {msg.role}: {msg.content}")
# End conversation
await client.workspaces.end_conversation(workspace_id)
# =============================================================================
# LIST AVAILABLE MODELS
# =============================================================================
async def list_models():
"""List all models available for your plan."""
async with AiAssistClient(api_key="aai_xxx") as client:
models = await client.models.list()
for model in models:
print(f"- {model['id']}: {model.get('owned_by', 'unknown')}")
# =============================================================================
# ERROR HANDLING
# =============================================================================
async def error_handling():
"""Proper error handling patterns."""
async with AiAssistClient(api_key="aai_xxx") as client:
try:
response = await client.chat.completions.create(
messages=[{"role": "user", "content": "Hello"}],
model="gpt-4o"
)
print(response.choices[0].message.content)
except AuthenticationError:
print("Invalid API key - check your credentials")
except RateLimitError as e:
print(f"Rate limited - retry after backoff")
# Implement exponential backoff
except AiAssistError as e:
print(f"API error: {e} (status: {e.status_code})")
# =============================================================================
# SYNCHRONOUS CLIENT (for non-async code)
# =============================================================================
def sync_example():
"""Use the synchronous client for simpler scripts."""
with SyncAiAssistClient(
api_key="aai_xxx",
base_url="https://your-instance.com"
) as client:
response = client.chat.completions.create(
messages=[
{"role": "user", "content": "Hello!"}
],
model="gpt-4o"
)
print(response.choices[0].message.content)
# =============================================================================
# REAL-WORLD: CUSTOMER SUPPORT BOT
# =============================================================================
async def customer_support_bot():
"""Production-ready customer support integration."""
async with AiAssistClient(
api_key="aai_xxx",
base_url="https://api.yourdomain.com",
timeout=60.0,
max_retries=3
) as client:
customer_id = "cust_12345"
# Check for existing conversation
existing = await client.workspaces.get_by_client_id(customer_id)
if existing.exists:
workspace_id = existing.workspace.id
print(f"Resuming conversation {workspace_id}")
# Show previous messages
for msg in existing.messages[-5:]: # Last 5
print(f"{msg.role}: {msg.content}")
else:
# Create new workspace with rich context
result = await client.workspaces.create(
client_id=customer_id,
system_prompt="""You are a helpful customer support agent for ACME Inc.
Guidelines:
- Be friendly and professional
- If you can't help, offer to escalate
- Never make promises about refunds without manager approval
- Always verify customer identity before sharing account details""",
context={
"customer_name": "John Doe",
"plan": "enterprise",
"tenure_months": 24,
"open_tickets": 0,
"last_purchase": "2024-12-15"
}
)
workspace_id = result.workspace.id
print(f"New conversation started: {workspace_id}")
# Conversation loop
while True:
user_input = input("\nYou: ").strip()
if not user_input or user_input.lower() in ["quit", "exit"]:
await client.workspaces.end_conversation(workspace_id)
print("Conversation ended.")
break
response = await client.workspaces.send_message(workspace_id, user_input)
if response.pending_approval:
print("\n[Pending manager review...]")
elif response.mode == "takeover":
print("\n[Connecting you to a team member...]")
else:
for msg in response.responses:
print(f"\nAgent: {msg.content}")
# =============================================================================
# RUN EXAMPLES
# =============================================================================
if __name__ == "__main__":
print("=== AiAssist Python Client Examples ===\n")
# Uncomment to run:
# asyncio.run(basic_chat())
# asyncio.run(streaming_chat())
# asyncio.run(workspace_conversation())
# asyncio.run(shadow_mode_workflow())
# asyncio.run(list_models())
# asyncio.run(customer_support_bot())
# sync_example()
print("See examples.py for full usage patterns.")