From 6efd92806f9542fadb6820a3dc8ae6a248f57abc Mon Sep 17 00:00:00 2001 From: RockYang Date: Wed, 10 Apr 2024 10:23:45 +0800 Subject: [PATCH] fixed bug for gpt-4-turbo-2024-0409 model function calls --- CHANGELOG.md | 5 +++++ api/core/types/chat.go | 2 +- api/core/types/function.go | 23 +++++++++-------------- api/handler/chatimpl/chat_handler.go | 23 ++++++++++++++--------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef712bf..5ec9f7e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # 更新日志 +## v4.0.3 +* 功能新增:允许为角色应用绑定模型,如指定某个角色只能使用某个模型 +* Bug修复:兼容 gpt-4-turbo-2024-04-09 模型的函数调用 Bug +* Bug修复:修复MidJourney在任务超时后出现后面的任务覆盖前面任务的问题 +* ## 4.0.2 diff --git a/api/core/types/chat.go b/api/core/types/chat.go index 7ba2a252..54917f24 100644 --- a/api/core/types/chat.go +++ b/api/core/types/chat.go @@ -8,7 +8,7 @@ type ApiRequest struct { Stream bool `json:"stream"` Messages []interface{} `json:"messages,omitempty"` Prompt []interface{} `json:"prompt,omitempty"` // 兼容 ChatGLM - Tools []interface{} `json:"tools,omitempty"` + Tools []Tool `json:"tools,omitempty"` Functions []interface{} `json:"functions,omitempty"` // 兼容中转平台 ToolChoice string `json:"tool_choice,omitempty"` diff --git a/api/core/types/function.go b/api/core/types/function.go index 8b5f183f..09808461 100644 --- a/api/core/types/function.go +++ b/api/core/types/function.go @@ -8,19 +8,14 @@ type ToolCall struct { } `json:"function"` } +type Tool struct { + Type string `json:"type"` + Function Function `json:"function"` +} + type Function struct { - Name string `json:"name"` - Description string `json:"description"` - Parameters Parameters `json:"parameters"` -} - -type Parameters struct { - Type string `json:"type"` - Required []string `json:"required"` - Properties map[string]Property `json:"properties"` -} - -type Property struct { - Type string `json:"type"` - Description string `json:"description"` + Name string `json:"name"` + Description string `json:"description"` + Parameters map[string]interface{} `json:"parameters"` + Required interface{} `json:"required,omitempty"` } diff --git a/api/handler/chatimpl/chat_handler.go b/api/handler/chatimpl/chat_handler.go index cee0987d..3a4cb91c 100644 --- a/api/handler/chatimpl/chat_handler.go +++ b/api/handler/chatimpl/chat_handler.go @@ -239,7 +239,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio break } - var tools = make([]interface{}, 0) + var tools = make([]types.Tool, 0) for _, v := range items { var parameters map[string]interface{} err = utils.JsonDecode(v.Parameters, ¶meters) @@ -248,15 +248,20 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio } required := parameters["required"] delete(parameters, "required") - tools = append(tools, gin.H{ - "type": "function", - "function": gin.H{ - "name": v.Name, - "description": v.Description, - "parameters": parameters, - "required": required, + tool := types.Tool{ + Type: "function", + Function: types.Function{ + Name: v.Name, + Description: v.Description, + Parameters: parameters, }, - }) + } + + // Fixed: compatible for gpt4-turbo-xxx model + if !strings.HasPrefix(req.Model, "gpt-4-turbo-") { + tool.Function.Required = required + } + tools = append(tools, tool) } if len(tools) > 0 {