tzap

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 19 Imported by: 7

Documentation

Overview

package tzap provides a library to simplify manual workflows when dealing with chatgpt.

Index

Constants

This section is empty.

Variables

View Source
var (
	LastExecutionTime time.Time

	MessageBuffer []string
)

Package-level variables for global throttling

View Source
var GlobalGraphVizLogThreads []GraphVizLogMessages
View Source
var GlobalTzaps []*Tzap
View Source
var Mem = map[string]*types.Message{}

Mem represents a global memory storage for messages.

Functions

func CheckData

func CheckData(data types.MappedInterface) error

func ConvertGraphvizToSVG added in v0.7.12

func ConvertGraphvizToSVG(inputFile string, outputFile string) error

func Flush

func Flush()

func GenerateGraphvizDotFile added in v0.7.12

func GenerateGraphvizDotFile(t *Tzap, graph *GraphVizGraph)

func GetMemory

func GetMemory(key string) string

GetMemory returns the content of a memory key, or an empty string if the key does not exist.

func GetNames

func GetNames(t *Tzap) []string

func GetThread added in v0.7.16

func GetThread(t *Tzap) []types.Message

func HandlePanic added in v0.7.8

func HandlePanic(fn func()) (err error)

func Log

func Log(t *Tzap, messages ...interface{})

func Logf

func Logf(t *Tzap, format string, args ...any)

func ResetFilepathOccurrences

func ResetFilepathOccurrences()

ResetFilepathOccurrences clears the global filepath occurrences tracking.

func ResetFlush

func ResetFlush()

func TotalLength

func TotalLength(t *Tzap) int

TotalLength calculates the total length of the Tzap instance chain.

func TruncateToMaxWords

func TruncateToMaxWords(messages []types.Message, wordLimit int) []types.Message

Not accurate way of counting tokens, but an approximation.

Types

type ErrorTzap added in v0.7.8

type ErrorTzap struct {
	Tzap *Tzap
	Err  error
}

func (*ErrorTzap) HandleError added in v0.7.8

func (t *ErrorTzap) HandleError(cb func(*ErrorTzap) error) *Tzap

type GraphVizEdge added in v0.7.6

type GraphVizEdge struct {
	FromNode *GraphVizNode
	ToNode   *GraphVizNode
	Tooltip  string
	Style    string
}

type GraphVizGraph added in v0.7.6

type GraphVizGraph struct {
	Label     string
	Tooltip   string
	Nodes     []*GraphVizNode
	Edges     []*GraphVizEdge
	SubGraphs []*GraphVizSubgraph
}

func FillGraphVizGraph added in v0.7.6

func FillGraphVizGraph() *GraphVizGraph

type GraphVizLogMessage added in v0.7.6

type GraphVizLogMessage struct {
	Role       string
	Content    string
	TokenCount int
	TzapId     int
	Direction  string
}

type GraphVizLogMessages added in v0.7.6

type GraphVizLogMessages struct {
	Messages   []GraphVizLogMessage
	TokenCount int
}

type GraphVizNode added in v0.7.6

type GraphVizNode struct {
	Id       string
	Label    string
	Tooltip  string
	Style    string
	Children []*GraphVizNode
}

type GraphVizSubgraph added in v0.7.6

type GraphVizSubgraph struct {
	Id        string
	Label     string
	Tooltip   string
	Nodes     []*GraphVizNode
	Edges     []*GraphVizEdge
	SubGraphs []*GraphVizSubgraph
}

type Tzap

type Tzap struct {
	Id                   int
	Name                 string
	InitialSystemContent string
	Message              types.Message
	Data                 types.MappedInterface `json:"-"`
	C                    context.Context       `json:"-"`
	TG                   types.TGenerator      `json:"-"`

	types.ITzap[*Tzap, any] `json:"-"`

	Parent *Tzap
}

Tzap is a structure that holds data and methods related to Tzap objects.

func InternalNew

func InternalNew() *Tzap

NewTzap creates a new Tzap with default values, and returns its pointer. Mainly for mocking purposes. Does not have a connector, will likely crash.

func NewWithConnector

func NewWithConnector(connector types.TzapConnector) *Tzap

func (*Tzap) Accumulate

func (t *Tzap) Accumulate(fn func(*Tzap) *Tzap) *Tzap

func (*Tzap) AddAssistantMessage

func (t *Tzap) AddAssistantMessage(contents ...string) *Tzap

AddAssistantMessage adds an assistant message to the Tzap

func (*Tzap) AddContext added in v0.7.8

func (t *Tzap) AddContext() *Tzap

onNewTzap is a helper method that appends the parent's context to the Tzap object.

func (*Tzap) AddSystemMessage

func (t *Tzap) AddSystemMessage(contents ...string) *Tzap

AddSystemMessage adds a system message to the Tzap

func (*Tzap) AddTzap

func (t *Tzap) AddTzap(tc *Tzap) *Tzap

AddTzap (mostly internal use) initializes and adds a new Tzap child to the current Tzap object.

func (*Tzap) AddUserMessage

