-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.py
More file actions
171 lines (132 loc) · 4.43 KB
/
Copy pathexamples.py
File metadata and controls
171 lines (132 loc) · 4.43 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
#!/usr/bin/env python3
"""
SVECTOR Python SDK Examples
"""
import os
from svector import SVECTOR
# Set up the client
api_key = os.getenv('SVECTOR_API_KEY', 'sk-your-api-key-here')
client = SVECTOR(api_key=api_key)
def example_basic_chat():
"""Basic chat example"""
print("Basic Chat Example")
print("-" * 50)
try:
response = client.chat.create(
model="spec-3-turbo",
messages=[
{"role": "user", "content": "What is artificial intelligence?"}
],
temperature=0.7,
max_tokens=100
)
print("Question: What is artificial intelligence?")
print("Answer:", response["choices"][0]["message"]["content"])
print("Usage:", response.get("usage", "N/A"))
except Exception as e:
print(f"Error: {e}")
print()
def example_streaming():
"""Streaming example"""
print(" Streaming Example")
print("-" * 50)
try:
print("Question: Write a haiku about Python programming")
print("Streaming Answer: ", end="", flush=True)
stream = client.chat.create(
model="spec-3-turbo",
messages=[
{"role": "user", "content": "Write a haiku about Python programming"}
],
temperature=0.8,
stream=True
)
for event in stream:
if event.get("choices") and event["choices"][0].get("delta", {}).get("content"):
print(event["choices"][0]["delta"]["content"], end="", flush=True)
print("\nStreaming completed!")
except Exception as e:
print(f"Error: {e}")
print()
def example_models():
"""Models listing example"""
print("📋 Models Example")
print("-" * 50)
try:
models = client.models.list()
print(f"Available models ({len(models['models'])}):")
for i, model in enumerate(models["models"], 1):
print(f" {i}. {model}")
except Exception as e:
print(f"Error: {e}")
print()
def example_conversation():
"""Multi-turn conversation example"""
print(" Multi-turn Conversation Example")
print("-" * 50)
conversation = [
{"role": "system", "content": "You are a helpful Python programming assistant."}
]
questions = [
"How do I create a list in Python?",
"Can you show me an example?",
"How do I add items to the list?"
]
try:
for i, question in enumerate(questions, 1):
print(f"👤 Question {i}: {question}")
conversation.append({"role": "user", "content": question})
response = client.chat.create(
model="spec-3-turbo",
messages=conversation,
temperature=0.3,
max_tokens=100
)
answer = response["choices"][0]["message"]["content"]
conversation.append({"role": "assistant", "content": answer})
print(f"Answer {i}: {answer}")
print()
except Exception as e:
print(f"Error: {e}")
print()
def example_error_handling():
"""Error handling example"""
print(" Error Handling Example")
print("-" * 50)
# Test with invalid API key
try:
invalid_client = SVECTOR(api_key="invalid-key")
response = invalid_client.chat.create(
model="spec-3-turbo",
messages=[{"role": "user", "content": "This should fail"}]
)
print("Should have failed with invalid API key")
except Exception as e:
print("Successfully caught authentication error:")
print(f" Error type: {type(e).__name__}")
print(f" Message: {e}")
print()
def main():
"""Run all examples"""
print("SVECTOR Python SDK Examples")
print("=" * 60)
print()
examples = [
example_basic_chat,
example_streaming,
example_models,
example_conversation,
example_error_handling
]
for example in examples:
try:
example()
except KeyboardInterrupt:
print("\n⚠️ Interrupted by user")
break
except Exception as e:
print(f"Example failed: {e}")
print()
print("🎉 Examples completed!")
if __name__ == "__main__":
main()