Parsing and implementation complete, not tested or working

This commit is contained in:
Cyber MacGeddon 2024-11-10 10:26:34 +00:00
parent bb7ac5d380
commit 3f60ba540b
4 changed files with 103 additions and 67 deletions

View file

@ -2,8 +2,8 @@
agent-manager-react \ agent-manager-react \
-p pulsar://localhost:6650 \ -p pulsar://localhost:6650 \
--tool-type \ --tool-type \
shuttle=knowledge-base:query \ shuttle=knowledge-query:query \
cats=knowledge-base:query \ cats=knowledge-query:query \
compute=text-completion:computation \ compute=text-completion:computation \
--tool-description \ --tool-description \
shuttle="Query a knowledge base with information about the space shuttle. The query should be a simple natural language question" \ shuttle="Query a knowledge base with information about the space shuttle. The query should be a simple natural language question" \

View file

@ -22,9 +22,9 @@ from ... clients.prompt_client import PromptClient
from ... clients.llm_client import LlmClient from ... clients.llm_client import LlmClient
from ... clients.graph_rag_client import GraphRagClient from ... clients.graph_rag_client import GraphRagClient
from . tools import CatsKb, ShuttleKb, Compute from . tools import KnowledgeQueryImpl, TextCompletionImpl
from . agent_manager import AgentManager from . agent_manager import AgentManager
from . types import Final, Action from . types import Final, Action, Tool
module = ".".join(__name__.split(".")[1:-1]) module = ".".join(__name__.split(".")[1:-1])
@ -49,32 +49,50 @@ class Processor(ConsumerProducer):
f"Tool-type string not well-formed: {t}" f"Tool-type string not well-formed: {t}"
) )
ttoks = toks[1].split(":", 1) ttoks = toks[1].split(":", 1)
if len(ttoks) == 1: if len(ttoks) < 1:
tool_base[toks[0]] = { raise RuntimeError(
"type": ttoks[0], f"Tool-type string not well-formed: {t}"
"input-arg": "query", )
"arguments": {},
}
else:
tool_base[toks[0]] = {
"type": ttoks[0],
"input-arg": ttoks[1],
"arguments": {},
}
# Parsing the prompt information to the prompt configuration if ttoks[0] == "knowledge-query":
impl = KnowledgeQueryImpl
elif ttoks[0] == "text-completion":
impl = TextCompletionImpl
else:
raise RuntimeError(
f"Tool-kind {ttoks[0]} not known"
)
if len(ttoks) == 1:
tool_base[toks[0]] = Tool(
name = ttoks[0],
description = "",
implementation = impl,
config = { "input": "query" },
arguments = {},
)
else:
tool_base[toks[0]] = Tool(
name = ttoks[0],
description = "",
implementation = impl,
config = { "input": ttoks[1] },
arguments = {},
)
# parsing the prompt information to the prompt configuration
# structure # structure
tool_desc_arg = params.get("tool_description", []) tool_desc_arg = params.get("tool_description", [])
if tool_desc_arg: if tool_desc_arg:
for t in tool_desc_arg: for t in tool_desc_arg:
toks = t.split("=", 1) toks = t.split("=", 1)
if len(toks) < 2: if len(toks) < 2:
raise RuntimeError( raise runtimeerror(
f"Tool-type string not well-formed: {t}" f"tool-type string not well-formed: {t}"
) )
if toks[0] not in tool_base: if toks[0] not in tool_base:
raise RuntimeError(f"Description, tool {toks[0]} not known") raise runtimeerror(f"description, tool {toks[0]} not known")
tool_base[toks[0]]["description"] = toks[1] tool_base[toks[0]].description = toks[1]
# Parsing the prompt information to the prompt configuration # Parsing the prompt information to the prompt configuration
# structure # structure
@ -93,13 +111,13 @@ class Processor(ConsumerProducer):
) )
if toks[0] not in tool_base: if toks[0] not in tool_base:
raise RuntimeError(f"Description, tool {toks[0]} not known") raise RuntimeError(f"Description, tool {toks[0]} not known")
tool_base[toks[0]]["arguments"][ttoks[0]] = { tool_base[toks[0]].arguments[ttoks[0]] = {
"name": ttoks[0], "name": ttoks[0],
"type": ttoks[1], "type": ttoks[1],
"description": ttoks[2], "description": ttoks[2],
} }
print(json.dumps(tool_base, indent=4)) print(json.dumps({k: str(v) for k, v in tool_base.items()}, indent=4))
sys.exit(0) sys.exit(0)
@ -169,7 +187,7 @@ class Processor(ConsumerProducer):
) )
tools = [ tools = [
CatsKb(self), ShuttleKb(self), Compute(self), # CatsKb(self), ShuttleKb(self), Compute(self),
] ]
self.agent = AgentManager(self, tools) self.agent = AgentManager(self, tools)
@ -347,7 +365,7 @@ class Processor(ConsumerProducer):
'--tool-type', nargs='*', '--tool-type', nargs='*',
help=f'''Specifies the type of an agent tool. Takes the form help=f'''Specifies the type of an agent tool. Takes the form
<id>=<specifier>. <id> is the name of the tool. <specifier> is one of <id>=<specifier>. <id> is the name of the tool. <specifier> is one of
knowledge-base, text-completion. Additional parameters are specified knowledge-query, text-completion. Additional parameters are specified
for different tools which are tool-specific. e.g. knowledge-query:<arg> for different tools which are tool-specific. e.g. knowledge-query:<arg>
which specifies the name of the arg whose content is fed into the knowledge which specifies the name of the arg whose content is fed into the knowledge
query as a question. text-completion:<arg> specifies the name of the arg query as a question. text-completion:<arg> specifies the name of the arg