func (t *Tzap) AddUserMessage(contents ...string) *Tzap

AddUserMessage adds a user message to the Tzap

func (*Tzap) AppendContent

func (t *Tzap) AppendContent(sep string, s ...string) *Tzap

AppendContent appends content to the current message in the Tzap

func (*Tzap) AppendMessage

func (t *Tzap) AppendMessage(content string) *Tzap

AppendMessage appends a message to the current message in the Tzap

func (*Tzap) AppendParentContext

func (t *Tzap) AppendParentContext() *Tzap

AppendParentContext assigns the parent's context to the Tzap object, if present.

func (*Tzap) ApplyErrorWorkflow added in v0.7.14

func (t *Tzap) ApplyErrorWorkflow(nt types.NamedWorkflow[*Tzap, *ErrorTzap], fn func(*ErrorTzap) error) *Tzap

func (*Tzap) ApplyWorkflow added in v0.7.14

func (t *Tzap) ApplyWorkflow(nt types.NamedWorkflow[*Tzap, *Tzap]) *Tzap

WARNING: ApplyWorkflow clones messages from previous Tzap instances. This duplicates the message.

func (*Tzap) ApplyWorkflowFN added in v0.7.14

func (t *Tzap) ApplyWorkflowFN(nt func(*Tzap) *Tzap) *Tzap

ApplyWorkflowFN applies a function that takes a Tzap instance and returns a modified Tzap instance. Returns the result of the given function applied to the current Tzap instance.

func (*Tzap) ApplyWorkflowP added in v0.7.14

func (t *Tzap) ApplyWorkflowP(workflow *Tzap) *Tzap

ApplyWorkflowP applies a given workflow Tzap instance to the current Tzap instance. Returns the applied workflow with its Parent set to the current Tzap instance.

func (*Tzap) AsAssistantMessage added in v0.7.19

func (t *Tzap) AsAssistantMessage() *Tzap

func (*Tzap) ChangeFilepath

func (t *Tzap) ChangeFilepath(filepath string) *Tzap

ChangeFilepath updates the filepath metadata in the Tzap data

func (*Tzap) CheckAndHandleGlobalOccurrences

func (t *Tzap) CheckAndHandleGlobalOccurrences(references int, filename string, noOccurrence, handleOccurrence func(*Tzap) *Tzap) *Tzap

CheckAndHandleGlobalOccurrences checks and handles the global occurrences of the given filename within the data. Calls either noOccurrence or handleOccurrence based on the provided references.

func (*Tzap) CheckAndHandleRecurrences

func (t *Tzap) CheckAndHandleRecurrences(references int, filename string, noReccurance, handleRecurrence func(*Tzap) *Tzap) *Tzap

CheckAndHandleRecurrences counts the number of recurrences of the given filename within the data and calls either noReccurance or handleRecurrence based on the threshold provided as references.

func (*Tzap) CloneTzap

func (t *Tzap) CloneTzap(tc *Tzap) *Tzap

CloneTzap (mostly internal use) clones a Tzap object and assigns values based on the provided Tzap object.

func (*Tzap) CombineMessage

func (t *Tzap) CombineMessage(nt1 func(*Tzap) *Tzap, nt2 func(*Tzap) *Tzap) *Tzap

CombineMessage combines two message functions and creates a new message in the Tzap

func (*Tzap) CountTokens

func (t *Tzap) CountTokens(content string) (int, error)

RequestOpenAIChat initializes the openai chat completion request and creates a new Tzap with the edited content.

func (*Tzap) Each

func (t *Tzap) Each(fn func(*Tzap)) *Tzap

func (*Tzap) ErrorTzap added in v0.7.8

func (t *Tzap) ErrorTzap(err error) *ErrorTzap

func (*Tzap) Exit

func (t *Tzap) Exit() *Tzap

Exit raises a panic to exit from the current Tzap.

func (*Tzap) FileMustContainHandleGlobalOccurrences

func (t *Tzap) FileMustContainHandleGlobalOccurrences(references int, filename string, noOccurrence, handleOccurrence func(*Tzap) *Tzap) *Tzap

FileMustContainHandleGlobalOccurrences checks and handles the global occurrences of the provided filename within the data. Calls either noOccurrence or handleOccurrence based on the provided references.

func (*Tzap) GetThreadAsJSON added in v0.8.3

func (t *Tzap) GetThreadAsJSON() (string, error)

func (*Tzap) HandleShutdown added in v0.7.15

func (t *Tzap) HandleShutdown()

func (*Tzap) HijackTzap

func (t *Tzap) HijackTzap(tc *Tzap) *Tzap

HijackTzap (mostly internal use) effectively de-attaches from previous Tzap by changing the own parent to parents parent. This can be used AddUserMessage("H").LoadTaskOrRequestNewTask().Hijack() .() Tzap replaces the current Tzap's context and parent with the provided Tzap's context and parent.

func (*Tzap) If added in v0.8.3

func (t *Tzap) If(b bool, tfn func(*Tzap) *Tzap, ffn func(*Tzap) *Tzap) *Tzap

