MCPcopy Create free account
hub / github.com/bricks-cloud/BricksLLM / getEmbeddingHandler

Function getEmbeddingHandler

internal/server/web/proxy/embedding.go:33–172  ·  view source on GitHub ↗
(prod, private bool, client http.Client, e estimator)

Source from the content-addressed store, hash-verified

31}
32
33func 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)

Callers 1

NewProxyServerFunction · 0.85

Calls 13

GetLogFromCtxFunction · 0.92
IncrFunction · 0.92
TimingFunction · 0.92
EstimateCostWithCostMapFunction · 0.92
JSONFunction · 0.85
copyHttpHeadersFunction · 0.85
logEmbeddingResponseFunction · 0.85
logOpenAiErrorFunction · 0.85
logErrorFunction · 0.70
GetMethod · 0.65

Tested by

no test coverage detected