callbacks

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 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.

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

type CombiningHandler struct {
	Callbacks []Handler
}

CombiningHandler is a callback handler that combine multi callbacks.

func (CombiningHandler) HandleAgentAction

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

func (CombiningHandler) HandleAgentFinish

func (l CombiningHandler) HandleAgentFinish(ctx context.Context, finish schema.AgentFinish)

func (CombiningHandler) HandleChainEnd

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

func (CombiningHandler) HandleChainError

func (l CombiningHandler) HandleChainError(ctx context.Context, err error)

func (CombiningHandler) HandleChainStart

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

func (CombiningHandler) HandleLLMError

func (l CombiningHandler) HandleLLMError(ctx context.Context, err error)

func (CombiningHandler) HandleLLMGenerateContentEnd

func (l CombiningHandler) HandleLLMGenerateContentEnd(ctx context.Context, res *llms.ContentResponse)

func (CombiningHandler) HandleLLMGenerateContentStart

func (l CombiningHandler) HandleLLMGenerateContentStart(ctx context.Context, ms []llms.MessageContent)

func (CombiningHandler) HandleLLMStart

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

func (CombiningHandler) HandleRetrieverEnd

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

func (CombiningHandler) HandleRetrieverStart

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

func (CombiningHandler) HandleStreamingFunc

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

func (CombiningHandler) HandleText

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

func (CombiningHandler) HandleToolEnd

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

func (CombiningHandler) HandleToolError

func (l CombiningHandler) HandleToolError(ctx context.Context, err error)

func (CombiningHandler) HandleToolStart

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)
	HandleLLMGenerateContentStart(ctx context.Context, ms []llms.MessageContent)
	HandleLLMGenerateContentEnd(ctx context.Context, res *llms.ContentResponse)
	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)
	HandleAgentFinish(ctx context.Context, finish schema.AgentFinish)
	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{}

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

func (l LogHandler) HandleAgentFinish(_ context.Context, finish schema.AgentFinish)

func (LogHandler) HandleChainEnd

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

func (LogHandler) HandleChainError

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

func (LogHandler) HandleChainStart

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

func (LogHandler) HandleLLMError

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

func (LogHandler) HandleLLMGenerateContentEnd

func (l LogHandler) HandleLLMGenerateContentEnd(_ context.Context, res *llms.ContentResponse)

func (LogHandler) HandleLLMGenerateContentStart

func (l LogHandler) HandleLLMGenerateContentStart(_ context.Context, ms []llms.MessageContent)

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

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

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

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

func (SimpleHandler) HandleAgentFinish(context.Context, schema.AgentFinish)

func (SimpleHandler) HandleChainEnd

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

func (SimpleHandler) HandleChainError

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

func (SimpleHandler) HandleChainStart

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

func (SimpleHandler) HandleLLMError

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

func (SimpleHandler) HandleLLMGenerateContentEnd

func (SimpleHandler) HandleLLMGenerateContentEnd(context.Context, *llms.ContentResponse)

func (SimpleHandler) HandleLLMGenerateContentStart

func (SimpleHandler) HandleLLMGenerateContentStart(context.Context, []llms.MessageContent)

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

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

func (SimpleHandler) HandleToolStart

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

type StreamLogHandler

type StreamLogHandler struct {
	SimpleHandler
}

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

func (StreamLogHandler) HandleStreamingFunc

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