Skip to content

Commit

Permalink
agents: fix the function call of openai llm.
Browse files Browse the repository at this point in the history
According to the [guides](https://platform.openai.com/docs/guides/function-calling) of openai function call,
`Once you've executed these function calls in your application, you can provide the result back to the model
by adding one new message to the conversation for each function call, each containing the result of one
function call, with a tool_call_id referencing the id from tool_calls`.
We need to provide the function call result back to the model in line with the specified structure,
otherwise the openai server will verify the parameters and return a 400 status code.
  • Loading branch information
chainhelen committed Sep 15, 2024
1 parent 1794009 commit 454f766
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 6 additions & 3 deletions agents/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package agents_test

import (
"context"
"fmt"
"os"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -133,11 +135,12 @@ func TestExecutorWithOpenAIFunctionAgent(t *testing.T) {
)

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
require.NoError(t, err)

require.True(t, strings.Contains(result, "47") || strings.Contains(result, "49"),
"correct answer 47 or 49 not in response")
correctAnswerList := []string{"47", "49", "50"}
require.True(t, slices.ContainsFunc(correctAnswerList, func(s string) bool {
return strings.Contains(result, s)
}), fmt.Sprintf("correct answer %s not in response", correctAnswerList))
}
16 changes: 14 additions & 2 deletions agents/openai_functions_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,20 @@ func (o *OpenAIFunctionsAgent) constructScratchPad(steps []schema.AgentStep) []l

messages := make([]llms.ChatMessage, 0)
for _, step := range steps {
messages = append(messages, llms.FunctionChatMessage{
Name: step.Action.Tool,
messages = append(messages, llms.AIChatMessage{
ToolCalls: []llms.ToolCall{
{
ID: step.Action.ToolID,
Type: "function",
FunctionCall: &llms.FunctionCall{
Name: step.Action.Tool,
Arguments: step.Action.ToolInput,
},
},
},
})
messages = append(messages, llms.ToolChatMessage{
ID: step.Action.ToolID,
Content: step.Observation,
})
}
Expand Down

0 comments on commit 454f766

Please sign in to comment.