From ebab3513c4a715626600008d0d60040b3f237382 Mon Sep 17 00:00:00 2001 From: Haruko386 Date: Tue, 12 May 2026 16:10:32 +0800 Subject: [PATCH] Go: implement provider: Baichuan (#14832) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What problem does this PR solve? This PR completes the Baichuan provider **The following functionalities are now supported:** **Baichuan:** - [x] Chat / Stream Chat - [x] Embedding - [ ] ~~Rerank~~ - [ ] ~~Model listing~~ - [ ] ~~Provider connection checking~~ - [ ] ~~Balance~~ **Verified examples from the CLI:** ```plaintext # Baichuan RAGFlow(user)> embed text 'walkerwhat' 'jumperwho' with 'Baichuan-Text-Embedding@test@baichuan' dimension 16; +-----------+-------+ | dimension | index | +-----------+-------+ | 1024 | 0 | | 1024 | 1 | +-----------+-------+ AGFlow(user)> chat with 'Baichuan-M2@test@baichuan' message 'who r u' Answer: I'm BaiChuan, a helpful AI assistant created by Baichuan-AI. I'm designed to be a knowledgeable, friendly, and reliable assistant for various tasks like answering questions, explaining concepts, writing content, and more. Feel free to ask me anything! 😊 Time: 1.637975 RAGFlow(user)> stream chat with 'Baichuan-M2@test@baichuan' message 'who r u' Answer: I'm BaiChuan-m2, an AI assistant developed by Baichuan-AI. My purpose is to help you with a wide range of tasks by providing information, answering questions, solving problems, and assisting with creative projects. Think of me as a helpful digital companion! If you have any questions or need assistance, just let me know.😊 Time: 1.692321 ``` ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Refactoring --- conf/models/baichuan.json | 90 +++++++ internal/entity/models/baichuan.go | 393 ++++++++++++++++++++++++++++ internal/entity/models/cohere.go | 9 - internal/entity/models/factory.go | 2 + internal/entity/models/fishaudio.go | 1 + 5 files changed, 486 insertions(+), 9 deletions(-) create mode 100644 conf/models/baichuan.json create mode 100644 internal/entity/models/baichuan.go diff --git a/conf/models/baichuan.json b/conf/models/baichuan.json new file mode 100644 index 0000000000..c7bc5f1c0d --- /dev/null +++ b/conf/models/baichuan.json @@ -0,0 +1,90 @@ +{ + "name": "Baichuan", + "url": { + "default": "https://api.baichuan-ai.com/v1" + }, + "url_suffix": { + "chat": "chat/completions", + "embedding": "embeddings" + }, + "class": "baichuan", + "models": [ + { + "name": "Baichuan4", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan4-Air", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan4-Turbo", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan-M3", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan-M3-plus", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan-M2-plus", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan-M2", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan3-Turbo", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan3-Turbo-128k", + "max_tokens": 131072, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan2-Turbo", + "max_tokens": 32768, + "model_types": [ + "chat" + ] + }, + { + "name": "Baichuan-Text-Embedding", + "max_tokens": 8192, + "model_types": [ + "embedding" + ] + } + ] +} \ No newline at end of file diff --git a/internal/entity/models/baichuan.go b/internal/entity/models/baichuan.go new file mode 100644 index 0000000000..5a8282164a --- /dev/null +++ b/internal/entity/models/baichuan.go @@ -0,0 +1,393 @@ +package models + +import ( + "bufio" + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "ragflow/internal/common" + "strings" + "time" +) + +// sk-6e16f0a6bfaa7fc58e30a50962665d1d +type BaichuanModel struct { + BaseURL map[string]string + URLSuffix URLSuffix + httpClient *http.Client +} + +func NewBaichuanModel(baseURL map[string]string, urlSuffix URLSuffix) *BaichuanModel { + return &BaichuanModel{ + BaseURL: baseURL, + URLSuffix: urlSuffix, + httpClient: &http.Client{ + Timeout: 120 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 100, + MaxIdleConnsPerHost: 10, + IdleConnTimeout: 90 * time.Second, + DisableCompression: false, + }, + }, + } +} + +func (b *BaichuanModel) NewInstance(baseURL map[string]string) ModelDriver { + return &BaichuanModel{ + BaseURL: baseURL, + URLSuffix: b.URLSuffix, + httpClient: &http.Client{ + Timeout: 120 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 100, + MaxIdleConnsPerHost: 10, + IdleConnTimeout: 90 * time.Second, + DisableCompression: false, + }, + }, + } +} + +func (b *BaichuanModel) Name() string { + return "baichuan" +} + +func (b *BaichuanModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) { + if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" { + return nil, fmt.Errorf("api key is nil or empty") + } + if len(messages) == 0 { + return nil, fmt.Errorf("messages is empty") + } + + var region = "default" + if apiConfig.Region != nil && *apiConfig.Region != "" { + region = *apiConfig.Region + } + + url := fmt.Sprintf("%s/%s", b.BaseURL[region], b.URLSuffix.Chat) + + // Convert messages to API format + apiMessages := make([]map[string]interface{}, len(messages)) + for i, msg := range messages { + apiMessages[i] = map[string]interface{}{ + "role": msg.Role, + "content": msg.Content, + } + } + + // Build request body + reqBody := map[string]interface{}{ + "model": modelName, + "messages": apiMessages, + "stream": false, + "temperature": 1, + } + + if chatModelConfig != nil { + if chatModelConfig.Temperature != nil { + reqBody["temperature"] = *chatModelConfig.Temperature + } + + if chatModelConfig.MaxTokens != nil { + reqBody["max_tokens"] = *chatModelConfig.MaxTokens + } + + if chatModelConfig.Stream != nil { + reqBody["stream"] = *chatModelConfig.Stream + } + + if chatModelConfig.TopP != nil { + reqBody["top_p"] = *chatModelConfig.TopP + } + } + + jsonData, err := json.Marshal(reqBody) + if err != nil { + return nil, fmt.Errorf("failed to marshal request: %w", err) + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey)) + + resp, err := b.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 body: %w", err) + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to send request: %d %s", resp.StatusCode, string(body)) + } + + // Parse response + var result map[string]interface{} + if err := json.Unmarshal(body, &result); err != nil { + return nil, fmt.Errorf("failed to unmarshal response: %w", err) + } + + choices, ok := result["choices"].([]interface{}) + if !ok { + return nil, fmt.Errorf("no choices in response") + } + + firstChoice, ok := choices[0].(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("no choices in response") + } + + messageMap, ok := firstChoice["message"].(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("no message in response") + } + + content, ok := messageMap["content"].(string) + if !ok { + return nil, fmt.Errorf("no message in response") + } + + // baichuan not support think + emptyReason := "" + chatResponse := &ChatResponse{ + Answer: &content, + ReasonContent: &emptyReason, + } + + return chatResponse, nil +} + +func (b *BaichuanModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error { + if len(messages) == 0 { + return fmt.Errorf("messages is empty") + } + + var region = "default" + if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { + region = *apiConfig.Region + } + + url := fmt.Sprintf("%s/%s", b.BaseURL[region], b.URLSuffix.Chat) + + // Convert messages to API format + apiMessages := make([]map[string]interface{}, len(messages)) + for i, msg := range messages { + apiMessages[i] = map[string]interface{}{ + "role": msg.Role, + "content": msg.Content, + } + } + + reqBody := map[string]interface{}{ + "model": modelName, + "messages": apiMessages, + "stream": true, + "temperature": 1, + } + + if modelConfig != nil { + if modelConfig.Stream != nil { + reqBody["stream"] = *modelConfig.Stream + } + + if modelConfig.MaxTokens != nil { + reqBody["max_tokens"] = *modelConfig.MaxTokens + } + + if modelConfig.Temperature != nil { + reqBody["temperature"] = *modelConfig.Temperature + } + + if modelConfig.TopP != nil { + reqBody["top_p"] = *modelConfig.TopP + } + + if modelConfig.Stop != nil { + reqBody["stop"] = *modelConfig.Stop + } + } + + jsonData, err := json.Marshal(reqBody) + if err != nil { + return fmt.Errorf("failed to marshal request: %w", err) + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey)) + + resp, err := b.httpClient.Do(req) + if err != nil { + return fmt.Errorf("failed to send request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("invalid status code: %d, body: %s", resp.StatusCode, string(body)) + } + + // SSE parsing: read line by line + scanner := bufio.NewScanner(resp.Body) + for scanner.Scan() { + line := scanner.Text() + common.Info(line) + + // SSE data line starts with "data:" + if !strings.HasPrefix(line, "data:") { + continue + } + + // Extract JSON after "data:" + data := strings.TrimSpace(line[5:]) + + // [DONE] marks the end of stream + if data == "[DONE]" { + break + } + + // Parse the JSON event + var event map[string]interface{} + if err = json.Unmarshal([]byte(data), &event); err != nil { + continue + } + + choices, ok := event["choices"].([]interface{}) + if !ok || len(choices) == 0 { + continue + } + + firstChoice, ok := choices[0].(map[string]interface{}) + if !ok { + continue + } + + delta, ok := firstChoice["delta"].(map[string]interface{}) + if !ok { + continue + } + + content, ok := delta["content"].(string) + if ok && content != "" { + if err := sender(&content, nil); err != nil { + return err + } + } + + finishReason, ok := firstChoice["finish_reason"].(string) + if ok && finishReason != "" { + break + } + } + + // Send [DONE] marker for OpenAI compatibility + endOfStream := "[DONE]" + if err = sender(&endOfStream, nil); err != nil { + return err + } + + return scanner.Err() +} + +func (b *BaichuanModel) Embed(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([]EmbeddingData, error) { + if len(texts) == 0 { + return []EmbeddingData{}, nil + } + + var region = "default" + if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { + region = *apiConfig.Region + } + + url := fmt.Sprintf("%s/%s", b.BaseURL[region], b.URLSuffix.Embedding) + + reqBody := map[string]interface{}{ + "model": *modelName, + "input": texts, + } + + jsonData, err := json.Marshal(reqBody) + if err != nil { + return nil, fmt.Errorf("failed to marshal request: %w", err) + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strings.TrimSpace(*apiConfig.ApiKey))) + + resp, err := b.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("Baichuan embedding API error: status %d, body: %s", resp.StatusCode, string(body)) + } + + var parsedResponse struct { + Data []struct { + Embedding []float64 `json:"embedding"` + Index int `json:"index"` + } `json:"data"` + } + + if err = json.Unmarshal(body, &parsedResponse); err != nil { + return nil, fmt.Errorf("failed to decode response: %w", err) + } + + if len(parsedResponse.Data) == 0 { + return nil, fmt.Errorf("Baichuan embedding response contains no data: %s", string(body)) + } + + var embeddings []EmbeddingData + for _, dataElem := range parsedResponse.Data { + embeddings = append(embeddings, EmbeddingData{ + Embedding: dataElem.Embedding, + Index: dataElem.Index, + }) + } + + return embeddings, nil +} + +func (b *BaichuanModel) Rerank(modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig) (*RerankResponse, error) { + return nil, fmt.Errorf("no such method") +} + +func (b *BaichuanModel) ListModels(apiConfig *APIConfig) ([]string, error) { + return nil, fmt.Errorf("no such method") +} + +func (b *BaichuanModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) { + return nil, fmt.Errorf("no such method") +} + +func (b *BaichuanModel) CheckConnection(apiConfig *APIConfig) error { + return fmt.Errorf("no such method") +} diff --git a/internal/entity/models/cohere.go b/internal/entity/models/cohere.go index 6a653ec7cc..f327400676 100644 --- a/internal/entity/models/cohere.go +++ b/internal/entity/models/cohere.go @@ -340,9 +340,6 @@ func (c *CoHereModel) Embed(modelName *string, texts []string, apiConfig *APICon baseURL := strings.TrimSuffix(c.BaseURL[region], "/") suffix := strings.TrimPrefix(c.URLSuffix.Embedding, "/") - if suffix == "" { - suffix = "v2/embed" - } url := fmt.Sprintf("%s/%s", baseURL, suffix) reqBody := map[string]interface{}{ @@ -417,9 +414,6 @@ func (c *CoHereModel) Rerank(modelName *string, query string, documents []string baseURL := strings.TrimSuffix(c.BaseURL[region], "/") suffix := strings.TrimPrefix(c.URLSuffix.Rerank, "/") - if suffix == "" { - suffix = "v2/rerank" - } url := fmt.Sprintf("%s/%s", baseURL, suffix) var topN = rerankConfig.TopN @@ -500,9 +494,6 @@ func (c *CoHereModel) ListModels(apiConfig *APIConfig) ([]string, error) { baseURL = "https://api.cohere.com" } suffix := c.URLSuffix.Models - if suffix == "" { - suffix = "v1/models" - } url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), strings.TrimPrefix(suffix, "/")) req, err := http.NewRequest("GET", url, nil) diff --git a/internal/entity/models/factory.go b/internal/entity/models/factory.go index f0974635b9..03a33aaacb 100644 --- a/internal/entity/models/factory.go +++ b/internal/entity/models/factory.go @@ -75,6 +75,8 @@ func (f *ModelFactory) CreateModelDriver(providerName string, baseURL map[string return NewFishAudioModel(baseURL, urlSuffix), nil case "stepfun": return NewStepFunModel(baseURL, urlSuffix), nil + case "baichuan": + return NewBaichuanModel(baseURL, urlSuffix), nil default: return NewDummyModel(baseURL, urlSuffix), nil } diff --git a/internal/entity/models/fishaudio.go b/internal/entity/models/fishaudio.go index c618ef7790..d767816006 100644 --- a/internal/entity/models/fishaudio.go +++ b/internal/entity/models/fishaudio.go @@ -26,6 +26,7 @@ func NewFishAudioModel(baseURL map[string]string, urlSuffix URLSuffix) *FishAudi }, } } + func (f *FishAudioModel) NewInstance(baseURL map[string]string) ModelDriver { return &FishAudioModel{ BaseURL: baseURL,