(prod, private bool, client http.Client, e estimator)
| 31 | } |
| 32 | |
| 33 | func getEmbeddingHandler(prod, private bool, client http.Client, e estimator) gin.HandlerFunc { |
| 34 | return func(c *gin.Context) { |
| 35 | log := util.GetLogFromCtx(c) |
| 36 | telemetry.Incr("bricksllm.proxy.get_embedding_handler.requests", nil, 1) |
| 37 | if c == nil || c.Request == nil { |
| 38 | JSON(c, http.StatusInternalServerError, "[BricksLLM] context is empty") |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | // raw, exists := c.Get("key") |
| 43 | // kc, ok := raw.(*key.ResponseKey) |
| 44 | // if !exists || !ok { |
| 45 | // telemetry.Incr("bricksllm.proxy.get_embedding_handler.api_key_not_registered", nil, 1) |
| 46 | // JSON(c, http.StatusUnauthorized, "[BricksLLM] api key is not registered") |
| 47 | // return |
| 48 | // } |
| 49 | |
| 50 | ctx, cancel := context.WithTimeout(context.Background(), c.GetDuration("requestTimeout")) |
| 51 | defer cancel() |
| 52 | |
| 53 | req, err := http.NewRequestWithContext(ctx, c.Request.Method, "https://api.openai.com/v1/embeddings", c.Request.Body) |
| 54 | if err != nil { |
| 55 | logError(log, "error when creating openai http request", prod, err) |
| 56 | JSON(c, http.StatusInternalServerError, "[BricksLLM] failed to create openai http request") |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | copyHttpHeaders(c.Request, req, c.GetBool("removeUserAgent")) |
| 61 | |
| 62 | start := time.Now() |
| 63 | |
| 64 | res, err := client.Do(req) |
| 65 | if err != nil { |
| 66 | telemetry.Incr("bricksllm.proxy.get_embedding_handler.http_client_error", nil, 1) |
| 67 | |
| 68 | logError(log, "error when sending embedding request to openai", prod, err) |
| 69 | JSON(c, http.StatusInternalServerError, "[BricksLLM] failed to send embedding request to openai") |
| 70 | return |
| 71 | } |
| 72 | defer res.Body.Close() |
| 73 | |
| 74 | dur := time.Since(start) |
| 75 | telemetry.Timing("bricksllm.proxy.get_embedding_handler.latency", dur, nil, 1) |
| 76 | |
| 77 | bytes, err := io.ReadAll(res.Body) |
| 78 | if err != nil { |
| 79 | logError(log, "error when reading openai embedding response body", prod, err) |
| 80 | JSON(c, http.StatusInternalServerError, "[BricksLLM] failed to read openai embedding response body") |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | var cost float64 = 0 |
| 85 | chatRes := &EmbeddingResponse{} |
| 86 | promptTokenCounts := 0 |
| 87 | base64ChatRes := &EmbeddingResponseBase64{} |
| 88 | if res.StatusCode == http.StatusOK { |
| 89 | telemetry.Incr("bricksllm.proxy.get_embedding_handler.success", nil, 1) |
| 90 | telemetry.Timing("bricksllm.proxy.get_embedding_handler.success_latency", dur, nil, 1) |
no test coverage detected