Refactor[Go Model Provider]: refactor baseURL and modelConfig (#15627)

### What problem does this PR solve?

As Title

### Type of change

- [x] Refactoring
This commit is contained in:
Haruko386
2026-06-04 17:50:22 +08:00
committed by GitHub
parent 04dc3bb19c
commit baeb0c0431
65 changed files with 2834 additions and 4410 deletions

View File

@@ -30,25 +30,11 @@ import (
)
// modelscopeStreamIdleTimeout bounds how long a stream can go without
// receiving any SSE line. Self-hosted ModelScope deployments can be slow,
// but a stream that stays silent for a full minute is more useful as a
// surfaced error than as a stuck goroutine.
var modelscopeStreamIdleTimeout = 60 * time.Second
// ModelScopeModel implements ModelDriver for ModelScope chat models.
//
// ModelScope exposes an OpenAI-compatible API under <endpoint>/v1.
// The tenant supplies the deployment endpoint (no default — matches the
// Python ModelScopeChat at rag/llm/chat_model.py which raises on a
// missing base URL). Both the root endpoint and the OpenAI-compatible
// endpoint (.../v1) are accepted; the driver normalizes both to the root
// before appending URLSuffix values like v1/chat/completions.
// Authentication is optional: deployments without auth ignore API keys,
// while auth-enabled deployments require Authorization: Bearer <api_key>.
type ModelScopeModel struct {
BaseURL map[string]string
URLSuffix URLSuffix
httpClient *http.Client
baseModel BaseModel
}
type modelscopeChatChoice struct {
@@ -88,32 +74,24 @@ func NewModelScopeModel(baseURL map[string]string, urlSuffix URLSuffix) *ModelSc
transport.ResponseHeaderTimeout = 60 * time.Second
return &ModelScopeModel{
BaseURL: baseURL,
URLSuffix: urlSuffix,
httpClient: &http.Client{
Transport: transport,
baseModel: BaseModel{
BaseURL: baseURL,
URLSuffix: urlSuffix,
httpClient: &http.Client{
Transport: transport,
},
},
}
}
func (m *ModelScopeModel) NewInstance(baseURL map[string]string) ModelDriver {
return NewModelScopeModel(baseURL, m.URLSuffix)
return NewModelScopeModel(baseURL, m.baseModel.URLSuffix)
}
func (m *ModelScopeModel) Name() string {
return "modelscope"
}
func (m *ModelScopeModel) baseURLForRegion(region string) (string, error) {
if base, ok := m.BaseURL[region]; ok && strings.TrimSpace(base) != "" {
return normalizeModelScopeBaseURL(base), nil
}
if base, ok := m.BaseURL["default"]; ok && strings.TrimSpace(base) != "" {
return normalizeModelScopeBaseURL(base), nil
}
return "", fmt.Errorf("modelscope: missing base URL, configure the ModelScope endpoint (e.g., http://127.0.0.1:8000 or http://127.0.0.1:8000/v1)")
}
func normalizeModelScopeBaseURL(base string) string {
trimmed := strings.TrimRight(strings.TrimSpace(base), "/")
if trimmed == "" {
@@ -125,20 +103,6 @@ func normalizeModelScopeBaseURL(base string) string {
return trimmed
}
func setModelScopeAuth(req *http.Request, apiConfig *APIConfig) {
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
return
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
}
func modelscopeRegion(apiConfig *APIConfig) string {
if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
return *apiConfig.Region
}
return "default"
}
func modelscopeReasoningFromStrings(reasoningContent string, reasoning string, thinking string) string {
switch {
case reasoningContent != "":
@@ -196,15 +160,20 @@ func buildModelScopeChatBody(modelName string, messages []Message, stream bool,
// ChatWithMessages sends multiple messages with roles and returns the response.
func (m *ModelScopeModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
return nil, err
}
if len(messages) == 0 {
return nil, fmt.Errorf("messages is empty")
}
baseURL, err := m.baseURLForRegion(modelscopeRegion(apiConfig))
baseURL, err := m.baseModel.GetBaseURL(apiConfig)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/%s", baseURL, m.URLSuffix.Chat)
baseURL = normalizeModelScopeBaseURL(baseURL)
url := fmt.Sprintf("%s/%s", baseURL, m.baseModel.URLSuffix.Chat)
reqBody := buildModelScopeChatBody(modelName, messages, false, chatModelConfig)
jsonData, err := json.Marshal(reqBody)
@@ -220,9 +189,9 @@ func (m *ModelScopeModel) ChatWithMessages(modelName string, messages []Message,
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
setModelScopeAuth(req, apiConfig)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
resp, err := m.httpClient.Do(req)
resp, err := m.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -259,6 +228,10 @@ func (m *ModelScopeModel) ChatWithMessages(modelName string, messages []Message,
// ChatStreamlyWithSender sends messages and streams response via sender.
func (m *ModelScopeModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, sender func(*string, *string) error) error {
if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
return err
}
if sender == nil {
return fmt.Errorf("sender is required")
}
@@ -269,11 +242,12 @@ func (m *ModelScopeModel) ChatStreamlyWithSender(modelName string, messages []Me
return fmt.Errorf("stream must be true in ChatStreamlyWithSender")
}
baseURL, err := m.baseURLForRegion(modelscopeRegion(apiConfig))
baseURL, err := m.baseModel.GetBaseURL(apiConfig)
if err != nil {
return err
}
url := fmt.Sprintf("%s/%s", baseURL, m.URLSuffix.Chat)
baseURL = normalizeModelScopeBaseURL(baseURL)
url := fmt.Sprintf("%s/%s", baseURL, m.baseModel.URLSuffix.Chat)
reqBody := buildModelScopeChatBody(modelName, messages, true, chatModelConfig)
jsonData, err := json.Marshal(reqBody)
@@ -289,9 +263,9 @@ func (m *ModelScopeModel) ChatStreamlyWithSender(modelName string, messages []Me
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
setModelScopeAuth(req, apiConfig)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
resp, err := m.httpClient.Do(req)
resp, err := m.baseModel.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
@@ -424,11 +398,16 @@ func (m *ModelScopeModel) ParseFile(modelName *string, content []byte, url *stri
// ListModels returns the model IDs exposed by ModelScope's OpenAI-compatible
// /v1/models endpoint.
func (m *ModelScopeModel) ListModels(apiConfig *APIConfig) ([]string, error) {
baseURL, err := m.baseURLForRegion(modelscopeRegion(apiConfig))
if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
return nil, err
}
baseURL, err := m.baseModel.GetBaseURL(apiConfig)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/%s", baseURL, m.URLSuffix.Models)
baseURL = normalizeModelScopeBaseURL(baseURL)
url := fmt.Sprintf("%s/%s", baseURL, m.baseModel.URLSuffix.Models)
ctx, cancel := context.WithTimeout(context.Background(), nonStreamCallTimeout)
defer cancel()
@@ -437,9 +416,11 @@ func (m *ModelScopeModel) ListModels(apiConfig *APIConfig) ([]string, error) {
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
setModelScopeAuth(req, apiConfig)
resp, err := m.httpClient.Do(req)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
resp, err := m.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}