hypher

package module
v0.0.0-...-8b2ea39 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-hypher

Build Status go.dev reference

Go module for building AI agents as computational graphs.

[!IMPORTANT] THIS IS A WILD THEORETICAL EXPERIMENT

This project attempts to implement AI agents as computational graphs in Go.

[!NOTE] The name of the project has been inspired by Hypha which is understood to be a long, branching structure -- like a graph -- which enables communication in fungi by conducting electrical impulses through hyphae.

Basics

AI agent is represented as a weighted directed acyclic graph (DAG).

The graph nodes are the fundamental computational units: they execute an action anre return its result. The action can be any computational task e.g. LLM prompt, function call, etc.

The actions are chained together through the graph edges. Each edge can have a weight assigned to it.

Nodes may have explicit input provided to them, though some nodes do not need any input to execute their action.

Some nodes are marked as input: they are the nodes where the agent execution starts.

There must be at least one output node: the node that generates the agent output i.e. the result of its (node) actions. It's usually the leaf node in the graph.

When the agent runs, it first does a topological sort of its nodes and then executes each node action in the order returned by the sort.

The topological sort is done in ascending order by the number of node incoming edges i.e. in-degrees of nodes. Naturally, the nodes that have no incoming edges are executed first and usually have inputs provided, though, the inputs may also come from the node Op(eration) they execute.

The output of each node is passed to all its successive nodes which in turn process it, along with their own input, and then execute their own action and pass the output to the next node: the agent graphs works in a similar way to a data stream -- the data flows from the inputs, are transformed by intermediate nodes on their way to the output node. The output node provides the agent execution result.

Given each agent is a graph it should be possible to create ensemble of agents by composing agent graphs to perform arbitrarily complex tasks.

Furthermore, in a similar way as the neural networks, we should be able to improve the agent operation through output feedback using some sort of evaluation of output and some type of optimiser.

In the context of LLM agents, we could theoretically improve agent operation by tuning its node prompts when running the agent in some simulation environments through several cycles: agents could maintain their prompts history and eval result for each simulation run and the final tuned agent would then simply pick the prompt that performs the best for the given task.

Documentation

Overview

Package go-hypher enables creating AI agents as computational graphs.

An agent is represented as a weighted Directed Acyclic Graph (DAG) which consists of nodes that perform a specific operation during agent execution aka Hypher Run.

Hypher Run executes all the nodes in the hypher Graph and computes the results of their operations. All the outputs of the nodes are then passed to theis successors which then use them as their inputs. This continues all the way down to the hypher graph output nodes where the agent result is stored and can be fetched from.

Given the agents are DAGs, they can form ensambles of agents through additional edges that link the agent DAGs as long as the resulting graph is also a DAG.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adder

type Adder interface {
	Graph
	graph.NodeAdder
	graph.WeightedEdgeAdder
}

Adder allows to add edges and nodes to graph.

type ConcMode

type ConcMode int

ConcMode is Graph run concurrency mode.

const (
	// ConcLevelMode runs nodes on the same graph level concurrently.
	ConcLevelMode ConcMode = iota
	// ConcAllMode runs all Graph nodes concurrently.
	ConcAllMode
)

type DOTEdge

type DOTEdge interface {
	Edge
	encoding.Attributer
}

DOTEdge is Graphviz DOT edge.

type DOTGraph

type DOTGraph interface {
	Graph
	encoding.Attributer
	// DOTID returns DOT ID.
	DOTID() string
	// SetDOTID sets DOT ID.
	SetDOTID(dotid string)
	// DOTAttributers sets DOT graph attributes.
	DOTAttributers() (graph, node, edge encoding.Attributer)
}

DOTNGraph is Graphviz DOT graph.

type DOTNode

type DOTNode interface {
	Node
	encoding.Attributer
	// DOTID returns DOT ID.
	DOTID() string
	// SetDOTID sets DOT ID.
	SetDOTID(dotid string)
}

DOTNode is Graphviz DOT node.

type Edge

type Edge interface {
	graph.WeightedEdge
	// UID returns edge UID.
	UID() string
	// Label returns edge label.
	Label() string
	// Attrs returns node attributes.
	Attrs() map[string]any
	// String is useful for debugging.
	String() string
}

Edge is a graph edge.

type EdgeUpdater

type EdgeUpdater interface {
	Graph
	graph.WeightedEdgeAdder
	graph.EdgeRemover
}

EdgeUpdater adds and removes edges.

type Execer

type Execer interface {
	// Exec executes an operation with the given inputs and returns the results.
	Exec(ctx context.Context, inputs ...Value) ([]Value, error)
}

