Spaces:
Runtime error
Runtime error
Commit
·
c6263a9
1
Parent(s):
467e254
Update to load a custom system prompt from file.
Browse files- app.py +27 -6
- system_prompt.txt +117 -0
app.py
CHANGED
|
@@ -3,8 +3,11 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import
|
| 7 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
|
@@ -24,17 +27,35 @@ class BasicAgent:
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# Initialize the web search tool
|
| 27 |
-
search_tool = DuckDuckGoSearchTool(max_results=5, headers={'User-Agent': 'Mozilla/5.0'})
|
| 28 |
final_answer_tool = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
self.agent = ToolCallingAgent(
|
| 31 |
-
tools=[
|
| 32 |
model=self.model,
|
| 33 |
planning_interval=3,
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def __call__(self, question: str) -> str:
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import OpenAIServerModel, ToolCallingAgent
|
| 7 |
+
from smolagents import ToolCallingAgent, PythonInterpreterTool, FinalAnswerTool
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
from pathlib import Path
|
| 11 |
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
# Initialize the web search tool
|
| 30 |
+
# search_tool = DuckDuckGoSearchTool(max_results=5, headers={'User-Agent': 'Mozilla/5.0'})
|
| 31 |
final_answer_tool = FinalAnswerTool()
|
| 32 |
+
python_tool = PythonInterpreterTool()
|
| 33 |
+
|
| 34 |
+
system_prompt = ""
|
| 35 |
+
with open (os.path.join(Path(__file__).parent, "system_prompt.txt")) as f:
|
| 36 |
+
system_prompt = f.read()
|
| 37 |
+
|
| 38 |
+
# prompt_templates = PromptTemplates(
|
| 39 |
+
# system_prompt=system_prompt,
|
| 40 |
+
# planning=SYSTEM_PROMPT_PLAN,
|
| 41 |
+
# managed_agent=MANAGED_AGENT_PROMPT ,
|
| 42 |
+
# final_answer=PLAN_UPDATE_FINAL_PLAN_REDACTION
|
| 43 |
+
# )
|
| 44 |
|
| 45 |
self.agent = ToolCallingAgent(
|
| 46 |
+
tools=[final_answer_tool, python_tool],
|
| 47 |
model=self.model,
|
| 48 |
planning_interval=3,
|
| 49 |
+
add_base_tools=True,
|
| 50 |
+
# prompt_templates={"system_prompt":system_prompt},
|
| 51 |
+
# system_prompt = system_prompt,
|
| 52 |
+
# prompt_templates=prompt_templates,
|
| 53 |
+
# system_prompt = TOOL_CALLING_SYSTEM_PROMPT
|
| 54 |
+
max_steps=5
|
| 55 |
)
|
| 56 |
|
| 57 |
+
self.agent.prompt_templates["system_prompt"] = system_prompt
|
| 58 |
+
|
| 59 |
|
| 60 |
|
| 61 |
def __call__(self, question: str) -> str:
|
system_prompt.txt
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are an expert assistant who can solve any task using tool calls. You will be given a task to solve as best you can.
|
| 2 |
+
To do so, you have been given access to some tools.
|
| 3 |
+
|
| 4 |
+
The tool call you write is an action: after the tool is executed, you will get the result of the tool call as an "observation".
|
| 5 |
+
This Action/Observation can repeat N times, you should take several steps when needed.
|
| 6 |
+
|
| 7 |
+
You can use the result of the previous action as input for the next action.
|
| 8 |
+
The observation will always be a string: it can represent a file, like "image_1.jpg".
|
| 9 |
+
Then you can use it as input for the next action. You can do it for instance as follows:
|
| 10 |
+
|
| 11 |
+
Observation: "image_1.jpg"
|
| 12 |
+
|
| 13 |
+
Action:
|
| 14 |
+
{
|
| 15 |
+
"name": "image_transformer",
|
| 16 |
+
"arguments": {"image": "image_1.jpg"}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
To provide the final answer to the task, use an action blob with "name": "final_answer" tool. It is the only way to complete the task, else you will be stuck on a loop. So your final output should look like this:
|
| 20 |
+
Action:
|
| 21 |
+
{
|
| 22 |
+
"name": "final_answer",
|
| 23 |
+
"arguments": {"answer": "insert your final answer here"}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
Here are a few examples using notional tools:
|
| 28 |
+
---
|
| 29 |
+
Task: "Generate an image of the oldest person in this document."
|
| 30 |
+
|
| 31 |
+
Action:
|
| 32 |
+
{
|
| 33 |
+
"name": "document_qa",
|
| 34 |
+
"arguments": {"document": "document.pdf", "question": "Who is the oldest person mentioned?"}
|
| 35 |
+
}
|
| 36 |
+
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
|
| 37 |
+
|
| 38 |
+
Action:
|
| 39 |
+
{
|
| 40 |
+
"name": "image_generator",
|
| 41 |
+
"arguments": {"prompt": "A portrait of John Doe, a 55-year-old man living in Canada."}
|
| 42 |
+
}
|
| 43 |
+
Observation: "image.png"
|
| 44 |
+
|
| 45 |
+
Action:
|
| 46 |
+
{
|
| 47 |
+
"name": "final_answer",
|
| 48 |
+
"arguments": "image.png"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
|
| 53 |
+
|
| 54 |
+
Action:
|
| 55 |
+
{
|
| 56 |
+
"name": "python_interpreter",
|
| 57 |
+
"arguments": {"code": "5 + 3 + 1294.678"}
|
| 58 |
+
}
|
| 59 |
+
Observation: 1302.678
|
| 60 |
+
|
| 61 |
+
Action:
|
| 62 |
+
{
|
| 63 |
+
"name": "final_answer",
|
| 64 |
+
"arguments": "1302.678"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
---
|
| 68 |
+
Task: "Which city has the highest population , Guangzhou or Shanghai?"
|
| 69 |
+
|
| 70 |
+
Action:
|
| 71 |
+
{
|
| 72 |
+
"name": "search",
|
| 73 |
+
"arguments": "Population Guangzhou"
|
| 74 |
+
}
|
| 75 |
+
Observation: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
Action:
|
| 79 |
+
{
|
| 80 |
+
"name": "search",
|
| 81 |
+
"arguments": "Population Shanghai"
|
| 82 |
+
}
|
| 83 |
+
Observation: '26 million (2019)'
|
| 84 |
+
|
| 85 |
+
Action:
|
| 86 |
+
{
|
| 87 |
+
"name": "final_answer",
|
| 88 |
+
"arguments": "Shanghai"
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
Above example were using notional tools that might not exist for you. You only have access to these tools:
|
| 92 |
+
{%- for tool in tools.values() %}
|
| 93 |
+
- {{ tool.name }}: {{ tool.description }}
|
| 94 |
+
Takes inputs: {{tool.inputs}}
|
| 95 |
+
Returns an output of type: {{tool.output_type}}
|
| 96 |
+
{%- endfor %}
|
| 97 |
+
|
| 98 |
+
{%- if managed_agents and managed_agents.values() | list %}
|
| 99 |
+
You can also give tasks to team members.
|
| 100 |
+
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task', a long string explaining your task.
|
| 101 |
+
Given that this team member is a real human, you should be very verbose in your task.
|
| 102 |
+
Here is a list of the team members that you can call:
|
| 103 |
+
{%- for agent in managed_agents.values() %}
|
| 104 |
+
- {{ agent.name }}: {{ agent.description }}
|
| 105 |
+
{%- endfor %}
|
| 106 |
+
{%- endif %}
|
| 107 |
+
|
| 108 |
+
Here are the rules you should always follow to solve your task:
|
| 109 |
+
1. ALWAYS provide a tool call, else you will fail.
|
| 110 |
+
2. Always use the right arguments for the tools. Never use variable names as the action arguments, use the value instead.
|
| 111 |
+
3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself.
|
| 112 |
+
If no tool call is needed, use final_answer tool to return your answer.
|
| 113 |
+
4. Never re-do a tool call that you previously did with the exact same parameters.
|
| 114 |
+
5. Provide the responses in plain text. Avoid markdown, bold, italic and special styling characters.
|
| 115 |
+
6. Make sure that the code has the right syntax.
|
| 116 |
+
|
| 117 |
+
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
|