Skip to content

Commit

Permalink
handle callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
sklinkert committed Jan 6, 2025
1 parent cc536e6 commit bbd85f1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tools/perplexity/perplexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/tmc/langchaingo/callbacks"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/tools"
Expand Down Expand Up @@ -47,7 +48,8 @@ func WithModel(model Model) Option {

// Tool implements the Perplexity AI integration
type Tool struct {
llm *openai.LLM
llm *openai.LLM
CallbacksHandler callbacks.Handler
}

var _ tools.Tool = (*Tool)(nil)
Expand Down Expand Up @@ -93,6 +95,10 @@ func (t *Tool) Description() string {

// Call executes a query against the Perplexity AI model and returns the response
func (t *Tool) Call(ctx context.Context, input string) (string, error) {
if t.CallbacksHandler != nil {
t.CallbacksHandler.HandleToolStart(ctx, input)
}

content := []llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeHuman, input),
}
Expand All @@ -104,8 +110,15 @@ func (t *Tool) Call(ctx context.Context, input string) (string, error) {
return nil
}))
if err != nil {
if t.CallbacksHandler != nil {
t.CallbacksHandler.HandleToolError(ctx, err)
}
return "", err
}

if t.CallbacksHandler != nil {
t.CallbacksHandler.HandleToolEnd(ctx, generatedText)
}

return generatedText, nil
}

0 comments on commit bbd85f1

Please sign in to comment.