Spaces:
Sleeping
Sleeping
Download app/agent.py from Team-PDI/IDP_webapp: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/Team-PDI/IDP_webapp/resolve/main/app/agent.py
- Command line
-
hf download hf://spaces/Team-PDI/IDP_webapp/app/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/Team-PDI/IDP_webapp/resolve/main/app/agent.py
1.74 kB
| from groq import Groq | |
| import groq | |
| import os | |
| import re | |
| # Tried in this order; each model has its own free daily limit | |
| MODELS = [ | |
| "openai/gpt-oss-20b", | |
| "openai/gpt-oss-120b", | |
| "qwen/qwen3.8-27b", | |
| ] | |
| FALLBACK_ANSWER = "Hmm, daar moet ik even over nadenken..." | |
| class GroqAgent: | |
| def __init__(self, system_content): | |
| self.client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| self.system_content = system_content | |
| def request_llm_response(self, message_content): | |
| messages = [ | |
| {"role": "system", "content": self.system_content}, | |
| {"role": "user", "content": message_content}, | |
| ] | |
| for model in MODELS: | |
| try: | |
| kwargs = { | |
| "model": model, | |
| "messages": messages, | |
| "max_completion_tokens": 400, | |
| } | |
| if model.startswith("openai/"): | |
| kwargs["reasoning_effort"] = "low" | |
| chat_completion = self.client.chat.completions.create(**kwargs) | |
| message = chat_completion.choices[0].message.content or "" | |
| # Qwen can include its thinking in <think> tags; strip it | |
| message = re.sub(r"<think>.*?</think>", "", message, flags=re.DOTALL).strip() | |
| if message: | |
| return message | |
| print(f"[agent] {model} returned an empty answer, trying next model") | |
| except groq.RateLimitError: | |
| print(f"[agent] {model} hit its rate limit, trying next model") | |
| except groq.APIError as e: | |
| print(f"[agent] {model} failed: {e}") | |
| return FALLBACK_ANSWER |