dawid-lorek commited on
Commit
fde864c
·
verified ·
1 Parent(s): bd702b9

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +46 -46
agent.py CHANGED
@@ -1,52 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  answer = response.choices[0].message.content.strip()
2
  final_line = ""
3
  for line in answer.splitlines():
4
  if line.strip().lower().startswith("final answer:"):
5
  final_line = line.split(":", 1)[-1].strip(" .\"'")
6
  break
7
-
8
- bads = [
9
- "", "unknown", "unable to determine", "unable to provide page numbers",
10
- "unable to access video content directly", "unable to analyze video content",
11
- "unable to determine without code", "unable to determine without file",
12
- "follow the steps to locate the paper and find the nasa award number in the acknowledgment section",
13
- "i am unable to view images or access external content directly", "unable to determine without access to the file",
14
- "no results found", "n/a", "[your final answer]", "i'm sorry", "i apologize"
15
- ]
16
- if final_line.lower() in bads or final_line.lower().startswith("unable") or final_line.lower().startswith("i'm sorry") or final_line.lower().startswith("i apologize"):
17
- # --- Try to extract a plausible answer from web or file ---
18
- # Example: For numbers
19
- numbers = re.findall(r'\b\d{2,}\b', search_snippet)
20
- if numbers:
21
- return numbers[0]
22
- # Example: For possible names (capitalize words)
23
- words = re.findall(r'\b[A-Z][a-z]{2,}\b', search_snippet)
24
- if words:
25
- return words[0]
26
- # Example: For Excel, code, or file extraction, return the result
27
- if file_result:
28
- file_numbers = re.findall(r'\b\d{2,}\b', file_result)
29
- if file_numbers:
30
- return file_numbers[0]
31
- file_words = re.findall(r'\b[A-Z][a-z]{2,}\b', file_result)
32
- if file_words:
33
- return file_words[0]
34
- # --- Try to re-ask the LLM to answer "without apologies" ---
35
- retry_prompt = (
36
- "Based ONLY on the search results and/or file content above, return a direct answer to the question. "
37
- "If you do not know, make your best plausible guess. Do NOT apologize or say you cannot assist. "
38
- f"File: {file_result}\n\nWeb: {search_snippet}\n\nQuestion: {question}\nFINAL ANSWER:"
39
- )
40
- response2 = self.llm.chat.completions.create(
41
- model="gpt-4o",
42
- messages=[{"role": "system", "content": retry_prompt}],
43
- temperature=0.1,
44
- max_tokens=128,
45
- )
46
- retry_answer = response2.choices[0].message.content.strip()
47
- for line in retry_answer.splitlines():
48
- if line.strip().lower().startswith("final answer:"):
49
- return line.split(":", 1)[-1].strip(" .\"'")
50
- if retry_answer:
51
- return retry_answer.strip(" .\"'")
52
- return final_line
 
1
+ import os
2
+ import re
3
+ import requests
4
+ from openai import OpenAI
5
+ from duckduckgo_search import DDGS
6
+
7
+ PROMPT = (
8
+ "You are a general AI assistant. I will ask you a question. "
9
+ "Report your thoughts, and finish your answer with the following template: "
10
+ "FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
11
+ "If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
12
+ "If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
13
+ "If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
14
+ )
15
+
16
+ class BasicAgent:
17
+ def __init__(self):
18
+ self.llm = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
19
+ print("BasicAgent initialized.")
20
+
21
+ def web_search(self, query: str, max_results: int = 5) -> str:
22
+ try:
23
+ with DDGS() as ddgs:
24
+ results = list(ddgs.text(query, max_results=max_results))
25
+ if not results:
26
+ return ""
27
+ formatted_results = ""
28
+ for i, result in enumerate(results, 1):
29
+ title = result.get('title', '')
30
+ body = result.get('body', '')
31
+ href = result.get('href', '')
32
+ formatted_results += f"{i}. {title}\n URL: {href}\n Description: {body}\n\n"
33
+ return formatted_results
34
+ except Exception as e:
35
+ return ""
36
+
37
+ def __call__(self, question: str, task_id: str = None) -> str:
38
+ search_snippet = self.web_search(question)
39
+ prompt = PROMPT + f"\n\nWeb search results:\n{search_snippet}\n\nQuestion: {question}"
40
+ response = self.llm.chat.completions.create(
41
+ model="gpt-4o",
42
+ messages=[{"role": "system", "content": prompt}],
43
+ temperature=0.0,
44
+ max_tokens=512,
45
+ )
46
  answer = response.choices[0].message.content.strip()
47
  final_line = ""
48
  for line in answer.splitlines():
49
  if line.strip().lower().startswith("final answer:"):
50
  final_line = line.split(":", 1)[-1].strip(" .\"'")
51
  break
52
+ return final_line or answer