Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import pandas as pd
|
|
| 5 |
|
| 6 |
# Import smol-agent and tool components
|
| 7 |
from smolagents import CodeAgent, LiteLLMModel, tool
|
| 8 |
-
from smolagents.tools import
|
| 9 |
from unstructured.partition.auto import partition
|
| 10 |
|
| 11 |
|
|
@@ -39,31 +39,32 @@ def file_reader(file_path: str) -> str:
|
|
| 39 |
class GaiaSmolAgent:
|
| 40 |
def __init__(self):
|
| 41 |
print("Initializing GaiaSmolAgent with OpenAI...")
|
| 42 |
-
# --- MODIFICATION 1: Use OPENAI_API_KEY ---
|
| 43 |
# Ensure you have set your OPENAI_API_KEY as a secret in your HF Space
|
| 44 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
| 45 |
if not api_key:
|
| 46 |
raise ValueError("API key 'OPENAI_API_KEY' not found in environment secrets.")
|
| 47 |
|
| 48 |
-
# --- MODIFICATION 2: Use an OpenAI Model ID ---
|
| 49 |
# The "Planner" model - for high-level reasoning
|
| 50 |
self.planner_model = LiteLLMModel(
|
| 51 |
model_id="gpt-4o", # Using OpenAI's gpt-4o model
|
| 52 |
api_key=api_key,
|
| 53 |
temperature=0.0,
|
| 54 |
)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# The "Executor" agent - for executing tasks with tools
|
| 57 |
self.executor_agent = CodeAgent(
|
| 58 |
model=self.planner_model, # Can use the same model
|
| 59 |
-
|
| 60 |
-
|
| 61 |
add_base_tools=True,
|
| 62 |
)
|
| 63 |
print("GaiaSmolAgent initialized successfully with OpenAI.")
|
| 64 |
|
| 65 |
def _generate_plan(self, question: str) -> list[str]:
|
| 66 |
"""Generates a step-by-step plan to answer the question."""
|
|
|
|
| 67 |
print(f"Generating plan for question: {question[:100]}...")
|
| 68 |
prompt = f"""
|
| 69 |
You are an expert planner. Your job is to create a clear, step-by-step plan to answer the given question.
|
|
@@ -91,14 +92,10 @@ class GaiaSmolAgent:
|
|
| 91 |
|
| 92 |
def __call__(self, question: str) -> str:
|
| 93 |
"""Runs the planner and executor to answer the question."""
|
|
|
|
| 94 |
print(f"Agent received question: {question[:100]}...")
|
| 95 |
-
|
| 96 |
-
# Step 1: Generate the plan
|
| 97 |
plan = self._generate_plan(question)
|
| 98 |
-
|
| 99 |
-
# Step 2: Execute the plan
|
| 100 |
final_answer = self.executor_agent.run(plan)
|
| 101 |
-
|
| 102 |
print(f"Agent returning final answer: {final_answer}")
|
| 103 |
return str(final_answer)
|
| 104 |
|
|
|
|
| 5 |
|
| 6 |
# Import smol-agent and tool components
|
| 7 |
from smolagents import CodeAgent, LiteLLMModel, tool
|
| 8 |
+
from smolagents.tools import DuckDuckGoSearchTool
|
| 9 |
from unstructured.partition.auto import partition
|
| 10 |
|
| 11 |
|
|
|
|
| 39 |
class GaiaSmolAgent:
|
| 40 |
def __init__(self):
|
| 41 |
print("Initializing GaiaSmolAgent with OpenAI...")
|
|
|
|
| 42 |
# Ensure you have set your OPENAI_API_KEY as a secret in your HF Space
|
| 43 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 44 |
if not api_key:
|
| 45 |
raise ValueError("API key 'OPENAI_API_KEY' not found in environment secrets.")
|
| 46 |
|
|
|
|
| 47 |
# The "Planner" model - for high-level reasoning
|
| 48 |
self.planner_model = LiteLLMModel(
|
| 49 |
model_id="gpt-4o", # Using OpenAI's gpt-4o model
|
| 50 |
api_key=api_key,
|
| 51 |
temperature=0.0,
|
| 52 |
)
|
| 53 |
+
|
| 54 |
+
from smolagents.tools import DuckDuckGoSearchTool
|
| 55 |
|
| 56 |
# The "Executor" agent - for executing tasks with tools
|
| 57 |
self.executor_agent = CodeAgent(
|
| 58 |
model=self.planner_model, # Can use the same model
|
| 59 |
+
# --- MODIFICATION 2: Use the correct tool name in the list ---
|
| 60 |
+
tools=[file_reader, DuckDuckGoSearchTool()], # Corrected class name
|
| 61 |
add_base_tools=True,
|
| 62 |
)
|
| 63 |
print("GaiaSmolAgent initialized successfully with OpenAI.")
|
| 64 |
|
| 65 |
def _generate_plan(self, question: str) -> list[str]:
|
| 66 |
"""Generates a step-by-step plan to answer the question."""
|
| 67 |
+
# ... (This method remains unchanged)
|
| 68 |
print(f"Generating plan for question: {question[:100]}...")
|
| 69 |
prompt = f"""
|
| 70 |
You are an expert planner. Your job is to create a clear, step-by-step plan to answer the given question.
|
|
|
|
| 92 |
|
| 93 |
def __call__(self, question: str) -> str:
|
| 94 |
"""Runs the planner and executor to answer the question."""
|
| 95 |
+
# ... (This method remains unchanged)
|
| 96 |
print(f"Agent received question: {question[:100]}...")
|
|
|
|
|
|
|
| 97 |
plan = self._generate_plan(question)
|
|
|
|
|
|
|
| 98 |
final_answer = self.executor_agent.run(plan)
|
|
|
|
| 99 |
print(f"Agent returning final answer: {final_answer}")
|
| 100 |
return str(final_answer)
|
| 101 |
|