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.
148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
package asr
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/Tencent/WeKnora/internal/logger"
|
||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||
openai "github.com/sashabaranov/go-openai"
|
||
)
|
||
|
||
const (
|
||
asrDefaultTimeout = 300 * time.Second // audio transcription can be slow
|
||
)
|
||
|
||
// OpenAIASR implements ASR via an OpenAI-compatible audio transcriptions API.
|
||
type OpenAIASR struct {
|
||
modelName string
|
||
modelID string
|
||
client *openai.Client
|
||
baseURL string
|
||
language string
|
||
}
|
||
|
||
// NewOpenAIASR creates an OpenAI-compatible ASR instance.
|
||
func NewOpenAIASR(config *Config) (*OpenAIASR, error) {
|
||
apiCfg := openai.DefaultConfig(config.APIKey)
|
||
if config.BaseURL != "" {
|
||
apiCfg.BaseURL = config.BaseURL
|
||
}
|
||
httpClient := &http.Client{Timeout: asrDefaultTimeout}
|
||
|
||
// 注入用户自定义 HTTP header(类似 OpenAI Python SDK 的 extra_headers)
|
||
if len(config.CustomHeaders) > 0 {
|
||
apiCfg.HTTPClient = secutils.WrapHTTPClientWithHeaders(httpClient, config.CustomHeaders)
|
||
} else {
|
||
apiCfg.HTTPClient = httpClient
|
||
}
|
||
|
||
return &OpenAIASR{
|
||
modelName: config.ModelName,
|
||
modelID: config.ModelID,
|
||
client: openai.NewClientWithConfig(apiCfg),
|
||
baseURL: config.BaseURL,
|
||
language: config.Language,
|
||
}, nil
|
||
}
|
||
|
||
// Transcribe sends audio bytes to the OpenAI-compatible audio transcriptions API.
|
||
func (s *OpenAIASR) Transcribe(ctx context.Context, audioBytes []byte, fileName string) (*TranscriptionResult, error) {
|
||
if len(audioBytes) == 0 {
|
||
return nil, fmt.Errorf("audio bytes are empty")
|
||
}
|
||
|
||
// Ensure fileName has an extension for proper MIME type detection
|
||
if fileName == "" {
|
||
fileName = "audio.mp3"
|
||
}
|
||
|
||
logger.Infof(ctx, "[ASR] Calling OpenAI-compatible transcription API, model=%s, baseURL=%s, audioSize=%d, file=%s",
|
||
s.modelName, s.baseURL, len(audioBytes), fileName)
|
||
|
||
req := openai.AudioRequest{
|
||
Model: s.modelName,
|
||
FilePath: fileName,
|
||
Reader: bytes.NewReader(audioBytes),
|
||
Format: openai.AudioResponseFormatVerboseJSON,
|
||
}
|
||
|
||
if s.language != "" {
|
||
req.Language = s.language
|
||
}
|
||
|
||
resp, err := s.client.CreateTranscription(ctx, req)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("ASR transcription request failed: %w", err)
|
||
}
|
||
|
||
jsonData, err := json.Marshal(resp)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("marshal transcription response: %w", err)
|
||
}
|
||
logger.Debugf(ctx, "[ASR] Transcription response: %s", string(jsonData))
|
||
|
||
text := strings.TrimSpace(resp.Text)
|
||
logger.Infof(ctx, "[ASR] Transcription completed, text length=%d", len(text))
|
||
|
||
var segments []Segment
|
||
for _, s := range resp.Segments {
|
||
segments = append(segments, Segment{
|
||
Start: s.Start,
|
||
End: s.End,
|
||
Text: strings.TrimSpace(s.Text),
|
||
})
|
||
}
|
||
|
||
return &TranscriptionResult{
|
||
Text: text,
|
||
Segments: segments,
|
||
}, nil
|
||
}
|
||
|
||
func (s *OpenAIASR) GetModelName() string { return s.modelName }
|
||
func (s *OpenAIASR) GetModelID() string { return s.modelID }
|
||
|
||
// DetectAudioFormat returns a file extension hint for the given audio bytes.
|
||
func DetectAudioFormat(data []byte, fileName string) string {
|
||
if fileName != "" {
|
||
ext := strings.ToLower(filepath.Ext(fileName))
|
||
if ext != "" {
|
||
return ext
|
||
}
|
||
}
|
||
// Try to detect from magic bytes
|
||
if len(data) >= 4 {
|
||
// MP3: starts with ID3 or 0xFF 0xFB
|
||
if data[0] == 'I' && data[1] == 'D' && data[2] == '3' {
|
||
return ".mp3"
|
||
}
|
||
if data[0] == 0xFF && (data[1]&0xE0) == 0xE0 {
|
||
return ".mp3"
|
||
}
|
||
// FLAC: starts with "fLaC"
|
||
if data[0] == 'f' && data[1] == 'L' && data[2] == 'a' && data[3] == 'C' {
|
||
return ".flac"
|
||
}
|
||
// OGG: starts with "OggS"
|
||
if data[0] == 'O' && data[1] == 'g' && data[2] == 'g' && data[3] == 'S' {
|
||
return ".ogg"
|
||
}
|
||
// WAV: starts with "RIFF"
|
||
if data[0] == 'R' && data[1] == 'I' && data[2] == 'F' && data[3] == 'F' {
|
||
return ".wav"
|
||
}
|
||
}
|
||
// M4A: check for ftyp box
|
||
if len(data) >= 8 && data[4] == 'f' && data[5] == 't' && data[6] == 'y' && data[7] == 'p' {
|
||
return ".m4a"
|
||
}
|
||
return ".mp3" // default
|
||
}
|