Spaces:
Sleeping
Sleeping
Create app/agent.py
Browse files- app/agent.py +49 -0
app/agent.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}"
|