callbacks

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: MIT Imports: 5 Imported by: 0

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.

DefualtKeywords 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 Handler

type Handler interface {
	HandleText(ctx context.Context, text string)
	HandleLLMStart(ctx context.Context, prompts []string)
	HandleLLMEnd(ctx context.Context, output llms.LLMResult)
	HandleChainStart(ctx context.Context, inputs map[string]any)
	HandleChainEnd(ctx context.Context, outputs map[string]any)
	HandleToolStart(ctx context.Context, input string)
	HandleToolEnd(ctx context.Context, output string)
	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) 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) 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) 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) HandleChainStart

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

func (SimpleHandler) HandleLLMEnd

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

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) HandleToolStart

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

Jump to

Keyboard shortcuts

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