(prod, private bool, client http.Client)
| 280 | } |
| 281 | |
| 282 | func getPassThroughHandler(prod, private bool, client http.Client) gin.HandlerFunc { |
| 283 | return func(c *gin.Context) { |
| 284 | log := util.GetLogFromCtx(c) |
| 285 | |
| 286 | tags := []string{ |
| 287 | fmt.Sprintf("path:%s", c.FullPath()), |
| 288 | } |
| 289 | |
| 290 | telemetry.Incr("bricksllm.proxy.get_pass_through_handler.requests", tags, 1) |
| 291 | |
| 292 | if c == nil || c.Request == nil { |
| 293 | JSON(c, http.StatusInternalServerError, "[BricksLLM] context is empty") |
| 294 | return |
| 295 | } |
| 296 | |
| 297 | ctx, cancel := context.WithTimeout(context.Background(), c.GetDuration("requestTimeout")) |
| 298 | defer cancel() |
| 299 | |
| 300 | targetUrl, err := buildProxyUrl(c) |
| 301 | if err != nil { |
| 302 | telemetry.Incr("bricksllm.proxy.get_pass_through_handler.proxy_url_not_found", tags, 1) |
| 303 | logError(log, "error when building proxy url", prod, err) |
| 304 | JSON(c, http.StatusNotFound, "[BricksLLM] cannot find corresponding proxy url") |
| 305 | return |
| 306 | } |
| 307 | |
| 308 | req, err := http.NewRequestWithContext(ctx, c.Request.Method, targetUrl, c.Request.Body) |
| 309 | if err != nil { |
| 310 | logError(log, "error when creating openai http request", prod, err) |
| 311 | JSON(c, http.StatusInternalServerError, "[BricksLLM] failed to create openai http request") |
| 312 | return |
| 313 | } |
| 314 | |
| 315 | // copy query params |
| 316 | req.URL.RawQuery = c.Request.URL.RawQuery |
| 317 | |
| 318 | copyHttpHeaders(c.Request, req, c.GetBool("removeUserAgent")) |
| 319 | |
| 320 | if c.FullPath() == "/api/providers/openai/v1/files" && c.Request.Method == http.MethodPost { |
| 321 | purpose := c.PostForm("purpose") |
| 322 | |
| 323 | var b bytes.Buffer |
| 324 | writer := multipart.NewWriter(&b) |
| 325 | err := writer.WriteField("purpose", purpose) |
| 326 | if err != nil { |
| 327 | telemetry.Incr("bricksllm.proxy.get_pass_through_handler.write_field_error", tags, 1) |
| 328 | logError(log, "error when writing field", prod, err) |
| 329 | JSON(c, http.StatusInternalServerError, "[BricksLLM] cannot write field") |
| 330 | return |
| 331 | } |
| 332 | |
| 333 | var form Form |
| 334 | c.ShouldBind(&form) |
| 335 | |
| 336 | fieldWriter, err := writer.CreateFormFile("file", form.File.Filename) |
| 337 | if err != nil { |
| 338 | telemetry.Incr("bricksllm.proxy.get_pass_through_handler.create_form_file_error", tags, 1) |
| 339 | logError(log, "error when creating form file", prod, err) |
no test coverage detected