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

Function getTranscriptionsHandler

internal/server/web/proxy/audio.go:170–332  ·  view source on GitHub ↗
(prod bool, client http.Client, e estimator)

Source from the content-addressed store, hash-verified

168}
169
170func getTranscriptionsHandler(prod bool, client http.Client, e estimator) gin.HandlerFunc {
171 return func(c *gin.Context) {
172 log := util.GetLogFromCtx(c)
173 telemetry.Incr("bricksllm.proxy.get_transcriptions_handler.requests", nil, 1)
174
175 if c == nil || c.Request == nil {
176 JSON(c, http.StatusInternalServerError, "[BricksLLM] context is empty")
177 return
178 }
179
180 ctx, cancel := context.WithTimeout(context.Background(), c.GetDuration("requestTimeout"))
181 defer cancel()
182
183 req, err := http.NewRequestWithContext(ctx, c.Request.Method, "https://api.openai.com/v1/audio/transcriptions", c.Request.Body)
184 if err != nil {
185 logError(log, "error when creating transcriptions openai http request", prod, err)
186 JSON(c, http.StatusInternalServerError, "[BricksLLM] failed to create openai transcriptions http request")
187 return
188 }
189
190 copyHttpHeaders(c.Request, req, c.GetBool("removeUserAgent"))
191
192 var b bytes.Buffer
193 writer := multipart.NewWriter(&b)
194
195 err = writeFieldToBuffer([]string{
196 "model",
197 "language",
198 "prompt",
199 "response_format",
200 "temperature",
201 }, c, writer, map[string]string{
202 "response_format": "verbose_json",
203 })
204 if err != nil {
205 telemetry.Incr("bricksllm.proxy.get_transcriptions_handler.write_field_to_buffer_error", nil, 1)
206 logError(log, "error when writing field to buffer", prod, err)
207 JSON(c, http.StatusInternalServerError, "[BricksLLM] cannot write field to buffer")
208 return
209 }
210
211 var form TransriptionForm
212 c.ShouldBind(&form)
213
214 if form.File != nil {
215 fieldWriter, err := writer.CreateFormFile("file", form.File.Filename)
216 if err != nil {
217 telemetry.Incr("bricksllm.proxy.get_transcriptions_handler.create_transcription_file_error", nil, 1)
218 logError(log, "error when creating transcription file", prod, err)
219 JSON(c, http.StatusInternalServerError, "[BricksLLM] cannot create transcription file")
220 return
221 }
222
223 opened, err := form.File.Open()
224 if err != nil {
225 telemetry.Incr("bricksllm.proxy.get_transcriptions_handler.open_transcription_file_error", nil, 1)
226 logError(log, "error when openning transcription file", prod, err)
227 JSON(c, http.StatusInternalServerError, "[BricksLLM] cannot open transcription file")

Callers 1

NewProxyServerFunction · 0.85

Calls 13

GetLogFromCtxFunction · 0.92
IncrFunction · 0.92
TimingFunction · 0.92
JSONFunction · 0.85
copyHttpHeadersFunction · 0.85
writeFieldToBufferFunction · 0.85
getContentTypeFunction · 0.85
convertVerboseJsonFunction · 0.85
logOpenAiErrorFunction · 0.85
logErrorFunction · 0.70
SetMethod · 0.65

Tested by

no test coverage detected