Node in AgentKit
The basic building block in AgentKit is a node, containing a natural language prompt for a specific subtask. The nodes are linked together by the dependency specifications, which specify the order of evaluation. Different arrangements of nodes can represent different different logic and throught processes. At inference time, AgentKit evaluates all nodes in the order specified by the dependencies as a directed acyclic graph (DAG).
Inside a Node
- class agentkit.node.BaseNode(key: str, prompt: str, graph: ~agentkit.graph.Graph, query_llm: ~collections.abc.Callable, compose_prompt: ~agentkit.compose_prompt.BaseComposePrompt, after_query: ~agentkit.after_query.BaseAfterQuery | None = None, error_msg_fn: ~collections.abc.Callable[[list, str, ~agentkit.exceptions.AfterQueryError], list] = <function error_msg_default>, verbose: bool = False, token_counter: ~collections.abc.Callable | None = None)
Base class for a node in the graph.
Each node in the graph is an instance of the BaseNode class. The node is evaluated by querying the LLM with a prompt.
- query_llm
Function to query the LLM.
- Type:
Callable
- _compose_prompt
ComposePrompt object.
- Type:
- after_query
AfterQuery object.
- Type:
- error_msg_fn
Function to add error message to the prompt.
- Type:
Callable
- token_counter
Function to count tokens.
- Type:
Callable
- __init__(key: str, prompt: str, graph: ~agentkit.graph.Graph, query_llm: ~collections.abc.Callable, compose_prompt: ~agentkit.compose_prompt.BaseComposePrompt, after_query: ~agentkit.after_query.BaseAfterQuery | None = None, error_msg_fn: ~collections.abc.Callable[[list, str, ~agentkit.exceptions.AfterQueryError], list] = <function error_msg_default>, verbose: bool = False, token_counter: ~collections.abc.Callable | None = None)
Initializes the BaseNode class.
- Parameters:
key (str) – Unique key for the node.
prompt (str) – Prompt for the node.
graph (Graph) – Graph object.
query_llm (Callable) – Function to query the LLM.
compose_prompt (BaseComposePrompt) – ComposePrompt object.
after_query (BaseAfterQuery) – AfterQuery object.
error_msg_fn (Callable) – Function to add error message to the prompt.
verbose (bool) – Verbose flag.
token_counter (Callable) – Function to count tokens.
Inside each node \(v\), AgentKit runs a built-in flow that preprocesses the input (Compose), queryies the LLM with a preprocessed input and prompt \(q_v\), and optionally postprocesses the output of the LLM (After-query). For example, node \(n_{4}\) can be designed to Identify the intentions of other road users. (left of Figure).
Each node in AgentKit takes outputs from its dependencies and outputs a string to complete a predefined subtask. The orange components (After-query) are optional and can be further customized with minimal programming through the AgentKit API. Left: The evaluation process inside a node consists of compose and after-query.
Compose
- class agentkit.compose_prompt.BaseComposePrompt(system_prompt: str = 'You are a helpful assistant.')
Base class for composing prompts. This class is used to compose prompts for the LLM.
Each compose prompt instance takes dependencies (QA results from dependent nodes) and a prompt as input and returns a prompt in OpenAI format.
- __init__(system_prompt: str = 'You are a helpful assistant.')
Initializes the BaseComposePrompt class.
- Parameters:
system_prompt (str) – System prompt to be used in the conversation.
The Compose operation is a built-in operation that preprocesses the input before querying the LLM. The Compose operation is designed to be customizable with minimal programming through the AgentKit API. For example, the Compose operation can be used to add a prefix to the input, remove irrelevant information, or add additional context to the input.
- class agentkit.compose_prompt.ComposePromptDB(system_prompt: str = 'You are a helpful assistant.')
Class for composing prompts with database values. This class is used to compose prompts for the LLM.
Each compose prompt instance takes dependencies (QA results from dependent nodes) and a prompt as input and returns a prompt in OpenAI format.
- node
Corresponding Node object.
- Type:
Node
- __init__(system_prompt: str = 'You are a helpful assistant.')
Initializes the ComposePromptDB class.
- Parameters:
system_prompt (str) – System prompt to be used in the conversation.
- add_dependencies(messages, dependencies, db)
Add dependencies to the messages with db values.
- Parameters:
messages (list) – List of messages.
dependencies (list) – List of dependencies.
db (dict) – Database values.
- Returns:
List of messages with dependencies in OpenAI format.
- Return type:
messages (list)
- compose(dependencies, prompt)
Compose prompt for the LLM with database augmentation.
Prompt text may contain placeholders for db values. This function will replace the placeholders with the actual values from db.
- Parameters:
dependencies (list) – List of dependencies.
prompt (str) – User prompt.
- Returns:
List of messages in OpenAI format. shrink_idx (int): Index to shrink the prompt in case of truncation.
- Return type:
msg (list)
- render_db(text, db)
Render db placeholders in the prompt text.
Prompt text may contain placeholders for db values. This function will replace the placeholders with the actual values from db.
Example:
Confirm if the subgoal ‘$db.subgoals.subgoal$’ is still accurate for the challenge.
Confirm if the subgoal ‘$db.subgoals.subgoal$’ is incomplete and up-to-date according to the completion criteria ‘$db.subgoals.guide$’.
- Parameters:
text (str) – Prompt text.
db (dict) – Database values.
- Returns:
Prompt text with db placeholders replaced with actual values. db_retrieval_results (list): List of db retrieval results.
- Return type:
text (str)
- set_node(node)
Set the node for the ComposePromptDB class.
- Parameters:
node (Node) – Corresponding Node object.
The Compose operation can take a database and perform retrival augmented generation (RAG). The database can be used to store and retrieve:
Permanent information like system prompts, user preferences, or context information.
External information like multi-modal environment observations, interaction history.
Temporary information like previous plan/strategy, intermediate results, user inputs.
After-query
- class agentkit.after_query.BaseAfterQuery
Base class for after query postprocessing.
Each after query instance performs postprocessing after the LLM query.
- node
The Node object that this after-query instance is associated with.
- Type:
- __init__()
- post_process()
Post process the result of the LLM query.
This method can be overridden by the derived class to perform postprocessing.
- set_node(node)
Set the node for the after query.
This function will be called automatically upon initialization of a SimpleDBNode
- Parameters:
node (SimpleDBNode) – Node object.
The After-query operation is a optional operation that postprocesses the output of the LLM.
- class agentkit.after_query.JsonAfterQuery
Class for after query postprocessing of Json objects.
- __init__()
- parse_json()
Parse the result of the LLM query.
This method parses self.node.result (str) and returns the parsed Json object. The method checks the length of the parsed Json object, and if the Json object contains the required keys.
- Raises:
AfterQueryError – If the answer is invalid.
- Returns:
List of Json objects, where the last Json object is the answer.
- Return type:
For example, the JsonAfterQuery operation can be used to extract structured information from the LLM output, and store them in a database for future use.