mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-05 07:10:29 +08:00
Relate to #17284. ## Summary Batch 5/6 migrated the rest of the Go model drivers onto the shared HTTP helpers (`doRequest`, `doStreamRequest`, `applyAuth`). This PR completes the batch for the remaining OpenAI-compatible chat-streaming drivers that were still hand-writing HTTP requests: - **7 drop-in migrations**: deepseek, gpustack, groq, longcat, moonshot, openai, siliconflow - **1 adapter migration**: minimax (relocated its `io.Pipe` error-interception into the `doStreamRequest` handler) - **1 full migration**: azure_openai (all four paths: chat, streaming, embeddings, list-models) plus the auth header hook - **1 receiver fix**: nvidia `NewInstance` value → pointer --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
328 lines
11 KiB
Go
328 lines
11 KiB
Go
//
|
|
// 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"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"ragflow/internal/common"
|
|
"strings"
|
|
)
|
|
|
|
// GPUStackModel implements ModelDriver for GPUStack
|
|
type GPUStackModel struct {
|
|
baseModel BaseModel
|
|
}
|
|
|
|
func NewGPUStackModel(baseURL map[string]string, urlSuffix URLSuffix) *GPUStackModel {
|
|
return &GPUStackModel{
|
|
baseModel: BaseModel{
|
|
BaseURL: baseURL,
|
|
URLSuffix: urlSuffix,
|
|
AllowEmptyAPIKey: true,
|
|
httpClient: NewDriverHTTPClient(true),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (g *GPUStackModel) NewInstance(baseURL map[string]string) ModelDriver {
|
|
return NewGPUStackModel(baseURL, g.baseModel.URLSuffix)
|
|
}
|
|
|
|
func (g *GPUStackModel) Name() string {
|
|
return "gpustack"
|
|
}
|
|
|
|
// ChatWithMessages sends multiple messages and returns the response.
|
|
func (g *GPUStackModel) ChatWithMessages(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, modelUsage *common.ModelUsage) (*ChatResponse, error) {
|
|
if err := g.baseModel.APIConfigCheck(apiConfig); err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.TrimSpace(modelName) == "" {
|
|
return nil, fmt.Errorf("model name is required")
|
|
}
|
|
if len(messages) == 0 {
|
|
return nil, fmt.Errorf("messages is empty")
|
|
}
|
|
|
|
baseURL, err := g.baseModel.GetBaseURL(apiConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
baseURL = strings.TrimSuffix(baseURL, "/")
|
|
url := fmt.Sprintf("%s/%s", baseURL, g.baseModel.URLSuffix.Chat)
|
|
reqBody := buildRequestBody(chatModelConfig, modelName, messages, false)
|
|
|
|
body, err := g.baseModel.doRequest(ctx, url, apiConfig, reqBody, nonStreamCallTimeout)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return HandleNonStreamingResponse(body, modelUsage, chatModelConfig, OpenAIParserConfig)
|
|
}
|
|
|
|
// ChatStreamlyWithSender streams the response via the sender.
|
|
func (g *GPUStackModel) ChatStreamlyWithSender(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
|
|
if err := g.baseModel.APIConfigCheck(apiConfig); err != nil {
|
|
return err
|
|
}
|
|
|
|
if sender == nil {
|
|
return fmt.Errorf("sender is required")
|
|
}
|
|
if strings.TrimSpace(modelName) == "" {
|
|
return fmt.Errorf("model name is required")
|
|
}
|
|
if len(messages) == 0 {
|
|
return fmt.Errorf("messages is empty")
|
|
}
|
|
if err := validateStreamConfig(chatModelConfig); err != nil {
|
|
return err
|
|
}
|
|
|
|
baseURL, err := g.baseModel.GetBaseURL(apiConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
baseURL = strings.TrimSuffix(baseURL, "/")
|
|
url := fmt.Sprintf("%s/%s", baseURL, g.baseModel.URLSuffix.Chat)
|
|
reqBody := buildRequestBody(chatModelConfig, modelName, messages, true)
|
|
reqBody["stream_options"] = map[string]any{"include_usage": true}
|
|
|
|
return g.baseModel.doStreamRequest(ctx, url, apiConfig, reqBody, streamCallTimeout, func(body io.ReadCloser) error {
|
|
return HandleStreamingResponse(body, modelUsage, chatModelConfig, OpenAIParserConfig, sender)
|
|
})
|
|
}
|
|
|
|
type gpustackModelInfo struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
type gpustackModelsResponse struct {
|
|
Data []ModelListItem `json:"data"`
|
|
}
|
|
|
|
func (g *GPUStackModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {
|
|
if err := g.baseModel.APIConfigCheck(apiConfig); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
baseURL, err := g.baseModel.GetBaseURL(apiConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
baseURL = strings.TrimSuffix(baseURL, "/")
|
|
url := fmt.Sprintf("%s/%s", baseURL, g.baseModel.URLSuffix.Models)
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if auth := BearerAuth(apiConfig); auth != "" {
|
|
req.Header.Set("Authorization", auth)
|
|
}
|
|
|
|
resp, err := g.baseModel.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
var parsed gpustackModelsResponse
|
|
if err = json.Unmarshal(body, &parsed); err != nil {
|
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
}
|
|
|
|
return ParseListModel(ModelList{Models: parsed.Data}), nil
|
|
}
|
|
|
|
func (g *GPUStackModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {
|
|
_, err := g.ListModels(ctx, apiConfig)
|
|
return err
|
|
}
|
|
|
|
// gpustackEmbeddingData is one element in a GPUStack embeddings response.
|
|
type gpustackEmbeddingData struct {
|
|
Embedding []float64 `json:"embedding"`
|
|
Index *int `json:"index"`
|
|
}
|
|
|
|
// gpustackEmbeddingResponse is the JSON body returned by GPUStack embeddings API.
|
|
type gpustackEmbeddingResponse struct {
|
|
Data []gpustackEmbeddingData `json:"data"`
|
|
}
|
|
|
|
// Embed requests embedding vectors via GPUStack's v1-openai/embeddings endpoint.
|
|
func (g *GPUStackModel) Embed(
|
|
ctx context.Context,
|
|
modelName *string,
|
|
texts []string,
|
|
apiConfig *APIConfig,
|
|
embeddingConfig *EmbeddingConfig,
|
|
modelUsage *common.ModelUsage,
|
|
) ([]EmbeddingData, error) {
|
|
if err := g.baseModel.APIConfigCheck(apiConfig); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(texts) == 0 {
|
|
return []EmbeddingData{}, nil
|
|
}
|
|
if modelName == nil || strings.TrimSpace(*modelName) == "" {
|
|
return nil, fmt.Errorf("model name is required")
|
|
}
|
|
|
|
baseURL, err := g.baseModel.GetBaseURL(apiConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
baseURL = strings.TrimSuffix(baseURL, "/")
|
|
if g.baseModel.URLSuffix.Embedding == "" {
|
|
return nil, fmt.Errorf("gpustack: embedding URL suffix is not configured")
|
|
}
|
|
url := fmt.Sprintf("%s/%s", baseURL, strings.TrimPrefix(g.baseModel.URLSuffix.Embedding, "/"))
|
|
|
|
reqBody := map[string]interface{}{
|
|
"model": *modelName,
|
|
"input": texts,
|
|
}
|
|
if embeddingConfig != nil && embeddingConfig.Dimension > 0 {
|
|
reqBody["dimensions"] = embeddingConfig.Dimension
|
|
}
|
|
|
|
jsonData, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if auth := BearerAuth(apiConfig); auth != "" {
|
|
req.Header.Set("Authorization", auth)
|
|
}
|
|
|
|
resp, err := g.baseModel.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("gpustack embeddings API error: %s, body: %s", resp.Status, string(body))
|
|
}
|
|
|
|
var parsed gpustackEmbeddingResponse
|
|
if err = json.Unmarshal(body, &parsed); err != nil {
|
|
return nil, fmt.Errorf("failed to parse response: %w", err)
|
|
}
|
|
|
|
embeddings := make([]EmbeddingData, len(texts))
|
|
filled := make([]bool, len(texts))
|
|
for _, item := range parsed.Data {
|
|
if item.Index == nil {
|
|
return nil, fmt.Errorf("gpustack: missing embedding index in response item")
|
|
}
|
|
idx := *item.Index
|
|
if idx < 0 || idx >= len(texts) {
|
|
return nil, fmt.Errorf("gpustack: embedding response index %d out of range for %d inputs", idx, len(texts))
|
|
}
|
|
if filled[idx] {
|
|
return nil, fmt.Errorf("gpustack: duplicate embedding index %d in response", idx)
|
|
}
|
|
if len(item.Embedding) == 0 {
|
|
return nil, fmt.Errorf("gpustack: empty embedding vector for input index %d", idx)
|
|
}
|
|
embeddings[idx] = EmbeddingData{
|
|
Embedding: item.Embedding,
|
|
Index: idx,
|
|
}
|
|
filled[idx] = true
|
|
}
|
|
for i, ok := range filled {
|
|
if !ok {
|
|
return nil, fmt.Errorf("gpustack: missing embedding for input index %d", i)
|
|
}
|
|
}
|
|
return embeddings, nil
|
|
}
|
|
|
|
func (g *GPUStackModel) Rerank(ctx context.Context, modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig, modelUsage *common.ModelUsage) (*RerankResponse, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[string]interface{}, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) TranscribeAudio(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage) (*ASRResponse, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) TranscribeAudioWithSender(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
|
|
return fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) AudioSpeech(ctx context.Context, modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, modelUsage *common.ModelUsage) (*TTSResponse, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) AudioSpeechWithSender(ctx context.Context, modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
|
|
return fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) ParseFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig, modelUsage *common.ModelUsage) (*ParseFileResponse, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) ListTasks(ctx context.Context, apiConfig *APIConfig) ([]ListTaskStatus, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|
|
|
|
func (g *GPUStackModel) ShowTask(ctx context.Context, taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
|
|
return nil, fmt.Errorf("%s, no such method", g.Name())
|
|
}
|