FD900 commited on
Commit
85a8961
·
verified ·
1 Parent(s): ccb690d

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +27 -38
agent.py CHANGED
@@ -1,49 +1,38 @@
1
  import os
2
- import openai
3
- from typing import Optional
4
- # If you're using tools like Bing search or SerpAPI, you can integrate them here.
5
- # from tools.search import web_search
6
 
7
- openai.api_key = os.getenv("OPENAI_API_KEY")
8
 
9
- def answer_question(question: str, file_context: Optional[str] = None, do_search: bool = False) -> str:
10
- """
11
- Answers a GAIA-level question using GPT-4, with optional file context.
12
- """
 
13
  try:
14
- # Optional external search (disabled for local-only runs)
15
- search_summary = ""
16
- # if do_search:
17
- # search_summary = web_search(question)
18
-
19
- # Build the system prompt
20
- system_prompt = """You are a highly capable assistant solving benchmark questions for the GAIA dataset.
21
- Each question requires a correct and concise answer. Use the context below (if provided), and do not hallucinate.
22
- If you're not certain, reason step-by-step, then provide the best possible final answer.
23
- Format your answer as a single short paragraph or number."""
24
-
25
- # Build the user prompt
26
- user_parts = []
27
- if file_context:
28
- user_parts.append(f"File context:\n{file_context.strip()}")
29
- if search_summary:
30
- user_parts.append(f"Web search:\n{search_summary.strip()}")
31
- user_parts.append(f"Question:\n{question}")
32
- user_prompt = "\n\n".join(user_parts)
33
-
34
- # Call GPT-4
35
- response = openai.ChatCompletion.create(
36
  model="gpt-4",
37
  messages=[
38
- {"role": "system", "content": system_prompt},
39
  {"role": "user", "content": user_prompt}
40
- ],
41
- temperature=0.2,
42
- max_tokens=300
43
  )
44
 
45
- answer = response.choices[0].message.content.strip()
46
- return answer
47
 
48
  except Exception as e:
49
- return f"[ERROR] Failed to answer: {e}"
 
1
  import os
2
+ from openai import OpenAI
3
+ from tools.file_loader import load_file_if_any
 
 
4
 
5
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
6
 
7
+ SYSTEM_PROMPT = """You are an advanced AI agent participating in the GAIA benchmark.
8
+ For each question, read the text and any attached file, then provide a concise and accurate final answer.
9
+ Only return the answer itself no explanations or formatting."""
10
+
11
+ def answer_question(question_obj):
12
  try:
13
+ # Load any attached file content
14
+ file_content = ""
15
+ if question_obj.get("file_path"):
16
+ file_content = load_file_if_any(question_obj["file_path"])
17
+
18
+ user_prompt = f"""Question:
19
+ {question_obj['question']}
20
+
21
+ Attached file content (if any):
22
+ {file_content}
23
+
24
+ Final answer:"""
25
+
26
+ # Call OpenAI GPT-4
27
+ response = client.chat.completions.create(
 
 
 
 
 
 
 
28
  model="gpt-4",
29
  messages=[
30
+ {"role": "system", "content": SYSTEM_PROMPT},
31
  {"role": "user", "content": user_prompt}
32
+ ]
 
 
33
  )
34
 
35
+ return response.choices[0].message.content.strip()
 
36
 
37
  except Exception as e:
38
+ return f"[ERROR] Failed to answer: {str(e)}"