Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import Tool, tool, OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, VisitWebpageTool
|
| 7 |
|
| 8 |
|
| 9 |
from dotenv import load_dotenv
|
|
@@ -18,14 +18,95 @@ OPENAI_TOKEN = os.getenv("OPENAPI_KEY")
|
|
| 18 |
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
-
class
|
| 22 |
def __init__(self):
|
| 23 |
print("BasicAgent initialized.")
|
| 24 |
def __call__(self, question: str) -> str:
|
| 25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 26 |
fixed_answer = "This is a default answer."
|
| 27 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return fixed_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 31 |
"""
|
|
@@ -48,7 +129,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 48 |
|
| 49 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 50 |
try:
|
| 51 |
-
agent =
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Error instantiating agent: {e}")
|
| 54 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import FinalAnswerTool, Tool, tool, OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, VisitWebpageTool
|
| 7 |
|
| 8 |
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 18 |
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
+
class SlpMultiAgent:
|
| 22 |
def __init__(self):
|
| 23 |
print("BasicAgent initialized.")
|
| 24 |
def __call__(self, question: str) -> str:
|
| 25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 26 |
fixed_answer = "This is a default answer."
|
| 27 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 28 |
+
|
| 29 |
+
# Increase max_tokens to allow for complete responses
|
| 30 |
+
model = OpenAIServerModel(
|
| 31 |
+
model_id="gpt-3.5-turbo",
|
| 32 |
+
temperature=0.0,
|
| 33 |
+
max_tokens=1500
|
| 34 |
+
)
|
| 35 |
+
# Here you can implement your agent logic, tools, and model calls
|
| 36 |
+
web_agent = CodeAgent(
|
| 37 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 38 |
+
model=model,
|
| 39 |
+
additional_authorized_imports=["pandas"],
|
| 40 |
+
max_steps=10,
|
| 41 |
+
name="WebAgent",
|
| 42 |
+
verbosity_level=0,
|
| 43 |
+
description="An agent that can search the web, visit webpages, and calculate cargo travel times between locations."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
manager_agent = CodeAgent(
|
| 47 |
+
model=OpenAIServerModel("gpt-3.5-turbo"),
|
| 48 |
+
tools=[],
|
| 49 |
+
managed_agents=[web_agent],
|
| 50 |
+
name="ManagerAgent",
|
| 51 |
+
description="A manager agent that can delegate tasks to other agents and manage their execution.",
|
| 52 |
+
additional_authorized_imports=[
|
| 53 |
+
"pandas",
|
| 54 |
+
],
|
| 55 |
+
planning_interval=5,
|
| 56 |
+
verbosity_level=2,
|
| 57 |
+
max_steps=15,
|
| 58 |
+
final_answer_checks=[check_reasoning]
|
| 59 |
+
)
|
| 60 |
+
manager_agent.run("""
|
| 61 |
+
You are a question answering agent. That specializes in complex questions that require multiple steps to answer.
|
| 62 |
+
Take a few steps and think about the question before answering.
|
| 63 |
+
You can use the tools available to you, but you should not use them unless necessary.
|
| 64 |
+
You should always try to answer the question using your own knowledge and reasoning.
|
| 65 |
+
If you need to use a tool, you should explain why you are using it and what you expect to find.
|
| 66 |
+
If you are not sure about something, you should say so and explain why you are not sure.
|
| 67 |
+
You should always try to provide a complete and accurate answer to the question.
|
| 68 |
+
If you are not able to answer the question, you should say so and explain why
|
| 69 |
+
|
| 70 |
+
Never try to process strings using code: when you have a string to read, just print it and you'll see it.
|
| 71 |
+
|
| 72 |
+
Here is the question: {question}
|
| 73 |
+
Thoughts: [your reasoning about how to solve the problem]
|
| 74 |
+
Code:
|
| 75 |
+
```py
|
| 76 |
+
# Your Python code here
|
| 77 |
+
```<end_code>
|
| 78 |
+
|
| 79 |
+
The code block MUST start with ```py on its own line and end with ```<end_code> on its own line.
|
| 80 |
+
""")
|
| 81 |
return fixed_answer
|
| 82 |
+
|
| 83 |
+
def check_reasoning(final_answer, agent_memory):
|
| 84 |
+
multimodal_model = OpenAIServerModel("gpt-4",
|
| 85 |
+
max_tokens=1500)
|
| 86 |
+
prompt = (
|
| 87 |
+
f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}. Now here is the plot that was made."
|
| 88 |
+
"Please check that the reasoning process and plot are correct: do they correctly answer the given task?"
|
| 89 |
+
"First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
|
| 90 |
+
"Don't be harsh: if the plot mostly solves the task, it should pass."
|
| 91 |
+
"To pass the question should be answered correctly and the reasoning should be sound."
|
| 92 |
+
"The final answer is: {final_answer}. "
|
| 93 |
+
)
|
| 94 |
+
messages = [
|
| 95 |
+
{
|
| 96 |
+
"role": "user",
|
| 97 |
+
"content": [
|
| 98 |
+
{
|
| 99 |
+
"type": "text",
|
| 100 |
+
"text": prompt,
|
| 101 |
+
}
|
| 102 |
+
],
|
| 103 |
+
}
|
| 104 |
+
]
|
| 105 |
+
output = multimodal_model(messages).content
|
| 106 |
+
print("Reasoning and plot check output:", output)
|
| 107 |
+
if "fail" in output.lower():
|
| 108 |
+
print("Reasoning check failed. Please review the agent's reasoning.")
|
| 109 |
+
|
| 110 |
|
| 111 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 112 |
"""
|
|
|
|
| 129 |
|
| 130 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 131 |
try:
|
| 132 |
+
agent = SlpMultiAgent()
|
| 133 |
except Exception as e:
|
| 134 |
print(f"Error instantiating agent: {e}")
|
| 135 |
return f"Error initializing agent: {e}", None
|