answer/pkg/agents/search_terms.go

55 lines
1.6 KiB
Go

package agents
import (
"context"
"errors"
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
"strings"
)
var ErrNoSearchTerms = errors.New("no search terms")
// GenerateSearchTerms will create search terms for the given question.
// alreadySearched is a list of search terms that have already been used, and should not be used again.
func (a Agent) GenerateSearchTerms(ctx context.Context, question string, alreadySearched []string) (string, error) {
var res string
var cantFind bool
fnSearch := gollm.NewFunction(
"search_terms",
"search DuckDuckGo with these search terms for the given question",
func(ctx *gollm.Context, args struct {
SearchTerms string `description:"The search terms to use for the search"`
}) (any, error) {
res = args.SearchTerms
return "", nil
})
fnCantThinkOfAny := gollm.NewFunction(
"cant_think_of_any",
"tell the user that you cannot think of any search terms for the given question",
func(ctx *gollm.Context, args struct{}) (any, error) {
cantFind = true
return "", nil
})
var suffix string
if len(alreadySearched) > 0 {
suffix = "The following search terms have already been used, please avoid them: " + strings.Join(alreadySearched, ", ") + "\n"
}
_, err := a.WithSystemPrompt(`You are to generate search terms for a question using DuckDuckGo. The question will be provided by the user.`).
WithSystemPromptSuffix(suffix).
WithToolbox(gollm.NewToolBox(fnSearch, fnCantThinkOfAny)).
CallAndExecute(ctx, question)
if err != nil {
return "", err
}
if cantFind {
return "cannot think of any search terms", ErrNoSearchTerms
}
return res, nil
}