WorkTzap executes the provided function and returns the Tzap object.

func (*Tzap) IsolatedTzap

func (t *Tzap) IsolatedTzap(fn func(*Tzap)) *Tzap

IsolatedTzap executes the provided function with a new isolated Tzap object. It does not modify the current Tzap object but returns it.

func (*Tzap) LoadCompletionOrRequestCompletion added in v0.7.14

func (t *Tzap) LoadCompletionOrRequestCompletion(filePath string) *Tzap

LoadTaskOrRequestNewTask loads a file if it exists, otherwise requests a new file content from OpenAI and applies the changes to the original file

func (*Tzap) LoadCompletionOrRequestCompletionMD5 added in v0.7.14

func (t *Tzap) LoadCompletionOrRequestCompletionMD5(filePath string) *Tzap

LoadCompletionOrRequestCompletionMD5 loads a task file if it exists and its MD5 checksum matches, otherwise requests a new task content from OpenAI and applies the changes to the original file

func (*Tzap) LoadFileAsCompletion added in v0.7.16

func (t *Tzap) LoadFileAsCompletion(filePath string) *Tzap

LoadFileAsCompletion loads a file and returns a Tzap with the file's content

func (*Tzap) LoadFileDir

func (t *Tzap) LoadFileDir(dir string) *Tzap

LoadFileDir exposes an array of Tzaps in the previous elements .Data["children"]. Each child is a .LoadTask(file)

func (*Tzap) LoadFiles

func (t *Tzap) LoadFiles(filepaths []string) *Tzap

LoadFiles exposes an array of Tzaps in with .Data["children"]. Each child is a .LoadTask(file)

func (*Tzap) LoadThread added in v0.7.16

func (t *Tzap) LoadThread(messages []types.Message) *Tzap

func (*Tzap) LoadThreadFile added in v0.7.20

func (t *Tzap) LoadThreadFile(filePath string) *ErrorTzap

func (*Tzap) LoadThreadString added in v0.8.3

func (t *Tzap) LoadThreadString(content string) *ErrorTzap

func (*Tzap) Map

func (t *Tzap) Map(fn func(*Tzap) *Tzap) *Tzap

Map iterates through the children Tzap objects and applies the provided function. Returns a new Tzap object containing the result of the function application.

func (*Tzap) Memorize

func (t *Tzap) Memorize(key string) *Tzap

Memorize stores the current Tzap message content under a given key.

func (*Tzap) MemorizeReq

func (t *Tzap) MemorizeReq(key string) *Tzap

MemorizeReq stores the Chat message content under a given key.

func (*Tzap) Memory

func (t *Tzap) Memory(role, key string) *Tzap

Memory adds a new Tzap tied to a memory key.

func (*Tzap) MutationTzap

func (t *Tzap) MutationTzap(fn func(*Tzap) *Tzap) *Tzap

MutationTzap applies the provided function to the current Tzap object and returns a new Tzap object.

func (*Tzap) New

func (t *Tzap) New() *Tzap

New returns a new Tzap with default values.

func (*Tzap) OffsetTokens

func (t *Tzap) OffsetTokens(content string, from int, to int) (string, int, error)

RequestOpenAIChat initializes the openai chat completion request and creates a new Tzap with the edited content.

func (*Tzap) PrependContent

func (t *Tzap) PrependContent(sep string, s ...string) *Tzap

PrependContent prepends content to the current message in the Tzap

func (*Tzap) Recursive

func (t *Tzap) Recursive(tf func(tzapThatCreatesNewChildren *Tzap) *Tzap) *Tzap

Recursive applies the provided function recursively to the Tzap object and its children.

func (*Tzap) Reduce added in v0.7.15

func (t *Tzap) Reduce(fn func(*Tzap, *Tzap) *Tzap) *Tzap

func (*Tzap) RequestChatCompletion added in v0.7.14

func (t *Tzap) RequestChatCompletion() *Tzap

RequestChatCompletion initializes the openai chat completion request and creates a new Tzap with the edited content.

func (*Tzap) RequestTextToSpeech

func (t *Tzap) RequestTextToSpeech(language string, voice string) *ErrorTzap

RequestTextToSpeech requests synthesized speech using specific (google voices) language and voice. It returns a pointer to a new Tzap containing the synthesised speech 'audioContent'.

func (*Tzap) RequestTextifySpeech

func (t *Tzap) RequestTextifySpeech(audioContent *[]byte, language string) *ErrorTzap

func (*Tzap) SetInitialSystemContent added in v0.7.14

func (t *Tzap) SetInitialSystemContent(content string) *Tzap

func (*Tzap) StoreCompletion added in v0.7.14

func (t *Tzap) StoreCompletion(filePath string) *Tzap

func (*Tzap) StoreThread added in v0.7.16

func (t *Tzap) StoreThread(filePath string) *ErrorTzap

func (*Tzap) WorkTzap

func (t *Tzap) WorkTzap(fn func(*Tzap)) *Tzap

WorkTzap executes the provided function and returns the Tzap object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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