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

Function ForwardViaBackend

core/services/cloudproxy/backend_forward.go:24–153  ·  view source on GitHub ↗

ForwardViaBackend loads the cloud-proxy gRPC backend, ships the request via the Forward RPC, and pumps the response back to the client. PII redaction runs request-side (the NER middleware + MITM input path); the response is forwarded unmodified.

(
	c echo.Context,
	cfg *config.ModelConfig,
	body []byte,
	loader *model.ModelLoader,
	appConfig *config.ApplicationConfig,
)

Source from the content-addressed store, hash-verified

22// client. PII redaction runs request-side (the NER middleware + MITM
23// input path); the response is forwarded unmodified.
24func ForwardViaBackend(
25 c echo.Context,
26 cfg *config.ModelConfig,
27 body []byte,
28 loader *model.ModelLoader,
29 appConfig *config.ApplicationConfig,
30) (resultErr error) {
31 // Passthrough forwards bypass core/backend/llm.go and therefore its
32 // trace.RecordBackendTrace call — instrument here so passthrough
33 // requests show up in the Traces UI alongside translate-mode ones.
34 // Named return is unusual for this package but lets the defer capture
35 // the final error across the function's many early-return paths
36 // without rewriting them.
37 var startTime time.Time
38 statusCode := 0
39 if appConfig.EnableTracing {
40 trace.InitBackendTracingIfEnabled(appConfig.TracingMaxItems, appConfig.TracingMaxBodyBytes)
41 startTime = time.Now()
42 }
43 defer func() {
44 if !appConfig.EnableTracing {
45 return
46 }
47 errStr := ""
48 if resultErr != nil {
49 errStr = resultErr.Error()
50 }
51 data := map[string]any{
52 "mode": cfg.Proxy.Mode,
53 "provider": cfg.Proxy.Provider,
54 "upstream": cfg.Proxy.UpstreamURL,
55 "upstream_model": cfg.Proxy.UpstreamModel,
56 }
57 if statusCode != 0 {
58 data["status"] = statusCode
59 }
60 trace.RecordBackendTrace(trace.BackendTrace{
61 Timestamp: startTime,
62 Duration: time.Since(startTime),
63 Type: trace.BackendTraceLLM,
64 ModelName: cfg.Name,
65 Backend: cfg.Backend,
66 Summary: trace.TruncateBytes(body, 200),
67 Body: trace.TruncateBytes(body, trace.MaxTraceBodyBytes),
68 Error: errStr,
69 Data: data,
70 })
71 }()
72
73 if cfg.Proxy.UpstreamURL == "" {
74 return echo.NewHTTPError(http.StatusInternalServerError,
75 fmt.Sprintf("cloudproxy: proxy.upstream_url empty for model %q", cfg.Name))
76 }
77
78 body, err := rewriteModel(body, cfg.Proxy.UpstreamModel)
79 if err != nil {
80 return echo.NewHTTPError(http.StatusBadRequest, err.Error())
81 }

Calls 15

RecordBackendTraceFunction · 0.92
TruncateBytesFunction · 0.92
rewriteModelFunction · 0.85
streamingFunction · 0.85
passthroughErrorFunction · 0.85
forwardStreamFunction · 0.85
forwardBufferedFunction · 0.85
LoadMethod · 0.65
ContextMethod · 0.65
RequestMethod · 0.65
ForwardMethod · 0.65

Tested by

no test coverage detected