samsonDzealot commited on
Commit
75ae6d4
·
verified ·
1 Parent(s): a4f149c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -36
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
- print(f"Calling LLM. Conversation history length: {len(conversation_history)}")
51
- headers = {
52
- "Authorization": f"Bearer {self.api_key}",
53
- "Content-Type": "application/json",
54
- "HTTP-Referer": os.getenv("SPACE_ID", "http://localhost"), # Recommended by OpenRouter
55
- "X-Title": os.getenv("SPACE_TITLE", "Test Agent") # Recommended by OpenRouter
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  }
57
- payload = {
58
- "model": self.model_name,
59
- "messages": conversation_history,
60
- "temperature": 0.7, # Adjust as needed
61
- # "max_tokens": 1000 # Adjust as needed
62
- }
63
- try:
64
- response = requests.post(self.llm_api_url, headers=headers, json=payload, timeout=120)
65
- response.raise_for_status()
66
- llm_response_data = response.json()
67
- if llm_response_data.get("choices") and llm_response_data["choices"][0].get("message"):
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")