diff --git a/templates/prompts/default-prompts.jsonnet b/templates/prompts/default-prompts.jsonnet index 6affd39d..be05b992 100644 --- a/templates/prompts/default-prompts.jsonnet +++ b/templates/prompts/default-prompts.jsonnet @@ -104,6 +104,10 @@ "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" + } } } diff --git a/trustgraph-flow/trustgraph/agent/react/agent_manager.py b/trustgraph-flow/trustgraph/agent/react/agent_manager.py index 11b258a6..5d071e30 100644 --- a/trustgraph-flow/trustgraph/agent/react/agent_manager.py +++ b/trustgraph-flow/trustgraph/agent/react/agent_manager.py @@ -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"):