Skip to content

Commit

Permalink
- Moved the agent-react template from hard-coded in agent_manager.py to
Browse files Browse the repository at this point in the history
  a template prompt managed by prompt manager.  The prompt is called
  agent-react.
  • Loading branch information
cybermaggedon committed Nov 12, 2024
1 parent 58d3665 commit 97a496a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 91 deletions.
4 changes: 4 additions & 0 deletions templates/prompts/default-prompts.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

}
Expand Down
102 changes: 11 additions & 91 deletions trustgraph-flow/trustgraph/agent/react/agent_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import ibis
import logging
import json

Expand All @@ -9,95 +8,21 @@

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
self.additional_context = additional_context

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,
Expand All @@ -124,26 +49,21 @@ def reason(self, question, history):
"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("```", "")

logger.info(f"response: {resp}")
print(json.dumps(obj, indent=4), flush=True)

obj = json.loads(resp)
logger.info(f"response: {obj}")

if obj.get("final-answer"):

Expand Down

0 comments on commit 97a496a

Please sign in to comment.