View file

@ -1,52 +1,67 @@
from . types import Tool, Argument # from . types import Tool
class CatsKb: # class CatsKb:
tool = Tool( # tool = Tool(
name = "cats-kb", # name = "cats-kb",
description = "Query a knowledge base with information about Mark's cats. The query should be a simple natural language question", # description = "Query a knowledge base with information about Mark's cats. The query should be a simple natural language question",
arguments = [ # arguments = [
Argument( # Argument(
name = "query", # name = "query",
type = "string", # type = "string",
description = "The search query string" # description = "The search query string"
) # )
] # ]
) # )
# def __init__(self, context):
# self.context = context
# def invoke(self, **arguments):
# return self.context.graph_rag.request(arguments.get("query"))
# class ShuttleKb:
# tool = Tool(
# name = "shuttle-kb",
# description = "Query a knowledge base with information about the space shuttle. The query should be a simple natural language question",
# arguments = [
# Argument(
# name = "query",
# type = "string",
# description = "The search query string"
# )
# ]
# )
# def __init__(self, context):
# self.context = context
# def invoke(self, **arguments):
# return self.context.graph_rag.request(arguments.get("query"))
# class Compute:
# tool = Tool(
# name = "compute",
# description = "A computation engine which can answer questions about maths and computation",
# arguments = [
# Argument(
# name = "computation",
# type = "string",
# description = "The computation to solve"
# )
# ]
# )
# def __init__(self, context):
# self.context = context
# def invoke(self, **arguments):
# return self.context.prompt.request(
# "question", { "question": arguments.get("computation") }
# )
class KnowledgeQueryImpl:
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context
def invoke(self, **arguments): def invoke(self, **arguments):
return self.context.graph_rag.request(arguments.get("query")) return self.context.graph_rag.request(arguments.get("query"))
class ShuttleKb: class TextCompletionImpl:
tool = Tool(
name = "shuttle-kb",
description = "Query a knowledge base with information about the space shuttle. The query should be a simple natural language question",
arguments = [
Argument(
name = "query",
type = "string",
description = "The search query string"
)
]
)
def __init__(self, context):
self.context = context
def invoke(self, **arguments):
return self.context.graph_rag.request(arguments.get("query"))
class Compute:
tool = Tool(
name = "compute",
description = "A computation engine which can answer questions about maths and computation",
arguments = [
Argument(
name = "computation",
type = "string",
description = "The computation to solve"
)
]
)
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context
def invoke(self, **arguments): def invoke(self, **arguments):

View file

@ -1,5 +1,6 @@
import dataclasses import dataclasses
from typing import Any, Dict
@dataclasses.dataclass @dataclasses.dataclass
class Argument: class Argument:
@ -12,6 +13,8 @@ class Tool:
name : str name : str
description : str description : str
arguments : list[Argument] arguments : list[Argument]
implementation : Any
config : Dict[str, str]
@dataclasses.dataclass @dataclasses.dataclass
class Action: class Action: