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:
wizardchen
2026-07-09 19:56:43 +08:00
committed by lyingbug
parent 200c344065
commit 20256287ec
17 changed files with 173 additions and 55 deletions
+6 -3
View File
@@ -13,13 +13,16 @@ import (
// worker) calls are throttled — see limiter.Gate / types.IsBackgroundTask.
type concurrencyVLM struct {
inner VLM
// 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 *concurrencyVLM) GetModelName() string { return w.inner.GetModelName() }
func (w *concurrencyVLM) GetModelID() string { return w.inner.GetModelID() }
func (w *concurrencyVLM) Predict(ctx context.Context, imgBytes [][]byte, prompt string) (string, error) {
release := limiter.Gate(ctx, w.inner.GetModelID())
release := limiter.GateN(ctx, w.inner.GetModelID(), w.limit)
defer release()
return w.inner.Predict(ctx, imgBytes, prompt)
}
@@ -27,9 +30,9 @@ func (w *concurrencyVLM) Predict(ctx context.Context, imgBytes [][]byte, prompt
// wrapVLMConcurrency installs the background concurrency governor as the
// outermost VLM decorator. Always applied; a cheap passthrough when no limiter
// is installed or the call is interactive.
func wrapVLMConcurrency(v VLM, err error) (VLM, error) {
func wrapVLMConcurrency(v VLM, limit int, err error) (VLM, error) {
if err != nil || v == nil {
return v, err
}
return &concurrencyVLM{inner: v}, nil
return &concurrencyVLM{inner: v, limit: limit}, nil
}
+17 -13
View File
@@ -29,7 +29,10 @@ type Config struct {
ModelID string
InterfaceType string // "ollama" or "openai" (default)
Provider string
Extra map[string]any
// MaxConcurrency caps concurrent background calls to this model; 0 falls
// back to the process-wide default (see limiter.GateN).
MaxConcurrency int
Extra map[string]any
// CustomHeaders 允许在调用远程 API 时附加自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
CustomHeaders map[string]string
AppID string
@@ -53,17 +56,18 @@ func ConfigFromModel(m *types.Model, appID, appSecret string) *Config {
}
}
return &Config{
ModelID: m.ID,
APIKey: m.Parameters.APIKey,
BaseURL: m.Parameters.BaseURL,
ModelName: m.Name,
Source: m.Source,
InterfaceType: ifType,
Provider: m.Parameters.Provider,
Extra: stringMapToAnyMap(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,
InterfaceType: ifType,
Provider: m.Parameters.Provider,
MaxConcurrency: m.Parameters.MaxConcurrency,
Extra: stringMapToAnyMap(m.Parameters.ExtraConfig),
CustomHeaders: m.Parameters.CustomHeaders,
AppID: appID,
AppSecret: appSecret,
}
}
@@ -90,7 +94,7 @@ func NewVLM(config *Config, ollamaService *ollama.OllamaService) (VLM, error) {
v, err = wrapVLMLangfuse(v, nil)
// Outermost: hold the per-model concurrency slot only around the real
// provider round-trip, so the wait is excluded from debug/langfuse timing.
return wrapVLMConcurrency(v, err)
return wrapVLMConcurrency(v, config.MaxConcurrency, err)
}
func newVLM(config *Config, ollamaService *ollama.OllamaService) (VLM, error) {