Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,6 @@ MODEL_NAME = "deepseek-ai/Deepseek-V3"
|
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("BasicAgent initialized.")
|
| 20 |
-
load_dotenv()
|
| 21 |
self.api_key = os.getenv("TEST_AGENT_KEY")
|
| 22 |
if not self.api_key:
|
| 23 |
raise ValueError("OpenRouter API Key not found.")
|
|
@@ -47,42 +46,41 @@ class BasicAgent:
|
|
| 47 |
print(f"Agent tools initialized: {list(self.tools.keys())}")
|
| 48 |
|
| 49 |
def _call_llm(self, conversation_history: list) -> str:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
content = llm_response_data["choices"][0]["message"].get("content", "").strip()
|
| 69 |
-
print(f"LLM raw response: {content[:200]}...")
|
| 70 |
-
return content
|
| 71 |
-
else:
|
| 72 |
-
print(f"LLM response malformed: {llm_response_data}")
|
| 73 |
-
return "Error: LLM response was malformed."
|
| 74 |
-
except requests.exceptions.Timeout:
|
| 75 |
-
print("Error: LLM API call timed out.")
|
| 76 |
-
return "Error: LLM call timed out."
|
| 77 |
-
except requests.exceptions.RequestException as e:
|
| 78 |
-
print(f"Error calling LLM API: {e}")
|
| 79 |
-
if e.response is not None:
|
| 80 |
-
print(f"LLM Error Response Status: {e.response.status_code}")
|
| 81 |
-
print(f"LLM Error Response Body: {e.response.text}")
|
| 82 |
-
return f"Error: Failed to communicate with LLM. {str(e)}"
|
| 83 |
-
except Exception as e:
|
| 84 |
-
print(f"An unexpected error occurred during LLM call: {e}")
|
| 85 |
-
return f"Error: An unexpected error occurred communicating with LLM. {str(e)}"
|
| 86 |
|
| 87 |
def __call__(self, question_data: dict) -> str:
|
| 88 |
task_id = question_data.get("task_id")
|
|
|
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("BasicAgent initialized.")
|
|
|
|
| 20 |
self.api_key = os.getenv("TEST_AGENT_KEY")
|
| 21 |
if not self.api_key:
|
| 22 |
raise ValueError("OpenRouter API Key not found.")
|
|
|
|
| 46 |
print(f"Agent tools initialized: {list(self.tools.keys())}")
|
| 47 |
|
| 48 |
def _call_llm(self, conversation_history: list) -> str:
|
| 49 |
+
print(f"Calling LLM. Conversation history length: {len(conversation_history)}")
|
| 50 |
+
|
| 51 |
+
# Hugging Face headers (simpler)
|
| 52 |
+
headers = {
|
| 53 |
+
"Authorization": f"Bearer {self.api_key}", # should be your HF token
|
| 54 |
+
"Content-Type": "application/json"
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Format conversation into a single prompt string
|
| 58 |
+
# You can improve this formatting later if needed
|
| 59 |
+
prompt = ""
|
| 60 |
+
for turn in conversation_history:
|
| 61 |
+
role = turn.get("role", "user").capitalize()
|
| 62 |
+
content = turn.get("content", "")
|
| 63 |
+
prompt += f"{role}: {content}\n"
|
| 64 |
+
prompt += "Assistant:"
|
| 65 |
+
|
| 66 |
+
# Payload for Hugging Face
|
| 67 |
+
payload = {
|
| 68 |
+
"inputs": prompt,
|
| 69 |
+
"parameters": {
|
| 70 |
+
"temperature": 0.7,
|
| 71 |
+
"max_new_tokens": 512
|
| 72 |
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
url = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-V3"
|
| 76 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
return response.json()[0]["generated_text"].split("Assistant:")[-1].strip()
|
| 80 |
+
except Exception:
|
| 81 |
+
return f"Error: {response.text}"
|
| 82 |
+
|
| 83 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def __call__(self, question_data: dict) -> str:
|
| 86 |
task_id = question_data.get("task_id")
|