callbacks

package
v0.1.2-alpha.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 5 Imported by: 52

Documentation

Overview

Package callbacks includes a standard interface for hooking into various stages of your LLM application. The package contains an implementation of this interface that prints to the standard output.

Index

Constants

This section is empty.

Variables

View Source
var DefaultKeywords = []string{"Final Answer:", "Final:", "AI:"}

DefaultKeywords is map of the agents final out prefix keywords.

Functions

This section is empty.

Types

type AgentFinalStreamHandler

type AgentFinalStreamHandler struct {
	SimpleHandler

	Keywords        []string
	LastTokens      string
	KeywordDetected bool
	PrintOutput     bool
	// contains filtered or unexported fields
}

func NewFinalStreamHandler

func NewFinalStreamHandler(keywords ...string) *AgentFinalStreamHandler

NewFinalStreamHandler creates a new instance of the AgentFinalStreamHandler struct.

It accepts a variadic number of strings as keywords. If any keywords are provided, the DefaultKeywords variable is updated with the provided keywords.

DefaultKeywords is map of the agents final out prefix keywords.

The function returns a pointer to the created AgentFinalStreamHandler struct.

func (*AgentFinalStreamHandler) GetEgress

func (handler *AgentFinalStreamHandler) GetEgress() chan []byte

GetEgress returns the egress channel of the AgentFinalStreamHandler.

It does not take any parameters. It returns a channel of type []byte.

func (*AgentFinalStreamHandler) HandleStreamingFunc

func (handler *AgentFinalStreamHandler) HandleStreamingFunc(_ context.Context, chunk []byte)

HandleStreamingFunc implements the callback interface that handles the streaming of data in the AgentFinalStreamHandler. The handler reads the incoming data and checks for the agents final output keywords, ie, `Final Answer:`, `Final:`, `AI:`. Upon detection of the keyword, it starst to stream the agents final output to the egress channel.

It takes in the context and a chunk of bytes as parameters. There is no return type for this function.

func (*AgentFinalStreamHandler) ReadFromEgress

func (handler *AgentFinalStreamHandler) ReadFromEgress(
	ctx context.Context,
	callback func(ctx context.Context, chunk []byte),
)

ReadFromEgress reads data from the egress channel and invokes the provided callback function with each chunk of data.

The callback function receives two parameters: - ctx: the context.Context object for the egress operation. - chunk: a byte slice representing a chunk of data from the egress channel.

type CombiningHandler added in v0.1.3

type CombiningHandler struct {
	Callbacks []Handler
}

CombiningHandler is a callback handler that combine multi callbacks.

func (CombiningHandler) HandleAgentAction added in v0.1.3

func (l CombiningHandler) HandleAgentAction(ctx context.Context, action schema.AgentAction)

func (CombiningHandler) HandleChainEnd added in v0.1.3

func (l CombiningHandler) HandleChainEnd(ctx context.Context, outputs map[string]any)

func (CombiningHandler) HandleChainStart added in v0.1.3

func (l CombiningHandler) HandleChainStart(ctx context.Context, inputs map[string]any)

func (CombiningHandler) HandleLLMEnd added in v0.1.3

func (l CombiningHandler) HandleLLMEnd(ctx context.Context, output llms.LLMResult)

func (CombiningHandler) HandleLLMStart added in v0.1.3

func (l CombiningHandler) HandleLLMStart(ctx context.Context, prompts []string)

func (CombiningHandler) HandleRetrieverEnd added in v0.1.3

func (l CombiningHandler) HandleRetrieverEnd(ctx context.Context, query string, documents []schema.Document)

func (CombiningHandler) HandleRetrieverStart added in v0.1.3

func (l CombiningHandler) HandleRetrieverStart(ctx context.Context, query string)

func (CombiningHandler) HandleStreamingFunc added in v0.1.3

func (l CombiningHandler) HandleStreamingFunc(ctx context.Context, chunk []byte)

func (CombiningHandler) HandleText added in v0.1.3

func (l CombiningHandler) HandleText(ctx context.Context, text string)

func (CombiningHandler) HandleToolEnd added in v0.1.3

func (l CombiningHandler) HandleToolEnd(ctx context.Context, output string)

func (CombiningHandler) HandleToolStart added in v0.1.3

func (l CombiningHandler) HandleToolStart(ctx context.Context, input string)

type Handler

