-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
139 lines (109 loc) · 4.08 KB
/
example.py
File metadata and controls
139 lines (109 loc) · 4.08 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
#!/usr/bin/env python3
"""
Example usage of the Cascading AI API Flow system
"""
import sys
from cascade import CascadingAPIClient
from utils import format_usage_display
def basic_example():
"""Basic usage example"""
print("[INFO] Basic Cascading AI API Example")
print("=" * 40)
try:
# Initialize the client (automatically uses all available providers)
client = CascadingAPIClient()
print(f"[OK] Initialized with providers: {', '.join(client.get_available_providers())}")
# Simple chat completion
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
]
response = client.chat_completion(messages)
print(f"\n[INFO] AI Response:\n{response}")
# Show usage stats
stats = client.get_usage_stats()
print(f"\n{format_usage_display(stats)}")
except Exception as e:
print(f"[FAIL] Error: {e}")
return False
return True
def advanced_example():
"""Advanced usage with custom parameters"""
print("\n[INFO] Advanced Example with Custom Parameters")
print("=" * 45)
try:
client = CascadingAPIClient()
# More complex conversation
messages = [
{"role": "system", "content": "You are a creative writing assistant. Keep responses engaging but concise."},
{"role": "user", "content": "Write a short story about a robot who learns to paint."}
]
# Custom parameters
response = client.chat_completion(
messages=messages,
max_tokens=300,
temperature=0.8,
max_retries=3
)
print(f"[INFO] Creative AI Response:\n{response}")
# Show updated stats
stats = client.get_usage_stats()
print(f"\n{format_usage_display(stats)}")
except Exception as e:
print(f"[FAIL] Advanced example failed: {e}")
return False
return True
def interactive_chat():
"""Interactive chat session"""
print("\n[INFO] Interactive Chat Session")
print("=" * 30)
print("Type 'quit', 'exit', or 'q' to end the session")
try:
client = CascadingAPIClient()
messages = [
{"role": "system", "content": "You are a helpful assistant. Keep responses concise and friendly."}
]
while True:
user_input = input("\n[USER] You: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
print("[INFO] Goodbye!")
break
if not user_input:
continue
messages.append({"role": "user", "content": user_input})
try:
response = client.chat_completion(messages, max_tokens=200)
print(f"[AI] AI: {response}")
messages.append({"role": "assistant", "content": response})
except Exception as e:
print(f"[FAIL] Error getting response: {e}")
break
except KeyboardInterrupt:
print("\n[INFO] Chat ended by user")
except Exception as e:
print(f"[FAIL] Chat session error: {e}")
def main():
"""Main example function"""
print("[INFO] Cascading AI API Flow - Examples")
print("=" * 50)
# Run basic example
if not basic_example():
print("\n[FAIL] Basic example failed. Check your API keys and setup.")
sys.exit(1)
# Run advanced example
if not advanced_example():
print("\n[WARN] Advanced example failed, but basic functionality works.")
# Ask for interactive chat
while True:
choice = input("\n[CHOICE] Would you like to try interactive chat? (y/n): ").strip().lower()
if choice in ['y', 'yes']:
interactive_chat()
break
elif choice in ['n', 'no']:
print("[INFO] Skipping interactive chat.")
break
else:
print("Please enter 'y' or 'n'")
print("\n[INFO] Examples completed! Check the logs and usage_tracking.json for details.")
if __name__ == "__main__":
main()