File size: 1,838 Bytes
a72497b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
import os
import openai
from typing import Optional
# If you're using tools like Bing search or SerpAPI, you can integrate them here.
# from tools.search import web_search  

openai.api_key = os.getenv("OPENAI_API_KEY")

def answer_question(question: str, file_context: Optional[str] = None, do_search: bool = False) -> str:
    """
    Answers a GAIA-level question using GPT-4, with optional file context.
    """
    try:
        # Optional external search (disabled for local-only runs)
        search_summary = ""
        # if do_search:
        #     search_summary = web_search(question)

        # Build the system prompt
        system_prompt = """You are a highly capable assistant solving benchmark questions for the GAIA dataset. 
Each question requires a correct and concise answer. Use the context below (if provided), and do not hallucinate.
If you're not certain, reason step-by-step, then provide the best possible final answer.
Format your answer as a single short paragraph or number."""

        # Build the user prompt
        user_parts = []
        if file_context:
            user_parts.append(f"File context:\n{file_context.strip()}")
        if search_summary:
            user_parts.append(f"Web search:\n{search_summary.strip()}")
        user_parts.append(f"Question:\n{question}")
        user_prompt = "\n\n".join(user_parts)

        # Call GPT-4
        response = openai.ChatCompletion.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt}
            ],
            temperature=0.2,
            max_tokens=300
        )

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

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