Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion agents/openai_functions_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (o *OpenAIFunctionsAgent) ParseOutput(contentResp *llms.ContentResponse) (
// No function/tool call - this is a finish
return nil, &schema.AgentFinish{
ReturnValues: map[string]any{
"output": choice.Content,
o.OutputKey: choice.Content,
},
Log: choice.Content,
}, nil
Expand Down
36 changes: 36 additions & 0 deletions agents/openai_functions_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/internal/httprr"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/fake"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/prompts"
"github.com/tmc/langchaingo/tools"
Expand Down Expand Up @@ -146,6 +147,41 @@ func TestOpenAIFunctionsAgentComplexCalculation(t *testing.T) {
}
}

func TestOpenAIFunctionsAgentOutputKey(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
name string
options []agents.Option
outputKey string
}{
{name: "default", outputKey: "output"},
{name: "custom", options: []agents.Option{agents.WithOutputKey("answer")}, outputKey: "answer"},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
const answer = "The answer is 42."
agent := agents.NewOpenAIFunctionsAgent(fake.NewFakeLLM([]string{answer}), nil, tt.options...)

t.Run("parse output", func(t *testing.T) {
actions, finish, err := agent.ParseOutput(&llms.ContentResponse{
Choices: []*llms.ContentChoice{{Content: answer}},
})
require.NoError(t, err)
require.Empty(t, actions)
require.NotNil(t, finish)
require.Equal(t, map[string]any{tt.outputKey: answer}, finish.ReturnValues)
require.Equal(t, answer, finish.Log)
})

t.Run("executor", func(t *testing.T) {
got, err := chains.Run(context.Background(), agents.NewExecutor(agent), "What is the answer?")
require.NoError(t, err)
require.Equal(t, answer, got)
})
})
}
}

// TestOpenAIFunctionsAgent_ParseOutput_NilResponse tests that ParseOutput handles nil response gracefully
func TestOpenAIFunctionsAgent_ParseOutput_NilResponse(t *testing.T) {
t.Parallel()
Expand Down
Loading