Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
0d53251
1
Parent(s): 40dd326
Refactor: Move prompt templates to separate Python file and update app.py
Browse files- app.py +112 -123
- prompt_templates.py +127 -0
app.py
CHANGED
|
@@ -7,6 +7,31 @@ import yaml
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
import re
|
| 9 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
|
@@ -68,129 +93,93 @@ model = OpenAIModel(
|
|
| 68 |
# Import tool from Hub
|
| 69 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
Please update your list of facts based on the previous history, and provide these headings:
|
| 131 |
-
### 1. Facts given in the task
|
| 132 |
-
### 2. Facts that we have learned
|
| 133 |
-
### 3. Facts still to look up
|
| 134 |
-
### 4. Facts still to derive
|
| 135 |
-
|
| 136 |
-
Now write your new list of facts below.""",
|
| 137 |
-
|
| 138 |
-
"update_plan_pre_messages": """You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
|
| 139 |
-
You have been given a task:
|
| 140 |
-
```
|
| 141 |
-
{{task}}
|
| 142 |
-
```
|
| 143 |
-
|
| 144 |
-
Find below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.
|
| 145 |
-
If the previous tries so far have met some success, you can make an updated plan based on these actions.
|
| 146 |
-
If you are stalled, you can make a completely new plan starting from scratch.""",
|
| 147 |
-
|
| 148 |
-
"update_plan_post_messages": """You're still working towards solving this task:
|
| 149 |
-
```
|
| 150 |
-
{{task}}
|
| 151 |
-
```
|
| 152 |
-
|
| 153 |
-
Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
|
| 154 |
-
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
|
| 155 |
-
Beware that you have {remaining_steps} steps remaining.
|
| 156 |
-
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
| 157 |
-
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
| 158 |
-
|
| 159 |
-
Now write your new plan below."""
|
| 160 |
-
},
|
| 161 |
-
|
| 162 |
-
"managed_agent": {
|
| 163 |
-
"task": """You're a helpful agent named '{{name}}'.
|
| 164 |
-
You have been submitted this task by your manager.
|
| 165 |
-
---
|
| 166 |
-
Task:
|
| 167 |
-
{{task}}
|
| 168 |
-
---
|
| 169 |
-
You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear understanding of the answer.
|
| 170 |
-
Your final_answer WILL HAVE to contain these parts:
|
| 171 |
-
### 1. Task outcome (short version):
|
| 172 |
-
### 2. Task outcome (extremely detailed version):
|
| 173 |
-
### 3. Additional context (if relevant):
|
| 174 |
-
|
| 175 |
-
Put all these in your final_answer tool, everything that you do not pass as an argument to final_answer will be lost.
|
| 176 |
-
And even if your task resolution is not successful, please return as much context as possible, so that your manager can act upon this feedback.""",
|
| 177 |
-
|
| 178 |
-
"report": """Here is the final answer from your managed agent '{{name}}':
|
| 179 |
-
{{final_answer}}"""
|
| 180 |
-
}
|
| 181 |
-
}
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
|
|
|
|
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
import re
|
| 9 |
import os
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from smolagents import Agent, Tool
|
| 12 |
+
from smolagents.tools import (
|
| 13 |
+
SearchTool,
|
| 14 |
+
CalculatorTool,
|
| 15 |
+
WikipediaTool,
|
| 16 |
+
FileSystemTool,
|
| 17 |
+
CodeInterpreterTool,
|
| 18 |
+
WebScrapingTool,
|
| 19 |
+
WeatherTool,
|
| 20 |
+
NewsTool,
|
| 21 |
+
ImageGenerationTool,
|
| 22 |
+
TextToSpeechTool,
|
| 23 |
+
TranslationTool,
|
| 24 |
+
EmailTool,
|
| 25 |
+
CalendarTool,
|
| 26 |
+
MapTool,
|
| 27 |
+
MusicTool,
|
| 28 |
+
VideoTool,
|
| 29 |
+
DatabaseTool,
|
| 30 |
+
APITool,
|
| 31 |
+
BrowserTool,
|
| 32 |
+
TerminalTool
|
| 33 |
+
)
|
| 34 |
+
from prompt_templates import PROMPT_TEMPLATES
|
| 35 |
|
| 36 |
from Gradio_UI import GradioUI
|
| 37 |
|
|
|
|
| 93 |
# Import tool from Hub
|
| 94 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 95 |
|
| 96 |
+
# Initialize tools
|
| 97 |
+
tools = [
|
| 98 |
+
SearchTool(),
|
| 99 |
+
CalculatorTool(),
|
| 100 |
+
WikipediaTool(),
|
| 101 |
+
FileSystemTool(),
|
| 102 |
+
CodeInterpreterTool(),
|
| 103 |
+
WebScrapingTool(),
|
| 104 |
+
WeatherTool(),
|
| 105 |
+
NewsTool(),
|
| 106 |
+
ImageGenerationTool(),
|
| 107 |
+
TextToSpeechTool(),
|
| 108 |
+
TranslationTool(),
|
| 109 |
+
EmailTool(),
|
| 110 |
+
CalendarTool(),
|
| 111 |
+
MapTool(),
|
| 112 |
+
MusicTool(),
|
| 113 |
+
VideoTool(),
|
| 114 |
+
DatabaseTool(),
|
| 115 |
+
APITool(),
|
| 116 |
+
BrowserTool(),
|
| 117 |
+
TerminalTool()
|
| 118 |
+
]
|
| 119 |
+
|
| 120 |
+
# Initialize agents
|
| 121 |
+
managed_agent = Agent(
|
| 122 |
+
tools=tools,
|
| 123 |
+
prompt_template=PROMPT_TEMPLATES["managed_agent"]
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
tool_agent = Agent(
|
| 127 |
+
tools=tools,
|
| 128 |
+
prompt_template=PROMPT_TEMPLATES["tool_agent"]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
tool_agent_with_memory = Agent(
|
| 132 |
+
tools=tools,
|
| 133 |
+
prompt_template=PROMPT_TEMPLATES["tool_agent_with_memory"],
|
| 134 |
+
memory=True
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
tool_agent_with_memory_and_planning = Agent(
|
| 138 |
+
tools=tools,
|
| 139 |
+
prompt_template=PROMPT_TEMPLATES["tool_agent_with_memory_and_planning"],
|
| 140 |
+
memory=True,
|
| 141 |
+
planning=True
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Create Gradio interface
|
| 145 |
+
def chat(message, history, agent_type):
|
| 146 |
+
# Select agent based on type
|
| 147 |
+
if agent_type == "Managed Agent":
|
| 148 |
+
agent = managed_agent
|
| 149 |
+
elif agent_type == "Tool Agent":
|
| 150 |
+
agent = tool_agent
|
| 151 |
+
elif agent_type == "Tool Agent with Memory":
|
| 152 |
+
agent = tool_agent_with_memory
|
| 153 |
+
else: # Tool Agent with Memory and Planning
|
| 154 |
+
agent = tool_agent_with_memory_and_planning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
# Get response from agent
|
| 157 |
+
response = agent.chat(message)
|
| 158 |
+
return response
|
| 159 |
+
|
| 160 |
+
# Create the interface
|
| 161 |
+
iface = gr.Interface(
|
| 162 |
+
fn=chat,
|
| 163 |
+
inputs=[
|
| 164 |
+
gr.Textbox(label="Message", placeholder="Type your message here..."),
|
| 165 |
+
gr.State([]), # For chat history
|
| 166 |
+
gr.Radio(
|
| 167 |
+
choices=[
|
| 168 |
+
"Managed Agent",
|
| 169 |
+
"Tool Agent",
|
| 170 |
+
"Tool Agent with Memory",
|
| 171 |
+
"Tool Agent with Memory and Planning"
|
| 172 |
+
],
|
| 173 |
+
label="Agent Type",
|
| 174 |
+
value="Managed Agent"
|
| 175 |
+
)
|
| 176 |
+
],
|
| 177 |
+
outputs=gr.Textbox(label="Response"),
|
| 178 |
+
title="AI Assistant",
|
| 179 |
+
description="Choose an agent type and start chatting!",
|
| 180 |
+
theme=gr.themes.Soft()
|
| 181 |
)
|
| 182 |
|
| 183 |
+
# Launch the app
|
| 184 |
+
if __name__ == "__main__":
|
| 185 |
+
iface.launch()
|
prompt_templates.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PROMPT_TEMPLATES = {
|
| 2 |
+
"managed_agent": """You are a helpful AI assistant that helps users with their tasks. You have access to the following tools:
|
| 3 |
+
|
| 4 |
+
{tools}
|
| 5 |
+
|
| 6 |
+
To use a tool, please use the following format:
|
| 7 |
+
|
| 8 |
+
```
|
| 9 |
+
Thought: I need to use a tool to help the user
|
| 10 |
+
Action: the_tool_name
|
| 11 |
+
Action Input: {{"input": "the input to the tool"}}
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
The user will then respond with the output of the tool. After using a tool, you should analyze the output and either:
|
| 15 |
+
1. Use another tool if needed
|
| 16 |
+
2. Provide a final answer to the user
|
| 17 |
+
|
| 18 |
+
If you don't need to use any tools, you can respond directly to the user.
|
| 19 |
+
|
| 20 |
+
Remember to:
|
| 21 |
+
- Always think about what tool would be most helpful
|
| 22 |
+
- Format your responses clearly
|
| 23 |
+
- Be helpful and friendly
|
| 24 |
+
- If you're not sure what to do, ask the user for clarification
|
| 25 |
+
|
| 26 |
+
Human: {input}
|
| 27 |
+
|
| 28 |
+
Assistant: Let me help you with that. First, I'll analyze what tools might be useful for your request.
|
| 29 |
+
|
| 30 |
+
{agent_scratchpad}""",
|
| 31 |
+
|
| 32 |
+
"tool_agent": """You are a helpful AI assistant that helps users with their tasks. You have access to the following tools:
|
| 33 |
+
|
| 34 |
+
{tools}
|
| 35 |
+
|
| 36 |
+
To use a tool, please use the following format:
|
| 37 |
+
|
| 38 |
+
```
|
| 39 |
+
Thought: I need to use a tool to help the user
|
| 40 |
+
Action: the_tool_name
|
| 41 |
+
Action Input: {{"input": "the input to the tool"}}
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
The user will then respond with the output of the tool. After using a tool, you should analyze the output and either:
|
| 45 |
+
1. Use another tool if needed
|
| 46 |
+
2. Provide a final answer to the user
|
| 47 |
+
|
| 48 |
+
If you don't need to use any tools, you can respond directly to the user.
|
| 49 |
+
|
| 50 |
+
Remember to:
|
| 51 |
+
- Always think about what tool would be most helpful
|
| 52 |
+
- Format your responses clearly
|
| 53 |
+
- Be helpful and friendly
|
| 54 |
+
- If you're not sure what to do, ask the user for clarification
|
| 55 |
+
|
| 56 |
+
Human: {input}
|
| 57 |
+
|
| 58 |
+
Assistant: Let me help you with that. First, I'll analyze what tools might be useful for your request.
|
| 59 |
+
|
| 60 |
+
{agent_scratchpad}""",
|
| 61 |
+
|
| 62 |
+
"tool_agent_with_memory": """You are a helpful AI assistant that helps users with their tasks. You have access to the following tools:
|
| 63 |
+
|
| 64 |
+
{tools}
|
| 65 |
+
|
| 66 |
+
To use a tool, please use the following format:
|
| 67 |
+
|
| 68 |
+
```
|
| 69 |
+
Thought: I need to use a tool to help the user
|
| 70 |
+
Action: the_tool_name
|
| 71 |
+
Action Input: {{"input": "the input to the tool"}}
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
The user will then respond with the output of the tool. After using a tool, you should analyze the output and either:
|
| 75 |
+
1. Use another tool if needed
|
| 76 |
+
2. Provide a final answer to the user
|
| 77 |
+
|
| 78 |
+
If you don't need to use any tools, you can respond directly to the user.
|
| 79 |
+
|
| 80 |
+
Remember to:
|
| 81 |
+
- Always think about what tool would be most helpful
|
| 82 |
+
- Format your responses clearly
|
| 83 |
+
- Be helpful and friendly
|
| 84 |
+
- If you're not sure what to do, ask the user for clarification
|
| 85 |
+
|
| 86 |
+
Previous conversation:
|
| 87 |
+
{chat_history}
|
| 88 |
+
|
| 89 |
+
Human: {input}
|
| 90 |
+
|
| 91 |
+
Assistant: Let me help you with that. First, I'll analyze what tools might be useful for your request.
|
| 92 |
+
|
| 93 |
+
{agent_scratchpad}""",
|
| 94 |
+
|
| 95 |
+
"tool_agent_with_memory_and_planning": """You are a helpful AI assistant that helps users with their tasks. You have access to the following tools:
|
| 96 |
+
|
| 97 |
+
{tools}
|
| 98 |
+
|
| 99 |
+
To use a tool, please use the following format:
|
| 100 |
+
|
| 101 |
+
```
|
| 102 |
+
Thought: I need to use a tool to help the user
|
| 103 |
+
Action: the_tool_name
|
| 104 |
+
Action Input: {{"input": "the input to the tool"}}
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
The user will then respond with the output of the tool. After using a tool, you should analyze the output and either:
|
| 108 |
+
1. Use another tool if needed
|
| 109 |
+
2. Provide a final answer to the user
|
| 110 |
+
|
| 111 |
+
If you don't need to use any tools, you can respond directly to the user.
|
| 112 |
+
|
| 113 |
+
Remember to:
|
| 114 |
+
- Always think about what tool would be most helpful
|
| 115 |
+
- Format your responses clearly
|
| 116 |
+
- Be helpful and friendly
|
| 117 |
+
- If you're not sure what to do, ask the user for clarification
|
| 118 |
+
|
| 119 |
+
Previous conversation:
|
| 120 |
+
{chat_history}
|
| 121 |
+
|
| 122 |
+
Human: {input}
|
| 123 |
+
|
| 124 |
+
Assistant: Let me help you with that. First, I'll analyze what tools might be useful for your request.
|
| 125 |
+
|
| 126 |
+
{agent_scratchpad}"""
|
| 127 |
+
}
|