-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
251 lines (199 loc) · 7.77 KB
/
Copy pathcli.py
File metadata and controls
251 lines (199 loc) · 7.77 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
#!/usr/bin/env python3
"""
SVECTOR CLI - Command Line Interface for SVECTOR AI
"""
import argparse
import json
import os
import sys
from pathlib import Path
from svector import SVECTOR
CONFIG_DIR = Path.home() / '.svector'
CONFIG_FILE = CONFIG_DIR / 'config.json'
def ensure_config_dir():
"""Ensure config directory exists"""
CONFIG_DIR.mkdir(exist_ok=True)
def load_config():
"""Load configuration from file"""
try:
if CONFIG_FILE.exists():
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading config: {e}")
return {}
def save_config(config):
"""Save configuration to file"""
try:
ensure_config_dir()
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=2)
except Exception as e:
print(f"Error saving config: {e}")
def get_client():
"""Get SVECTOR client with API key"""
config = load_config()
api_key = os.getenv('SVECTOR_API_KEY') or config.get('api_key')
if not api_key:
print("No API key found.")
print("Set it with: svector config set-key <your-api-key>")
print("Or set SVECTOR_API_KEY environment variable")
sys.exit(1)
return SVECTOR(api_key=api_key)
def cmd_chat(args):
"""Handle chat command"""
client = get_client()
try:
response = client.chat.create(
model=args.model,
messages=[{"role": "user", "content": args.message}],
temperature=args.temperature,
max_tokens=args.max_tokens,
files=[{"type": "file", "id": args.file}] if args.file else None
)
print("SVECTOR AI:")
print(response["choices"][0]["message"]["content"])
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def cmd_stream(args):
"""Handle stream command"""
client = get_client()
try:
print(" SVECTOR AI (streaming):")
stream = client.chat.create(
model=args.model,
messages=[{"role": "user", "content": args.message}],
temperature=args.temperature,
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() # New line at the end
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def cmd_models(args):
"""Handle models command"""
client = get_client()
try:
models = client.models.list()
print("📋 Available models:")
for i, model in enumerate(models["models"], 1):
print(f" {i}. {model}")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def cmd_config(args):
"""Handle config command"""
config = load_config()
if args.config_action == "set-key":
if not args.api_key:
print("Usage: svector config set-key <api-key>")
sys.exit(1)
config["api_key"] = args.api_key
save_config(config)
print("API key saved successfully")
elif args.config_action == "show":
print("Current configuration:")
print(json.dumps(config, indent=2))
else:
print("Usage: svector config [set-key|show]")
sys.exit(1)
def cmd_file(args):
"""Handle file command"""
client = get_client()
if args.file_action == "upload":
if not args.filepath:
print("Usage: svector file upload <filepath>")
sys.exit(1)
if not os.path.exists(args.filepath):
print(f"File not found: {args.filepath}")
sys.exit(1)
try:
print(f" Uploading {args.filepath}...")
response = client.files.create(args.filepath, purpose="default")
print(f"File uploaded with ID: {response['file_id']}")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
else:
print("Usage: svector file upload <filepath>")
sys.exit(1)
def cmd_ask(args):
"""Handle ask command"""
client = get_client()
if not args.file:
print("Please specify a file ID with --file <file-id>")
sys.exit(1)
try:
response = client.chat.create(
model="spec-3-turbo",
messages=[{"role": "user", "content": args.question}],
files=[{"type": "file", "id": args.file}]
)
print("SVECTOR AI (with file):")
print(response["choices"][0]["message"]["content"])
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def main():
"""Main CLI entry point"""
parser = argparse.ArgumentParser(
description="SVECTOR CLI - Advanced AI with RAG capabilities",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
svector chat "What is artificial intelligence?"
svector stream "Write a poem about technology"
svector models
svector config set-key sk-your-api-key-here
svector file upload document.pdf
svector ask "Summarize this document" --file file-123
For more info: https://www.svector.co.in
"""
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Chat command
chat_parser = subparsers.add_parser("chat", help="Send a chat message")
chat_parser.add_argument("message", help="Message to send")
chat_parser.add_argument("--model", default="spec-3-turbo", help="Model to use")
chat_parser.add_argument("--temperature", type=float, default=0.7, help="Temperature (0.0-2.0)")
chat_parser.add_argument("--max-tokens", type=int, default=150, help="Max tokens")
chat_parser.add_argument("--file", help="File ID for RAG")
chat_parser.set_defaults(func=cmd_chat)
# Stream command
stream_parser = subparsers.add_parser("stream", help="Stream a chat response")
stream_parser.add_argument("message", help="Message to send")
stream_parser.add_argument("--model", default="spec-3-turbo", help="Model to use")
stream_parser.add_argument("--temperature", type=float, default=0.7, help="Temperature (0.0-2.0)")
stream_parser.set_defaults(func=cmd_stream)
# Models command
models_parser = subparsers.add_parser("models", help="List available models")
models_parser.set_defaults(func=cmd_models)
# Config command
config_parser = subparsers.add_parser("config", help="Configuration management")
config_subparsers = config_parser.add_subparsers(dest="config_action")
setkey_parser = config_subparsers.add_parser("set-key", help="Set API key")
setkey_parser.add_argument("api_key", help="Your SVECTOR API key")
show_parser = config_subparsers.add_parser("show", help="Show current config")
config_parser.set_defaults(func=cmd_config)
# File command
file_parser = subparsers.add_parser("file", help="File operations")
file_subparsers = file_parser.add_subparsers(dest="file_action")
upload_parser = file_subparsers.add_parser("upload", help="Upload a file")
upload_parser.add_argument("filepath", help="Path to file to upload")
file_parser.set_defaults(func=cmd_file)
# Ask command
ask_parser = subparsers.add_parser("ask", help="Ask a question about a file")
ask_parser.add_argument("question", help="Question to ask")
ask_parser.add_argument("--file", required=True, help="File ID to query")
ask_parser.set_defaults(func=cmd_ask)
args = parser.parse_args()
if not args.command:
parser.print_help()
return
args.func(args)
if __name__ == "__main__":
main()