Repository navigation
Expand file tree
/
Copy pathsetup_gcp_llm.py
More file actions
306 lines (244 loc) · 9.1 KB
/
Copy pathsetup_gcp_llm.py
File metadata and controls
306 lines (244 loc) · 9.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
#!/usr/bin/env python3
"""
GCP LLM Configuration and Test Script
Helps you configure and test ShopMindAI's connection to your GCP LLM server.
Usage:
python setup_gcp_llm.py --ip YOUR_GCP_VM_IP
python setup_gcp_llm.py --test
python setup_gcp_llm.py --interactive
"""
import os
import sys
import argparse
import json
import requests
from pathlib import Path
from typing import Optional, Tuple
def find_env_file() -> Path:
"""Find or create .env file."""
env_path = Path(".env")
if not env_path.exists():
env_example = Path(".env.example")
if env_example.exists():
print(f"✓ Creating .env from .env.example")
env_path.write_text(env_example.read_text())
else:
print(f"✗ No .env or .env.example found")
return None
return env_path
def load_env_vars() -> dict:
"""Load environment variables from .env file."""
env_path = find_env_file()
if not env_path:
return {}
env_vars = {}
for line in env_path.read_text().split('\n'):
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
env_vars[key.strip()] = value.strip()
return env_vars
def save_env_vars(env_vars: dict) -> bool:
"""Save environment variables to .env file."""
env_path = find_env_file()
if not env_path:
return False
content = env_path.read_text()
for key, value in env_vars.items():
# Replace existing or add new
import re
pattern = f'^{key}=.*$'
if re.search(pattern, content, re.MULTILINE):
content = re.sub(pattern, f'{key}={value}', content, flags=re.MULTILINE)
else:
content += f'\n{key}={value}'
env_path.write_text(content)
return True
def test_llm_health(gcp_model_url: str) -> Tuple[bool, str]:
"""Test health endpoint of GCP LLM server."""
if not gcp_model_url:
return False, "GCP_MODEL_URL not configured"
# Extract base URL (remove /generate if present)
base_url = gcp_model_url.replace('/generate', '')
health_url = f"{base_url}/health"
try:
response = requests.get(health_url, timeout=5)
if response.status_code == 200:
return True, f"✓ Health check passed: {response.text[:100]}"
else:
return False, f"✗ Health check failed: {response.status_code}"
except requests.exceptions.Timeout:
return False, f"✗ Health check timed out (5s)"
except requests.exceptions.ConnectionError as e:
return False, f"✗ Connection error: {str(e)}"
except Exception as e:
return False, f"✗ Unexpected error: {str(e)}"
def test_llm_generation(gcp_model_url: str) -> Tuple[bool, str]:
"""Test generation endpoint of GCP LLM server."""
if not gcp_model_url:
return False, "GCP_MODEL_URL not configured"
test_prompt = "What is the capital of France?"
try:
response = requests.post(
gcp_model_url,
json={"prompt": test_prompt},
timeout=30
)
if response.status_code == 200:
try:
data = response.json()
if "response" in data:
result = data["response"][:100]
return True, f"✓ Generation successful: {result}..."
else:
return False, f"✗ Missing 'response' field in JSON: {data}"
except json.JSONDecodeError:
return False, f"✗ Invalid JSON response: {response.text[:100]}"
else:
return False, f"✗ Generation failed: {response.status_code}"
except requests.exceptions.Timeout:
return False, f"✗ Generation timed out (30s)"
except requests.exceptions.ConnectionError as e:
return False, f"✗ Connection error: {str(e)}"
except Exception as e:
return False, f"✗ Unexpected error: {str(e)}"
def interactive_setup() -> Optional[str]:
"""Interactive setup wizard."""
print("\n" + "="*60)
print("GCP LLM Configuration Wizard")
print("="*60 + "\n")
print("Enter your GCP LLM server details:\n")
gcp_ip = input("GCP VM External IP (or hostname): ").strip()
if not gcp_ip:
print("✗ IP address is required")
return None
gcp_port = input("LLM Server Port [9000]: ").strip() or "9000"
gcp_model_url = f"http://{gcp_ip}:{gcp_port}/generate"
print(f"\nConfigured URL: {gcp_model_url}")
# Test connection
print("\nTesting connection...")
health_ok, health_msg = test_llm_health(gcp_model_url)
print(f" Health: {health_msg}")
if health_ok:
print(" Testing generation...")
gen_ok, gen_msg = test_llm_generation(gcp_model_url)
print(f" Generation: {gen_msg}")
if gen_ok:
confirm = input("\n✓ Connection successful! Save to .env? (y/n): ").strip().lower()
if confirm == 'y':
env_vars = {
'DEFAULT_PROVIDER': 'gcp',
'GCP_MODEL_URL': gcp_model_url
}
if save_env_vars(env_vars):
print("✓ Configuration saved to .env")
return gcp_model_url
return gcp_model_url
else:
print("\n✗ Connection failed. Please check:")
print(" 1. GCP VM is running")
print(" 2. LLM server is running on the VM")
print(" 3. Firewall allows port 9000")
print(" 4. IP address is correct")
return None
def auto_setup(gcp_ip: str) -> Optional[str]:
"""Automatic setup with given IP."""
gcp_model_url = f"http://{gcp_ip}:9000/generate"
print(f"\nConfiguring: {gcp_model_url}")
# Test connection
health_ok, health_msg = test_llm_health(gcp_model_url)
print(f" {health_msg}")
if health_ok:
gen_ok, gen_msg = test_llm_generation(gcp_model_url)
print(f" {gen_msg}")
if gen_ok:
env_vars = {
'DEFAULT_PROVIDER': 'gcp',
'GCP_MODEL_URL': gcp_model_url
}
if save_env_vars(env_vars):
print(f"\n✓ Configuration saved to .env")
return gcp_model_url
return None
def test_current_config() -> bool:
"""Test current .env configuration."""
env_vars = load_env_vars()
gcp_model_url = env_vars.get('GCP_MODEL_URL')
if not gcp_model_url:
print("✗ GCP_MODEL_URL not configured in .env")
return False
print(f"Testing: {gcp_model_url}\n")
# Test health
health_ok, health_msg = test_llm_health(gcp_model_url)
print(f"Health: {health_msg}")
if not health_ok:
return False
# Test generation
gen_ok, gen_msg = test_llm_generation(gcp_model_url)
print(f"Generation: {gen_msg}")
if health_ok and gen_ok:
print("\n✓ All tests passed!")
return True
else:
print("\n✗ Some tests failed. Check configuration.")
return False
def show_status() -> None:
"""Show current configuration status."""
env_vars = load_env_vars()
print("\n" + "="*60)
print("Current GCP LLM Configuration")
print("="*60 + "\n")
print(f"DEFAULT_PROVIDER: {env_vars.get('DEFAULT_PROVIDER', 'NOT SET')}")
print(f"GCP_MODEL_URL: {env_vars.get('GCP_MODEL_URL', 'NOT SET')}")
gcp_model_url = env_vars.get('GCP_MODEL_URL')
if gcp_model_url:
print(f"\nTesting connection...")
health_ok, health_msg = test_llm_health(gcp_model_url)
print(f" Health: {health_msg}")
def main():
parser = argparse.ArgumentParser(
description="Configure ShopMindAI for GCP LLM",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Interactive setup
python setup_gcp_llm.py --interactive
# Quick setup with IP
python setup_gcp_llm.py --ip 35.192.1.2
# Test current configuration
python setup_gcp_llm.py --test
# Show status
python setup_gcp_llm.py --status
"""
)
parser.add_argument('--interactive', '-i', action='store_true',
help='Interactive configuration wizard')
parser.add_argument('--ip', type=str,
help='GCP VM IP address')
parser.add_argument('--test', '-t', action='store_true',
help='Test current configuration')
parser.add_argument('--status', '-s', action='store_true',
help='Show current status')
args = parser.parse_args()
# If no args, show status
if not any(vars(args).values()):
show_status()
return
# Handle each option
if args.interactive:
interactive_setup()
elif args.ip:
auto_setup(args.ip)
elif args.test:
test_current_config()
elif args.status:
show_status()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n✗ Cancelled")
sys.exit(1)
except Exception as e:
print(f"\n✗ Error: {str(e)}")
sys.exit(1)