Files
langchaingo/callbacks/combining.go
T
Dmitry Ng 6317685b67 feat: integrate streaming functionality across LLMs and callbacks
- Updated LLMs and callback interfaces to utilize a new streaming package for handling content chunks.
- Changed function signatures to accept streaming.Chunk instead of byte slices for better type safety and clarity.
- Enhanced examples and tests to demonstrate the new streaming capabilities, ensuring compatibility with existing functionality.
- Added reasoning and tool call handling in streaming responses for improved processing of LLM outputs.
2025-06-23 18:52:06 +03:00

113 lines
3.0 KiB
Go

package callbacks
import (
"context"
"github.com/vxcontrol/langchaingo/llms"
"github.com/vxcontrol/langchaingo/llms/streaming"
"github.com/vxcontrol/langchaingo/schema"
)
// CombiningHandler is a callback handler that combine multi callbacks.
type CombiningHandler struct {
Callbacks []Handler
}
var _ Handler = CombiningHandler{}
func (l CombiningHandler) HandleText(ctx context.Context, text string) {
for _, handle := range l.Callbacks {
handle.HandleText(ctx, text)
}
}
func (l CombiningHandler) HandleLLMStart(ctx context.Context, prompts []string) {
for _, handle := range l.Callbacks {
handle.HandleLLMStart(ctx, prompts)
}
}
func (l CombiningHandler) HandleLLMGenerateContentStart(ctx context.Context, ms []llms.MessageContent) {
for _, handle := range l.Callbacks {
handle.HandleLLMGenerateContentStart(ctx, ms)
}
}
func (l CombiningHandler) HandleLLMGenerateContentEnd(ctx context.Context, res *llms.ContentResponse) {
for _, handle := range l.Callbacks {
handle.HandleLLMGenerateContentEnd(ctx, res)
}
}
func (l CombiningHandler) HandleChainStart(ctx context.Context, inputs map[string]any) {
for _, handle := range l.Callbacks {
handle.HandleChainStart(ctx, inputs)
}
}
func (l CombiningHandler) HandleChainEnd(ctx context.Context, outputs map[string]any) {
for _, handle := range l.Callbacks {
handle.HandleChainEnd(ctx, outputs)
}
}
func (l CombiningHandler) HandleToolStart(ctx context.Context, input string) {
for _, handle := range l.Callbacks {
handle.HandleToolStart(ctx, input)
}
}
func (l CombiningHandler) HandleToolEnd(ctx context.Context, output string) {
for _, handle := range l.Callbacks {
handle.HandleToolEnd(ctx, output)
}
}
func (l CombiningHandler) HandleAgentAction(ctx context.Context, action schema.AgentAction) {
for _, handle := range l.Callbacks {
handle.HandleAgentAction(ctx, action)
}
}
func (l CombiningHandler) HandleAgentFinish(ctx context.Context, finish schema.AgentFinish) {
for _, handle := range l.Callbacks {
handle.HandleAgentFinish(ctx, finish)
}
}
func (l CombiningHandler) HandleRetrieverStart(ctx context.Context, query string) {
for _, handle := range l.Callbacks {
handle.HandleRetrieverStart(ctx, query)
}
}
func (l CombiningHandler) HandleRetrieverEnd(ctx context.Context, query string, documents []schema.Document) {
for _, handle := range l.Callbacks {
handle.HandleRetrieverEnd(ctx, query, documents)
}
}
func (l CombiningHandler) HandleStreamingFunc(ctx context.Context, chunk streaming.Chunk) {
for _, handle := range l.Callbacks {
handle.HandleStreamingFunc(ctx, chunk)
}
}
func (l CombiningHandler) HandleChainError(ctx context.Context, err error) {
for _, handle := range l.Callbacks {
handle.HandleChainError(ctx, err)
}
}
func (l CombiningHandler) HandleLLMError(ctx context.Context, err error) {
for _, handle := range l.Callbacks {
handle.HandleLLMError(ctx, err)
}
}
func (l CombiningHandler) HandleToolError(ctx context.Context, err error) {
for _, handle := range l.Callbacks {
handle.HandleToolError(ctx, err)
}
}