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

Method SetOpenAIRequest

core/http/middleware/request.go:210–264  ·  view source on GitHub ↗
(c echo.Context)

Source from the content-addressed store, hash-verified

208}
209
210func (re *RequestExtractor) SetOpenAIRequest(c echo.Context) error {
211 input, ok := c.Get(CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.OpenAIRequest)
212 if !ok || input.Model == "" {
213 return echo.ErrBadRequest
214 }
215
216 cfg, ok := c.Get(CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig)
217 if !ok || cfg == nil {
218 return echo.ErrBadRequest
219 }
220
221 // Extract or generate the correlation ID
222 correlationID := c.Request().Header.Get("X-Correlation-ID")
223 if correlationID == "" {
224 correlationID = uuid.New().String()
225 }
226 c.Response().Header().Set("X-Correlation-ID", correlationID)
227
228 // Use the request context directly - Echo properly supports context cancellation!
229 // No need for workarounds like handleConnectionCancellation
230 reqCtx := c.Request().Context()
231 c1, cancel := context.WithCancel(re.applicationConfig.Context)
232
233 // Cancel when request context is cancelled (client disconnects)
234 go func() {
235 select {
236 case <-reqCtx.Done():
237 cancel()
238 case <-c1.Done():
239 // Already cancelled
240 }
241 }()
242
243 // Add the correlation ID to the new context
244 ctxWithCorrelationID := context.WithValue(c1, CorrelationIDKey, correlationID)
245 ctxWithCorrelationID = distributedhdr.Inherit(ctxWithCorrelationID, reqCtx)
246
247 input.Context = ctxWithCorrelationID
248 input.Cancel = cancel
249
250 err := mergeOpenAIRequestAndModelConfig(cfg, input)
251 if err != nil {
252 return err
253 }
254
255 if cfg.Model == "" {
256 xlog.Debug("replacing empty cfg.Model with input value", "input.Model", input.Model)
257 cfg.Model = input.Model
258 }
259
260 c.Set(CONTEXT_LOCALS_KEY_LOCALAI_REQUEST, input)
261 c.Set(CONTEXT_LOCALS_KEY_MODEL_CONFIG, cfg)
262
263 return nil
264}
265
266// extractToolChoiceFunctionName parses a tool_choice map and returns the
267// specific function name. Accepts both the OpenAI-spec nested shape

Callers 3

request_test.goFile · 0.80
RegisterLocalAIRoutesFunction · 0.80
RegisterOpenAIRoutesFunction · 0.80

Calls 8

InheritFunction · 0.92
HeaderMethod · 0.80
GetMethod · 0.65
RequestMethod · 0.65
StringMethod · 0.65
SetMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected