Spaces:
Sleeping
Sleeping
| 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}" |