fix: function call 兼容中转 API

This commit is contained in:
RockYang
2024-01-07 22:32:59 +08:00
parent 7000168fd4
commit 485bdbc56a
4 changed files with 80 additions and 21 deletions

View File

@@ -229,6 +229,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio
}
var tools = make([]interface{}, 0)
var functions = make([]interface{}, 0)
for _, v := range items {
var parameters map[string]interface{}
err = utils.JsonDecode(v.Parameters, &parameters)
@@ -246,12 +247,22 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio
"required": required,
},
})
functions = append(functions, gin.H{
"name": v.Name,
"description": v.Description,
"parameters": parameters,
"required": required,
})
}
if len(tools) > 0 {
req.Tools = tools
req.ToolChoice = "auto"
//if len(tools) > 0 {
// req.Tools = tools
// req.ToolChoice = "auto"
//}
if len(functions) > 0 {
req.Functions = functions
}
case types.XunFei:
req.Temperature = h.App.ChatConfig.XunFei.Temperature
req.MaxTokens = h.App.ChatConfig.XunFei.MaxTokens
@@ -438,6 +449,8 @@ func (h *ChatHandler) doRequest(ctx context.Context, req types.ApiRequest, platf
apiURL = fmt.Sprintf("%s?access_token=%s", apiURL, token)
}
logger.Debugf(utils.JsonEncode(req))
// 创建 HttpClient 请求对象
var client *http.Client
requestBody, err := json.Marshal(req)

View File

@@ -76,17 +76,27 @@ func (h *ChatHandler) sendOpenAiMessage(
break
}
var fun types.ToolCall
var tool types.ToolCall
if len(responseBody.Choices[0].Delta.ToolCalls) > 0 {
fun = responseBody.Choices[0].Delta.ToolCalls[0]
if toolCall && fun.Function.Name == "" {
arguments = append(arguments, fun.Function.Arguments)
tool = responseBody.Choices[0].Delta.ToolCalls[0]
if toolCall && tool.Function.Name == "" {
arguments = append(arguments, tool.Function.Arguments)
continue
}
}
if !utils.IsEmptyValue(fun) {
res := h.db.Where("name = ?", fun.Function.Name).First(&function)
// 兼容 Function Call
fun := responseBody.Choices[0].Delta.FunctionCall
if fun.Name != "" {
tool = *new(types.ToolCall)
tool.Function.Name = fun.Name
} else if toolCall {
arguments = append(arguments, fun.Arguments)
continue
}
if !utils.IsEmptyValue(tool) {
res := h.db.Where("name = ?", tool.Function.Name).First(&function)
if res.Error == nil {
toolCall = true
utils.ReplyChunkMessage(ws, types.WsMessage{Type: types.WsStart})
@@ -95,7 +105,8 @@ func (h *ChatHandler) sendOpenAiMessage(
continue
}
if responseBody.Choices[0].FinishReason == "tool_calls" { // 函数调用完毕
if responseBody.Choices[0].FinishReason == "tool_calls" ||
responseBody.Choices[0].FinishReason == "function_call" { // 函数调用完毕
break
}