Spaces:
Running
Running
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
app.py
CHANGED
@@ -509,15 +509,76 @@ class SpaceGenerator:
|
|
509 |
f"Get your API key at: https://openrouter.ai/keys"
|
510 |
)
|
511 |
else:
|
512 |
-
#
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
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 + [
|