answer/pkg/agent/ask.go

49 lines
1.1 KiB
Go

package agent
import (
"encoding/json"
"fmt"
"gitea.stevedudenhoeffer.com/steve/answer/pkg/toolbox"
)
var AskTool = toolbox.FromFunction(
func(ctx *Context, args struct {
Question string `description:"the question to answer"`
}) (toolbox.FuncResponse, error) {
var q Question
q.Question = args.Question
ctx = ctx.WithQuestion(q)
answers, err := ask(ctx, q)
if err != nil {
return toolbox.FuncResponse{}, err
}
tb := toolbox.ToolBox{}
tb.Register(SummarizeAnswers)
b, err := json.Marshal(answers.Answers)
if err != nil {
return toolbox.FuncResponse{}, fmt.Errorf("failed to marshal answers: %w", err)
}
q = Question{Question: string(b)}
ctx = ctx.WithQuestion(q)
answers, err = tb.Run(ctx, q)
if err != nil {
return toolbox.FuncResponse{}, fmt.Errorf("failed to summarize answers: %w", err)
}
if len(answers.Answers) == 0 {
return toolbox.FuncResponse{}, fmt.Errorf("no response from model")
}
return toolbox.FuncResponse{Result: answers.Answers[0].Answer}, nil
}).
WithName("ask").
WithDescription("Ask the agent a question, this is useful for splitting a question into multiple parts")