mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-30 09:57:57 -04:00
#10970 gave the four PredictOptions RPCs a model-identity check so a backend reached through a stale distributed route rejects the request instead of answering from whatever model it holds (#10952). Every other modality shares that exposure: the route is cached by host:port, a worker can recycle a stopped backend's port for another model's backend, and a liveness-only probe cannot tell a stale row from a valid one. Extends the same mechanism to the 21 remaining request messages that reach a backend through the router, using the pattern #10970 established rather than a parallel one: - proto: ModelIdentity on each modality request message. - controller: populated from ModelConfig.Model at the call site that also builds ModelOptions, so load-time and request-time values are equal by construction. - backends: one generic guard in pkg/grpc/server.go (27 Go backends), the method set in backend/python/common (36 Python backends), llama-cpp (AudioTranscription/Stream, Rerank, Score) and privacy-filter (TokenClassify). - reconcile already drops the stale row on IsModelMismatch; no change. TTSRequest and SoundGenerationRequest get a SEPARATE ModelIdentity field rather than reusing their existing `model`: FileStagingClient rewrites `model` to a worker-local path, so comparing it would reject valid requests in exactly the configuration this guards. AudioEncode/AudioDecode are deliberately left unguarded: the opus codec backend is loaded from a literal rather than a ModelConfig, so no value carries the equality guarantee the comparison depends on. The four bidirectional stream RPCs are out of scope; they bypass reconcile. Empty means skip on both sides, so an old controller, an old backend, and the bare request structs in tests/e2e-backends all keep working. Assisted-by: Claude Code:claude-opus-4-8 [Read] [Edit] [Bash] Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
|
|
grpcPkg "github.com/mudler/LocalAI/pkg/grpc"
|
|
"github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
)
|
|
|
|
// SoundDetectionRequest carries the knobs the HTTP layer collects for an
|
|
// audio-tagging / sound-event-classification call. Audio is the path to the
|
|
// uploaded clip on disk; TopK and Threshold are optional (0 = backend default).
|
|
type SoundDetectionRequest struct {
|
|
Audio string
|
|
TopK int32
|
|
Threshold float32
|
|
}
|
|
|
|
// modelIdentity: see the note on TranscriptionRequest.toProto.
|
|
func (r *SoundDetectionRequest) toProto(modelIdentity string) *proto.SoundDetectionRequest {
|
|
return &proto.SoundDetectionRequest{
|
|
ModelIdentity: modelIdentity,
|
|
Src: r.Audio,
|
|
TopK: r.TopK,
|
|
Threshold: r.Threshold,
|
|
}
|
|
}
|
|
|
|
func loadSoundDetectionModel(ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (grpcPkg.Backend, error) {
|
|
if modelConfig.Backend == "" {
|
|
return nil, fmt.Errorf("sound classification: model %q has no backend set; supported backends include ced", modelConfig.Name)
|
|
}
|
|
opts := ModelOptions(modelConfig, appConfig)
|
|
m, err := ml.Load(opts...)
|
|
if err != nil {
|
|
recordModelLoadFailure(appConfig, modelConfig.Name, modelConfig.Backend, err, nil)
|
|
return nil, err
|
|
}
|
|
if m == nil {
|
|
return nil, fmt.Errorf("could not load sound classification model")
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// ModelSoundDetection runs the SoundDetection RPC against the configured
|
|
// backend and returns a normalized schema.SoundClassificationResult.
|
|
func ModelSoundDetection(ctx context.Context, req SoundDetectionRequest, ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (*schema.SoundClassificationResult, error) {
|
|
m, err := loadSoundDetectionModel(ml, modelConfig, appConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r, err := m.SoundDetection(ctx, req.toProto(modelConfig.Model))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return soundClassificationResultFromProto(modelConfig.Name, r), nil
|
|
}
|
|
|
|
// soundClassificationResultFromProto maps the backend detections to the
|
|
// HTTP-facing schema, keeping the backend's score-descending order.
|
|
func soundClassificationResultFromProto(modelName string, r *proto.SoundDetectionResponse) *schema.SoundClassificationResult {
|
|
out := &schema.SoundClassificationResult{
|
|
Model: modelName,
|
|
Detections: []schema.SoundClassification{},
|
|
}
|
|
if r == nil {
|
|
return out
|
|
}
|
|
for _, d := range r.Detections {
|
|
if d == nil {
|
|
continue
|
|
}
|
|
out.Detections = append(out.Detections, schema.SoundClassification{
|
|
Index: int(d.Index),
|
|
Label: d.Label,
|
|
Score: d.Score,
|
|
})
|
|
}
|
|
sort.SliceStable(out.Detections, func(i, j int) bool {
|
|
return out.Detections[i].Score > out.Detections[j].Score
|
|
})
|
|
return out
|
|
}
|