Execer executes a hypher operation. This usually means executing the hypher Node, by running its Op.

type Graph

type Graph interface {
	graph.Weighted
	// UID returns graph UID.
	UID() string
	// Edges returns graph edges iterator.
	Edges() graph.Edges
	// Label returns graph label.
	Label() string
	// Attrs are graph attributes.
	Attrs() map[string]any
	// String is useful for debugging.
	String() string
}

Graph is weighted graph.

type Inputer

type Inputer interface {
	// Inputs returns input values.
	Inputs() []Value
}

Inputer returns its inputs.

type LabelSetter

type LabelSetter interface {
	SetLabel(string)
}

LabelSetter sets label.

type Loader

type Loader interface {
	Load(context.Context, string) (Graph, error)
}

Loader loads a graph from a databse or a filesystem.

type Marshaler

type Marshaler interface {
	// Marshal marshals graph into bytes.
	Marshal(g Graph) ([]byte, error)
}

Marshaler is used for marshaling graphs.

type Node

type Node interface {
	graph.Node
	// UID returns node UID.
	UID() string
	// Label returns node label.
	Label() string
	// Attrs returns node attributes.
	Attrs() map[string]any
	// String is useful for debugging.
	String() string
}

Node is a graph node.

type NodeUpdater

type NodeUpdater interface {
	Graph
	graph.NodeAdder
	graph.NodeRemover
}

NodeUpdater adds and removes nodes.

type Nodes

type Nodes []Node

Nodes is a slice of Nodes.

type Op

type Op interface {
	// Type of the Op.
	Type() string
	// Desc describes the Op.
	Desc() string
	// Do runs the Op.
	Do(ctx context.Context, inputs ...Value) ([]Value, error)
	// String is useful for debugging.
	String() string
}

Op is an operation run by a Node.

type Option

type Option func(*Options)

Option is functional graph option.

func WithAttrs

func WithAttrs(attrs map[string]any) Option

WithAttrs sets Attrs option,

func WithConcMode

func WithConcMode(mode ConcMode) Option

WithRunAll sets Parallel option.

func WithDotID

func WithDotID(dotid string) Option

WithDotID sets DotID option.

func WithGraph

func WithGraph(g Graph) Option

WithGraph sets hypher Graph.

func WithID

func WithID(id int64) Option

WithID sets ID option.

func WithLabel

func WithLabel(label string) Option

WithLabel sets Label option.

func WithOp

func WithOp(op Op) Option

WithOp sets Op.

func WithUID

func WithUID(uid string) Option

WithUID sets UID option.

func WithWeight

func WithWeight(weight float64) Option

WithWeight sets Weight option.

type Options

type Options struct {
	// ID configures ID
	ID int64
	// UID configures UID
	UID string
	// Label configures Label.
	Label string
	// Attrs configures Attrs.
	Attrs map[string]any
	// DotID configures DOT ID
	DotID string
	// Weight configures Edge weight.
	Weight float64
	// Graph configures Node's graph
	Graph Graph
	// ConcMode configures Graph run concurrency mode.
	ConcMode ConcMode
	// Op configures Node's Op.
	Op Op
}

Options configure graph.

type Outputer

type Outputer interface {
	// Outputs returns output values.
	Outputs() []Value
}

Outputer returns its outputs.

type Remover

type Remover interface {
	Graph
	graph.NodeRemover
	graph.EdgeRemover
}

Remover allows to remove nodes and edges from graph.

type Reseter

type Reseter interface {
	// Reset inputs and outputs.
	Reset()
}

Reseter resets inputs and outputs.

type Runner

type Runner interface {
	// Run an operation with the given inputs and options.
	Run(ctx context.Context, inputs map[string]Value, opts ...Option) error
}

Runner is used to trigger a hypher Run. This usually means running the hypher Graph, by executing all its nodes.

type Syncer

type Syncer interface {
	Sync(context.Context, Graph) error
}

Syncer syncs the graph to a database or a filesystem.

type UIDSetter

type UIDSetter interface {
	SetUID(string)
}

UIDSetter sets UID.

type Unmarshaler

type Unmarshaler interface {
	// Unmarshal unmarshals arbitrary bytes into graph.
	Unmarshal([]byte, Graph) error
}

Unmarshaler is used for unmarshaling graphs.

type Updater

type Updater interface {
	Adder
	Remover
}

Updater allows to update graph.

type Value

type Value map[string]any

Value is an I/O value.

type WeightSetter

type WeightSetter interface {
	SetWeight(float64)
}

WeightSetter sets weight.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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