|
| 1 | +package middleware_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/labstack/echo/v4" |
| 12 | + "github.com/mudler/LocalAI/core/config" |
| 13 | + . "github.com/mudler/LocalAI/core/http/middleware" |
| 14 | + "github.com/mudler/LocalAI/core/schema" |
| 15 | + "github.com/mudler/LocalAI/pkg/model" |
| 16 | + "github.com/mudler/LocalAI/pkg/system" |
| 17 | + . "github.com/onsi/ginkgo/v2" |
| 18 | + . "github.com/onsi/gomega" |
| 19 | +) |
| 20 | + |
| 21 | +// newRequestApp creates a minimal Echo app with SetModelAndConfig middleware. |
| 22 | +func newRequestApp(re *RequestExtractor) *echo.Echo { |
| 23 | + e := echo.New() |
| 24 | + e.POST("/v1/chat/completions", |
| 25 | + func(c echo.Context) error { |
| 26 | + return c.String(http.StatusOK, "ok") |
| 27 | + }, |
| 28 | + re.SetModelAndConfig(func() schema.LocalAIRequest { |
| 29 | + return new(schema.OpenAIRequest) |
| 30 | + }), |
| 31 | + ) |
| 32 | + return e |
| 33 | +} |
| 34 | + |
| 35 | +func postJSON(e *echo.Echo, path, body string) *httptest.ResponseRecorder { |
| 36 | + req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)) |
| 37 | + req.Header.Set("Content-Type", "application/json") |
| 38 | + rec := httptest.NewRecorder() |
| 39 | + e.ServeHTTP(rec, req) |
| 40 | + return rec |
| 41 | +} |
| 42 | + |
| 43 | +var _ = Describe("SetModelAndConfig middleware", func() { |
| 44 | + var ( |
| 45 | + app *echo.Echo |
| 46 | + modelDir string |
| 47 | + ) |
| 48 | + |
| 49 | + BeforeEach(func() { |
| 50 | + var err error |
| 51 | + modelDir, err = os.MkdirTemp("", "localai-test-models-*") |
| 52 | + Expect(err).ToNot(HaveOccurred()) |
| 53 | + |
| 54 | + ss := &system.SystemState{ |
| 55 | + Model: system.Model{ModelsPath: modelDir}, |
| 56 | + } |
| 57 | + appConfig := config.NewApplicationConfig() |
| 58 | + appConfig.SystemState = ss |
| 59 | + |
| 60 | + mcl := config.NewModelConfigLoader(modelDir) |
| 61 | + ml := model.NewModelLoader(ss) |
| 62 | + |
| 63 | + re := NewRequestExtractor(mcl, ml, appConfig) |
| 64 | + app = newRequestApp(re) |
| 65 | + }) |
| 66 | + |
| 67 | + AfterEach(func() { |
| 68 | + os.RemoveAll(modelDir) |
| 69 | + }) |
| 70 | + |
| 71 | + Context("when the model does not exist", func() { |
| 72 | + It("returns 404 with a helpful error message", func() { |
| 73 | + rec := postJSON(app, "/v1/chat/completions", |
| 74 | + `{"model":"nonexistent-model","messages":[{"role":"user","content":"hi"}]}`) |
| 75 | + |
| 76 | + Expect(rec.Code).To(Equal(http.StatusNotFound)) |
| 77 | + |
| 78 | + var resp schema.ErrorResponse |
| 79 | + Expect(json.Unmarshal(rec.Body.Bytes(), &resp)).To(Succeed()) |
| 80 | + Expect(resp.Error).ToNot(BeNil()) |
| 81 | + Expect(resp.Error.Message).To(ContainSubstring("nonexistent-model")) |
| 82 | + Expect(resp.Error.Message).To(ContainSubstring("not found")) |
| 83 | + Expect(resp.Error.Type).To(Equal("invalid_request_error")) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + Context("when the model exists as a config file", func() { |
| 88 | + BeforeEach(func() { |
| 89 | + cfgContent := []byte("name: test-model\nbackend: llama-cpp\n") |
| 90 | + err := os.WriteFile(filepath.Join(modelDir, "test-model.yaml"), cfgContent, 0644) |
| 91 | + Expect(err).ToNot(HaveOccurred()) |
| 92 | + }) |
| 93 | + |
| 94 | + It("passes through to the handler", func() { |
| 95 | + rec := postJSON(app, "/v1/chat/completions", |
| 96 | + `{"model":"test-model","messages":[{"role":"user","content":"hi"}]}`) |
| 97 | + |
| 98 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + Context("when the model exists as a pre-loaded config", func() { |
| 103 | + var mcl *config.ModelConfigLoader |
| 104 | + |
| 105 | + BeforeEach(func() { |
| 106 | + // Simulate a model installed via gallery: config is loaded in memory |
| 107 | + // (not just a YAML file on disk). Recreate the app with the pre-loaded config. |
| 108 | + ss := &system.SystemState{ |
| 109 | + Model: system.Model{ModelsPath: modelDir}, |
| 110 | + } |
| 111 | + appConfig := config.NewApplicationConfig() |
| 112 | + appConfig.SystemState = ss |
| 113 | + |
| 114 | + mcl = config.NewModelConfigLoader(modelDir) |
| 115 | + // Pre-load a config as if installed via gallery |
| 116 | + cfgContent := []byte("name: gallery-model\nbackend: llama-cpp\nmodel: gallery-model\n") |
| 117 | + err := os.WriteFile(filepath.Join(modelDir, "gallery-model.yaml"), cfgContent, 0644) |
| 118 | + Expect(err).ToNot(HaveOccurred()) |
| 119 | + Expect(mcl.ReadModelConfig(filepath.Join(modelDir, "gallery-model.yaml"))).To(Succeed()) |
| 120 | + |
| 121 | + ml := model.NewModelLoader(ss) |
| 122 | + re := NewRequestExtractor(mcl, ml, appConfig) |
| 123 | + app = newRequestApp(re) |
| 124 | + }) |
| 125 | + |
| 126 | + It("passes through to the handler", func() { |
| 127 | + rec := postJSON(app, "/v1/chat/completions", |
| 128 | + `{"model":"gallery-model","messages":[{"role":"user","content":"hi"}]}`) |
| 129 | + |
| 130 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + Context("when the model name contains a slash (HuggingFace ID)", func() { |
| 135 | + It("skips the existence check and passes through", func() { |
| 136 | + rec := postJSON(app, "/v1/chat/completions", |
| 137 | + `{"model":"stabilityai/stable-diffusion-xl-base-1.0","messages":[{"role":"user","content":"hi"}]}`) |
| 138 | + |
| 139 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + Context("when no model is specified", func() { |
| 144 | + It("passes through without checking", func() { |
| 145 | + rec := postJSON(app, "/v1/chat/completions", |
| 146 | + `{"messages":[{"role":"user","content":"hi"}]}`) |
| 147 | + |
| 148 | + // No model name → middleware doesn't reject, handler runs |
| 149 | + Expect(rec.Code).To(Equal(http.StatusOK)) |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments