Plan-b working

This commit is contained in:
Cyber MacGeddon 2024-11-09 11:10:19 +00:00
parent 4cc029f00c
commit 82235a0758
4 changed files with 986 additions and 79 deletions

View file

@ -9,6 +9,7 @@ import textwrap
import time
import dataclasses
import sys
import base64
def wrap(text, width=75):
@ -50,6 +51,7 @@ class Action:
thought : str
name : str
arguments : dict
observation : str
@dataclasses.dataclass
class Final:
@ -173,12 +175,13 @@ Question: {{question}}
Input:
{% for h in history %}
{
"action": {{h.action}},
"action": "{{h.action}}",
"arguments": [
{% for k, v in h.arguments.items() %} {
"{{k}}": "{{v}}",
{%endfor%} }
]
],
"observation": "{{h.observation}}"
}
{% endfor %}"""
@ -206,7 +209,8 @@ Input:
{
"thought": h.thought,
"action": h.name,
"arguments": h.arguments
"arguments": h.arguments,
"observation": h.observation,
}
for h in self.history
],
@ -214,7 +218,6 @@ Input:
print(prompt)
resp = prompt_client.request(
"question",
{
@ -225,6 +228,11 @@ Input:
resp = resp.replace("```json", "")
resp = resp.replace("```", "")
print("---")
print(resp)
print("---")
print(base64.b64encode(resp.encode("utf-8")).decode("utf-8"))
obj = json.loads(resp)
if obj.get("final-answer"):
@ -272,8 +280,32 @@ Input:
else:
self.think(act.thought)
print(act.name)
action = None
for tool in self.tools:
if tool.tool.name == act.name:
action = tool
if action is None:
raise RuntimeError(f"No action for {act.name}!")
resp = action.invoke(**act.arguments)
resp = resp.strip()
print(resp)
act.observation = resp
print("act>", act)
self.history.append(act)
print(self.history)
raise RuntimeError("Too many iterations")
@ -284,78 +316,3 @@ am = AgentManager(prompt_client, tools, q)
act = am.invoke()
print(act)
sys.exit(0)
tpl = ibis.Template(template)
history = []
output(wrap(q))
print()
for iter in range(0, 10):
prompt = tpl.render({
"tools": tools,
"question": q,
"tool_names": ",".join([t["function"] for t in tools]),
"history": [json.dumps(h, indent=2) for h in history],
})
print("-" * 76)
print(prompt)
print("-" * 76)
resp = prompt_client.request(
"question",
{
"question": prompt
}
)
print("-" * 76)
print(resp)
print("-" * 76)
resp = resp.replace("```json", "")
resp = resp.replace("```", "")
obj = json.loads(resp)
output(obj["thought"], "? ")
print()
if "final-answer" in obj:
history.append(obj)
break
if "action" in obj:
if obj["action"] == "cats-kb":
ans = graph_rag_query(obj["arguments"]["query"])
obj["observation"] = ans.strip()
elif obj["action"] == "compute":
if "196" in obj["arguments"]["query"]:
obj["observation"] = "196 > 1.319501555"
else:
obj["observation"] = "The answer is 1.319501155"
elif obj["action"] == "shuttle-kb":
ans = graph_rag_query(obj["arguments"]["query"])
obj["observation"] = ans.strip()
else:
raise RuntimeError("Unknown action:", obj["action"])
output(obj["observation"], "! ")
print()
history.append(obj)
print("-" * 76)
print(history)
print("-" * 76)
continue
raise RuntimeError("Iteration output is not action or final-answer")
output(obj["final-answer"], "< ")