- Moved the agent-react template from hard-coded in agent_manager.py to (#151)

a template prompt managed by prompt manager.  The prompt is called
  agent-react.
This commit is contained in:
cybermaggedon 2024-11-12 19:04:15 +00:00 committed by GitHub
parent 58d3665947
commit 081dd697c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 91 deletions

View file

@ -1,5 +1,4 @@
import ibis
import logging
import json
@ -9,79 +8,6 @@ logger = logging.getLogger(__name__)
class AgentManager:
template="""Answer the following questions as best you can. You have
access to the following functions:
{% for tool in tools %}{
"function": "{{ tool.name }}",
"description": "{{ tool.description }}",
"arguments": [
{% for arg in tool.arguments %} {
"name": "{{ arg.name }}",
"type": "{{ arg.type }}",
"description": "{{ arg.description }}",
}
{% endfor %}
]
}
{% endfor %}
You can either choose to call a function to get more information, or
return a final answer.
To call a function, respond with a JSON object of the following format:
{
"thought": "your thought about what to do",
"action": "the action to take, should be one of [{{tool_names}}]",
"arguments": {
"argument1": "argument_value",
"argument2": "argument_value"
}
}
To provide a final answer, response a JSON object of the following format:
{
"thought": "I now know the final answer",
"final-answer": "the final answer to the original input question"
}
Previous steps are included in the input. Each step has the following
format in your output:
{
"thought": "your thought about what to do",
"action": "the action taken",
"arguments": {
"argument1": action argument,
"argument2": action argument2
},
"observation": "the result of the action",
}
Respond by describing either one single thought/action/arguments or
the final-answer. Pause after providing one action or final-answer.
{% if context %}Additional context has been provided:
{{context}}{% endif %}
Question: {{question}}
Input:
{% for h in history %}
{
"action": "{{h.action}}",
"arguments": [
{% for k, v in h.arguments.items() %} {
"{{k}}": "{{v}}",
{%endfor%} }
],
"observation": "{{h.observation}}"
}
{% endfor %}"""
def __init__(self, context, tools, additional_context=None):
self.context = context
self.tools = tools
@ -89,15 +15,14 @@ Input:
def reason(self, question, history):
tpl = ibis.Template(self.template)
tools = self.tools
tool_names = ",".join([
t for t in self.tools.keys()
])
prompt = tpl.render({
variables = {
"question": question,
"tools": [
{
"name": tool.name,
@ -124,26 +49,21 @@ Input:
"observation": h.observation,
}
for h in history
],
})
]
}
print(prompt)
print(json.dumps(variables, indent=4), flush=True)
logger.info(f"prompt: {prompt}")
logger.info(f"prompt: {variables}")
resp = self.context.prompt.request(
"question",
{
"question": prompt
}
obj = self.context.prompt.request(
"agent-react",
variables
)
resp = resp.replace("```json", "")
resp = resp.replace("```", "")
print(json.dumps(obj, indent=4), flush=True)
logger.info(f"response: {resp}")
obj = json.loads(resp)
logger.info(f"response: {obj}")
if obj.get("final-answer"):