feat(models): add max concurrency setting for background tasks
Introduce a new `max_concurrency` parameter to the model configuration, allowing control over the maximum number of concurrent background tasks (ingestion/enrichment) per model. This feature is applicable to chat, embedding, and VLM models, with a value of 0 or undefined falling back to the global default. Update UI components and localization files to support this new setting, ensuring a seamless user experience.
This commit is contained in:
@@ -99,13 +99,16 @@ type Chat interface {
|
||||
}
|
||||
|
||||
type ChatConfig struct {
|
||||
Source types.ModelSource
|
||||
BaseURL string
|
||||
ModelName string
|
||||
APIKey string
|
||||
ModelID string
|
||||
Provider string
|
||||
ExtraConfig map[string]string
|
||||
Source types.ModelSource
|
||||
BaseURL string
|
||||
ModelName string
|
||||
APIKey string
|
||||
ModelID string
|
||||
Provider string
|
||||
// MaxConcurrency caps concurrent background calls to this model; 0 falls
|
||||
// back to the process-wide default (see limiter.GateN).
|
||||
MaxConcurrency int
|
||||
ExtraConfig map[string]string
|
||||
// CustomHeaders 允许在调用远程 OpenAI 兼容 API 时附加自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
|
||||
CustomHeaders map[string]string
|
||||
AppID string
|
||||
@@ -121,16 +124,17 @@ func ConfigFromModel(m *types.Model, appID, appSecret string) *ChatConfig {
|
||||
return nil
|
||||
}
|
||||
return &ChatConfig{
|
||||
ModelID: m.ID,
|
||||
APIKey: m.Parameters.APIKey,
|
||||
BaseURL: m.Parameters.BaseURL,
|
||||
ModelName: m.Name,
|
||||
Source: m.Source,
|
||||
Provider: m.Parameters.Provider,
|
||||
ExtraConfig: m.Parameters.ExtraConfig,
|
||||
CustomHeaders: m.Parameters.CustomHeaders,
|
||||
AppID: appID,
|
||||
AppSecret: appSecret,
|
||||
ModelID: m.ID,
|
||||
APIKey: m.Parameters.APIKey,
|
||||
BaseURL: m.Parameters.BaseURL,
|
||||
ModelName: m.Name,
|
||||
Source: m.Source,
|
||||
Provider: m.Parameters.Provider,
|
||||
MaxConcurrency: m.Parameters.MaxConcurrency,
|
||||
ExtraConfig: m.Parameters.ExtraConfig,
|
||||
CustomHeaders: m.Parameters.CustomHeaders,
|
||||
AppID: appID,
|
||||
AppSecret: appSecret,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +154,7 @@ func NewChat(config *ChatConfig, ollamaService *ollama.OllamaService) (Chat, err
|
||||
c, err = wrapChatLangfuse(c, err)
|
||||
// Outermost: hold the per-model concurrency slot only around the real
|
||||
// provider round-trip, so the wait is excluded from debug/langfuse timing.
|
||||
return wrapChatConcurrency(c, err)
|
||||
return wrapChatConcurrency(c, config.MaxConcurrency, err)
|
||||
}
|
||||
|
||||
// NewRemoteChat 根据 provider 创建远程聊天实例。
|
||||
|
||||
@@ -26,19 +26,22 @@ import (
|
||||
// inner debug/langfuse timing.
|
||||
type concurrencyChat struct {
|
||||
inner Chat
|
||||
// limit is this model's configured per-model background cap; 0 falls back
|
||||
// to the process-wide default (see limiter.GateN).
|
||||
limit int
|
||||
}
|
||||
|
||||
func (w *concurrencyChat) GetModelName() string { return w.inner.GetModelName() }
|
||||
func (w *concurrencyChat) GetModelID() string { return w.inner.GetModelID() }
|
||||
|
||||
func (w *concurrencyChat) Chat(ctx context.Context, messages []Message, opts *ChatOptions) (*types.ChatResponse, error) {
|
||||
release := limiter.Gate(ctx, w.inner.GetModelID())
|
||||
release := limiter.GateN(ctx, w.inner.GetModelID(), w.limit)
|
||||
defer release()
|
||||
return w.inner.Chat(ctx, messages, opts)
|
||||
}
|
||||
|
||||
func (w *concurrencyChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) {
|
||||
release := limiter.Gate(ctx, w.inner.GetModelID())
|
||||
release := limiter.GateN(ctx, w.inner.GetModelID(), w.limit)
|
||||
ch, err := w.inner.ChatStream(ctx, messages, opts)
|
||||
if err != nil || ch == nil {
|
||||
release()
|
||||
@@ -71,9 +74,9 @@ func (w *concurrencyChat) ChatStream(ctx context.Context, messages []Message, op
|
||||
// wrapChatConcurrency installs the background concurrency governor as the
|
||||
// outermost Chat decorator. It is always applied; when no limiter is installed
|
||||
// or the call is interactive, the wrapper is a cheap passthrough.
|
||||
func wrapChatConcurrency(c Chat, err error) (Chat, error) {
|
||||
func wrapChatConcurrency(c Chat, limit int, err error) (Chat, error) {
|
||||
if err != nil || c == nil {
|
||||
return c, err
|
||||
}
|
||||
return &concurrencyChat{inner: c}, nil
|
||||
return &concurrencyChat{inner: c, limit: limit}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user