-
Notifications
You must be signed in to change notification settings - Fork 3
Dependency Injection
Sometimes when writing a tool, it is useful to have access to the agent, or aspects of the archytas environment. Tools are written as functions/classes to be called by the LLM, so in order to get access, we provide a dependency injection framework.
The following dependencies are available to be hooked into:
-
agent:
(AgentRef)the underlying agent with connections to the OpenAI API -
tool function:
(ToolFnRef)the instance of the function being called by the agent -
tool name:
(ToolNameRef)the name of the function being called by the agent -
loop controller:
(LoopControllerRef)an object for breaking out of the react loop early
To use a dependency, simply include an argument in your @tool function signature with the type of the dependency you want. e.g.
@tool
def myTool(args..., agent:AgentRef)
...
@tool
def myTool(args..., raw_tool:ToolFnRef)
...
@tool
def myTool(args..., tool_name:ToolNameRef)
...
@tool
def myTool(args..., loop_controller:LoopControllerRef)
...Note that the argument name need not exactly match, but it is good practice to use the standard names for injected dependencies.
A reference to the underlying LLM agent. This can be useful in a number of cases. It allows your tool to access anything that the agent has such as chat history. You can also use the agent to make sub-queries to the LLM.
The pirate_subquery demo tool demonstrates receiving the injected agent reference, and using it to make a sub-query to the same agent
@tool()
def pirate_subquery(query:str, agent:AgentRef) -> str:
"""
Runs a subquery using a oneshot agent in which answers will be worded like a pirate.
Args:
query (str): The query to run against the agent.
Returns:
str: Result of the subquery in pirate vernacular.
"""
prompt = """
You are an pirate. Answer all questions truthfully using pirate vernacular.
"""
return agent.oneshot(prompt=prompt, query=query)agent:AgentRef is a special type that is ignored when generating the prompt. Instead at runtime, when the function is called, it gets passed a reference to the agent, which is used in the body of the function.
This is the instance of the function (or instance method) that is being called by the LLM. This is relatively unexplored in terms of usefulness.
This is just the name of the tool being called, as provided by the LLM. Typically this will exactly match the current function name, unless the name was overridden
(TODO)
- effect handler