graph

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const END = "END"

END is a special constant used to represent the end node in the graph.

Variables

View Source
var (
	// ErrEntryPointNotSet is returned when the entry point of the graph is not set.
	ErrEntryPointNotSet = errors.New("entry point not set")

	// ErrNodeNotFound is returned when a node is not found in the graph.
	ErrNodeNotFound = errors.New("node not found")

	// ErrNoOutgoingEdge is returned when no outgoing edge is found for a node.
	ErrNoOutgoingEdge = errors.New("no outgoing edge found for node")
)

Functions

This section is empty.

Types

type Branch

type Branch[state any] struct {
	Path    func(ctx context.Context, state *state) ([]string, error)
	Mapping func(x string) string
	Then    string
	Source  string
}

func (*Branch[s]) From

func (b *Branch[s]) From() string

func (*Branch[s]) To

func (b *Branch[s]) To(ctx context.Context, state *s) []string

type ConditionalEdgeOptions

type ConditionalEdgeOptions[T any] struct {
	Mapping func(x string) string
	Then    string
}

func WithMap

func WithMap[T any](pathMap map[string]string) ConditionalEdgeOptions[T]

func WithThen

func WithThen[T any](then string) ConditionalEdgeOptions[T]

type Edge

type Edge[T any] interface {
	// From is the name of the node from which the edge originates.
	From() string

	// To is the name of the node to which the edge points.
	To(ctx context.Context, state *T) []string
}

Edge represents an edge in the message graph.

type MessageState

type MessageState struct {
	Messages []llms.MessageContent
}

func NewMessageState

func NewMessageState() MessageState

func (*MessageState) AddMessage

func (s *MessageState) AddMessage(message llms.MessageContent)

func (*MessageState) LastMessage

func (s *MessageState) LastMessage() llms.MessageContent

func (*MessageState) LastMessageOfRole

func (s *MessageState) LastMessageOfRole(role llms.ChatMessageType) llms.MessageContent

type Node

type Node[T any] struct {
	// Name is the unique identifier for the node.
	Name string

	// Function is the function associated with the node.
	// It takes a context and a slice of MessageContent as input and returns a slice of MessageContent and an error.
	Function func(ctx context.Context, state *T) error
}

Node represents a node in the message graph.

type Runnable

type Runnable[T any] struct {
	// Graph is the underlying StateGraph object.
	Graph *StateGraph[T]
}

Runnable represents a compiled message graph that can be invoked.

func (*Runnable[T]) Invoke

func (r *Runnable[T]) Invoke(ctx context.Context, state *T) error

Invoke executes the compiled message graph with the given input messages. It returns the resulting messages and an error if any occurs during the execution. Invoke executes the compiled message graph with the given input messages. It returns the resulting messages and an error if any occurs during the execution.

type SimpleEdge

type SimpleEdge[state any] struct {
	// contains filtered or unexported fields
}

func (*SimpleEdge[state]) From

func (e *SimpleEdge[state]) From() string

func (*SimpleEdge[state]) To

func (e *SimpleEdge[state]) To(ctx context.Context, _ *state) []string

type StateGraph

type StateGraph[T any] struct {
	// contains filtered or unexported fields
}

StateGraph represents a message graph.

func NewStateGraph

func NewStateGraph[T any]() *StateGraph[T]

NewStateGraph creates a new instance of StateGraph.

func (*StateGraph[T]) AddConditionalEdges

func (g *StateGraph[T]) AddConditionalEdges(
	source string,
	path func(ctx context.Context, state *T) ([]string, error),
	options ...ConditionalEdgeOptions[T],
) *StateGraph[T]

AddConditionalEdges adds a conditional edge from the starting node to any number of destination nodes. It allows for dynamic determination of the next nodes based on the provided path function.

Parameters: - source (string): The starting node. This conditional edge will run when exiting this node. - path (Callable[T, []string]): The callable that determines the next node or nodes. If not specifying pathMap, it should return one or more nodes. If it returns "END", the graph will stop execution. - pathMap (map[string]string, optional): Optional mapping of paths to node names. If omitted, the paths returned by path should be node names. - then (string, optional): The name of a node to execute after the nodes selected by path.

Returns: - *StateGraph[T]: The StateGraph instance to allow method chaining.

func (*StateGraph[T]) AddEdge

func (g *StateGraph[T]) AddEdge(from, to string)

AddEdge adds a new edge to the message graph between the "from" and "to" nodes.

func (*StateGraph[T]) AddNode

func (g *StateGraph[T]) AddNode(name string, fn func(ctx context.Context, state *T) error)

AddNode adds a new node to the message graph with the given name and function.

func (*StateGraph[T]) Compile

func (g *StateGraph[T]) Compile() (*Runnable[T], error)

Compile compiles the message graph and returns a Runnable instance. It returns an error if the entry point is not set.

func (*StateGraph[T]) SetEntryPoint

func (g *StateGraph[T]) SetEntryPoint(name string)

SetEntryPoint sets the entry point node name for the message graph.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL