mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 00:03:11 +00:00
fix(ollama): preserve tool calls from final stream frame | 保留流式末尾帧的 tool calls (#7376)
This commit is contained in:
1 parent
2bfb89c1b9
commit
8e5e09166c
2 files changed
+196
-37
No files matched your search
@@ -79,6 +79,51 @@ func ollamaToolCallsToOpenAI(toolCalls []OllamaToolCall, startIndex int, include
|
||||
return result, startIndex
|
||||
}
|
||||
|
||||
func buildOllamaStreamDelta(chunk *ollamaChatStreamChunk, responseID string, created int64, model string, toolCallIndex *int) (dto.ChatCompletionsStreamResponse, bool) {
|
||||
delta := dto.ChatCompletionsStreamResponse{
|
||||
Id: responseID,
|
||||
Object: "chat.completion.chunk",
|
||||
Created: created,
|
||||
Model: model,
|
||||
Choices: []dto.ChatCompletionsStreamResponseChoice{{
|
||||
Index: 0,
|
||||
Delta: dto.ChatCompletionsStreamResponseChoiceDelta{Role: "assistant"},
|
||||
}},
|
||||
}
|
||||
|
||||
var content string
|
||||
if chunk.Message != nil {
|
||||
content = chunk.Message.Content
|
||||
} else {
|
||||
content = chunk.Response
|
||||
}
|
||||
if content != "" {
|
||||
delta.Choices[0].Delta.SetContentString(content)
|
||||
}
|
||||
|
||||
hasPayload := content != ""
|
||||
if chunk.Message != nil && len(chunk.Message.Thinking) > 0 {
|
||||
raw := strings.TrimSpace(string(chunk.Message.Thinking))
|
||||
if raw != "" && raw != "null" {
|
||||
// Unmarshal the JSON string to get the actual content without quotes
|
||||
var thinkingContent string
|
||||
if err := common.Unmarshal(chunk.Message.Thinking, &thinkingContent); err == nil {
|
||||
delta.Choices[0].Delta.SetReasoningContent(thinkingContent)
|
||||
} else {
|
||||
// Fallback to raw string if it's not a JSON string
|
||||
delta.Choices[0].Delta.SetReasoningContent(raw)
|
||||
}
|
||||
hasPayload = true
|
||||
}
|
||||
}
|
||||
if chunk.Message != nil && len(chunk.Message.ToolCalls) > 0 {
|
||||
delta.Choices[0].Delta.ToolCalls, *toolCallIndex = ollamaToolCallsToOpenAI(chunk.Message.ToolCalls, *toolCallIndex, true)
|
||||
hasPayload = true
|
||||
}
|
||||
|
||||
return delta, hasPayload
|
||||
}
|
||||
|
||||
func toUnix(ts string) int64 {
|
||||
if ts == "" {
|
||||
return time.Now().Unix()
|
||||
@@ -130,49 +175,18 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http
|
||||
created = toUnix(chunk.CreatedAt)
|
||||
|
||||
if !chunk.Done {
|
||||
// delta content
|
||||
var content string
|
||||
if chunk.Message != nil {
|
||||
content = chunk.Message.Content
|
||||
} else {
|
||||
content = chunk.Response
|
||||
}
|
||||
delta := dto.ChatCompletionsStreamResponse{
|
||||
Id: responseId,
|
||||
Object: "chat.completion.chunk",
|
||||
Created: created,
|
||||
Model: model,
|
||||
Choices: []dto.ChatCompletionsStreamResponseChoice{{
|
||||
Index: 0,
|
||||
Delta: dto.ChatCompletionsStreamResponseChoiceDelta{Role: "assistant"},
|
||||
}},
|
||||
}
|
||||
if content != "" {
|
||||
delta.Choices[0].Delta.SetContentString(content)
|
||||
}
|
||||
if chunk.Message != nil && len(chunk.Message.Thinking) > 0 {
|
||||
raw := strings.TrimSpace(string(chunk.Message.Thinking))
|
||||
if raw != "" && raw != "null" {
|
||||
// Unmarshal the JSON string to get the actual content without quotes
|
||||
var thinkingContent string
|
||||
if err := common.Unmarshal(chunk.Message.Thinking, &thinkingContent); err == nil {
|
||||
delta.Choices[0].Delta.SetReasoningContent(thinkingContent)
|
||||
} else {
|
||||
// Fallback to raw string if it's not a JSON string
|
||||
delta.Choices[0].Delta.SetReasoningContent(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
// tool calls
|
||||
if chunk.Message != nil && len(chunk.Message.ToolCalls) > 0 {
|
||||
delta.Choices[0].Delta.ToolCalls, toolCallIndex = ollamaToolCallsToOpenAI(chunk.Message.ToolCalls, toolCallIndex, true)
|
||||
}
|
||||
delta, _ := buildOllamaStreamDelta(&chunk, responseId, created, model, &toolCallIndex)
|
||||
if data, err := common.Marshal(delta); err == nil {
|
||||
_ = helper.StringData(c, string(data))
|
||||
}
|
||||
continue
|
||||
}
|
||||
// done frame
|
||||
if delta, hasPayload := buildOllamaStreamDelta(&chunk, responseId, created, model, &toolCallIndex); hasPayload {
|
||||
if data, err := common.Marshal(delta); err == nil {
|
||||
_ = helper.StringData(c, string(data))
|
||||
}
|
||||
}
|
||||
// finalize once and break loop
|
||||
usage.PromptTokens = chunk.PromptEvalCount
|
||||
usage.CompletionTokens = chunk.EvalCount
|
||||
|
||||
@@ -97,3 +97,148 @@ func TestOllamaChatHandlerNonStreamToolCalls(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildOllamaStreamDelta(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
startIndex int
|
||||
wantContent string
|
||||
wantReasoning string
|
||||
wantToolCalls []struct {
|
||||
id string
|
||||
name string
|
||||
index int
|
||||
arguments string
|
||||
}
|
||||
wantNextIndex int
|
||||
wantPayload bool
|
||||
}{
|
||||
{
|
||||
name: "chat content",
|
||||
raw: `{"message":{"content":"hello"}}`,
|
||||
wantContent: "hello",
|
||||
wantPayload: true,
|
||||
},
|
||||
{
|
||||
name: "generate content",
|
||||
raw: `{"response":"hello"}`,
|
||||
wantContent: "hello",
|
||||
wantPayload: true,
|
||||
},
|
||||
{
|
||||
name: "thinking json string",
|
||||
raw: `{"message":{"thinking":"consider this"}}`,
|
||||
wantReasoning: "consider this",
|
||||
wantPayload: true,
|
||||
},
|
||||
{
|
||||
name: "thinking raw fallback",
|
||||
raw: `{"message":{"thinking":{"step":"consider this"}}}`,
|
||||
wantReasoning: `{"step":"consider this"}`,
|
||||
wantPayload: true,
|
||||
},
|
||||
{
|
||||
name: "multiple tool calls",
|
||||
raw: `{"message":{"tool_calls":[{"id":"call_upstream","function":{"name":"get_weather","arguments":{"city":"Paris"}}},{"function":{"name":"get_time","arguments":{"timezone":"UTC"}}}]}}`,
|
||||
startIndex: 2,
|
||||
wantToolCalls: []struct {
|
||||
id string
|
||||
name string
|
||||
index int
|
||||
arguments string
|
||||
}{
|
||||
{id: "call_upstream", name: "get_weather", index: 2, arguments: `{"city":"Paris"}`},
|
||||
{id: "call_3", name: "get_time", index: 3, arguments: `{"timezone":"UTC"}`},
|
||||
},
|
||||
wantNextIndex: 4,
|
||||
wantPayload: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var chunk ollamaChatStreamChunk
|
||||
require.NoError(t, common.Unmarshal([]byte(tt.raw), &chunk))
|
||||
|
||||
toolCallIndex := tt.startIndex
|
||||
delta, hasPayload := buildOllamaStreamDelta(&chunk, "response-id", 123, "model", &toolCallIndex)
|
||||
assert.Equal(t, tt.wantPayload, hasPayload)
|
||||
assert.Equal(t, tt.wantContent, delta.Choices[0].Delta.GetContentString())
|
||||
assert.Equal(t, tt.wantReasoning, delta.Choices[0].Delta.GetReasoningContent())
|
||||
assert.Equal(t, tt.wantNextIndex, toolCallIndex)
|
||||
|
||||
if tt.wantToolCalls == nil {
|
||||
assert.Empty(t, delta.Choices[0].Delta.ToolCalls)
|
||||
return
|
||||
}
|
||||
require.Len(t, delta.Choices[0].Delta.ToolCalls, len(tt.wantToolCalls))
|
||||
for i, want := range tt.wantToolCalls {
|
||||
got := delta.Choices[0].Delta.ToolCalls[i]
|
||||
assert.Equal(t, want.id, got.ID)
|
||||
assert.Equal(t, "function", got.Type)
|
||||
assert.Equal(t, want.name, got.Function.Name)
|
||||
assert.Equal(t, want.arguments, got.Function.Arguments)
|
||||
require.NotNil(t, got.Index)
|
||||
assert.Equal(t, want.index, *got.Index)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOllamaStreamHandlerDoneToolCalls(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(`{"model":"qwen3-coder","created_at":"2026-05-27T12:00:00Z","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_weather","arguments":{"city":"北京"}}},{"function":{"name":"get_time","arguments":{"timezone":"Asia/Shanghai"}}}]},"done":true,"done_reason":"stop","prompt_eval_count":5,"eval_count":7}`)),
|
||||
}
|
||||
|
||||
usage, apiErr := ollamaStreamHandler(c, &relaycommon.RelayInfo{
|
||||
ChannelMeta: &relaycommon.ChannelMeta{UpstreamModelName: "fallback-model"},
|
||||
}, resp)
|
||||
require.Nil(t, apiErr)
|
||||
require.NotNil(t, usage)
|
||||
assert.Equal(t, 12, usage.TotalTokens)
|
||||
|
||||
var chunks []dto.ChatCompletionsStreamResponse
|
||||
for _, line := range strings.Split(w.Body.String(), "\n") {
|
||||
data, ok := strings.CutPrefix(line, "data: ")
|
||||
if !ok || data == "[DONE]" {
|
||||
continue
|
||||
}
|
||||
var chunk dto.ChatCompletionsStreamResponse
|
||||
require.NoError(t, common.Unmarshal([]byte(data), &chunk))
|
||||
chunks = append(chunks, chunk)
|
||||
}
|
||||
|
||||
var toolChunkIndex, stopChunkIndex int = -1, -1
|
||||
for i := range chunks {
|
||||
if len(chunks[i].Choices) == 0 {
|
||||
continue
|
||||
}
|
||||
if len(chunks[i].Choices[0].Delta.ToolCalls) > 0 {
|
||||
toolChunkIndex = i
|
||||
}
|
||||
if chunks[i].Choices[0].FinishReason != nil {
|
||||
stopChunkIndex = i
|
||||
}
|
||||
}
|
||||
require.NotEqual(t, -1, toolChunkIndex)
|
||||
require.NotEqual(t, -1, stopChunkIndex)
|
||||
assert.Less(t, toolChunkIndex, stopChunkIndex)
|
||||
|
||||
toolCalls := chunks[toolChunkIndex].Choices[0].Delta.ToolCalls
|
||||
require.Len(t, toolCalls, 2)
|
||||
assert.Equal(t, "call_0", toolCalls[0].ID)
|
||||
assert.Equal(t, "call_1", toolCalls[1].ID)
|
||||
require.NotNil(t, toolCalls[0].Index)
|
||||
require.NotNil(t, toolCalls[1].Index)
|
||||
assert.Equal(t, 0, *toolCalls[0].Index)
|
||||
assert.Equal(t, 1, *toolCalls[1].Index)
|
||||
assert.Equal(t, constant.FinishReasonToolCalls, *chunks[stopChunkIndex].Choices[0].FinishReason)
|
||||
assert.Contains(t, w.Body.String(), "data: [DONE]")
|
||||
}
|
||||
Reference in new issue
Block a user