mirror of
https://github.com/mudler/LocalAI.git
synced 2026-10-28 09:08:54 -04:00
* feat(grpc): add a Forecast RPC for time-series models Add Forecast to the backend service with columnar request and response messages: flat row-major arrays, NaN for missing values, and one message per series so a caller can send series of different shapes in one call. The response keeps the request order. Wire the RPC through the server, client, embedded client, connection evicting client, in-flight tracking client and the base backend, which answers unimplemented. core/backend gains a Forecast helper that stamps the model identity and records a trace, like the other single-purpose RPCs. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(config): add the forecast usecase FLAG_FORECAST marks a model as a time-series forecaster. The timestral-cpp backend declares it as its default usecase, so a config with that backend needs no extra lines. The backend is also listed as a non-text backend, so a forecast model is never offered as a chat model. It shows up in the gallery filter and in the capability list. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(api): add POST /v1/forecast Take one or more numeric series and return quantile trajectories for the next prediction_length steps. Targets are [variates][steps] or a flat array for one variate; JSON null marks a missing value. Past and known covariates and categorical codes are checked for length before the backend sees them. Quantiles default to 0.1, 0.5 and 0.9 in the backend. The route is gated by a new default-on forecast API feature. The endpoint is listed in the swagger spec, the /api/instructions index, the UI model filter and the capability symbols. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(backends): add the timestral-cpp forecast backend Wrap the timestral.cpp C ABI with purego, the way the other ggml backends do: libtimestral is built from a pinned commit with ggml embedded, and the Go binary loads it at start. Forecast validates the counts against the array lengths, groups the series by shape, makes one library call per group and puts the results back in the order the caller sent them. Quantiles default to 0.1, 0.5 and 0.9, and a level the model does not have is refused with the list of valid ones. A mutex guards the handle because the library is not reentrant. Request size is bounded by two model options. The unit specs use a fake library. The integration specs and a gRPC spec run against the 5M F32 model that test.sh downloads and checks against a fixed SHA-256. Assisted-by: Claude:claude-sonnet-5-5 go-vet * build(backends): register timestral-cpp in the build and the library Add the CPU image to the backend matrix for amd64 and arm64, plus an Apple silicon entry that is not built or tested yet. The image has no GPU keys because the engine runs on the CPU only. Wire the make targets, the path filter and the dependency bump bot. The library entries were generated with backendcatalog. The images are not published yet, so the release entries are unpinned until a maintainer runs --repin. The gallery reader test now counts the names in the library instead of a fixed number, so adding a backend does not break it. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(gallery): add the Timestral forecast models Add Timestral 5M, 28M, 123M and 209M as F32 entries, each with its F16 build as a variant. Q8_0 and the K-quants are left out until the quality question is settled. Each entry runs on the timestral-cpp backend, on the CPU only. The weights use Mistral's Modified MIT licence, which bars use by companies above a monthly revenue threshold. The first paragraph after the summary says so, the licence file is installed next to the weights, and the revenue-restricted-license tag makes the entries filterable. The memory each build needs has not been measured, so no size is set. Assisted-by: Claude:claude-sonnet-5-5 * docs(forecast): document /v1/forecast and the Timestral models Add a feature page with the request and response shapes, the error cases, the model options and the licence note. List the engine in the README, the compatibility table and the backend categories. Assisted-by: Claude:claude-sonnet-5-5 --------- Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
238 lines
9.7 KiB
Go
238 lines
9.7 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mudler/LocalAI/pkg/grpc/base"
|
|
"github.com/mudler/LocalAI/pkg/grpc/grpcerrors"
|
|
pb "github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
// modalityBackend answers every non-PredictOptions inference RPC successfully
|
|
// and counts the calls that reached it. `served` is therefore the signal for
|
|
// "the identity guard let this through" — the same role it plays in
|
|
// model_identity_test.go for the four PredictOptions RPCs.
|
|
type modalityBackend struct {
|
|
base.SingleThread
|
|
|
|
loaded string
|
|
served int
|
|
}
|
|
|
|
func (b *modalityBackend) Load(opts *pb.ModelOptions) error {
|
|
b.loaded = opts.Model
|
|
return nil
|
|
}
|
|
|
|
func (b *modalityBackend) GenerateImage(*pb.GenerateImageRequest) error { b.served++; return nil }
|
|
func (b *modalityBackend) GenerateVideo(*pb.GenerateVideoRequest) error { b.served++; return nil }
|
|
func (b *modalityBackend) Animate3D(*pb.Animate3DRequest) error { b.served++; return nil }
|
|
func (b *modalityBackend) TTS(*pb.TTSRequest) error { b.served++; return nil }
|
|
|
|
func (b *modalityBackend) TTSStream(_ *pb.TTSRequest, ch chan []byte) error {
|
|
b.served++
|
|
ch <- []byte{0}
|
|
close(ch)
|
|
return nil
|
|
}
|
|
|
|
func (b *modalityBackend) SoundGeneration(*pb.SoundGenerationRequest) error { b.served++; return nil }
|
|
|
|
func (b *modalityBackend) AudioTranscription(context.Context, *pb.TranscriptRequest) (pb.TranscriptResult, error) {
|
|
b.served++
|
|
return pb.TranscriptResult{Text: "ok"}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) AudioTranscriptionStream(_ context.Context, _ *pb.TranscriptRequest, ch chan *pb.TranscriptStreamResponse) error {
|
|
b.served++
|
|
ch <- &pb.TranscriptStreamResponse{Delta: "ok"}
|
|
close(ch)
|
|
return nil
|
|
}
|
|
|
|
func (b *modalityBackend) Detect(*pb.DetectOptions) (pb.DetectResponse, error) {
|
|
b.served++
|
|
return pb.DetectResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) Depth(*pb.DepthRequest) (pb.DepthResponse, error) {
|
|
b.served++
|
|
return pb.DepthResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) Forecast(*pb.ForecastRequest) (pb.ForecastResponse, error) {
|
|
b.served++
|
|
return pb.ForecastResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) FaceVerify(*pb.FaceVerifyRequest) (pb.FaceVerifyResponse, error) {
|
|
b.served++
|
|
return pb.FaceVerifyResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) FaceAnalyze(*pb.FaceAnalyzeRequest) (pb.FaceAnalyzeResponse, error) {
|
|
b.served++
|
|
return pb.FaceAnalyzeResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) VoiceVerify(*pb.VoiceVerifyRequest) (pb.VoiceVerifyResponse, error) {
|
|
b.served++
|
|
return pb.VoiceVerifyResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) VoiceAnalyze(*pb.VoiceAnalyzeRequest) (pb.VoiceAnalyzeResponse, error) {
|
|
b.served++
|
|
return pb.VoiceAnalyzeResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) VoiceEmbed(*pb.VoiceEmbedRequest) (pb.VoiceEmbedResponse, error) {
|
|
b.served++
|
|
return pb.VoiceEmbedResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) VAD(*pb.VADRequest) (pb.VADResponse, error) {
|
|
b.served++
|
|
return pb.VADResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) Diarize(*pb.DiarizeRequest) (pb.DiarizeResponse, error) {
|
|
b.served++
|
|
return pb.DiarizeResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) SoundDetection(context.Context, *pb.SoundDetectionRequest) (*pb.SoundDetectionResponse, error) {
|
|
b.served++
|
|
return &pb.SoundDetectionResponse{}, nil
|
|
}
|
|
|
|
func (b *modalityBackend) AudioTransform(*pb.AudioTransformRequest) (*pb.AudioTransformResult, error) {
|
|
b.served++
|
|
return &pb.AudioTransformResult{}, nil
|
|
}
|
|
|
|
var _ AIModel = (*modalityBackend)(nil)
|
|
|
|
// callAllModalities exercises every remaining inference RPC that the generic Go
|
|
// server implements, sending `identity` in each request's ModelIdentity field.
|
|
// One call per RPC, so the returned error count is also the RPC count.
|
|
//
|
|
// Score and TokenClassify are absent on purpose: the generic Go server does
|
|
// not implement them (they fall through to UnimplementedBackendServer), so
|
|
// only the C++ and Python backends can enforce them. Rerank is different: the
|
|
// Go server does serve it (see server_rerank_test.go), but only for backends
|
|
// that opt in via RerankModel — modalityBackend does not, so it is left out
|
|
// here too rather than adding a no-op implementation with nothing to guard.
|
|
func callAllModalities(c Backend, identity string) map[string]error {
|
|
ctx := context.Background()
|
|
errs := map[string]error{}
|
|
|
|
_, errs["GenerateImage"] = c.GenerateImage(ctx, &pb.GenerateImageRequest{ModelIdentity: identity})
|
|
_, errs["GenerateVideo"] = c.GenerateVideo(ctx, &pb.GenerateVideoRequest{ModelIdentity: identity})
|
|
_, errs["Animate3D"] = c.Animate3D(ctx, &pb.Animate3DRequest{ModelIdentity: identity})
|
|
_, errs["TTS"] = c.TTS(ctx, &pb.TTSRequest{ModelIdentity: identity})
|
|
errs["TTSStream"] = c.TTSStream(ctx, &pb.TTSRequest{ModelIdentity: identity}, func(*pb.Reply) {})
|
|
_, errs["SoundGeneration"] = c.SoundGeneration(ctx, &pb.SoundGenerationRequest{ModelIdentity: identity})
|
|
_, errs["AudioTranscription"] = c.AudioTranscription(ctx, &pb.TranscriptRequest{ModelIdentity: identity})
|
|
errs["AudioTranscriptionStream"] = c.AudioTranscriptionStream(ctx, &pb.TranscriptRequest{ModelIdentity: identity}, func(*pb.TranscriptStreamResponse) {})
|
|
_, errs["Detect"] = c.Detect(ctx, &pb.DetectOptions{ModelIdentity: identity})
|
|
_, errs["Depth"] = c.Depth(ctx, &pb.DepthRequest{ModelIdentity: identity})
|
|
_, errs["Forecast"] = c.Forecast(ctx, &pb.ForecastRequest{ModelIdentity: identity})
|
|
_, errs["FaceVerify"] = c.FaceVerify(ctx, &pb.FaceVerifyRequest{ModelIdentity: identity})
|
|
_, errs["FaceAnalyze"] = c.FaceAnalyze(ctx, &pb.FaceAnalyzeRequest{ModelIdentity: identity})
|
|
_, errs["VoiceVerify"] = c.VoiceVerify(ctx, &pb.VoiceVerifyRequest{ModelIdentity: identity})
|
|
_, errs["VoiceAnalyze"] = c.VoiceAnalyze(ctx, &pb.VoiceAnalyzeRequest{ModelIdentity: identity})
|
|
_, errs["VoiceEmbed"] = c.VoiceEmbed(ctx, &pb.VoiceEmbedRequest{ModelIdentity: identity})
|
|
_, errs["VAD"] = c.VAD(ctx, &pb.VADRequest{ModelIdentity: identity})
|
|
_, errs["Diarize"] = c.Diarize(ctx, &pb.DiarizeRequest{ModelIdentity: identity})
|
|
_, errs["SoundDetection"] = c.SoundDetection(ctx, &pb.SoundDetectionRequest{ModelIdentity: identity})
|
|
_, errs["AudioTransform"] = c.AudioTransform(ctx, &pb.AudioTransformRequest{ModelIdentity: identity})
|
|
|
|
return errs
|
|
}
|
|
|
|
// #10970 protected only the four PredictOptions RPCs. Every other modality
|
|
// shares the exposure — the controller's cached row names a host:port, the port
|
|
// can be recycled by another model's backend, and a liveness-only probe cannot
|
|
// tell the difference — so each one must reject a request naming another model
|
|
// rather than answer from whatever it holds.
|
|
var _ = Describe("modality RPC model identity guard", func() {
|
|
newServed := func(addr, loadedModel string) (Backend, *modalityBackend) {
|
|
b := &modalityBackend{}
|
|
Provide(addr, b)
|
|
c := NewClient(addr, true, nil, false)
|
|
_, err := c.LoadModel(context.Background(), &pb.ModelOptions{Model: loadedModel})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(b.loaded).To(Equal(loadedModel))
|
|
return c, b
|
|
}
|
|
|
|
It("rejects every modality RPC when the identity names another model", func() {
|
|
c, b := newServed("test://modality-mismatch", "a.gguf")
|
|
|
|
for rpc, err := range callAllModalities(c, "b.gguf") {
|
|
Expect(err).To(HaveOccurred(), "%s must reject a wrong-model request", rpc)
|
|
Expect(grpcerrors.IsModelMismatch(err)).To(BeTrue(), "%s: want a mismatch error, got %v", rpc, err)
|
|
// The router reacts differently to the two signals, so a mismatch
|
|
// must never be mistaken for a not-loaded.
|
|
Expect(grpcerrors.IsModelNotLoaded(err)).To(BeFalse(), "%s", rpc)
|
|
}
|
|
Expect(b.served).To(Equal(0), "no request may reach the model on a mismatch")
|
|
})
|
|
|
|
It("serves when the identity matches the loaded model", func() {
|
|
c, b := newServed("test://modality-match", "a.gguf")
|
|
|
|
errs := callAllModalities(c, "a.gguf")
|
|
for rpc, err := range errs {
|
|
Expect(err).ToNot(HaveOccurred(), "%s", rpc)
|
|
}
|
|
Expect(b.served).To(Equal(len(errs)))
|
|
})
|
|
|
|
// Compatibility, old controller -> new backend. Every existing deployment
|
|
// sends no identity, and tests/e2e-backends/backend_test.go drives real
|
|
// backends with bare request structs at many call sites. Tightening this
|
|
// breaks all of them, so it must fail here first.
|
|
It("serves when the request carries no identity", func() {
|
|
c, b := newServed("test://modality-empty-request", "a.gguf")
|
|
|
|
errs := callAllModalities(c, "")
|
|
for rpc, err := range errs {
|
|
Expect(err).ToNot(HaveOccurred(), "%s", rpc)
|
|
}
|
|
Expect(b.served).To(Equal(len(errs)))
|
|
})
|
|
|
|
// The backend side of the same rule: a model loaded without an identity
|
|
// (an old controller did the load) cannot judge anything, so it must serve.
|
|
It("serves when the backend has no recorded identity", func() {
|
|
c, b := newServed("test://modality-empty-loaded", "")
|
|
|
|
errs := callAllModalities(c, "b.gguf")
|
|
for rpc, err := range errs {
|
|
Expect(err).ToNot(HaveOccurred(), "%s", rpc)
|
|
}
|
|
Expect(b.served).To(Equal(len(errs)))
|
|
})
|
|
|
|
// AudioEncode/AudioDecode are deliberately NOT guarded: the opus codec
|
|
// backend they target is loaded with a literal model.WithModel("opus") and
|
|
// has no ModelConfig, so there is no value with the structural guarantee
|
|
// the rest of this mechanism depends on. Sending an identity that merely
|
|
// looks right would be convention, not construction — the exact
|
|
// false-rejection risk #10970 refused to take. Pinned here so a future
|
|
// "completeness" pass has to make that decision deliberately.
|
|
It("leaves the codec RPCs unguarded", func() {
|
|
c, _ := newServed("test://modality-codec", "a.gguf")
|
|
|
|
// Whatever these answer, it must never be a model mismatch: no
|
|
// identity is carried, so nothing is compared.
|
|
_, err := c.AudioEncode(context.Background(), &pb.AudioEncodeRequest{})
|
|
Expect(grpcerrors.IsModelMismatch(err)).To(BeFalse())
|
|
_, err = c.AudioDecode(context.Background(), &pb.AudioDecodeRequest{})
|
|
Expect(grpcerrors.IsModelMismatch(err)).To(BeFalse())
|
|
})
|
|
})
|