Spaces:
Runtime error
Runtime error
Cascade Bot
commited on
Commit
·
4439b52
1
Parent(s):
6166f7f
feat: add Groq API integration
Browse files- Added groq_api.py to handle Groq API interactions
- Updated __init__.py to include GroqAPI in exports
- Added GroqAPI initialization in ChatInterface
- Updated _handle_chat to use Groq API as fallback
- Improved error handling and logging
- api/__init__.py +2 -1
- api/groq_api.py +70 -0
- app.py +36 -16
api/__init__.py
CHANGED
@@ -2,5 +2,6 @@
|
|
2 |
|
3 |
from .openai_compatible import OpenAICompatibleAPI
|
4 |
from .venture_api import VentureAPI
|
|
|
5 |
|
6 |
-
__all__ = ['OpenAICompatibleAPI', 'VentureAPI']
|
|
|
2 |
|
3 |
from .openai_compatible import OpenAICompatibleAPI
|
4 |
from .venture_api import VentureAPI
|
5 |
+
from .groq_api import GroqAPI
|
6 |
|
7 |
+
__all__ = ['OpenAICompatibleAPI', 'VentureAPI', 'GroqAPI']
|
api/groq_api.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Groq API Integration
|
3 |
+
------------------
|
4 |
+
Handles interactions with the Groq API for language model inference.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import os
|
8 |
+
import logging
|
9 |
+
from typing import Dict, Any, Optional
|
10 |
+
import requests
|
11 |
+
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
+
class GroqAPI:
|
15 |
+
"""Handles interactions with the Groq API."""
|
16 |
+
|
17 |
+
def __init__(self, api_key: Optional[str] = None):
|
18 |
+
"""Initialize the Groq API client."""
|
19 |
+
self.api_key = api_key or os.getenv('GROQ_API_KEY')
|
20 |
+
self.base_url = "https://api.groq.com/v1"
|
21 |
+
self.default_model = "mixtral-8x7b-32768"
|
22 |
+
|
23 |
+
async def predict(self, prompt: str, model: Optional[str] = None) -> Dict[str, Any]:
|
24 |
+
"""Make a prediction using the Groq API."""
|
25 |
+
try:
|
26 |
+
if not self.api_key:
|
27 |
+
return {
|
28 |
+
"success": False,
|
29 |
+
"error": "GROQ_API_KEY not set in environment variables",
|
30 |
+
"answer": "Error: GROQ_API_KEY not configured"
|
31 |
+
}
|
32 |
+
|
33 |
+
headers = {
|
34 |
+
"Authorization": f"Bearer {self.api_key}",
|
35 |
+
"Content-Type": "application/json"
|
36 |
+
}
|
37 |
+
|
38 |
+
data = {
|
39 |
+
"model": model or self.default_model,
|
40 |
+
"messages": [{"role": "user", "content": prompt}],
|
41 |
+
"temperature": 0.7,
|
42 |
+
"max_tokens": 1000
|
43 |
+
}
|
44 |
+
|
45 |
+
response = requests.post(
|
46 |
+
f"{self.base_url}/chat/completions",
|
47 |
+
headers=headers,
|
48 |
+
json=data
|
49 |
+
)
|
50 |
+
|
51 |
+
if response.status_code == 200:
|
52 |
+
result = response.json()
|
53 |
+
return {
|
54 |
+
"success": True,
|
55 |
+
"answer": result["choices"][0]["message"]["content"]
|
56 |
+
}
|
57 |
+
else:
|
58 |
+
return {
|
59 |
+
"success": False,
|
60 |
+
"error": f"API Error: {response.status_code}",
|
61 |
+
"answer": f"Error: {response.text}"
|
62 |
+
}
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"Error calling Groq API: {str(e)}")
|
66 |
+
return {
|
67 |
+
"success": False,
|
68 |
+
"error": str(e),
|
69 |
+
"answer": f"Error: {str(e)}"
|
70 |
+
}
|
app.py
CHANGED
@@ -34,6 +34,7 @@ from reasoning import (
|
|
34 |
)
|
35 |
from api.openai_compatible import OpenAICompatibleAPI
|
36 |
from api.venture_api import VentureAPI
|
|
|
37 |
|
38 |
# Configure logging
|
39 |
logging.basicConfig(level=logging.INFO)
|
@@ -99,6 +100,7 @@ class ChatInterface:
|
|
99 |
self.orchestrator = AgentOrchestrator(config)
|
100 |
self.team_manager = TeamManager(self.orchestrator)
|
101 |
self.reasoning_engine = UnifiedReasoningEngine()
|
|
|
102 |
|
103 |
# Set up the agentic system
|
104 |
self.agentic_system = AgenticSystem(config)
|
@@ -412,22 +414,40 @@ class ChatInterface:
|
|
412 |
async def _handle_chat(self, message: str) -> str:
|
413 |
"""Handle general chat interactions with error recovery."""
|
414 |
try:
|
415 |
-
#
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
431 |
|
432 |
except Exception as e:
|
433 |
logger.error(f"Error in chat response: {str(e)}")
|
|
|
34 |
)
|
35 |
from api.openai_compatible import OpenAICompatibleAPI
|
36 |
from api.venture_api import VentureAPI
|
37 |
+
from api.groq_api import GroqAPI
|
38 |
|
39 |
# Configure logging
|
40 |
logging.basicConfig(level=logging.INFO)
|
|
|
100 |
self.orchestrator = AgentOrchestrator(config)
|
101 |
self.team_manager = TeamManager(self.orchestrator)
|
102 |
self.reasoning_engine = UnifiedReasoningEngine()
|
103 |
+
self.groq_api = GroqAPI()
|
104 |
|
105 |
# Set up the agentic system
|
106 |
self.agentic_system = AgenticSystem(config)
|
|
|
414 |
async def _handle_chat(self, message: str) -> str:
|
415 |
"""Handle general chat interactions with error recovery."""
|
416 |
try:
|
417 |
+
# First try using the reasoning engine
|
418 |
+
try:
|
419 |
+
result = await self.reasoning_engine.reason(
|
420 |
+
query=message,
|
421 |
+
context={
|
422 |
+
"chat_history": self.chat_history,
|
423 |
+
"active_objectives": self.active_objectives,
|
424 |
+
"groq_api": self.groq_api
|
425 |
+
}
|
426 |
+
)
|
427 |
+
|
428 |
+
# Handle UnifiedResult object
|
429 |
+
if isinstance(result, UnifiedResult):
|
430 |
+
if not result.success:
|
431 |
+
# If reasoning engine fails, fallback to Groq API
|
432 |
+
groq_result = await self.groq_api.predict(message)
|
433 |
+
if groq_result["success"]:
|
434 |
+
return groq_result["answer"]
|
435 |
+
else:
|
436 |
+
return "I encountered an error. Please try rephrasing your question."
|
437 |
+
return result.answer if hasattr(result, 'answer') else str(result)
|
438 |
+
elif isinstance(result, dict):
|
439 |
+
return result.get('response', str(result))
|
440 |
+
else:
|
441 |
+
return str(result)
|
442 |
+
|
443 |
+
except Exception as reasoning_error:
|
444 |
+
logger.error(f"Reasoning engine error: {str(reasoning_error)}")
|
445 |
+
# Fallback to Groq API
|
446 |
+
groq_result = await self.groq_api.predict(message)
|
447 |
+
if groq_result["success"]:
|
448 |
+
return groq_result["answer"]
|
449 |
+
else:
|
450 |
+
raise Exception(f"Both reasoning engine and Groq API failed: {groq_result.get('error')}")
|
451 |
|
452 |
except Exception as e:
|
453 |
logger.error(f"Error in chat response: {str(e)}")
|