From ffee7dc5ea5cff35b3d3dade3b30010aabf54368 Mon Sep 17 00:00:00 2001 From: Kent Dong Date: Wed, 19 Jun 2024 13:48:20 +0800 Subject: [PATCH] fix: Accommodate the incomplete function name in the initial event from Qwen (#1045) --- plugins/wasm-go/extensions/ai-proxy/provider/qwen.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/wasm-go/extensions/ai-proxy/provider/qwen.go b/plugins/wasm-go/extensions/ai-proxy/provider/qwen.go index 400dde9be..54a7b46da 100644 --- a/plugins/wasm-go/extensions/ai-proxy/provider/qwen.go +++ b/plugins/wasm-go/extensions/ai-proxy/provider/qwen.go @@ -349,11 +349,19 @@ func (m *qwenProvider) buildChatCompletionStreamingResponse(ctx wrapper.HttpCont responses := make([]*chatCompletionResponse, 0) qwenChoice := qwenResponse.Output.Choices[0] + // Yes, Qwen uses a string "null" as null. + finished := qwenChoice.FinishReason != "" && qwenChoice.FinishReason != "null" message := qwenChoice.Message deltaContentMessage := &chatMessage{Role: message.Role, Content: message.Content} deltaToolCallsMessage := &chatMessage{Role: message.Role, ToolCalls: append([]toolCall{}, message.ToolCalls...)} if !incrementalStreaming { + for _, tc := range message.ToolCalls { + if tc.Function.Arguments == "" && !finished { + // We don't push any tool call until its arguments are available. + return nil + } + } if pushedMessage, ok := ctx.GetContext(ctxKeyPushedMessage).(qwenMessage); ok { if message.Content == "" { message.Content = pushedMessage.Content @@ -386,8 +394,7 @@ func (m *qwenProvider) buildChatCompletionStreamingResponse(ctx wrapper.HttpCont responses = append(responses, &response) } - // Yes, Qwen uses a string "null" as null. - if qwenChoice.FinishReason != "" && qwenChoice.FinishReason != "null" { + if finished { finishResponse := *&baseMessage finishResponse.Choices = append(finishResponse.Choices, chatCompletionChoice{FinishReason: qwenChoice.FinishReason})