agentkit.graph

Classes

Graph()

A class to represent a DAG.

class agentkit.graph.Graph

A class to represent a DAG.

This class represents a DAG with nodes and edges. It is used to represent the dependencies between different nodes in a graph. The graph can be evaluated in a topological order. The graph can also have temporary nodes and edges. Temporary nodes and edges are used to represent dynamic changes in the graph during evaluation. Temporary nodes and edges are cleared after each evaluation. The evaluation processs can also be logged to wandb.

nodes

A dictionary of all nodes in the graph.

Type:

dict

temporary_nodes

A dictionary of all temporary nodes in the graph.

Type:

dict

temporary_edges

A list of all temporary edges in the graph.

Type:

list

temporary_removed_edges

A list of all edges that have been temporarily removed.

Type:

list

history

A dictionary of all results from the graph.

Type:

dict

num_iter

The number of iterations the graph has gone through.

Type:

int

history_list

A list of all results from the graph.

Type:

list

order

A list of the order in which the nodes were evaluated.

Type:

list

queue

A list of nodes that need to be evaluated.

Type:

list

wandb_root_span

The root span for wandb logging.

Type:

wandb.sdk.data_types.trace_tree.Trace

chain_span

The chain span for logging the current step.

Type:

wandb.sdk.data_types.trace_tree.Trace

add_edge(from_key, to_key, prepend=False)

Add an edge between two nodes in the graph.

Parameters:
  • from_key (str) – The key of the node to add the edge from.

  • to_key (str) – The key of the node to add the edge to.

  • prepend (bool) – Whether to prepend the edge to the front of the adjacent_from list.

Raises:

ValueError – If the from_key or to_key is not found in the graph.

add_edge_temporary(from_key, to_key, prepend=False)

Add a temporary edge between two nodes in the graph.

This function adds a temporary edge between the two nodes. Temporary edges are used to represent dynamic changes in the graph during evaluation. Temporary edges are cleared after each evaluation.

Parameters:
  • from_key (str) – The key of the node to add the edge from.

  • to_key (str) – The key of the node to add the edge to.

  • prepend (bool) – Whether to prepend the edge to the front of the adjacent_from list.

Raises:
  • AssertionError – If the from_key or to_key is not found in the graph.

  • AssertionError – If the edge already exists between the two nodes.

Note

It is recommended to use temporary edges to represent only dynamic changes in the graph.

add_node(node)

Add a node to the graph.

Parameters:

node (Node) – The node to add to the graph.

Raises:

AssertionError – If the node already exists in the graph.

add_order(from_key, to_key)

Add an order (non-dependency edge) between two nodes in the graph.

This function can be used to specify the order without adding an edge, in order to avoid a node being evaluated before another node.

Parameters:
  • from_key (str) – The key of the node to add the order from.

  • to_key (str) – The key of the node to add the order to.

Raises:
  • AssertionError – If the from_key or to_key is not found in the graph.

  • AssertionError – If the edge already exists between the two nodes.

Note

This function does not add an edge between the two nodes.

add_temporary_node(node)

Add a temporary node to the graph.

Parameters:

node (Node) – The temporary node to add to the graph.

Raises:

AssertionError – If the node already exists in the graph.

evaluate()

Evaluate the graph in a topological order.

This function evaluates the graph in a topological order. The order of evaluation is determined by the dependencies between the nodes. The graph can also have temporary nodes and edges. Temporary nodes and edges are used to represent dynamic changes in the graph during evaluation. Temporary nodes and edges are cleared after each evaluation.

Returns:

A dictionary of the results from the graph.

Return type:

dict

Raises:

AssertionError – If the temporary nodes are not cleared before calling evaluate().

get_node_with_temporary(key)

Get a node from the graph.

Parameters:

key (str) – The key of the node to get.

Returns:

The node with the key.

Return type:

Node

Raises:

ValueError – If the key is not found in the graph.

has_edge_with_temporary(from_key, to_key)

Check if there is an edge between two nodes in the graph.

Parameters:
  • from_key (str) – The key of the node to check from.

  • to_key (str) – The key of the node to check to.

Returns:

True if there is an edge between the two nodes, False otherwise.

Return type:

bool

Raises:

AssertionError – If the from_key is not found in the graph.

remove_edge_temporary(from_key, to_key)

Remove a temporary edge between two nodes in the graph.

This function temporarily removes an edge between the two nodes. Temporary removals are reverted after each evaluation.

Parameters:
  • from_key (str) – The key of the node to remove the edge from.

  • to_key (str) – The key of the node to remove the edge to.

Raises:
  • AssertionError – If the edge does not exist between the two nodes.

  • AssertionError – If the from_key or to_key is not found in the graph.

Note

It is recommended to use this function only for dynamic changes in the graph.

skip_nodes_temporary(keys)

Skip nodes temporarily in the graph.

This function temporarily skips nodes in the graph. The previously recorded outputs of the skipped nodes will be reused in the evaluation. Temporary skips are reverted after each evaluation.

Parameters:

keys (list) – A list of keys of the nodes to skip.

Raises:

AssertionError – If the key is not found in the graph.

Note

It is recommended to use this function only for dynamic changes in the graph.