mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-05-02 11:52:37 +02:00
- 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:
parent
58d3665947
commit
081dd697c4
2 changed files with 15 additions and 91 deletions
|
|
@ -104,6 +104,10 @@
|
||||||
"response-type": "text",
|
"response-type": "text",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"agent-react":: {
|
||||||
|
"prompt": "Answer the following questions as best you can. You have\naccess to the following functions:\n\n{% for tool in tools %}{\n \"function\": \"{{ tool.name }}\",\n \"description\": \"{{ tool.description }}\",\n \"arguments\": [\n{% for arg in tool.arguments %} {\n \"name\": \"{{ arg.name }}\",\n \"type\": \"{{ arg.type }}\",\n \"description\": \"{{ arg.description }}\",\n }\n{% endfor %}\n ]\n}\n{% endfor %}\n\nYou can either choose to call a function to get more information, or\nreturn a final answer.\n \nTo call a function, respond with a JSON object of the following format:\n\n{\n \"thought\": \"your thought about what to do\",\n \"action\": \"the action to take, should be one of [{{tool_names}}]\",\n \"arguments\": {\n \"argument1\": \"argument_value\",\n \"argument2\": \"argument_value\"\n }\n}\n\nTo provide a final answer, response a JSON object of the following format:\n\n{\n \"thought\": \"I now know the final answer\",\n \"final-answer\": \"the final answer to the original input question\"\n}\n\nPrevious steps are included in the input. Each step has the following\nformat in your output:\n\n{\n \"thought\": \"your thought about what to do\",\n \"action\": \"the action taken\",\n \"arguments\": {\n \"argument1\": action argument,\n \"argument2\": action argument2\n },\n \"observation\": \"the result of the action\",\n}\n\nRespond by describing either one single thought/action/arguments or\nthe final-answer. Pause after providing one action or final-answer.\n\n{% if context %}Additional context has been provided:\n{{context}}{% endif %}\n\nQuestion: {{question}}\n\nInput:\n \n{% for h in history %}\n{\n \"action\": \"{{h.action}}\",\n \"arguments\": [\n{% for k, v in h.arguments.items() %} {\n \"{{k}}\": \"{{v}}\",\n{%endfor%} }\n ],\n \"observation\": \"{{h.observation}}\"\n}\n{% endfor %}",
|
||||||
|
"response-type": "json"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
|
|
||||||
import ibis
|
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
@ -9,79 +8,6 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class AgentManager:
|
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):
|
def __init__(self, context, tools, additional_context=None):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.tools = tools
|
self.tools = tools
|
||||||
|
|
@ -89,15 +15,14 @@ Input:
|
||||||
|
|
||||||
def reason(self, question, history):
|
def reason(self, question, history):
|
||||||
|
|
||||||
tpl = ibis.Template(self.template)
|
|
||||||
|
|
||||||
tools = self.tools
|
tools = self.tools
|
||||||
|
|
||||||
tool_names = ",".join([
|
tool_names = ",".join([
|
||||||
t for t in self.tools.keys()
|
t for t in self.tools.keys()
|
||||||
])
|
])
|
||||||
|
|
||||||
prompt = tpl.render({
|
variables = {
|
||||||
|
"question": question,
|
||||||
"tools": [
|
"tools": [
|
||||||
{
|
{
|
||||||
"name": tool.name,
|
"name": tool.name,
|
||||||
|
|
@ -124,26 +49,21 @@ Input:
|
||||||
"observation": h.observation,
|
"observation": h.observation,
|
||||||
}
|
}
|
||||||
for h in history
|
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(
|
obj = self.context.prompt.request(
|
||||||
"question",
|
"agent-react",
|
||||||
{
|
variables
|
||||||
"question": prompt
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
resp = resp.replace("```json", "")
|
print(json.dumps(obj, indent=4), flush=True)
|
||||||
resp = resp.replace("```", "")
|
|
||||||
|
|
||||||
logger.info(f"response: {resp}")
|
logger.info(f"response: {obj}")
|
||||||
|
|
||||||
obj = json.loads(resp)
|
|
||||||
|
|
||||||
if obj.get("final-answer"):
|
if obj.get("final-answer"):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue