brain

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: GPL-3.0 Imports: 12 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Learn added in v0.2.0

func Learn(ctx context.Context, l Interface, tag string, msg *Message) error

Learn records a message into a brain.

func Recall added in v0.2.0

func Recall(ctx context.Context, br Interface, tag string) iter.Seq2[Message, error]

Recall iterates over all messages a brain knows with a given tag.

func ReduceEntropy added in v0.2.0

func ReduceEntropy(w string) string

ReduceEntropy transforms a term in a way which makes it more likely to equal other terms transformed the same way.

func Speak added in v0.2.0

func Speak(ctx context.Context, s Interface, tag, prompt string) (string, []string, error)

Speak produces a new message and the trace of messages used to form it from the given prompt. If the brain does not produce any terms, the result is the empty string regardless of the prompt, with no error.

Types

type Builder added in v0.2.0

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

Builder builds a spoken message along with its message trace.

func (*Builder) Append added in v0.2.0

func (b *Builder) Append(id string, term []byte)

Append adds a term to the builder.

func (*Builder) Reset added in v0.2.0

func (b *Builder) Reset()

Reset restores the builder to an empty state.

func (*Builder) String added in v0.2.0

func (b *Builder) String() string

String returns the built message.

func (*Builder) Trace added in v0.2.0

func (b *Builder) Trace() []string

Trace returns a direct reference to the message trace.

type Interface added in v0.2.0

type Interface interface {
	// Learn records a set of tuples.
	//
	// One tuple has an empty prefix to denote the start of the message, and
	// a different tuple has the empty string as its suffix to denote the end
	// of the message. The positions of each in the argument are not guaranteed.
	//
	// Each tuple's prefix has entropy reduction transformations applied.
	//
	// Tuples in the argument may share storage for prefixes.
	Learn(ctx context.Context, tag string, msg *Message, tuples []Tuple) error

	// Speak generates a full message and appends it to w.
	//
	// The prompt is in reverse order and has entropy reduction applied.
	Speak(ctx context.Context, tag string, prompt []string, w *Builder) error

	// Forget forgets everything learned from a single given message.
	// If nothing has been learned from the message, it must prevent anything
	// from being learned from a message with that ID.
	Forget(ctx context.Context, tag, id string) error

	// Recall reads out messages the brain knows.
	// At minimum, the message ID and text of each message must be retrieved;
	// other fields may be filled if they are available.
	//
	// Repeated calls using the pagination token returned from the previous
	// must yield every message that the brain had recorded at the time of the
	// first call exactly once. Messages learned after the first call of an
	// enumeration are read at most once.
	//
	// The first call of an enumeration uses an empty pagination token as input.
	// If the returned pagination token is empty, it is interpreted as the end
	// of the enumeration.
	Recall(ctx context.Context, tag, page string, out []Message) (n int, next string, err error)
}

Interface is a store of learned messages which can reproduce them by parts.

It must be safe to call all methods of a brain concurrently with each other.

type Message added in v0.2.0

type Message = message.Received[userhash.Hash]

Message is the message type used by a Interface.

type Skip added in v0.2.0

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

Skip computes numbers of elements to skip between samples to draw a single term uniformly from arbitrarily sized sequences.

To draw a sample, create a new Skip, accept the first term of the sequence, skip N(rand(), rand()) terms, accept the next, and repeat N followed by an accept until the sequence is exhausted.

Conceptually, drawing a sample from an arbitrary sequence can be performed by assigning each term a random weight and selecting the term that receives the largest. Skip transforms this procedure into modeling the number of random numbers it would take to find the next larger weight. Using this approach allows uniform sampling with O(log n) random numbers instead of O(n).

func (*Skip) N added in v0.2.0

func (s *Skip) N(a, b uint64) uint64

N computes the next skip length given two uniformly distributed random uint64s. The typical call will look like:

s.N(rand.Uint64(), rand.Uint64())

type Tuple added in v0.2.0

type Tuple struct {
	// Prefix is the entropy-reduced prefix in reverse order relative to the
	// source message.
	Prefix []string
	// Suffix is the full-entropy term following the prefix.
	Suffix string
}

Tuple is a single Markov chain tuple.

Directories

Path Synopsis
Package braintest provides integration testing facilities for brains.
Package braintest provides integration testing facilities for brains.

Jump to

Keyboard shortcuts

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