fd3e2992b1
Introduce per-model `custom_headers` config (similar to OpenAI Python SDK's `extra_headers`) so users can inject gateway auth tokens, trace IDs, etc. into every outbound API request for chat / embedding / rerank / VLLM / ASR models. Reserved headers like Authorization / Content-Type are always preserved to avoid breaking auth or signing flows. Along the way, unify production and "test connection" paths onto a single `ConfigFromModel(*types.Model, appID, appSecret)` constructor in each model package. Both `service.modelService.GetXxxModel` and the four `handler.initialization.Check*Model` / `TestEmbeddingModel` endpoints now go through the same field mapping, so new parameters only need to be added in one place. Backend: - Add `CustomHeaders map[string]string` to `types.ModelParameters` with JSON/YAML `omitempty` tags for forward compatibility. - New `internal/utils/extraheaders.go` providing `ApplyCustomHeaders` (for hand-rolled HTTP paths) and `WrapHTTPClientWithHeaders` / `CustomHeadersRoundTripper` (for SDK paths like go-openai). Reserved headers (Authorization, api-key, Content-Type, Accept, Host, Content-Length, User-Agent) are filtered out. - Inject headers in every remote model path: chat (SDK + raw HTTP), embedding (openai/aliyun/jina/volcengine/nvidia/azure_openai), rerank (remote_api/aliyun/jina/nvidia/zhipu — via shared `customHeaderSetter` interface), VLM and ASR (via http.Client wrapping). - Add `ConfigFromModel` to each of chat/embedding/rerank/vlm/asr, consolidating field mapping (ExtraConfig, CustomHeaders, InterfaceType defaulting, WeKnoraCloud credentials, etc.) and cover it with dedicated unit tests per package. - Refactor `service/model.go`: replace ~100 lines of hand-written Config literals with one-line `ConfigFromModel` calls; drop the now-unused `stringMapToAnyMap` helper. - Refactor `handler/initialization.go` test-connection endpoints: merge four ad-hoc request structs into one `ModelTestRequest`, extract `buildTestModel` and `resolveTenantWeKnoraCloudCreds` helpers, and route all four endpoints through `ConfigFromModel` + `NewXxx` so the test path is now behaviorally identical to production. Frontend: - Add `custom_headers` to the `ModelConfig` API type. - `ModelEditorDialog.vue`: new Key-Value header editor (add/remove rows), auto-converts map <-> array when loading/saving, and passes the current header set to the `Test Connection` button so the preview reflects exactly what production will send. - `ModelSettings.vue`: serialize the header array back into a map for the backend (drops empty rows). - i18n: add custom-header labels / descriptions / placeholders to zh-CN, en-US, ru-RU, ko-KR. Tests: - `internal/utils/extraheaders_test.go` covers reserved-header filtering and round-tripper wrapping (including nil-client fallback). - New `config_from_model_test.go` in chat / embedding / rerank / vlm / asr verifies all fields (CustomHeaders, ExtraConfig, InterfaceType defaulting, AppID/AppSecret) are propagated end-to-end.
65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package asr
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
// Segment represents a transcribed segment with timestamps.
|
|
type Segment struct {
|
|
Start float64 `json:"start"`
|
|
End float64 `json:"end"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// TranscriptionResult holds the full text and its segments.
|
|
type TranscriptionResult struct {
|
|
Text string `json:"text"`
|
|
Segments []Segment `json:"segments,omitempty"`
|
|
}
|
|
|
|
// ASR defines the interface for Automatic Speech Recognition model operations.
|
|
type ASR interface {
|
|
// Transcribe sends audio bytes to the ASR model and returns the transcribed text and segments.
|
|
Transcribe(ctx context.Context, audioBytes []byte, fileName string) (*TranscriptionResult, error)
|
|
|
|
GetModelName() string
|
|
GetModelID() string
|
|
}
|
|
|
|
// Config holds the configuration needed to create an ASR instance.
|
|
type Config struct {
|
|
Source types.ModelSource
|
|
BaseURL string
|
|
ModelName string
|
|
APIKey string
|
|
ModelID string
|
|
Language string // optional: specify language for transcription
|
|
// CustomHeaders 允许在调用远程 API 时附加自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
|
|
CustomHeaders map[string]string
|
|
}
|
|
|
|
// ConfigFromModel 根据 types.Model 构造 asr.Config。
|
|
// 生产路径(从 DB 拉起)和测试连接路径(临时表单)共享这份映射。
|
|
// 当前 ASR 不涉及 WeKnoraCloud 凭证,所以签名不含 appID/appSecret。
|
|
func ConfigFromModel(m *types.Model) *Config {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
return &Config{
|
|
ModelID: m.ID,
|
|
APIKey: m.Parameters.APIKey,
|
|
BaseURL: m.Parameters.BaseURL,
|
|
ModelName: m.Name,
|
|
Source: m.Source,
|
|
CustomHeaders: m.Parameters.CustomHeaders,
|
|
}
|
|
}
|
|
|
|
// NewASR creates an ASR instance based on the provided configuration.
|
|
// All ASR vendors use the OpenAI-compatible /v1/audio/transcriptions API.
|
|
func NewASR(config *Config) (ASR, error) {
|
|
return NewOpenAIASR(config)
|
|
}
|