Skip to content

Commit

Permalink
refactor: move callback template to utils
Browse files Browse the repository at this point in the history
Change-Id: I0296020a9f49565084281d71f79d47df25da5fef
  • Loading branch information
shentongmartin committed Dec 18, 2024
1 parent 87f38b5 commit 5a3d358
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 493 deletions.
2 changes: 1 addition & 1 deletion callbacks/handler_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// if err != nil {...}
// runnable.Invoke(ctx, params, compose.WithCallback(handler)) // => only implement functions which you want to override
//
// Deprecated: In most situations, it is preferred to use template.NewHandlerHelper. Otherwise, use NewHandlerBuilder().OnStartFn()...Build().
// Deprecated: In most situations, it is preferred to use callbacks.NewHandlerHelper. Otherwise, use NewHandlerBuilder().OnStartFn()...Build().
type HandlerBuilder struct {
OnStartFn func(ctx context.Context, info *RunInfo, input CallbackInput) context.Context
OnEndFn func(ctx context.Context, info *RunInfo, output CallbackOutput) context.Context
Expand Down
2 changes: 1 addition & 1 deletion callbacks/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const (

// TimingChecker checks if the handler is needed for the given callback aspect timing.
// It's recommended for callback handlers to implement this interface, but not mandatory.
// If a callback handler is created by using template.HandlerHelper or handlerBuilder, then this interface is automatically implemented.
// If a callback handler is created by using callbacks.HandlerHelper or handlerBuilder, then this interface is automatically implemented.
// Eino's callback mechanism will try to use this interface to determine whether any handlers are needed for the given timing.
// Also, the callback handler that is not needed for that timing will be skipped.
type TimingChecker interface {
Expand Down
51 changes: 0 additions & 51 deletions callbacks/template/default.go

This file was deleted.

23 changes: 0 additions & 23 deletions components/document/callback_extra_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package document

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)
Expand Down Expand Up @@ -71,24 +69,3 @@ func ConvLoaderCallbackOutput(src callbacks.CallbackOutput) *LoaderCallbackOutpu
return nil
}
}

// LoaderCallbackHandler is the handler for the loader callback.
type LoaderCallbackHandler struct {
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *LoaderCallbackInput) context.Context
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *LoaderCallbackOutput) context.Context
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *LoaderCallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
23 changes: 0 additions & 23 deletions components/document/callback_extra_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package document

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)
Expand Down Expand Up @@ -68,24 +66,3 @@ func ConvTransformerCallbackOutput(src callbacks.CallbackOutput) *TransformerCal
return nil
}
}

// TransformerCallbackHandler is the handler for the transformer callback.
type TransformerCallbackHandler struct {
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *TransformerCallbackInput) context.Context
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *TransformerCallbackOutput) context.Context
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *TransformerCallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
23 changes: 0 additions & 23 deletions components/embedding/callback_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package embedding

import (
"context"

"github.com/cloudwego/eino/callbacks"
)

Expand Down Expand Up @@ -97,24 +95,3 @@ func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
return nil
}
}

// CallbackHandler is the handler for the embedding callback.
type CallbackHandler struct {
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *CallbackInput) context.Context
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *CallbackOutput) context.Context
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *CallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
23 changes: 0 additions & 23 deletions components/indexer/callback_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package indexer

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)
Expand Down Expand Up @@ -66,24 +64,3 @@ func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
return nil
}
}

// CallbackHandler is the handler for the indexer callback.
type CallbackHandler struct {
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *CallbackInput) context.Context
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *CallbackOutput) context.Context
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *CallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
26 changes: 0 additions & 26 deletions components/model/callback_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package model

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)
Expand Down Expand Up @@ -100,27 +98,3 @@ func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
return nil
}
}

// CallbackHandler is the handler for the model callback.
type CallbackHandler struct {
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *CallbackInput) context.Context
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *CallbackOutput) context.Context
OnEndWithStreamOutput func(ctx context.Context, runInfo *callbacks.RunInfo, output *schema.StreamReader[*CallbackOutput]) context.Context
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *CallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
case callbacks.TimingOnEndWithStreamOutput:
return ch.OnEndWithStreamOutput != nil
default:
return false
}
}
26 changes: 0 additions & 26 deletions components/prompt/callback_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package prompt

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)
Expand Down Expand Up @@ -70,27 +68,3 @@ func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
return nil
}
}

