Upload folder using huggingface_hub
Browse files- agent.py +10 -14
- app.py +47 -30
- benchmarking.ipynb +1 -1
- prompts/__init__.py +25 -0
- prompts/default.py +108 -0
agent.py
CHANGED
@@ -1,29 +1,25 @@
|
|
1 |
from transformers import ReactCodeAgent, HfApiEngine
|
2 |
-
from prompts import
|
3 |
from tools.squad_tools import SquadRetrieverTool, SquadQueryTool
|
4 |
-
from tools.text_to_image import TextToImageTool
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
DEFAULT_MODEL_NAME = "http://localhost:1234/v1"
|
8 |
if model_name is None:
|
9 |
model_name = DEFAULT_MODEL_NAME
|
10 |
|
11 |
llm_engine = HfApiEngine(model_name)
|
12 |
|
13 |
-
TASK_SOLVING_TOOLBOX = [
|
14 |
-
SquadRetrieverTool(),
|
15 |
-
SquadQueryTool()
|
16 |
-
]
|
17 |
-
|
18 |
-
if include_image_tools:
|
19 |
-
TASK_SOLVING_TOOLBOX.append(TextToImageTool())
|
20 |
-
|
21 |
# Initialize the agent with both tools
|
22 |
agent = ReactCodeAgent(
|
23 |
-
tools=
|
24 |
llm_engine=llm_engine,
|
25 |
-
system_prompt=
|
26 |
)
|
27 |
|
28 |
return agent
|
29 |
-
|
|
|
1 |
from transformers import ReactCodeAgent, HfApiEngine
|
2 |
+
from prompts import *
|
3 |
from tools.squad_tools import SquadRetrieverTool, SquadQueryTool
|
|
|
4 |
|
5 |
+
DEFAULT_TASK_SOLVING_TOOLBOX = [SquadRetrieverTool(), SquadQueryTool()]
|
6 |
+
|
7 |
+
def get_agent(
|
8 |
+
model_name=None,
|
9 |
+
system_prompt=DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT,
|
10 |
+
toolbox=DEFAULT_TASK_SOLVING_TOOLBOX,
|
11 |
+
):
|
12 |
DEFAULT_MODEL_NAME = "http://localhost:1234/v1"
|
13 |
if model_name is None:
|
14 |
model_name = DEFAULT_MODEL_NAME
|
15 |
|
16 |
llm_engine = HfApiEngine(model_name)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Initialize the agent with both tools
|
19 |
agent = ReactCodeAgent(
|
20 |
+
tools=toolbox,
|
21 |
llm_engine=llm_engine,
|
22 |
+
system_prompt=system_prompt,
|
23 |
)
|
24 |
|
25 |
return agent
|
|
app.py
CHANGED
@@ -6,20 +6,31 @@ from gradio import Request
|
|
6 |
import pickle
|
7 |
import os
|
8 |
from dotenv import load_dotenv
|
9 |
-
from agent import get_agent
|
|
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
sessions_path = "sessions.pkl"
|
14 |
-
sessions =
|
|
|
|
|
15 |
|
16 |
# If currently hosted on HuggingFace Spaces, use the default model, otherwise use the local model
|
17 |
-
model_name =
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
app = None
|
22 |
|
|
|
23 |
def append_example_message(x: gr.SelectData, messages):
|
24 |
if x.value["text"] is not None:
|
25 |
message = x.value["text"]
|
@@ -33,19 +44,22 @@ def append_example_message(x: gr.SelectData, messages):
|
|
33 |
messages.append(ChatMessage(role="user", content=message))
|
34 |
return messages
|
35 |
|
|
|
36 |
def add_message(message, messages):
|
37 |
messages.append(ChatMessage(role="user", content=message))
|
38 |
return messages
|
39 |
|
|
|
40 |
def interact_with_agent(messages, request: Request):
|
41 |
session_hash = request.session_hash
|
42 |
-
prompt = messages[-1][
|
43 |
agent.logs = sessions.get(session_hash + "_logs", [])
|
44 |
for msg in stream_from_transformers_agent(agent, prompt):
|
45 |
messages.append(msg)
|
46 |
yield messages
|
47 |
yield messages
|
48 |
|
|
|
49 |
def persist(component):
|
50 |
|
51 |
def resume_session(value, request: Request):
|
@@ -68,34 +82,37 @@ def persist(component):
|
|
68 |
|
69 |
return component
|
70 |
|
|
|
71 |
with gr.Blocks(fill_height=True) as demo:
|
72 |
-
chatbot = persist(
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
<h2>I am your friendly guide to the Stanford Question and Answer Dataset (SQuAD).</h2>
|
86 |
""",
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
99 |
text_input = gr.Textbox(lines=1, label="Chat Message", scale=0)
|
100 |
chat_msg = text_input.submit(add_message, [text_input, chatbot], [chatbot])
|
101 |
bot_msg = chat_msg.then(interact_with_agent, [chatbot], [chatbot])
|
|
|
6 |
import pickle
|
7 |
import os
|
8 |
from dotenv import load_dotenv
|
9 |
+
from agent import get_agent, DEFAULT_TASK_SOLVING_TOOLBOX
|
10 |
+
from tools.text_to_image import TextToImageTool
|
11 |
|
12 |
load_dotenv()
|
13 |
|
14 |
sessions_path = "sessions.pkl"
|
15 |
+
sessions = (
|
16 |
+
pickle.load(open(sessions_path, "rb")) if os.path.exists(sessions_path) else {}
|
17 |
+
)
|
18 |
|
19 |
# If currently hosted on HuggingFace Spaces, use the default model, otherwise use the local model
|
20 |
+
model_name = (
|
21 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct"
|
22 |
+
if os.getenv("SPACE_ID") is not None
|
23 |
+
else "http://localhost:1234/v1"
|
24 |
+
)
|
25 |
|
26 |
+
# Add image tools to the default task solving toolbox, for a more visually interactive experience
|
27 |
+
TASK_SOLVING_TOOLBOX = DEFAULT_TASK_SOLVING_TOOLBOX + [TextToImageTool()]
|
28 |
+
|
29 |
+
agent = get_agent(model_name=model_name, toolbox=TASK_SOLVING_TOOLBOX)
|
30 |
|
31 |
app = None
|
32 |
|
33 |
+
|
34 |
def append_example_message(x: gr.SelectData, messages):
|
35 |
if x.value["text"] is not None:
|
36 |
message = x.value["text"]
|
|
|
44 |
messages.append(ChatMessage(role="user", content=message))
|
45 |
return messages
|
46 |
|
47 |
+
|
48 |
def add_message(message, messages):
|
49 |
messages.append(ChatMessage(role="user", content=message))
|
50 |
return messages
|
51 |
|
52 |
+
|
53 |
def interact_with_agent(messages, request: Request):
|
54 |
session_hash = request.session_hash
|
55 |
+
prompt = messages[-1]["content"]
|
56 |
agent.logs = sessions.get(session_hash + "_logs", [])
|
57 |
for msg in stream_from_transformers_agent(agent, prompt):
|
58 |
messages.append(msg)
|
59 |
yield messages
|
60 |
yield messages
|
61 |
|
62 |
+
|
63 |
def persist(component):
|
64 |
|
65 |
def resume_session(value, request: Request):
|
|
|
82 |
|
83 |
return component
|
84 |
|
85 |
+
|
86 |
with gr.Blocks(fill_height=True) as demo:
|
87 |
+
chatbot = persist(
|
88 |
+
gr.Chatbot(
|
89 |
+
value=[],
|
90 |
+
label="SQuAD Agent",
|
91 |
+
type="messages",
|
92 |
+
avatar_images=(
|
93 |
+
None,
|
94 |
+
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
95 |
+
),
|
96 |
+
scale=1,
|
97 |
+
autoscroll=True,
|
98 |
+
show_copy_all_button=True,
|
99 |
+
show_copy_button=True,
|
100 |
+
placeholder="""<h1>SQuAD Agent</h1>
|
101 |
<h2>I am your friendly guide to the Stanford Question and Answer Dataset (SQuAD).</h2>
|
102 |
""",
|
103 |
+
examples=[
|
104 |
+
{
|
105 |
+
"text": "What is on top of the Notre Dame building?",
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"text": "Tell me what's on top of the Notre Dame building, and draw a picture of it.",
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"text": "Draw a picture of whatever is on top of the Notre Dame building.",
|
112 |
+
},
|
113 |
+
],
|
114 |
+
)
|
115 |
+
)
|
116 |
text_input = gr.Textbox(lines=1, label="Chat Message", scale=0)
|
117 |
chat_msg = text_input.submit(add_message, [text_input, chatbot], [chatbot])
|
118 |
bot_msg = chat_msg.then(interact_with_agent, [chatbot], [chatbot])
|
benchmarking.ipynb
CHANGED
@@ -331,7 +331,7 @@
|
|
331 |
"from agent import get_agent\n",
|
332 |
"\n",
|
333 |
"benchmarks = [\n",
|
334 |
-
" (get_agent(
|
335 |
"]\n",
|
336 |
"\n",
|
337 |
"for agent, name in tqdm(benchmarks):\n",
|
|
|
331 |
"from agent import get_agent\n",
|
332 |
"\n",
|
333 |
"benchmarks = [\n",
|
334 |
+
" (get_agent(), \"baseline\"), # Baseline agent with default settings\n",
|
335 |
"]\n",
|
336 |
"\n",
|
337 |
"for agent, name in tqdm(benchmarks):\n",
|
prompts/__init__.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import all prompts as both constants and in a PROMPTS dictionary,
|
2 |
+
# from all files in the prompts directory that aren't __init__.py
|
3 |
+
|
4 |
+
import os
|
5 |
+
|
6 |
+
def load_constants(constants_dir):
|
7 |
+
"""Loads constants from .py files in the specified directory."""
|
8 |
+
|
9 |
+
constants = {}
|
10 |
+
|
11 |
+
for filename in os.listdir(constants_dir):
|
12 |
+
if filename.endswith(".py") and filename != "__init__.py":
|
13 |
+
module_name = filename[:-3] # Remove .py extension
|
14 |
+
module = __import__(f"{constants_dir}.{module_name}", fromlist=[module_name])
|
15 |
+
|
16 |
+
for name, value in vars(module).items():
|
17 |
+
if name.isupper(): # Convention for constants
|
18 |
+
constants[name] = value
|
19 |
+
|
20 |
+
return constants
|
21 |
+
|
22 |
+
PROMPTS = load_constants("prompts")
|
23 |
+
|
24 |
+
# Import all prompts locally as well, for code completion
|
25 |
+
from prompts.default import DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
prompts/default.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT = """You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
|
2 |
+
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
|
3 |
+
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
|
4 |
+
|
5 |
+
Your most important tool is the `squad_retriever` tool, which can retrieve documents relevant to a given question from the Stanford Question Answering Dataset (SQuAD).
|
6 |
+
Not all questions will require the `squad_retriever` tool, but whenever you need to answer a question, you should start with this tool first, and then refine your answer only as needed to align with the question and chat history.
|
7 |
+
|
8 |
+
At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
|
9 |
+
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_action>' sequence.
|
10 |
+
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
11 |
+
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
12 |
+
In the end you have to return a final answer using the `final_answer` tool.
|
13 |
+
|
14 |
+
Here are a few examples using notional tools:
|
15 |
+
---
|
16 |
+
Task: "Generate an image of the oldest person in this document."
|
17 |
+
|
18 |
+
Thought: I will proceed step by step and use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer.
|
19 |
+
Code:
|
20 |
+
```py
|
21 |
+
answer = document_qa(document=document, question="Who is the oldest person mentioned?")
|
22 |
+
print(answer)
|
23 |
+
```<end_action>
|
24 |
+
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
|
25 |
+
|
26 |
+
Thought: I will now generate an image showcasing the oldest person.
|
27 |
+
Code:
|
28 |
+
```py
|
29 |
+
image = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
|
30 |
+
final_answer(image)
|
31 |
+
```<end_action>
|
32 |
+
|
33 |
+
---
|
34 |
+
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
|
35 |
+
|
36 |
+
Thought: I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool
|
37 |
+
Code:
|
38 |
+
```py
|
39 |
+
result = 5 + 3 + 1294.678
|
40 |
+
final_answer(result)
|
41 |
+
```<end_action>
|
42 |
+
|
43 |
+
---
|
44 |
+
Task: "Which city has the highest population: Guangzhou or Shanghai?"
|
45 |
+
|
46 |
+
Thought: I need to get the populations for both cities and compare them: I will use the tool `search` to get the population of both cities.
|
47 |
+
Code:
|
48 |
+
```py
|
49 |
+
population_guangzhou = search("Guangzhou population")
|
50 |
+
print("Population Guangzhou:", population_guangzhou)
|
51 |
+
population_shanghai = search("Shanghai population")
|
52 |
+
print("Population Shanghai:", population_shanghai)
|
53 |
+
```<end_action>
|
54 |
+
Observation:
|
55 |
+
Population Guangzhou: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
|
56 |
+
Population Shanghai: '26 million (2019)'
|
57 |
+
|
58 |
+
Thought: Now I know that Shanghai has the highest population.
|
59 |
+
Code:
|
60 |
+
```py
|
61 |
+
final_answer("Shanghai")
|
62 |
+
```<end_action>
|
63 |
+
|
64 |
+
---
|
65 |
+
Task: "What is the current age of the pope, raised to the power 0.36?"
|
66 |
+
|
67 |
+
Thought: I will use the tool `wiki` to get the age of the pope, then raise it to the power 0.36.
|
68 |
+
Code:
|
69 |
+
```py
|
70 |
+
pope_age = wiki(query="current pope age")
|
71 |
+
print("Pope age:", pope_age)
|
72 |
+
```<end_action>
|
73 |
+
Observation:
|
74 |
+
Pope age: "The pope Francis is currently 85 years old."
|
75 |
+
|
76 |
+
Thought: I know that the pope is 85 years old. Let's compute the result using python code.
|
77 |
+
Code:
|
78 |
+
```py
|
79 |
+
pope_current_age = 85 ** 0.36
|
80 |
+
final_answer(pope_current_age)
|
81 |
+
```<end_action>
|
82 |
+
|
83 |
+
Above example were using notional tools that might not exist for you. On top of performing computations in the Python code snippets that you create, you have access to those tools (and no other tool):
|
84 |
+
|
85 |
+
<<tool_descriptions>>
|
86 |
+
|
87 |
+
<<managed_agents_descriptions>>
|
88 |
+
|
89 |
+
Here are the rules you should always follow to solve your task:
|
90 |
+
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_action>' sequence, else you will fail.
|
91 |
+
2. Use only variables that you have defined!
|
92 |
+
3. Always use the right arguments for the tools. DO NOT pass the arguments as a dict as in 'answer = wiki({'query': "What is the place where James Bond lives?"})', but use the arguments directly as in 'answer = wiki(query="What is the place where James Bond lives?")'.
|
93 |
+
4. Take care to not chain too many sequential tool calls in the same code block, especially when the output format is unpredictable. For instance, a call to search has an unpredictable return format, so do not have another tool call that depends on its output in the same block: rather output results with print() to use them in the next block.
|
94 |
+
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
|
95 |
+
6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.
|
96 |
+
7. Never create any notional variables in our code, as having these in your logs might derail you from the true variables.
|
97 |
+
8. You can use imports in your code, but only from the following list of modules: <<authorized_imports>>
|
98 |
+
9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
|
99 |
+
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
|
100 |
+
11. Only use the tools that have been provided to you.
|
101 |
+
12. If the task questions the rationale of your previous answers, explain your rationale for the previous answers and attempt to correct any mistakes in your previous answers.
|
102 |
+
13. Never give the entire response from the squad_retriever tool as your final answer. Instead, use it to inform your final answer.
|
103 |
+
|
104 |
+
As for your identity, your name is Agent SQuAD, you are an AI Agent, an expert guide to all questions and answers in the Stanford Question Answering Dataset (SQuAD), and you are SQuADtacular!
|
105 |
+
Do not use the squad_retriever tool to answer questions about yourself, such as "what is your name" or "what are you".
|
106 |
+
|
107 |
+
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
|
108 |
+
"""
|