From 2fcac6fc84375cc8565dbcaf3cf1bd2c9dfb34f7 Mon Sep 17 00:00:00 2001 From: FluffyKebab Date: Mon, 22 May 2023 15:37:07 +0200 Subject: [PATCH] agents: fix lint --- agents/executor/errors.go | 15 +++++++-------- agents/executor/executor.go | 17 +++++++++++------ agents/executor/executor_test.go | 2 ++ agents/executor/initialize.go | 12 +++++++++--- tools/calculator.go | 8 +++++--- tools/serpapi/internal/client.go | 3 +-- tools/serpapi/serpapi.go | 2 +- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/agents/executor/errors.go b/agents/executor/errors.go index 4f2f6330..6ee0d077 100644 --- a/agents/executor/errors.go +++ b/agents/executor/errors.go @@ -3,16 +3,15 @@ package executor import "errors" var ( - // ErrExecutorInputNotString is returned if an input to the executor call - // function is not a string. + // ErrExecutorInputNotString is returned if an input to the executor call function is not a string. ErrExecutorInputNotString = errors.New("input to executor not string") - // ErrAgentNoReturn is returned if the agent returns no actions and no - // finish. + // ErrAgentNoReturn is returned if the agent returns no actions and no finish. ErrAgentNoReturn = errors.New("no actions or finish was returned by the agent") - // ErrNotFinished is returned if the agent does not give a finish before - // the number of iterations is larger then max iterations. + // ErrNotFinished is returned if the agent does not give a finish before the number of iterations + // is larger then max iterations. ErrNotFinished = errors.New("agent not finished before max iterations") - // ErrUnknownAgentType is returned if the type given to the initializer is - // invalid. + // ErrUnknownAgentType is returned if the type given to the initializer is invalid. ErrUnknownAgentType = errors.New("unknown agent type") + // ErrInvalidOptions is returned if the options given to the initializer is invalid. + ErrInvalidOptions = errors.New("invalid options") ) diff --git a/agents/executor/executor.go b/agents/executor/executor.go index 544d5f2f..f925d067 100644 --- a/agents/executor/executor.go +++ b/agents/executor/executor.go @@ -36,11 +36,7 @@ func (e Executor) Call(ctx context.Context, inputValues map[string]any, _ ...cha if err != nil { return nil, err } - - nameToTool := make(map[string]tools.Tool, len(e.Tools)) - for _, tool := range e.Tools { - nameToTool[tool.Name()] = tool - } + nameToTool := getNameToTool(e.Tools) steps := make([]schema.AgentStep, 0) iterations := 0 @@ -93,7 +89,7 @@ func (e Executor) GetOutputKeys() []string { return e.Agent.GetOutputKeys() } -func (e Executor) GetMemory() schema.Memory { +func (e Executor) GetMemory() schema.Memory { //nolint:ireturn return memory.NewSimple() } @@ -110,3 +106,12 @@ func inputsToString(inputValues map[string]any) (map[string]string, error) { return inputs, nil } + +func getNameToTool(t []tools.Tool) map[string]tools.Tool { + nameToTool := make(map[string]tools.Tool, len(t)) + for _, tool := range t { + nameToTool[tool.Name()] = tool + } + + return nameToTool +} diff --git a/agents/executor/executor_test.go b/agents/executor/executor_test.go index 158a7b5f..ee3ac18a 100644 --- a/agents/executor/executor_test.go +++ b/agents/executor/executor_test.go @@ -15,6 +15,8 @@ import ( ) func TestMRKL(t *testing.T) { + t.Parallel() + if openaiKey := os.Getenv("OPENAI_API_KEY"); openaiKey == "" { t.Skip("OPENAI_API_KEY not set") } diff --git a/agents/executor/initialize.go b/agents/executor/initialize.go index 9b2caa99..e45de890 100644 --- a/agents/executor/initialize.go +++ b/agents/executor/initialize.go @@ -7,6 +7,8 @@ import ( "github.com/tmc/langchaingo/tools" ) +const _defaultMaxIterations = 5 + // AgentType is a string type representing the type of agent to create. type AgentType string @@ -27,7 +29,7 @@ type Option func(p *options) func defaultOptions() options { return options{ "verbose": false, - "maxIterations": 3, + "maxIterations": _defaultMaxIterations, } } @@ -58,8 +60,12 @@ func Initialize( for _, opt := range opts { opt(&options) } - var agent agents.Agent + maxIterations, ok := options["maxIterations"].(int) + if !ok { + return Executor{}, ErrInvalidOptions + } + var agent agents.Agent switch agentType { case ZeroShotReactDescription: agent = mrkl.NewOneShotAgent(llm, tools, options) @@ -70,6 +76,6 @@ func Initialize( return Executor{ Agent: agent, Tools: tools, - MaxIterations: options["maxIterations"].(int), + MaxIterations: maxIterations, }, nil } diff --git a/tools/calculator.go b/tools/calculator.go index 8b143be6..173f1341 100644 --- a/tools/calculator.go +++ b/tools/calculator.go @@ -2,6 +2,7 @@ package tools import ( "context" + "fmt" "go.starlark.net/lib/math" "go.starlark.net/starlark" @@ -23,12 +24,13 @@ func (c Calculator) Name() string { return "calculator" } -// Call evaluates the input using a starlak evaluator and returns the -// result as a string. +// Call evaluates the input using a starlak evaluator and returns the result as a +// string. If the evaluator errors the error is given in the result to give the +// agent the ability to retry. func (c Calculator) Call(_ context.Context, input string) (string, error) { v, err := starlark.Eval(&starlark.Thread{Name: "main"}, "input", input, math.Module.Members) if err != nil { - return "I don't know how to do that.", nil + return fmt.Sprintf("error from evaluator: %s", err.Error()), nil //nolint:nilerr } return v.String(), nil diff --git a/tools/serpapi/internal/client.go b/tools/serpapi/internal/client.go index 378597ed..48a324b2 100644 --- a/tools/serpapi/internal/client.go +++ b/tools/serpapi/internal/client.go @@ -21,8 +21,7 @@ var ( ) type Client struct { - apiKey string - baseURL string + apiKey string } func New(apiKey string) *Client { diff --git a/tools/serpapi/serpapi.go b/tools/serpapi/serpapi.go index 16a837b6..160a477a 100644 --- a/tools/serpapi/serpapi.go +++ b/tools/serpapi/serpapi.go @@ -46,7 +46,7 @@ func (t Tool) Call(ctx context.Context, input string) (string, error) { result, err := t.client.Search(ctx, input) if err != nil { if errors.Is(err, internal.ErrNoGoodResult) { - return "No good Google Search Result was found", nil + return "No good Google Search Results was found", nil } return "", err