mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-05 23:24:05 +08:00
feat(go-models): migrate batch 5 model drivers to unified handlers (#17700)
## Summary Relate to #17284. Completes the batch 5 migration of 7 OpenAI-compatible drivers (`vllm`, `volcengine`, `xai`, `xiaomi`, `xinference`, `xunfei`, `zhipu-ai`) onto the unified request/response helpers (`doRequest`/`doStreamRequest` + `HandleNonStreamingResponse`/`HandleStreamingResponse` + `ParserConfig`), established by `deepseek` in #17634. This branch is rebased on the current `pr/migrate-models-batch5` and fixes the issues in the previous state of the PR. Co-authored-by: Haruko386 <tryeverypossible@163.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
81
internal/entity/models/response_handler_test.go
Normal file
81
internal/entity/models/response_handler_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestHandleStreamingResponseBareRootFinishReason verifies the shared handler
|
||||
// terminates the stream when an event carries only a root-level finish_reason
|
||||
// with no choices and no trailing [DONE]. Before the fix this event was
|
||||
// skipped by the choices guard and the handler returned "stream ended before
|
||||
// [DONE] or finish_reason".
|
||||
func TestHandleStreamingResponseBareRootFinishReason(t *testing.T) {
|
||||
sse := "data: {\"finish_reason\":\"stop\"}\n"
|
||||
|
||||
var contentChunks, reasonChunks []string
|
||||
err := HandleStreamingResponse(
|
||||
io.NopCloser(bytes.NewBufferString(sse)),
|
||||
nil,
|
||||
nil,
|
||||
OpenAIParserConfig,
|
||||
func(content *string, reason *string) error {
|
||||
if content != nil && *content != "" {
|
||||
contentChunks = append(contentChunks, *content)
|
||||
}
|
||||
if reason != nil && *reason != "" {
|
||||
reasonChunks = append(reasonChunks, *reason)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("HandleStreamingResponse: %v", err)
|
||||
}
|
||||
|
||||
// The terminal [DONE] marker is the only content the handler emits for a
|
||||
// finish_reason-only event: no tokens arrived and no reasoning was sent.
|
||||
if got := strings.Join(contentChunks, ""); got != "[DONE]" {
|
||||
t.Errorf("content=%q, want only the [DONE] marker", got)
|
||||
}
|
||||
if len(reasonChunks) != 0 {
|
||||
t.Errorf("reasoning=%q, want none for a finish_reason-only event", strings.Join(reasonChunks, ""))
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleStreamingResponseTruncatedStream verifies a stream that ends
|
||||
// without [DONE], a root-level finish_reason, or a per-choice finish_reason
|
||||
// is rejected as truncated. This is the counterpart of the bare
|
||||
// finish_reason case: only a genuine truncation must fail.
|
||||
func TestHandleStreamingResponseTruncatedStream(t *testing.T) {
|
||||
sse := "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n"
|
||||
|
||||
err := HandleStreamingResponse(
|
||||
io.NopCloser(bytes.NewBufferString(sse)),
|
||||
nil,
|
||||
nil,
|
||||
OpenAIParserConfig,
|
||||
func(*string, *string) error { return nil },
|
||||
)
|
||||
if err == nil || !strings.Contains(err.Error(), "stream ended before [DONE] or finish_reason") {
|
||||
t.Fatalf("expected truncation error, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user