Skip to content

Commit

Permalink
agents: GetTools added to Agent interface (tmc#831)
Browse files Browse the repository at this point in the history
* feat: serpapi api key options

* feat: agent tools interface
  • Loading branch information
gluonfield authored May 11, 2024
1 parent d9e644e commit 7b5d90a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions agents/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/tools"
)

// Agent is the interface all agents must implement.
Expand All @@ -13,4 +14,5 @@ type Agent interface {
Plan(ctx context.Context, intermediateSteps []schema.AgentStep, inputs map[string]string) ([]schema.AgentAction, *schema.AgentFinish, error) //nolint:lll
GetInputKeys() []string
GetOutputKeys() []string
GetTools() []tools.Tool
}
4 changes: 4 additions & 0 deletions agents/conversational.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (a *ConversationalAgent) GetOutputKeys() []string {
return []string{a.OutputKey}
}

func (a *ConversationalAgent) GetTools() []tools.Tool {
return a.Tools
}

func constructScratchPad(steps []schema.AgentStep) string {
var scratchPad string
if len(steps) > 0 {
Expand Down
6 changes: 2 additions & 4 deletions agents/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const _intermediateStepsOutputKey = "intermediateSteps"
// Executor is the chain responsible for running agents.
type Executor struct {
Agent Agent
Tools []tools.Tool
Memory schema.Memory
CallbacksHandler callbacks.Handler
ErrorHandler *ParserErrorHandler
Expand All @@ -32,15 +31,14 @@ var (
)

// NewExecutor creates a new agent executor with an agent and the tools the agent can use.
func NewExecutor(agent Agent, tools []tools.Tool, opts ...Option) *Executor {
func NewExecutor(agent Agent, opts ...Option) *Executor {
options := executorDefaultOptions()
for _, opt := range opts {
opt(&options)
}

return &Executor{
Agent: agent,
Tools: tools,
Memory: options.memory,
MaxIterations: options.maxIterations,
ReturnIntermediateSteps: options.returnIntermediateSteps,
Expand All @@ -54,7 +52,7 @@ func (e *Executor) Call(ctx context.Context, inputValues map[string]any, _ ...ch
if err != nil {
return nil, err
}
nameToTool := getNameToTool(e.Tools)
nameToTool := getNameToTool(e.Agent.GetTools())

steps := make([]schema.AgentStep, 0)
for i := 0; i < e.MaxIterations; i++ {
Expand Down
7 changes: 5 additions & 2 deletions agents/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (a testAgent) GetOutputKeys() []string {
return a.outputKeys
}

func (a *testAgent) GetTools() []tools.Tool {
return nil
}

func TestExecutorWithErrorHandler(t *testing.T) {
t.Parallel()

Expand All @@ -56,7 +60,6 @@ func TestExecutorWithErrorHandler(t *testing.T) {
}
executor := agents.NewExecutor(
a,
nil,
agents.WithMaxIterations(3),
agents.WithParserErrorHandler(agents.NewParserErrorHandler(nil)),
)
Expand Down Expand Up @@ -129,7 +132,7 @@ func TestExecutorWithOpenAIFunctionAgent(t *testing.T) {
}),
)

e := agents.NewExecutor(a, toolList)
e := agents.NewExecutor(a)
require.NoError(t, err)

result, err := chains.Run(context.Background(), e, "what is HK singer Eason Chan's years old?") //nolint:lll
Expand Down
2 changes: 1 addition & 1 deletion agents/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func Initialize(
default:
return &Executor{}, ErrUnknownAgentType
}
return NewExecutor(agent, tools, opts...), nil
return NewExecutor(agent, opts...), nil
}
4 changes: 4 additions & 0 deletions agents/mrkl.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (a *OneShotZeroAgent) GetOutputKeys() []string {
return []string{a.OutputKey}
}

func (a *OneShotZeroAgent) GetTools() []tools.Tool {
return a.Tools
}

func (a *OneShotZeroAgent) parseOutput(output string) ([]schema.AgentAction, *schema.AgentFinish, error) {
if strings.Contains(output, _finalAnswerAction) {
splits := strings.Split(output, _finalAnswerAction)
Expand Down
4 changes: 4 additions & 0 deletions agents/openai_functions_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (o *OpenAIFunctionsAgent) GetOutputKeys() []string {
return []string{o.OutputKey}
}

func (o *OpenAIFunctionsAgent) GetTools() []tools.Tool {
return o.Tools
}

func createOpenAIFunctionPrompt(opts Options) prompts.ChatPromptTemplate {
messageFormatters := []prompts.MessageFormatter{prompts.NewSystemMessagePromptTemplate(opts.systemMessage, nil)}
messageFormatters = append(messageFormatters, opts.extraMessages...)
Expand Down

0 comments on commit 7b5d90a

Please sign in to comment.