medmiu commited on
Commit
e2d1248
·
verified ·
1 Parent(s): aeb9fdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -1
app.py CHANGED
@@ -207,6 +207,64 @@ agent = workflow.compile()
207
 
208
  # 6. Run Function
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  def get_concise_answer(prompt):
211
  response = requests.post(
212
  url="https://openrouter.ai/api/v1/chat/completions",
@@ -241,7 +299,7 @@ def get_concise_answer(prompt):
241
  data = response.json()
242
  model_response = data['choices'][0]['message']['content']
243
  return model_response
244
-
245
  def run_groq_agent(query: str):
246
  response = agent.invoke({
247
  "messages": [HumanMessage(content=query)],
 
207
 
208
  # 6. Run Function
209
 
210
+ def get_concise_answer(
211
+ prompt: str,
212
+ model: str = "deepseek/deepseek-chat-v3-0324:free",
213
+ timeout: int = 1000,
214
+ ) -> Optional[str]:
215
+ """
216
+ Fetches a concise AI response from OpenRouter API.
217
+
218
+ Args:
219
+ prompt (str): The user's input/question.
220
+ model (str): The AI model to use (default: deepseek-chat-v3-0324).
221
+ timeout (int): Request timeout in seconds (default: 10).
222
+
223
+ Returns:
224
+ str: The concise AI response, or None if an error occurs.
225
+ """
226
+ try:
227
+ response = requests.post(
228
+ url="https://openrouter.ai/api/v1/chat/completions",
229
+ headers=HEADERS, # Ensure HEADERS is defined elsewhere
230
+ data=json.dumps({
231
+ "model": model,
232
+ "messages": [
233
+ {
234
+ "role": "system",
235
+ "content": """
236
+ You are an AI assistant that answers questions directly and concisely.
237
+ Respond with only the factual answer, no prefixes, explanations, or extra text.
238
+ Examples:
239
+ - User: Who is the CEO of Tesla?
240
+ - AI: Elon Musk
241
+ - User: Capital of Japan?
242
+ - AI: Tokyo
243
+ """
244
+ },
245
+ {"role": "user", "content": prompt}
246
+ ],
247
+ }),
248
+ timeout=timeout,
249
+ )
250
+ response.raise_for_status() # Raises HTTPError for bad responses (4xx, 5xx)
251
+ data = response.json()
252
+
253
+ # Check if response has the expected structure
254
+ if "choices" not in data or not data["choices"]:
255
+ return None
256
+
257
+ model_response = data["choices"][0]["message"]["content"]
258
+ return model_response.strip() # Remove extra whitespace
259
+
260
+ except requests.exceptions.RequestException as e:
261
+ print(f"API Request failed: {e}")
262
+ return None
263
+ except (KeyError, json.JSONDecodeError) as e:
264
+ print(f"Failed to parse API response: {e}")
265
+ return None
266
+
267
+ '''
268
  def get_concise_answer(prompt):
269
  response = requests.post(
270
  url="https://openrouter.ai/api/v1/chat/completions",
 
299
  data = response.json()
300
  model_response = data['choices'][0]['message']['content']
301
  return model_response
302
+ '''
303
  def run_groq_agent(query: str):
304
  response = agent.invoke({
305
  "messages": [HumanMessage(content=query)],