fix(tools): dont error if AI invested a tool
This commit is contained in:
@ -22,6 +22,8 @@ type ToolsHandlers struct {
|
|||||||
|
|
||||||
var NoToolCallError = errors.New("An assistant tool call with no tool calls was provided.")
|
var NoToolCallError = errors.New("An assistant tool call with no tool calls was provided.")
|
||||||
|
|
||||||
|
const NonExistantTool = "This tool does not exist"
|
||||||
|
|
||||||
func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentAssistantToolCall) ([]AgentTextMessage, error) {
|
func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentAssistantToolCall) ([]AgentTextMessage, error) {
|
||||||
if len(toolCallMessage.ToolCalls) == 0 {
|
if len(toolCallMessage.ToolCalls) == 0 {
|
||||||
return []AgentTextMessage{}, NoToolCallError
|
return []AgentTextMessage{}, NoToolCallError
|
||||||
@ -33,19 +35,21 @@ func (handler ToolsHandlers) Handle(info ToolHandlerInfo, toolCallMessage AgentA
|
|||||||
fnName := toolCall.Function.Name
|
fnName := toolCall.Function.Name
|
||||||
arguments := toolCall.Function.Arguments
|
arguments := toolCall.Function.Arguments
|
||||||
|
|
||||||
fnHandler, exists := handler.handlers[fnName]
|
|
||||||
if !exists {
|
|
||||||
return []AgentTextMessage{}, errors.New("Could not find tool with this name.")
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := fnHandler.Fn(info, arguments, toolCallMessage.ToolCalls[0])
|
|
||||||
|
|
||||||
responseMessage := AgentTextMessage{
|
responseMessage := AgentTextMessage{
|
||||||
Role: "tool",
|
Role: "tool",
|
||||||
Name: fnName,
|
Name: fnName,
|
||||||
ToolCallId: toolCall.Id,
|
ToolCallId: toolCall.Id,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fnHandler, exists := handler.handlers[fnName]
|
||||||
|
if !exists {
|
||||||
|
responseMessage.Content = NonExistantTool
|
||||||
|
responses[i] = responseMessage
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := fnHandler.Fn(info, arguments, toolCallMessage.ToolCalls[0])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
responseMessage.Content = err.Error()
|
responseMessage.Content = err.Error()
|
||||||
} else {
|
} else {
|
||||||
|
@ -151,6 +151,14 @@ func (suite *ToolTestSuite) TestMultipleToolCallsWithErrors() {
|
|||||||
{
|
{
|
||||||
Index: 1,
|
Index: 1,
|
||||||
Id: "2",
|
Id: "2",
|
||||||
|
Function: FunctionCall{
|
||||||
|
Name: "non-existant",
|
||||||
|
Arguments: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Index: 2,
|
||||||
|
Id: "3",
|
||||||
Function: FunctionCall{
|
Function: FunctionCall{
|
||||||
Name: "a",
|
Name: "a",
|
||||||
Arguments: "no-error",
|
Arguments: "no-error",
|
||||||
@ -170,8 +178,14 @@ func (suite *ToolTestSuite) TestMultipleToolCallsWithErrors() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Role: "tool",
|
Role: "tool",
|
||||||
Content: "\"no-error\"",
|
Content: "This tool does not exist",
|
||||||
ToolCallId: "2",
|
ToolCallId: "2",
|
||||||
|
Name: "non-existant",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Role: "tool",
|
||||||
|
Content: "\"no-error\"",
|
||||||
|
ToolCallId: "3",
|
||||||
Name: "a",
|
Name: "a",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user