milwright commited on
Commit
a4b4814
Β·
1 Parent(s): d67177b

fix: make preview tab use actual openrouter api calls with config from main tab

Browse files
Files changed (1) hide show
  1. app.py +70 -9
app.py CHANGED
@@ -509,15 +509,76 @@ class SpaceGenerator:
509
  f"Get your API key at: https://openrouter.ai/keys"
510
  )
511
  else:
512
- # Show actual configuration being used
513
- response = (
514
- f"[Preview Mode - Configuration Active]\n\n"
515
- f"**Model:** {config.get('model', 'Unknown')}\n"
516
- f"**Temperature:** {config.get('temperature', 0.7)}\n"
517
- f"**Max Tokens:** {config.get('max_tokens', 750)}\n"
518
- f"**API Key Variable:** {config.get('api_key_var', 'Not set')}\n\n"
519
- f"In the deployed space, this configuration will be used to generate actual responses."
520
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
 
522
  # Update chat history
523
  chat_history = chat_history + [
 
509
  f"Get your API key at: https://openrouter.ai/keys"
510
  )
511
  else:
512
+ # Make actual API call to OpenRouter
513
+ try:
514
+ headers = {
515
+ "Authorization": f"Bearer {api_key}",
516
+ "Content-Type": "application/json"
517
+ }
518
+
519
+ # Build messages for API
520
+ messages = [{"role": "system", "content": config.get('system_prompt', 'You are a helpful AI assistant.')}]
521
+
522
+ # Add conversation history
523
+ for msg in chat_history:
524
+ if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
525
+ messages.append({
526
+ "role": msg['role'],
527
+ "content": msg['content']
528
+ })
529
+
530
+ # Add current message
531
+ messages.append({
532
+ "role": "user",
533
+ "content": message
534
+ })
535
+
536
+ # Get grounding context from URLs if configured
537
+ grounding_context = ""
538
+ urls = config.get('grounding_urls', [])
539
+ if urls and len(urls) > 0:
540
+ grounding_context = "\nπŸ“š **Reference Context:**\n"
541
+ for i, url in enumerate(urls[:2], 1): # Primary URLs only
542
+ try:
543
+ content = fetch_url_content(url)
544
+ if not content.startswith("❌") and not content.startswith("⏱️"):
545
+ grounding_context += f"\n**Source {i}:** {content}\n"
546
+ except:
547
+ pass
548
+
549
+ # Add grounding context to the user message if available
550
+ if grounding_context:
551
+ messages[-1]["content"] = f"{grounding_context}\n\n{message}"
552
+
553
+ data = {
554
+ "model": config.get('model', 'openai/gpt-3.5-turbo'),
555
+ "messages": messages,
556
+ "temperature": config.get('temperature', 0.7),
557
+ "max_tokens": config.get('max_tokens', 750),
558
+ "stream": False
559
+ }
560
+
561
+ response = requests.post(
562
+ "https://openrouter.ai/api/v1/chat/completions",
563
+ headers=headers,
564
+ json=data,
565
+ timeout=30
566
+ )
567
+
568
+ if response.status_code == 200:
569
+ result = response.json()
570
+ response = result['choices'][0]['message']['content']
571
+ else:
572
+ error_data = response.json()
573
+ error_message = error_data.get('error', {}).get('message', 'Unknown error')
574
+ response = f"❌ API Error ({response.status_code}): {error_message}"
575
+
576
+ except requests.exceptions.Timeout:
577
+ response = "⏰ Request timeout (30s limit). Try a shorter message or different model."
578
+ except requests.exceptions.ConnectionError:
579
+ response = "🌐 Connection error. Check your internet connection and try again."
580
+ except Exception as e:
581
+ response = f"❌ Error: {str(e)}"
582
 
583
  # Update chat history
584
  chat_history = chat_history + [