MCPcopy
hub / github.com/QuantumNous/new-api / EmbeddingHelper

Function EmbeddingHelper

relay/embedding_handler.go:19–94  ·  view source on GitHub ↗
(c *gin.Context, info *relaycommon.RelayInfo)

Source from the content-addressed store, hash-verified

17)
18
19func EmbeddingHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
20 info.InitChannelMeta(c)
21
22 embeddingReq, ok := info.Request.(*dto.EmbeddingRequest)
23 if !ok {
24 return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected *dto.EmbeddingRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
25 }
26
27 request, err := common.DeepCopy(embeddingReq)
28 if err != nil {
29 return types.NewError(fmt.Errorf("failed to copy request to EmbeddingRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
30 }
31
32 err = helper.ModelMappedHelper(c, info, request)
33 if err != nil {
34 return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
35 }
36
37 adaptor := GetAdaptor(info.ApiType)
38 if adaptor == nil {
39 return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
40 }
41 adaptor.Init(info)
42
43 convertedRequest, err := adaptor.ConvertEmbeddingRequest(c, info, *request)
44 if err != nil {
45 return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
46 }
47 relaycommon.AppendRequestConversionFromRequest(info, convertedRequest)
48 jsonData, err := common.Marshal(convertedRequest)
49 if err != nil {
50 return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
51 }
52
53 if len(info.ParamOverride) > 0 {
54 jsonData, err = relaycommon.ApplyParamOverrideWithRelayInfo(jsonData, info)
55 if err != nil {
56 return newAPIErrorFromParamOverride(err)
57 }
58 }
59
60 logger.LogDebug(c, "converted embedding request body: %s", jsonData)
61 body, size, closer, err := relaycommon.NewOutboundJSONBody(jsonData)
62 if err != nil {
63 return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
64 }
65 defer closer.Close()
66 jsonData = nil
67 info.UpstreamRequestBodySize = size
68 var requestBody io.Reader = body
69 statusCodeMappingStr := c.GetString("status_code_mapping")
70 resp, err := adaptor.DoRequest(c, info, requestBody)
71 if err != nil {
72 return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
73 }
74
75 var httpResp *http.Response
76 if resp != nil {

Callers 1

relayHandlerFunction · 0.92

Calls 15

NewErrorWithStatusCodeFunction · 0.92
ErrOptionWithSkipRetryFunction · 0.92
NewErrorFunction · 0.92
ModelMappedHelperFunction · 0.92
LogDebugFunction · 0.92
NewOpenAIErrorFunction · 0.92
RelayErrorHandlerFunction · 0.92
ResetStatusCodeFunction · 0.92
PostTextConsumeQuotaFunction · 0.92
GetAdaptorFunction · 0.85
InitChannelMetaMethod · 0.80

Tested by

no test coverage detected