File size: 1,277 Bytes
a72497b
85a8961
 
a72497b
85a8961
a72497b
a1a42c2
 
 
 
85a8961
 
a72497b
a1a42c2
85a8961
 
 
 
a1a42c2
85a8961
 
 
 
 
 
 
 
a1a42c2
85a8961
a72497b
 
85a8961
a72497b
85a8961
a72497b
 
85a8961
a72497b
 
85a8961
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
from openai import OpenAI
from tools.file_loader import load_file_if_any

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

SYSTEM_PROMPT = """You are an advanced AI agent participating in the GAIA benchmark.
Your task is to provide accurate, concise answers to each question using both the text of the question and any provided file content.
Only return the final answer. Do not include explanations, formatting, or phrases like "Answer:".
Be exact and to the point."""

def answer_question(question_obj):
    try:
        # Load file content if available
        file_content = ""
        if question_obj.get("file_path"):
            file_content = load_file_if_any(question_obj["file_path"])

        # Compose user prompt
        user_prompt = f"""Question:
{question_obj['question']}

Attached file content (if any):
{file_content}

Final answer:"""

        # Call OpenAI
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": user_prompt}
            ]
        )

        return response.choices[0].message.content.strip()

    except Exception as e:
        return f"[ERROR] Failed to answer: {str(e)}"