// CallbackHandler is the handler for the callback.
type CallbackHandler struct {
// OnStart is the callback function for the start of the callback.
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *CallbackInput) context.Context
// OnEnd is the callback function for the end of the callback.
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *CallbackOutput) context.Context
// OnError is the callback function for the error of the callback.
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *CallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
26 changes: 0 additions & 26 deletions components/retriever/callback_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package retriever

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)
Expand Down Expand Up @@ -74,27 +72,3 @@ func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
return nil
}
}

// CallbackHandler is the handler for the retriever callback.
type CallbackHandler struct {
// OnStart is the callback function for the start of the retriever.
OnStart func(ctx context.Context, runInfo *callbacks.RunInfo, input *CallbackInput) context.Context
// OnEnd is the callback function for the end of the retriever.
OnEnd func(ctx context.Context, runInfo *callbacks.RunInfo, output *CallbackOutput) context.Context
// OnError is the callback function for the error of the retriever.
OnError func(ctx context.Context, runInfo *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *CallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
27 changes: 0 additions & 27 deletions components/tool/callback_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package tool

import (
"context"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/schema"
)

// CallbackInput is the input for the tool callback.
Expand Down Expand Up @@ -62,27 +59,3 @@ func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput {
return nil
}
}

// CallbackHandler is the handler for the tool callback.
type CallbackHandler struct {
OnStart func(ctx context.Context, info *callbacks.RunInfo, input *CallbackInput) context.Context
OnEnd func(ctx context.Context, info *callbacks.RunInfo, input *CallbackOutput) context.Context
OnEndWithStreamOutput func(ctx context.Context, info *callbacks.RunInfo, output *schema.StreamReader[*CallbackOutput]) context.Context
OnError func(ctx context.Context, info *callbacks.RunInfo, err error) context.Context
}

// Needed checks if the callback handler is needed for the given timing.
func (ch *CallbackHandler) Needed(ctx context.Context, runInfo *callbacks.RunInfo, timing callbacks.CallbackTiming) bool {
switch timing {
case callbacks.TimingOnStart:
return ch.OnStart != nil
case callbacks.TimingOnEnd:
return ch.OnEnd != nil
case callbacks.TimingOnEndWithStreamOutput:
return ch.OnEndWithStreamOutput != nil
case callbacks.TimingOnError:
return ch.OnError != nil
default:
return false
}
}
4 changes: 2 additions & 2 deletions flow/agent/multiagent/host/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"io"

"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/callbacks/template"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/flow/agent"
"github.com/cloudwego/eino/schema"
template "github.com/cloudwego/eino/utils/callbacks"
)

// MultiAgentCallback is the callback interface for host multi-agent.
Expand Down Expand Up @@ -114,7 +114,7 @@ func convertCallbacks(opts ...agent.AgentOption) callbacks.Handler {
return ctx
}

return template.NewHandlerHelper().ChatModel(&model.CallbackHandler{
return template.NewHandlerHelper().ChatModel(&template.ModelCallbackHandler{
OnEnd: onChatModelEnd,
OnEndWithStreamOutput: onChatModelEndWithStreamOutput,
}).Handler()
Expand Down
8 changes: 3 additions & 5 deletions flow/agent/react/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ package react

import (
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/callbacks/template"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/components/tool"
template "github.com/cloudwego/eino/utils/callbacks"
)

// BuildAgentCallback builds a callback handler for agent.
// eg.
// e.g.
//
// callback := BuildAgentCallback(modelHandler, toolHandler)
// agent, err := react.NewAgent(ctx, &AgentConfig{})
// agent.Generate(ctx, input, agent.WithComposeOptions(compose.WithCallbacks(callback)))
func BuildAgentCallback(modelHandler *model.CallbackHandler, toolHandler *tool.CallbackHandler) callbacks.Handler {
func BuildAgentCallback(modelHandler *template.ModelCallbackHandler, toolHandler *template.ToolCallbackHandler) callbacks.Handler {
return template.NewHandlerHelper().ChatModel(modelHandler).Tool(toolHandler).Handler()
}
Loading

0 comments on commit 5a3d358

Please sign in to comment.