MCPcopy
hub / github.com/mudler/LocalAI / SetModelAndConfig

Method SetModelAndConfig

core/http/middleware/request.go:123–208  ·  view source on GitHub ↗

TODO: If context and cancel above belong on all methods, move that part of above into here! Otherwise, it's in its own method below for now

(initializer func() schema.LocalAIRequest)

Source from the content-addressed store, hash-verified

121// TODO: If context and cancel above belong on all methods, move that part of above into here!
122// Otherwise, it's in its own method below for now
123func (re *RequestExtractor) SetModelAndConfig(initializer func() schema.LocalAIRequest) echo.MiddlewareFunc {
124 return func(next echo.HandlerFunc) echo.HandlerFunc {
125 return func(c echo.Context) error {
126 input := initializer()
127 if input == nil {
128 return echo.NewHTTPError(http.StatusBadRequest, "unable to initialize body")
129 }
130 if err := c.Bind(input); err != nil {
131 return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("failed parsing request body: %v", err))
132 }
133
134 // If this request doesn't have an associated model name, fetch it from earlier in the middleware chain
135 if input.ModelName(nil) == "" {
136 localModelName, ok := c.Get(CONTEXT_LOCALS_KEY_MODEL_NAME).(string)
137 if ok && localModelName != "" {
138 xlog.Debug("overriding empty model name in request body with value found earlier in middleware chain", "context localModelName", localModelName)
139 input.ModelName(&localModelName)
140 }
141 }
142
143 modelName := input.ModelName(nil)
144 cfg, err := re.modelConfigLoader.LoadModelConfigFileByNameDefaultOptions(modelName, re.applicationConfig)
145
146 if err != nil {
147 xlog.Warn("Model Configuration File not found", "model", modelName, "error", err)
148 } else if cfg.Model == "" && modelName != "" {
149 xlog.Debug("config does not include model, using input", "input.ModelName", modelName)
150 cfg.Model = modelName
151 }
152
153 // If a model name was specified, verify it actually exists before proceeding.
154 // Check both configured models and loose model files in the model path.
155 // Skip the check for HuggingFace model IDs (contain "/") since backends
156 // like diffusers may download these on the fly.
157 if modelName != "" && !strings.Contains(modelName, "/") {
158 exists, existsErr := galleryop.CheckIfModelExists(re.modelConfigLoader, re.modelLoader, modelName, galleryop.ALWAYS_INCLUDE)
159 if existsErr == nil && !exists {
160 return c.JSON(http.StatusNotFound, schema.ErrorResponse{
161 Error: &schema.APIError{
162 Message: fmt.Sprintf("model %q not found. To see available models, call GET /v1/models", modelName),
163 Code: http.StatusNotFound,
164 Type: "invalid_request_error",
165 },
166 })
167 }
168 }
169
170 // Resolve a model alias to its target before the disabled check and
171 // before storing MODEL_CONFIG, so every modality (chat, embeddings,
172 // tts, image, ...) inherits redirection. The response keeps echoing
173 // the alias name (input.ModelName is left unchanged); usage accounting
174 // records requested=alias / served=target.
175 if cfg != nil && cfg.IsAlias() {
176 resolved, _, aliasErr := re.modelConfigLoader.ResolveAlias(cfg)
177 if aliasErr != nil {
178 return c.JSON(http.StatusBadRequest, schema.ErrorResponse{
179 Error: &schema.APIError{
180 Message: aliasErr.Error(),

Callers 9

newRequestAppFunction · 0.80
request_test.goFile · 0.80
RegisterJINARoutesFunction · 0.80
RegisterElevenLabsRoutesFunction · 0.80
RegisterOllamaRoutesFunction · 0.80
RegisterAnthropicRoutesFunction · 0.80
RegisterLocalAIRoutesFunction · 0.80
RegisterOpenAIRoutesFunction · 0.80

Calls 12

CheckIfModelExistsFunction · 0.92
ContainsMethod · 0.80
JSONMethod · 0.80
IsAliasMethod · 0.80
ResolveAliasMethod · 0.80
IsDisabledMethod · 0.80
ModelNameMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
nextFunction · 0.50
ErrorMethod · 0.45

Tested by 1

newRequestAppFunction · 0.64