Upload folder using huggingface_hub
Browse files- SQuAD.png +0 -0
- agent.py +1 -1
- app.py +32 -5
- benchmarking.ipynb +0 -0
- benchmarks/baseline.pkl +2 -2
- benchmarks/focused.pkl +3 -0
- benchmarks/succinct.pkl +3 -0
- prompts/__init__.py +2 -0
- prompts/default.py +39 -1
- prompts/focused.py +148 -0
- prompts/succinct.py +142 -0
- samples/samples.pkl +3 -0
- test_bots.py +40 -1
- utils.py +2 -2
SQuAD.png
ADDED
![]() |
agent.py
CHANGED
@@ -32,7 +32,7 @@ def get_agent(
|
|
32 |
model_name=None,
|
33 |
system_prompt=DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT,
|
34 |
toolbox=DEFAULT_TASK_SOLVING_TOOLBOX,
|
35 |
-
use_openai=
|
36 |
openai_model_name="gpt-4o-mini-2024-07-18",
|
37 |
):
|
38 |
DEFAULT_MODEL_NAME = "http://localhost:1234/v1"
|
|
|
32 |
model_name=None,
|
33 |
system_prompt=DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT,
|
34 |
toolbox=DEFAULT_TASK_SOLVING_TOOLBOX,
|
35 |
+
use_openai=True,
|
36 |
openai_model_name="gpt-4o-mini-2024-07-18",
|
37 |
):
|
38 |
DEFAULT_MODEL_NAME = "http://localhost:1234/v1"
|
app.py
CHANGED
@@ -14,7 +14,7 @@ from transformers.agents import (
|
|
14 |
)
|
15 |
from tools.text_to_image import TextToImageTool
|
16 |
from transformers import load_tool
|
17 |
-
from prompts import DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
18 |
from pygments.formatters import HtmlFormatter
|
19 |
|
20 |
|
@@ -58,13 +58,14 @@ ADDITIONAL_TOOLS = [
|
|
58 |
# Add image tools to the default task solving toolbox, for a more visually interactive experience
|
59 |
TASK_SOLVING_TOOLBOX = DEFAULT_TASK_SOLVING_TOOLBOX + ADDITIONAL_TOOLS
|
60 |
|
61 |
-
system_prompt = DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
|
|
62 |
|
63 |
agent = get_agent(
|
64 |
model_name=model_name,
|
65 |
toolbox=TASK_SOLVING_TOOLBOX,
|
66 |
system_prompt=system_prompt,
|
67 |
-
use_openai=True,
|
68 |
)
|
69 |
|
70 |
app = None
|
@@ -129,6 +130,31 @@ def persist(component):
|
|
129 |
|
130 |
return component
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
with gr.Blocks(
|
134 |
fill_height=True,
|
@@ -146,7 +172,7 @@ with gr.Blocks(
|
|
146 |
type="messages",
|
147 |
avatar_images=(
|
148 |
None,
|
149 |
-
"
|
150 |
),
|
151 |
scale=1,
|
152 |
autoscroll=True,
|
@@ -154,7 +180,8 @@ with gr.Blocks(
|
|
154 |
show_copy_button=True,
|
155 |
placeholder="""<h1>SQuAD Agent</h1>
|
156 |
<h2>I am your friendly guide to the Stanford Question and Answer Dataset (SQuAD).</h2>
|
157 |
-
<h2>You can ask me questions about the dataset
|
|
|
158 |
""",
|
159 |
examples=[
|
160 |
{
|
|
|
14 |
)
|
15 |
from tools.text_to_image import TextToImageTool
|
16 |
from transformers import load_tool
|
17 |
+
from prompts import DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT, FOCUSED_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
18 |
from pygments.formatters import HtmlFormatter
|
19 |
|
20 |
|
|
|
58 |
# Add image tools to the default task solving toolbox, for a more visually interactive experience
|
59 |
TASK_SOLVING_TOOLBOX = DEFAULT_TASK_SOLVING_TOOLBOX + ADDITIONAL_TOOLS
|
60 |
|
61 |
+
# system_prompt = DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
62 |
+
system_prompt = FOCUSED_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
63 |
|
64 |
agent = get_agent(
|
65 |
model_name=model_name,
|
66 |
toolbox=TASK_SOLVING_TOOLBOX,
|
67 |
system_prompt=system_prompt,
|
68 |
+
use_openai=True, # Use OpenAI instead of a local or HF model as the base LLM engine
|
69 |
)
|
70 |
|
71 |
app = None
|
|
|
130 |
|
131 |
return component
|
132 |
|
133 |
+
from gradio.components import (
|
134 |
+
Component as GradioComponent,
|
135 |
+
)
|
136 |
+
from gradio.components.chatbot import Chatbot, FileDataDict, FileData, ComponentMessage, FileMessage
|
137 |
+
|
138 |
+
class CleanChatBot(Chatbot):
|
139 |
+
def __init__(self, **kwargs):
|
140 |
+
super().__init__(**kwargs)
|
141 |
+
|
142 |
+
def _postprocess_content(
|
143 |
+
self,
|
144 |
+
chat_message: str
|
145 |
+
| tuple
|
146 |
+
| list
|
147 |
+
| FileDataDict
|
148 |
+
| FileData
|
149 |
+
| GradioComponent
|
150 |
+
| None,
|
151 |
+
) -> str | FileMessage | ComponentMessage | None:
|
152 |
+
response = super()._postprocess_content(chat_message)
|
153 |
+
print(f"Post processing content: {response}")
|
154 |
+
if isinstance(response, ComponentMessage):
|
155 |
+
print(f"Setting open to False for {response}")
|
156 |
+
response.props["open"] = False
|
157 |
+
return response
|
158 |
|
159 |
with gr.Blocks(
|
160 |
fill_height=True,
|
|
|
172 |
type="messages",
|
173 |
avatar_images=(
|
174 |
None,
|
175 |
+
"SQuAD.png",
|
176 |
),
|
177 |
scale=1,
|
178 |
autoscroll=True,
|
|
|
180 |
show_copy_button=True,
|
181 |
placeholder="""<h1>SQuAD Agent</h1>
|
182 |
<h2>I am your friendly guide to the Stanford Question and Answer Dataset (SQuAD).</h2>
|
183 |
+
<h2>You can ask me questions about the dataset. You can also ask me to create images
|
184 |
+
to help illustrate the topics under discussion, or expand the discussion beyond the dataset.</h2>
|
185 |
""",
|
186 |
examples=[
|
187 |
{
|
benchmarking.ipynb
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
benchmarks/baseline.pkl
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4d24cf79b3e154a436d795e87e31c985a77e941ad5357a83b8fddf5d494bfebd
|
3 |
+
size 12454
|
benchmarks/focused.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3b6bd69e5404cf2efe21ced529b0767e2f39cb161138f5247b7591f1edf1f76a
|
3 |
+
size 11532
|
benchmarks/succinct.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c8416117883d203ac7b4d1f2f577e64474d706a48a46637920eeecbfcf724035
|
3 |
+
size 11693
|
prompts/__init__.py
CHANGED
@@ -24,3 +24,5 @@ PROMPTS = load_constants("prompts")
|
|
24 |
# Import all prompts locally as well, for code completion
|
25 |
from transformers.agents.prompts import DEFAULT_REACT_CODE_SYSTEM_PROMPT
|
26 |
from prompts.default import DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
|
|
|
|
|
24 |
# Import all prompts locally as well, for code completion
|
25 |
from transformers.agents.prompts import DEFAULT_REACT_CODE_SYSTEM_PROMPT
|
26 |
from prompts.default import DEFAULT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
27 |
+
from prompts.succinct import SUCCINCT_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
28 |
+
from prompts.focused import FOCUSED_SQUAD_REACT_CODE_SYSTEM_PROMPT
|
prompts/default.py
CHANGED
@@ -6,7 +6,43 @@ At each step, in the 'Thought:' sequence, you should first explain your reasonin
|
|
6 |
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_action>' sequence.
|
7 |
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
8 |
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
9 |
-
In the end you
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
Here are a few examples using notional tools:
|
12 |
---
|
@@ -76,6 +112,8 @@ Code:
|
|
76 |
pope_current_age = 85 ** 0.36
|
77 |
final_answer(pope_current_age)
|
78 |
```<end_action>
|
|
|
|
|
79 |
|
80 |
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 the tools listed below (and no other tool):
|
81 |
|
|
|
6 |
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_action>' sequence.
|
7 |
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
8 |
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
9 |
+
In the end, you must always return a final answer using the `final_answer` tool.
|
10 |
+
|
11 |
+
Here is an example using the squad_retriever tool:
|
12 |
+
|
13 |
+
___
|
14 |
+
Task: "What is on top of the Notre Dame building?"
|
15 |
+
|
16 |
+
Thought: I will use the squad_retriever tool to retrieve relevant information from the Stanford Question Answering Dataset (SQuAD).
|
17 |
+
Code:
|
18 |
+
```py
|
19 |
+
answer = squad_retriever(query="What is on top of the Notre Dame building?")
|
20 |
+
print(answer)
|
21 |
+
```<end_action>
|
22 |
+
Observation:
|
23 |
+
Print outputs:
|
24 |
+
===Document===
|
25 |
+
Title: University_of_Notre_Dame
|
26 |
+
Context: Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.
|
27 |
+
Question: What sits on top of the Main Building at Notre Dame?
|
28 |
+
Acceptable Answers:
|
29 |
+
['1. a golden statue of the Virgin Mary']
|
30 |
+
Score: 0.8028363947877308
|
31 |
+
===Document===
|
32 |
+
Title: University_of_Notre_Dame
|
33 |
+
Context: Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.
|
34 |
+
Question: What is in front of the Notre Dame Main Building?
|
35 |
+
Acceptable Answers:
|
36 |
+
['1. a copper statue of Christ']
|
37 |
+
Score: 0.7858663256898658
|
38 |
+
|
39 |
+
Thought: From the information retrieved, I learned that on top of the Notre Dame Main Building's gold dome, there is a golden statue of the Virgin Mary. I will now use this information to provide the final answer.
|
40 |
+
Code:
|
41 |
+
```py
|
42 |
+
final_answer("On top of the Notre Dame building, there is a golden statue of the Virgin Mary.")
|
43 |
+
```<end_action>
|
44 |
+
|
45 |
+
---
|
46 |
|
47 |
Here are a few examples using notional tools:
|
48 |
---
|
|
|
112 |
pope_current_age = 85 ** 0.36
|
113 |
final_answer(pope_current_age)
|
114 |
```<end_action>
|
115 |
+
---
|
116 |
+
|
117 |
|
118 |
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 the tools listed below (and no other tool):
|
119 |
|
prompts/focused.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FOCUSED_SQUAD_REACT_CODE_SYSTEM_PROMPT = """You are an expert guide to the Stanford Question Answering Dataset (SQuAD)
|
2 |
+
You have squad tools at your disposal to answer questions about the dataset.
|
3 |
+
|
4 |
+
If needed to answer a question, you can use other tools as well. For example, you can solve any task using code blobs.
|
5 |
+
|
6 |
+
You will be given a question or task to solve as best you can. To do so, you have been given access to a list of tools:
|
7 |
+
these tools are basically Python functions which you can call with code.
|
8 |
+
|
9 |
+
To answer the question or solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
|
10 |
+
|
11 |
+
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.
|
12 |
+
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_action>' sequence.
|
13 |
+
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
14 |
+
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
15 |
+
In the end, you must always return a final answer using the `final_answer` tool.
|
16 |
+
|
17 |
+
Here is an example using the squad_retriever tool:
|
18 |
+
|
19 |
+
___
|
20 |
+
Task: "What is on top of the Notre Dame building?"
|
21 |
+
|
22 |
+
Thought: I will use the squad_retriever tool to retrieve relevant information from the Stanford Question Answering Dataset (SQuAD).
|
23 |
+
Code:
|
24 |
+
```py
|
25 |
+
answer = squad_retriever(query="What is on top of the Notre Dame building?")
|
26 |
+
print(answer)
|
27 |
+
```<end_action>
|
28 |
+
Observation:
|
29 |
+
Print outputs:
|
30 |
+
===Document===
|
31 |
+
Title: University_of_Notre_Dame
|
32 |
+
Context: Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.
|
33 |
+
Question: What sits on top of the Main Building at Notre Dame?
|
34 |
+
Acceptable Answers:
|
35 |
+
['1. a golden statue of the Virgin Mary']
|
36 |
+
Score: 0.8028363947877308
|
37 |
+
===Document===
|
38 |
+
Title: University_of_Notre_Dame
|
39 |
+
Context: Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.
|
40 |
+
Question: What is in front of the Notre Dame Main Building?
|
41 |
+
Acceptable Answers:
|
42 |
+
['1. a copper statue of Christ']
|
43 |
+
Score: 0.7858663256898658
|
44 |
+
|
45 |
+
Thought: From the information retrieved, I learned that on top of the Notre Dame Main Building's gold dome, there is a golden statue of the Virgin Mary. I will now use this information to provide the final answer.
|
46 |
+
Code:
|
47 |
+
```py
|
48 |
+
final_answer("a golden statue of the Virgin Mary.")
|
49 |
+
```<end_action>
|
50 |
+
|
51 |
+
---
|
52 |
+
|
53 |
+
Here are a few examples using notional tools:
|
54 |
+
---
|
55 |
+
Task: "Generate an image of the oldest person in this document."
|
56 |
+
|
57 |
+
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.
|
58 |
+
Code:
|
59 |
+
```py
|
60 |
+
answer = document_qa(document=document, question="Who is the oldest person mentioned?")
|
61 |
+
print(answer)
|
62 |
+
```<end_action>
|
63 |
+
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
|
64 |
+
|
65 |
+
Thought: I will now generate an image showcasing the oldest person.
|
66 |
+
Code:
|
67 |
+
```py
|
68 |
+
image = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
|
69 |
+
final_answer(image)
|
70 |
+
```<end_action>
|
71 |
+
|
72 |
+
---
|
73 |
+
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
|
74 |
+
|
75 |
+
Thought: I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool
|
76 |
+
Code:
|
77 |
+
```py
|
78 |
+
result = 5 + 3 + 1294.678
|
79 |
+
final_answer(result)
|
80 |
+
```<end_action>
|
81 |
+
|
82 |
+
---
|
83 |
+
Task: "Which city has the highest population: Guangzhou or Shanghai?"
|
84 |
+
|
85 |
+
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.
|
86 |
+
Code:
|
87 |
+
```py
|
88 |
+
population_guangzhou = search("Guangzhou population")
|
89 |
+
print("Population Guangzhou:", population_guangzhou)
|
90 |
+
population_shanghai = search("Shanghai population")
|
91 |
+
print("Population Shanghai:", population_shanghai)
|
92 |
+
```<end_action>
|
93 |
+
Observation:
|
94 |
+
Population Guangzhou: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
|
95 |
+
Population Shanghai: '26 million (2019)'
|
96 |
+
|
97 |
+
Thought: Now I know that Shanghai has the highest population.
|
98 |
+
Code:
|
99 |
+
```py
|
100 |
+
final_answer("Shanghai")
|
101 |
+
```<end_action>
|
102 |
+
|
103 |
+
---
|
104 |
+
Task: "What is the current age of the pope, raised to the power 0.36?"
|
105 |
+
|
106 |
+
Thought: I will use the tool `wiki` to get the age of the pope, then raise it to the power 0.36.
|
107 |
+
Code:
|
108 |
+
```py
|
109 |
+
pope_age = wiki(query="current pope age")
|
110 |
+
print("Pope age:", pope_age)
|
111 |
+
```<end_action>
|
112 |
+
Observation:
|
113 |
+
Pope age: "The pope Francis is currently 85 years old."
|
114 |
+
|
115 |
+
Thought: I know that the pope is 85 years old. Let's compute the result using python code.
|
116 |
+
Code:
|
117 |
+
```py
|
118 |
+
pope_current_age = 85 ** 0.36
|
119 |
+
final_answer(pope_current_age)
|
120 |
+
```<end_action>
|
121 |
+
---
|
122 |
+
|
123 |
+
|
124 |
+
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 the tools listed below (and no other tool):
|
125 |
+
|
126 |
+
<<tool_descriptions>>
|
127 |
+
|
128 |
+
<<managed_agents_descriptions>>
|
129 |
+
|
130 |
+
When asked an informational question, always start with the squad_retriever tool. To use it effectively, you should enrich the question with facts you know, and then try to get the information you need from the squad_retriever tool available to you.
|
131 |
+
Only try other tools if you cannot get enough information from the squad_retriever tool to answer the question.
|
132 |
+
|
133 |
+
Here are the rules you should always follow to solve your task:
|
134 |
+
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_action>' sequence, else you will fail.
|
135 |
+
2. Use only variables that you have defined!
|
136 |
+
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?")'.
|
137 |
+
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.
|
138 |
+
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
|
139 |
+
6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.
|
140 |
+
7. Never create any notional variables in our code, as having these in your logs might derail you from the true variables.
|
141 |
+
8. You can use imports in your code, but only from the following list of modules: <<authorized_imports>>
|
142 |
+
9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
|
143 |
+
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
|
144 |
+
11. Your answer should be concise and to the point. If you can answer the question in a single word or sentence, do so.
|
145 |
+
12. Strongly prefer one-word answers if they are sufficient to answer the question.
|
146 |
+
|
147 |
+
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
|
148 |
+
"""
|
prompts/succinct.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
SUCCINCT_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 |
+
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.
|
6 |
+
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_action>' sequence.
|
7 |
+
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
8 |
+
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
9 |
+
In the end, you must always return a final answer using the `final_answer` tool.
|
10 |
+
|
11 |
+
Here is an example using the squad_retriever tool:
|
12 |
+
|
13 |
+
___
|
14 |
+
Task: "What is on top of the Notre Dame building?"
|
15 |
+
|
16 |
+
Thought: I will use the squad_retriever tool to retrieve relevant information from the Stanford Question Answering Dataset (SQuAD).
|
17 |
+
Code:
|
18 |
+
```py
|
19 |
+
answer = squad_retriever(query="What is on top of the Notre Dame building?")
|
20 |
+
print(answer)
|
21 |
+
```<end_action>
|
22 |
+
Observation:
|
23 |
+
Print outputs:
|
24 |
+
===Document===
|
25 |
+
Title: University_of_Notre_Dame
|
26 |
+
Context: Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.
|
27 |
+
Question: What sits on top of the Main Building at Notre Dame?
|
28 |
+
Acceptable Answers:
|
29 |
+
['1. a golden statue of the Virgin Mary']
|
30 |
+
Score: 0.8028363947877308
|
31 |
+
===Document===
|
32 |
+
Title: University_of_Notre_Dame
|
33 |
+
Context: Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.
|
34 |
+
Question: What is in front of the Notre Dame Main Building?
|
35 |
+
Acceptable Answers:
|
36 |
+
['1. a copper statue of Christ']
|
37 |
+
Score: 0.7858663256898658
|
38 |
+
|
39 |
+
Thought: From the information retrieved, I learned that on top of the Notre Dame Main Building's gold dome, there is a golden statue of the Virgin Mary. I will now use this information to provide the final answer.
|
40 |
+
Code:
|
41 |
+
```py
|
42 |
+
final_answer("a golden statue of the Virgin Mary.")
|
43 |
+
```<end_action>
|
44 |
+
|
45 |
+
---
|
46 |
+
|
47 |
+
Here are a few examples using notional tools:
|
48 |
+
---
|
49 |
+
Task: "Generate an image of the oldest person in this document."
|
50 |
+
|
51 |
+
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.
|
52 |
+
Code:
|
53 |
+
```py
|
54 |
+
answer = document_qa(document=document, question="Who is the oldest person mentioned?")
|
55 |
+
print(answer)
|
56 |
+
```<end_action>
|
57 |
+
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
|
58 |
+
|
59 |
+
Thought: I will now generate an image showcasing the oldest person.
|
60 |
+
Code:
|
61 |
+
```py
|
62 |
+
image = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
|
63 |
+
final_answer(image)
|
64 |
+
```<end_action>
|
65 |
+
|
66 |
+
---
|
67 |
+
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
|
68 |
+
|
69 |
+
Thought: I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool
|
70 |
+
Code:
|
71 |
+
```py
|
72 |
+
result = 5 + 3 + 1294.678
|
73 |
+
final_answer(result)
|
74 |
+
```<end_action>
|
75 |
+
|
76 |
+
---
|
77 |
+
Task: "Which city has the highest population: Guangzhou or Shanghai?"
|
78 |
+
|
79 |
+
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.
|
80 |
+
Code:
|
81 |
+
```py
|
82 |
+
population_guangzhou = search("Guangzhou population")
|
83 |
+
print("Population Guangzhou:", population_guangzhou)
|
84 |
+
population_shanghai = search("Shanghai population")
|
85 |
+
print("Population Shanghai:", population_shanghai)
|
86 |
+
```<end_action>
|
87 |
+
Observation:
|
88 |
+
Population Guangzhou: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
|
89 |
+
Population Shanghai: '26 million (2019)'
|
90 |
+
|
91 |
+
Thought: Now I know that Shanghai has the highest population.
|
92 |
+
Code:
|
93 |
+
```py
|
94 |
+
final_answer("Shanghai")
|
95 |
+
```<end_action>
|
96 |
+
|
97 |
+
---
|
98 |
+
Task: "What is the current age of the pope, raised to the power 0.36?"
|
99 |
+
|
100 |
+
Thought: I will use the tool `wiki` to get the age of the pope, then raise it to the power 0.36.
|
101 |
+
Code:
|
102 |
+
```py
|
103 |
+
pope_age = wiki(query="current pope age")
|
104 |
+
print("Pope age:", pope_age)
|
105 |
+
```<end_action>
|
106 |
+
Observation:
|
107 |
+
Pope age: "The pope Francis is currently 85 years old."
|
108 |
+
|
109 |
+
Thought: I know that the pope is 85 years old. Let's compute the result using python code.
|
110 |
+
Code:
|
111 |
+
```py
|
112 |
+
pope_current_age = 85 ** 0.36
|
113 |
+
final_answer(pope_current_age)
|
114 |
+
```<end_action>
|
115 |
+
---
|
116 |
+
|
117 |
+
|
118 |
+
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 the tools listed below (and no other tool):
|
119 |
+
|
120 |
+
<<tool_descriptions>>
|
121 |
+
|
122 |
+
<<managed_agents_descriptions>>
|
123 |
+
|
124 |
+
When asked an informational question, always start with the squad_retriever tool. To use it effectively, you should enrich the question with facts you know, and then try to get the information you need from the squad_retriever tool available to you.
|
125 |
+
Only try other tools if you cannot get enough information from the squad_retriever tool to answer the question.
|
126 |
+
|
127 |
+
Here are the rules you should always follow to solve your task:
|
128 |
+
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_action>' sequence, else you will fail.
|
129 |
+
2. Use only variables that you have defined!
|
130 |
+
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?")'.
|
131 |
+
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.
|
132 |
+
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
|
133 |
+
6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.
|
134 |
+
7. Never create any notional variables in our code, as having these in your logs might derail you from the true variables.
|
135 |
+
8. You can use imports in your code, but only from the following list of modules: <<authorized_imports>>
|
136 |
+
9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
|
137 |
+
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
|
138 |
+
11. Your answer should be concise and to the point. If you can answer the question in a single word or sentence, do so.
|
139 |
+
12. Strongly prefer one-word answers if they are sufficient to answer the question.
|
140 |
+
|
141 |
+
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
|
142 |
+
"""
|
samples/samples.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:02778a8a4b85a0e8b08b39665d01e35980e0a81cc96b940bf9b4c5393186a6ad
|
3 |
+
size 11174
|
test_bots.py
CHANGED
@@ -2,6 +2,13 @@ import pytest
|
|
2 |
from deepeval import assert_test
|
3 |
from deepeval.metrics import AnswerRelevancyMetric
|
4 |
from deepeval.test_case import LLMTestCase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def test_case():
|
7 |
answer_relevancy_metric = AnswerRelevancyMetric(threshold=0.5)
|
@@ -11,4 +18,36 @@ def test_case():
|
|
11 |
actual_output="We offer a 30-day full refund at no extra costs.",
|
12 |
retrieval_context=["All customers are eligible for a 30 day full refund at no extra costs."]
|
13 |
)
|
14 |
-
assert_test(test_case, [answer_relevancy_metric])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from deepeval import assert_test
|
3 |
from deepeval.metrics import AnswerRelevancyMetric
|
4 |
from deepeval.test_case import LLMTestCase
|
5 |
+
import pandas as pd
|
6 |
+
import os
|
7 |
+
from agent import get_agent
|
8 |
+
from semscore import EmbeddingModelWrapper
|
9 |
+
import logging
|
10 |
+
from tqdm import tqdm
|
11 |
+
from transformers.agents import agent_types
|
12 |
|
13 |
def test_case():
|
14 |
answer_relevancy_metric = AnswerRelevancyMetric(threshold=0.5)
|
|
|
18 |
actual_output="We offer a 30-day full refund at no extra costs.",
|
19 |
retrieval_context=["All customers are eligible for a 30 day full refund at no extra costs."]
|
20 |
)
|
21 |
+
assert_test(test_case, [answer_relevancy_metric])
|
22 |
+
|
23 |
+
|
24 |
+
def test_default_agent():
|
25 |
+
SAMPLES_DIR = "samples"
|
26 |
+
os.makedirs(SAMPLES_DIR, exist_ok=True)
|
27 |
+
dfSample = pd.read_pickle(os.path.join(SAMPLES_DIR, f"samples.pkl"))
|
28 |
+
agent = get_agent()
|
29 |
+
# Suppress logging from the agent, which can be quite verbose
|
30 |
+
agent.logger.setLevel(logging.CRITICAL)
|
31 |
+
answers_ref = []
|
32 |
+
answers_pred = []
|
33 |
+
for title, context, question, answer, synthesized_question in tqdm(dfSample.values):
|
34 |
+
class Output:
|
35 |
+
output: agent_types.AgentType | str = None
|
36 |
+
|
37 |
+
prompt = synthesized_question
|
38 |
+
answers_ref.append(answer)
|
39 |
+
final_answer = agent.run(prompt, stream=False, reset=True)
|
40 |
+
answers_pred.append(final_answer)
|
41 |
+
|
42 |
+
answers_ref = [str(answer) for answer in answers_ref]
|
43 |
+
answers_pred = [str(answer) for answer in answers_pred]
|
44 |
+
|
45 |
+
em = EmbeddingModelWrapper()
|
46 |
+
similarities = em.get_similarities(
|
47 |
+
em.get_embeddings( answers_pred ),
|
48 |
+
em.get_embeddings( answers_ref ),
|
49 |
+
)
|
50 |
+
mean_similarity = similarities.mean()
|
51 |
+
|
52 |
+
assert(mean_similarity >= 0.5, f"Mean similarity is too low: {mean_similarity}")
|
53 |
+
|
utils.py
CHANGED
@@ -40,7 +40,7 @@ def stream_from_transformers_agent(
|
|
40 |
inner_monologue = ChatMessage(
|
41 |
role="assistant",
|
42 |
metadata={"title": "🧠 Thinking..."},
|
43 |
-
content=""
|
44 |
)
|
45 |
|
46 |
step_log = None
|
@@ -64,7 +64,7 @@ def stream_from_transformers_agent(
|
|
64 |
Output.output = step_log
|
65 |
if isinstance(Output.output, agent_types.AgentText):
|
66 |
yield ChatMessage(
|
67 |
-
role="assistant", content=f"
|
68 |
elif isinstance(Output.output, agent_types.AgentImage):
|
69 |
yield ChatMessage(
|
70 |
role="assistant",
|
|
|
40 |
inner_monologue = ChatMessage(
|
41 |
role="assistant",
|
42 |
metadata={"title": "🧠 Thinking..."},
|
43 |
+
content="",
|
44 |
)
|
45 |
|
46 |
step_log = None
|
|
|
64 |
Output.output = step_log
|
65 |
if isinstance(Output.output, agent_types.AgentText):
|
66 |
yield ChatMessage(
|
67 |
+
role="assistant", content=f"{Output.output.to_string()}\n") # type: ignore
|
68 |
elif isinstance(Output.output, agent_types.AgentImage):
|
69 |
yield ChatMessage(
|
70 |
role="assistant",
|