FD900 commited on
Commit
5e5d067
·
verified ·
1 Parent(s): 950a522

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +19 -31
agent.py CHANGED
@@ -1,37 +1,25 @@
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
- file_content = ""
14
- if question_obj.get("file_path"):
15
- file_content = load_file_if_any(question_obj["file_path"])
16
-
17
- user_prompt = f"""Question:
18
- {question_obj['question']}
19
-
20
- Attached file content (if any):
21
- {file_content}
22
 
23
- Final answer:"""
24
-
25
- # Use GPT-3.5-turbo (free-tier)
26
- response = client.chat.completions.create(
27
- model="gpt-3.5-turbo",
28
- messages=[
29
- {"role": "system", "content": SYSTEM_PROMPT},
30
- {"role": "user", "content": user_prompt}
31
- ]
32
- )
33
-
34
- return response.choices[0].message.content.strip()
35
 
 
 
 
 
 
 
 
36
  except Exception as e:
37
- return f"[ERROR] Failed to answer: {str(e)}"
 
1
+ # agent.py (Updated for Hugging Face inference with FLAN-T5)
 
 
2
 
3
+ from transformers import pipeline
4
 
5
+ # Load the Hugging Face inference pipeline
6
+ qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
 
7
 
8
+ def answer_question(question: str, file_context: str = None, do_search: bool = False) -> str:
9
+ """
10
+ Answers the question using file context (if any) with a Hugging Face model.
 
 
 
 
 
 
 
 
11
 
12
+ Args:
13
+ question (str): The question to be answered.
14
+ file_context (str, optional): Optional context extracted from a file.
15
+ do_search (bool): Ignored for local mode.
 
 
 
 
 
 
 
 
16
 
17
+ Returns:
18
+ str: The generated answer.
19
+ """
20
+ prompt = question if not file_context else f"Context: {file_context}\nQuestion: {question}"
21
+ try:
22
+ result = qa_pipeline(prompt, max_length=256, do_sample=False)
23
+ return result[0]["generated_text"].strip()
24
  except Exception as e:
25
+ return f"[ERROR] Hugging Face pipeline failed: {e}"