Consolidated a bunch of reused code to agents
This commit is contained in:
@@ -2,64 +2,53 @@ package agents
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"errors"
|
||||
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SearchTerms struct {
|
||||
Model gollm.ChatCompletion
|
||||
Context []string
|
||||
}
|
||||
var ErrNoSearchTerms = errors.New("no search terms")
|
||||
|
||||
// SearchTerms will create search terms for the given question.
|
||||
// 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 (q SearchTerms) SearchTerms(ctx context.Context, question string, alreadySearched []string) (string, error) {
|
||||
func (a Agent) GenerateSearchTerms(ctx context.Context, question string, alreadySearched []string) (string, error) {
|
||||
var res string
|
||||
|
||||
req := gollm.Request{
|
||||
Toolbox: gollm.NewToolBox(
|
||||
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"`
|
||||
}) (string, error) {
|
||||
res = args.SearchTerms
|
||||
return "", nil
|
||||
}),
|
||||
),
|
||||
Messages: []gollm.Message{
|
||||
{
|
||||
Role: gollm.RoleSystem,
|
||||
Text: `You are to generate search terms for a question using DuckDuckGo. The question will be provided by the user.`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if len(alreadySearched) > 0 {
|
||||
req.Messages = append(req.Messages, gollm.Message{
|
||||
Role: gollm.RoleSystem,
|
||||
Text: fmt.Sprintf("The following search terms have already been used: %v", alreadySearched),
|
||||
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"
|
||||
}
|
||||
|
||||
req.Messages = append(req.Messages, gollm.Message{
|
||||
Role: gollm.RoleUser,
|
||||
Text: fmt.Sprintf("The question is: %s", question),
|
||||
})
|
||||
_, 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)
|
||||
|
||||
resp, err := q.Model.ChatComplete(ctx, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(resp.Choices) == 0 {
|
||||
return "", fmt.Errorf("no choices found")
|
||||
if cantFind {
|
||||
return "cannot think of any search terms", ErrNoSearchTerms
|
||||
}
|
||||
|
||||
choice := resp.Choices[0]
|
||||
|
||||
_, _ = req.Toolbox.ExecuteCallbacks(gollm.NewContext(ctx, req, &choice, nil), choice.Calls, nil, nil)
|
||||
return res, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user