type Handler interface {
	HandleText(ctx context.Context, text string)
	HandleLLMStart(ctx context.Context, prompts []string)
	HandleLLMEnd(ctx context.Context, output llms.LLMResult)
	HandleLLMError(ctx context.Context, err error)
	HandleChainStart(ctx context.Context, inputs map[string]any)
	HandleChainEnd(ctx context.Context, outputs map[string]any)
	HandleChainError(ctx context.Context, err error)
	HandleToolStart(ctx context.Context, input string)
	HandleToolEnd(ctx context.Context, output string)
	HandleToolError(ctx context.Context, err error)
	HandleAgentAction(ctx context.Context, action schema.AgentAction)
	HandleRetrieverStart(ctx context.Context, query string)
	HandleRetrieverEnd(ctx context.Context, query string, documents []schema.Document)
	HandleStreamingFunc(ctx context.Context, chunk []byte)
}

Handler is the interface that allows for hooking into specific parts of an LLM application.

type HandlerHaver

type HandlerHaver interface {
	GetCallbackHandler() Handler
}

HandlerHaver is an interface used to get callbacks handler.

type LogHandler

type LogHandler struct {
	SimpleHandler
}

LogHandler is a callback handler that prints to the standard output.

func (LogHandler) HandleAgentAction

func (l LogHandler) HandleAgentAction(_ context.Context, action schema.AgentAction)

func (LogHandler) HandleChainEnd

func (l LogHandler) HandleChainEnd(_ context.Context, outputs map[string]any)

func (LogHandler) HandleChainError added in v0.1.3

func (l LogHandler) HandleChainError(_ context.Context, err error)

func (LogHandler) HandleChainStart

func (l LogHandler) HandleChainStart(_ context.Context, inputs map[string]any)

func (LogHandler) HandleLLMEnd

func (l LogHandler) HandleLLMEnd(_ context.Context, output llms.LLMResult)

func (LogHandler) HandleLLMError added in v0.1.3

func (l LogHandler) HandleLLMError(_ context.Context, err error)

func (LogHandler) HandleLLMStart

func (l LogHandler) HandleLLMStart(_ context.Context, prompts []string)

func (LogHandler) HandleRetrieverEnd

func (l LogHandler) HandleRetrieverEnd(_ context.Context, query string, documents []schema.Document)

func (LogHandler) HandleRetrieverStart

func (l LogHandler) HandleRetrieverStart(_ context.Context, query string)

func (LogHandler) HandleText

func (l LogHandler) HandleText(_ context.Context, text string)

func (LogHandler) HandleToolEnd

func (l LogHandler) HandleToolEnd(_ context.Context, output string)

func (LogHandler) HandleToolError added in v0.1.3

func (l LogHandler) HandleToolError(_ context.Context, err error)

func (LogHandler) HandleToolStart

func (l LogHandler) HandleToolStart(_ context.Context, input string)

type SimpleHandler

type SimpleHandler struct{}

func (SimpleHandler) HandleAgentAction

func (SimpleHandler) HandleAgentAction(context.Context, schema.AgentAction)

func (SimpleHandler) HandleChainEnd

func (SimpleHandler) HandleChainEnd(context.Context, map[string]any)

func (SimpleHandler) HandleChainError added in v0.1.3

func (SimpleHandler) HandleChainError(context.Context, error)

func (SimpleHandler) HandleChainStart

func (SimpleHandler) HandleChainStart(context.Context, map[string]any)

func (SimpleHandler) HandleLLMEnd

func (SimpleHandler) HandleLLMEnd(context.Context, llms.LLMResult)

func (SimpleHandler) HandleLLMError added in v0.1.3

func (SimpleHandler) HandleLLMError(context.Context, error)

func (SimpleHandler) HandleLLMStart

func (SimpleHandler) HandleLLMStart(context.Context, []string)

func (SimpleHandler) HandleRetrieverEnd

func (SimpleHandler) HandleRetrieverEnd(context.Context, string, []schema.Document)

func (SimpleHandler) HandleRetrieverStart

func (SimpleHandler) HandleRetrieverStart(context.Context, string)

func (SimpleHandler) HandleStreamingFunc

func (SimpleHandler) HandleStreamingFunc(context.Context, []byte)

func (SimpleHandler) HandleText

func (SimpleHandler) HandleText(context.Context, string)

func (SimpleHandler) HandleToolEnd

func (SimpleHandler) HandleToolEnd(context.Context, string)

func (SimpleHandler) HandleToolError added in v0.1.3

func (SimpleHandler) HandleToolError(context.Context, error)

func (SimpleHandler) HandleToolStart

func (SimpleHandler) HandleToolStart(context.Context, string)

type StreamLogHandler added in v0.1.2

type StreamLogHandler struct {
	SimpleHandler
}

StreamLogHandler is a callback handler that prints to the standard output streaming.

func (StreamLogHandler) HandleStreamingFunc added in v0.1.2

func (StreamLogHandler) HandleStreamingFunc(_ context.Context, chunk []byte)

Jump to

Keyboard shortcuts

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