mirror of
https://github.com/mudler/LocalAI.git
synced 2026-05-29 19:19:19 -04:00
* fix(traces): cap backend trace Data field so the admin UI stays responsive The previous fix (#9946) capped API trace bodies but missed backend traces, which carry the same blast radius: - LLM backend traces store the full chat messages JSON, full response, and full streaming deltas. Every agent-pool reasoning step ships the full RAG-augmented history (50-500 KiB per trace, often 100+ traces queued). - TTS / audio_transform / transcript traces embed a 30s audio snippet as base64, around 1.3 MiB per trace. Both blow the /api/backend-traces JSON past tens of MiB. The admin Traces page then keeps re-downloading and re-parsing the buffer faster than the 5s auto-refresh and stays in the loading state forever, the same symptom the API-side fix addressed. Apply two complementary caps, both honoring LOCALAI_TRACING_MAX_BODY_BYTES: Option A (safety net in core/trace): RecordBackendTrace walks the Data map recursively and replaces any string value larger than the cap with "<truncated: N bytes>". Catches anything a future producer forgets. Option B (head-preserving at the producer): - core/backend/llm.go: TruncateToBytes on messages, response, and chat_deltas content/reasoning_content so the leading content stays readable in the UI. - core/trace/audio_snippet.go: omit audio_wav_base64 when the encoded blob would exceed the cap (truncated base64 is undecodable). The quality metrics still ship and the UI's WaveformPlayer simply skips when the field is absent. TruncateToBytes is bounded to <= maxBytes so Option A leaves the producer's head-preserving output alone instead of replacing it with the bare marker. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Assisted-by: Claude:claude-opus-4-7 * fix(react-ui): expose tracing_max_body_bytes in Settings and Traces panels The setting was already plumbed through env (LOCALAI_TRACING_MAX_BODY_BYTES), CLI flag, and the runtime_settings.json GET/PUT schema, but neither the main Settings page nor the inline Traces panel offered an input for it. Admins hitting the "Traces UI stuck loading" symptom had to know to set an env var or PUT raw JSON to /api/settings to dial the cap. Add a "Max Body Bytes" row next to "Max Items" in both places. Same input type, same disabled-when-tracing-off semantics, placeholder shows the 65536 default so users see what they're inheriting. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Assisted-by: Claude:claude-opus-4-7 * test(react-ui): disambiguate Max Items locator after adding Max Body Bytes The Tracing settings panel now has two number inputs. The previous spec matched 'input[type="number"]' which became ambiguous and triggered a Playwright strict-mode violation in CI. Switch to getByPlaceholder('100') for Max Items and add a parallel spec for the new Max Body Bytes field using getByPlaceholder('65536'). Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Assisted-by: Claude:claude-opus-4-7 --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
211 lines
6.5 KiB
Go
211 lines
6.5 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"maps"
|
|
"time"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
"github.com/mudler/LocalAI/core/trace"
|
|
|
|
grpcPkg "github.com/mudler/LocalAI/pkg/grpc"
|
|
"github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
)
|
|
|
|
// TranscriptionRequest groups the parameters accepted by ModelTranscription.
|
|
// Use this so callers don't have to pass long positional arg lists when they
|
|
// only care about a subset of fields.
|
|
type TranscriptionRequest struct {
|
|
Audio string
|
|
Language string
|
|
Translate bool
|
|
Diarize bool
|
|
Prompt string
|
|
Temperature float32
|
|
TimestampGranularities []string
|
|
}
|
|
|
|
func (r *TranscriptionRequest) toProto(threads uint32) *proto.TranscriptRequest {
|
|
return &proto.TranscriptRequest{
|
|
Dst: r.Audio,
|
|
Language: r.Language,
|
|
Translate: r.Translate,
|
|
Diarize: r.Diarize,
|
|
Threads: threads,
|
|
Prompt: r.Prompt,
|
|
Temperature: r.Temperature,
|
|
TimestampGranularities: r.TimestampGranularities,
|
|
}
|
|
}
|
|
|
|
func loadTranscriptionModel(ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (grpcPkg.Backend, error) {
|
|
if modelConfig.Backend == "" {
|
|
modelConfig.Backend = model.WhisperBackend
|
|
}
|
|
opts := ModelOptions(modelConfig, appConfig)
|
|
transcriptionModel, err := ml.Load(opts...)
|
|
if err != nil {
|
|
recordModelLoadFailure(appConfig, modelConfig.Name, modelConfig.Backend, err, nil)
|
|
return nil, err
|
|
}
|
|
if transcriptionModel == nil {
|
|
return nil, fmt.Errorf("could not load transcription model")
|
|
}
|
|
return transcriptionModel, nil
|
|
}
|
|
|
|
func ModelTranscription(ctx context.Context, audio, language string, translate, diarize bool, prompt string, ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (*schema.TranscriptionResult, error) {
|
|
return ModelTranscriptionWithOptions(ctx, TranscriptionRequest{
|
|
Audio: audio,
|
|
Language: language,
|
|
Translate: translate,
|
|
Diarize: diarize,
|
|
Prompt: prompt,
|
|
}, ml, modelConfig, appConfig)
|
|
}
|
|
|
|
func ModelTranscriptionWithOptions(ctx context.Context, req TranscriptionRequest, ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (*schema.TranscriptionResult, error) {
|
|
transcriptionModel, err := loadTranscriptionModel(ml, modelConfig, appConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var startTime time.Time
|
|
var audioSnippet map[string]any
|
|
if appConfig.EnableTracing {
|
|
trace.InitBackendTracingIfEnabled(appConfig.TracingMaxItems, appConfig.TracingMaxBodyBytes)
|
|
startTime = time.Now()
|
|
// Capture audio before the backend call — the backend may delete the file.
|
|
audioSnippet = trace.AudioSnippet(req.Audio, appConfig.TracingMaxBodyBytes)
|
|
}
|
|
|
|
r, err := transcriptionModel.AudioTranscription(ctx, req.toProto(uint32(*modelConfig.Threads)))
|
|
if err != nil {
|
|
if appConfig.EnableTracing {
|
|
errData := map[string]any{
|
|
"audio_file": req.Audio,
|
|
"language": req.Language,
|
|
"translate": req.Translate,
|
|
"diarize": req.Diarize,
|
|
"prompt": req.Prompt,
|
|
}
|
|
if audioSnippet != nil {
|
|
maps.Copy(errData, audioSnippet)
|
|
}
|
|
trace.RecordBackendTrace(trace.BackendTrace{
|
|
Timestamp: startTime,
|
|
Duration: time.Since(startTime),
|
|
Type: trace.BackendTraceTranscription,
|
|
ModelName: modelConfig.Name,
|
|
Backend: modelConfig.Backend,
|
|
Summary: trace.TruncateString(req.Audio, 200),
|
|
Error: err.Error(),
|
|
Data: errData,
|
|
})
|
|
}
|
|
return nil, err
|
|
}
|
|
tr := transcriptResultFromProto(r)
|
|
|
|
if appConfig.EnableTracing {
|
|
data := map[string]any{
|
|
"audio_file": req.Audio,
|
|
"language": req.Language,
|
|
"translate": req.Translate,
|
|
"diarize": req.Diarize,
|
|
"prompt": req.Prompt,
|
|
"result_text": tr.Text,
|
|
"segments_count": len(tr.Segments),
|
|
}
|
|
if audioSnippet != nil {
|
|
maps.Copy(data, audioSnippet)
|
|
}
|
|
trace.RecordBackendTrace(trace.BackendTrace{
|
|
Timestamp: startTime,
|
|
Duration: time.Since(startTime),
|
|
Type: trace.BackendTraceTranscription,
|
|
ModelName: modelConfig.Name,
|
|
Backend: modelConfig.Backend,
|
|
Summary: trace.TruncateString(req.Audio+" -> "+tr.Text, 200),
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
return tr, err
|
|
}
|
|
|
|
// TranscriptionStreamChunk is a streaming event emitted by
|
|
// ModelTranscriptionStream. Either Delta carries an incremental text fragment,
|
|
// or Final carries the completed transcription as the very last event.
|
|
type TranscriptionStreamChunk struct {
|
|
Delta string
|
|
Final *schema.TranscriptionResult
|
|
}
|
|
|
|
// ModelTranscriptionStream runs the gRPC streaming transcription RPC and
|
|
// invokes onChunk for each event the backend produces. Backends that don't
|
|
// support real streaming should still emit one terminal event with Final set,
|
|
// which the HTTP layer turns into a single delta + done SSE pair.
|
|
func ModelTranscriptionStream(ctx context.Context, req TranscriptionRequest, ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig, onChunk func(TranscriptionStreamChunk)) error {
|
|
transcriptionModel, err := loadTranscriptionModel(ml, modelConfig, appConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pbReq := req.toProto(uint32(*modelConfig.Threads))
|
|
pbReq.Stream = true
|
|
|
|
return transcriptionModel.AudioTranscriptionStream(ctx, pbReq, func(chunk *proto.TranscriptStreamResponse) {
|
|
if chunk == nil {
|
|
return
|
|
}
|
|
out := TranscriptionStreamChunk{Delta: chunk.Delta}
|
|
if chunk.FinalResult != nil {
|
|
out.Final = transcriptResultFromProto(chunk.FinalResult)
|
|
}
|
|
onChunk(out)
|
|
})
|
|
}
|
|
|
|
func transcriptResultFromProto(r *proto.TranscriptResult) *schema.TranscriptionResult {
|
|
if r == nil {
|
|
return &schema.TranscriptionResult{}
|
|
}
|
|
tr := &schema.TranscriptionResult{
|
|
Text: r.Text,
|
|
Language: r.Language,
|
|
Duration: float64(r.Duration),
|
|
}
|
|
|
|
for _, s := range r.Segments {
|
|
var tks []int
|
|
for _, t := range s.Tokens {
|
|
tks = append(tks, int(t))
|
|
}
|
|
var words []schema.TranscriptionWord
|
|
for _, w := range s.Words {
|
|
var word = schema.TranscriptionWord{
|
|
Start: time.Duration(w.Start),
|
|
End: time.Duration(w.End),
|
|
Text: w.Text,
|
|
}
|
|
words = append(words, word)
|
|
tr.Words = append(tr.Words, word)
|
|
}
|
|
tr.Segments = append(tr.Segments,
|
|
schema.TranscriptionSegment{
|
|
Text: s.Text,
|
|
Id: int(s.Id),
|
|
Start: time.Duration(s.Start),
|
|
End: time.Duration(s.End),
|
|
Tokens: tks,
|
|
Speaker: s.Speaker,
|
|
Words: words,
|
|
})
|
|
}
|
|
return tr
|
|
}
|