node

package
v0.0.0-...-131b1cd Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogNode

func LogNode(n Node, color string, format string, kwargs ...any)

LogNode is a helper printing function to display information from a node.

func RouterSimpleDeciderFunc

func RouterSimpleDeciderFunc(
	ctx context.Context,
	input Input,
	def Node,
	chains map[string]Node,
) (Node, Input, error)

RouterSimpleDeciderFunc is a decider that will use an LLM output formatted as JSON structure that will be decoded by RouterSimpleInput.

Types

type BaseNode

type BaseNode struct {
	// contains filtered or unexported fields
}

BaseNode is the generic implementation of a the Node interface. It is usually embedded into every kind of nodes.

func New

func New(info Info) *BaseNode

New creates a new *BaseNode with the given Info.

func (*BaseNode) Chain

func (n *BaseNode) Chain(next Node)

Chain chains the receiver with the given Node. When the receiver is executed, its generated output will be fed into the given Node. You cannot unlink a node. If you try to chain an already chained node the function will panic.

func (*BaseNode) Execute

func (n *BaseNode) Execute(ctx context.Context, input Input) (output string, err error)

Execute starts the execution process of the chain, starting from the receiver. The given Input will be passed as is to the next Node, which will pass it on to the next, and so on. It will return the final output generated by the last Node in the chain.

This does not do anything per se. It just provides the basic chaining mechanism to other kind of Nodes.

func (*BaseNode) Info

func (n *BaseNode) Info() Info

Info returns the receiver's Info.

func (*BaseNode) Next

func (n *BaseNode) Next() Node

Next returns the previously chained Node. It will return nil if the received has not been chained yet.

type ChatMemory

type ChatMemory struct {
	*BaseNode
	// contains filtered or unexported fields
}

ChatMemory holds information about chat history. This will be rewritten as this does not belong here.

func NewChatMemory

func NewChatMemory(info Info, system string, botname string, username string) *ChatMemory

NewChatMemory returns a new ChatMemory with the provided information.

func (*ChatMemory) AddBotMessage

func (c *ChatMemory) AddBotMessage(content string)

AddBotMessage add a new bot message to the history

func (*ChatMemory) AddUserMessage

func (c *ChatMemory) AddUserMessage(content string)

AddUserMessage add a new user message to the history

func (*ChatMemory) BotName

func (c *ChatMemory) BotName() string

BotName returns the current botname.

func (*ChatMemory) Execute

func (c *ChatMemory) Execute(ctx context.Context, input Input) (string, error)

Execute implements the Node interface.

func (*ChatMemory) History

func (c *ChatMemory) History() []string

History returns the current history.

func (*ChatMemory) System

func (c *ChatMemory) System() string

System returns the current system name.

func (*ChatMemory) UserName

func (c *ChatMemory) UserName() string

UserName returns the current username.

func (*ChatMemory) WithStorage

func (c *ChatMemory) WithStorage(storage *[]string) *ChatMemory

WithStorage sets the storage backend. Yeah it's an array.. I told you this will be rewritten.

type Error

type Error struct {
	N   Node
	Err error
}

An Error represents an error returned by a node.

func NewError

func NewError(n Node, template string, val ...any) Error

NewError retrurns a new Error.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) Is

func (e Error) Is(err error) bool

Is implements the error interface.

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap implements the error interface.

type Func

type Func struct {
	*BaseNode
	// contains filtered or unexported fields
}

A Func is a node that will run a given function as its execution method.

func NewFunc

func NewFunc(info Info, executor func(context.Context, Input, Node) (string, error)) *Func

NewFunc returns a new Func node that will use the provided function during it's execution.

func (*Func) Execute

func (n *Func) Execute(ctx context.Context, input Input) (string, error)

Execute implements the Node interface.

type Info

type Info struct {
	Name        string
	Description string
	Parameters  string
}

Info holds the basic information about a Node. Every Node info can be accessed by calling node.Info(). This can be used to help identify a chain so it can be called by an agent.

type Input

type Input struct {
	// contains filtered or unexported fields
}

Input represents the data passed to a Node.

func NewInput

func NewInput(in string, options ...engine.LLMOption) Input

NewInput returns a new Input with the given string and llm.Options. Note that the llm.Option will be carried out until it reaches a LLM Node. After that they will be discarded.

func (Input) Debug

func (i Input) Debug() bool

Debug returns the state of the debug mode.

func (Input) Get

func (i Input) Get(key string) any

Get returns the value of the given key.

func (Input) Input

func (i Input) Input() string

Input returns the current input string.

func (Input) LLMOptions

func (i Input) LLMOptions() []engine.LLMOption

LLMOptions returns the current llm.Options.

func (Input) ResetLLMOptions

func (i Input) ResetLLMOptions() Input

ResetLLMOptions returns a copy of the Input after removing all llm.Options. This is called by the LLM nodes.

func (Input) Scratchpad

func (i Input) Scratchpad() string

Scratchpad returns the current scratchpad.

func (Input) Set

func (i Input) Set(k string, v any) Input

Set returns a copy of the receiver with the given key and value added to it.

func (Input) WithDebug

func (i Input) WithDebug(debug bool) Input

WithDebug returns a copy of the receiver with the Debug flag activated. The Node will usually print detailed information about them when they deal with an input with Debug set to true.

func (Input) WithInput

func (i Input) WithInput(in string) Input

WithInput returns a copy of the receiver replacing the previous input.

func (Input) WithLLMOptions

func (i Input) WithLLMOptions(options ...engine.LLMOption) Input

