Spaces:
Running
Running
Upload src/llm/llm_client.py with huggingface_hub
Browse files- src/llm/llm_client.py +18 -8
src/llm/llm_client.py
CHANGED
|
@@ -40,17 +40,27 @@ class VLLMClient(LLMClient):
|
|
| 40 |
class GroqClient(LLMClient):
|
| 41 |
def __init__(self, api_key: str = None, model: str = "llama-3.3-70b-versatile"):
|
| 42 |
self.api_key = api_key or os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
| 43 |
self.model = model
|
| 44 |
self.client = openai.OpenAI(
|
| 45 |
base_url="https://api.groq.com/openai/v1",
|
| 46 |
-
api_key=self.api_key
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
def chat(self, messages: List[Dict[str, str]], **kwargs) -> str:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
class GroqClient(LLMClient):
|
| 41 |
def __init__(self, api_key: str = None, model: str = "llama-3.3-70b-versatile"):
|
| 42 |
self.api_key = api_key or os.getenv("GROQ_API_KEY")
|
| 43 |
+
if not self.api_key:
|
| 44 |
+
raise ValueError("GROQ_API_KEY not found in environment variables")
|
| 45 |
self.model = model
|
| 46 |
self.client = openai.OpenAI(
|
| 47 |
base_url="https://api.groq.com/openai/v1",
|
| 48 |
+
api_key=self.api_key,
|
| 49 |
+
timeout=30.0 # Add timeout
|
| 50 |
)
|
| 51 |
|
| 52 |
def chat(self, messages: List[Dict[str, str]], **kwargs) -> str:
|
| 53 |
+
try:
|
| 54 |
+
response = self.client.chat.completions.create(
|
| 55 |
+
model=self.model,
|
| 56 |
+
messages=messages,
|
| 57 |
+
timeout=30.0, # Add timeout to request
|
| 58 |
+
**kwargs
|
| 59 |
+
)
|
| 60 |
+
return response.choices[0].message.content
|
| 61 |
+
except Exception as e:
|
| 62 |
+
# Better error message
|
| 63 |
+
error_msg = f"Groq API Error: {str(e)}"
|
| 64 |
+
if "Connection error" in str(e):
|
| 65 |
+
error_msg += "\n\nPossible causes:\n1. Network blocked by Hugging Face Spaces\n2. Groq API is down\n3. Invalid API key"
|
| 66 |
+
raise Exception(error_msg) from e
|