WithLLMOptions returns a copy of the receiver with the given llm.Options added to it.

func (Input) WithScratchpad

func (i Input) WithScratchpad(scratchpad string) Input

WithScratchpad returns a copy of the receiver with the given scratchpad added.

type LLM

type LLM struct {
	*BaseNode
	// contains filtered or unexported fields
}

LLM is a node responsible for running its input into an inference engine.

func NewLLM

func NewLLM(info Info, engine engine.LLM, options ...engine.LLMOption) *LLM

NewLLM returns a new LLM using the given engine.

func (*LLM) Execute

func (n *LLM) Execute(ctx context.Context, input Input) (string, error)

Execute implements the Node interface.

type Node

type Node interface {
	Info() Info
	Chain(Node)
	Next() Node
	Execute(context.Context, Input) (string, error)
}

Node is the base object of the framework.

A Node can be chained to another node. Together they form a chain.

[prompt:genstory] -> [llm:mistral] -> [prompt:summarize] -> [llm:zephyr]

A chain can contains nest chains:

[prompt:search] -> [ [prompt:summarize] -> [llm] ] -> [func:format] -> [llm]

A node can be executed by calling its Execute method. The execution is given a context.Context and an Input. It returns a string and an eventual error. This output will then be fed to the next Node in the chain. This process continues until the execution reaches a node with no Next() Node. Then the output is unstacked and returned by the initial executed Node.

type Prompt

type Prompt struct {
	*BaseNode
	// contains filtered or unexported fields
}

A Prompt is a Node that is responsible for generating a prompt from a template using the informations contained in the given input.

func NewPrompt

func NewPrompt(info Info, template string, options ...engine.LLMOption) *Prompt

NewPrompt returns a new *Prompt with the given template and ll.Options. The llm.Options will always be appended to the input, before the input's own LLMOptions. So input's options will take precedence.

func (*Prompt) Execute

func (n *Prompt) Execute(ctx context.Context, input Input) (output string, err error)

Execute implements the Node interface.

func (*Prompt) Options

func (n *Prompt) Options() []engine.LLMOption

Options returns the current llm.Options.

func (*Prompt) WithMaxRetries

func (n *Prompt) WithMaxRetries(maxRetries int) *Prompt

WithMaxRetries sets the maximum number of retries the prompt is willing to make when the execution returns a PromptError.

type PromptError

type PromptError struct {
	Err        error
	Scratchpad string
}

PromptError represents an error in the generated text generation. The scratchpad will be used by the Prompt to fill the input.Scratchpad before retrying.

func NewPromptError

func NewPromptError(scratchpad string, err error) PromptError

NewPromptError returns a PromptError.

func (PromptError) Error

func (e PromptError) Error() string

Error implements the error interface.

func (PromptError) Is

func (e PromptError) Is(err error) bool

Is implements the error interface.

func (PromptError) Unwrap

func (e PromptError) Unwrap() error

Unwrap implements the error interface.

type Router

type Router struct {
	*BaseNode
	// contains filtered or unexported fields
}

A Router is a node that can route its Input to one of several chains. The decision is made by a RouterDeciderFunc.

func NewRouter

func NewRouter(
	info Info,
	decider RouterDeciderFunc,
	defaultChain Node,
	chains ...Node,
) *Router

NewRouter returns a new Router.

func (*Router) Chain

func (n *Router) Chain(next Node)

Chain is overriding the default Node behavior. A Router never uses its internal Next(). Instead, Chain() will chain all the Router's subchains (including the default) to the given Node. When the router is executed, it will hand off execution to the selected subchain.

func (*Router) Execute

func (n *Router) Execute(ctx context.Context, input Input) (string, error)

Execute implements the Node interface.

type RouterDeciderFunc

type RouterDeciderFunc func(context.Context, Input, Node, map[string]Node) (Node, Input, error)

RouterDeciderFunc is the type of the function used by the Router. It is responsible for making a decision on which chains the router should send the input to. It is given the Router's context, the Input as well as a map of available subchains, keyed by their name. It must return which Node to use and which Input to pass to it.

type RouterSimpleInput

type RouterSimpleInput struct {
	Params map[string]any `json:"params,omitempty"`
	Name   string         `json:"name"`
	Input  string         `json:"input,omitempty"`
}

RouterSimpleInput is the data structure that will be used to decide how to router the traffic by the RouterSimpleDeciderFunc.

type Subchain

type Subchain struct {
	*BaseNode
	// contains filtered or unexported fields
}

A Subchain is a Node that holds a separate set of Nodes.

It can be considered as a single Node.

[node] -> [[node]->[node]->[node]] -> [node]

It can be useful to handle a separate set of Input or llm.Options. Subchains can also be used in Router nodes, that will execute a certain Subchain based on a condition.

[classify] -> [llm] -> [router] ?-> [summarize] ->[llm1] ?-> [search] ->
[llm2] ?-> [generate] -> [llm3]

Subchain embeds the BaseNode and can be used as any other node.

func NewSubchain

func NewSubchain(info Info, nodes ...Node) *Subchain

NewSubchain creates a new Subchain with the given Info and given Nodes. The function will chain all of them in order. The given Node must not be already chained or it will panic.

func NewSubchainWithName

func NewSubchainWithName(name string, nodes ...Node) *Subchain

NewSubchainWithName is a convenience function to create a named Subchain.

func (*Subchain) Execute

func (n *Subchain) Execute(ctx context.Context, input Input) (string, error)

Execute executes the Subchain. It will first execute all the internal nodes, then execute the receiver's Next() Node.

Jump to

Keyboard shortcuts

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