This commit introduces a major architectural refactoring to improve quota management, centralize logging, and streamline the relay handling logic. Key changes: - **Pre-consume Quota:** Implements a new mechanism to check and reserve user quota *before* making the request to the upstream provider. This ensures more accurate quota deduction and prevents users from exceeding their limits due to concurrent requests. - **Unified Relay Handlers:** Refactors the relay logic to use generic handlers (e.g., `ChatHandler`, `ImageHandler`) instead of provider-specific implementations. This significantly reduces code duplication and simplifies adding new channels. - **Centralized Logger:** A new dedicated `logger` package is introduced, and all system logging calls are migrated to use it, moving this responsibility out of the `common` package. - **Code Reorganization:** DTOs are generalized (e.g., `dalle.go` -> `openai_image.go`) and utility code is moved to more appropriate packages (e.g., `common/http.go` -> `service/http.go`) for better code structure.
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package common_handler
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"one-api/common"
|
|
"one-api/constant"
|
|
"one-api/dto"
|
|
"one-api/relay/channel/xinference"
|
|
relaycommon "one-api/relay/common"
|
|
"one-api/service"
|
|
"one-api/types"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func RerankHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*dto.Usage, *types.NewAPIError) {
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, types.NewOpenAIError(err, types.ErrorCodeReadResponseBodyFailed, http.StatusInternalServerError)
|
|
}
|
|
service.CloseResponseBodyGracefully(resp)
|
|
if common.DebugEnabled {
|
|
println("reranker response body: ", string(responseBody))
|
|
}
|
|
var jinaResp dto.RerankResponse
|
|
if info.ChannelType == constant.ChannelTypeXinference {
|
|
var xinRerankResponse xinference.XinRerankResponse
|
|
err = common.Unmarshal(responseBody, &xinRerankResponse)
|
|
if err != nil {
|
|
return nil, types.NewOpenAIError(err, types.ErrorCodeBadResponseBody, http.StatusInternalServerError)
|
|
}
|
|
jinaRespResults := make([]dto.RerankResponseResult, len(xinRerankResponse.Results))
|
|
for i, result := range xinRerankResponse.Results {
|
|
respResult := dto.RerankResponseResult{
|
|
Index: result.Index,
|
|
RelevanceScore: result.RelevanceScore,
|
|
}
|
|
if info.ReturnDocuments {
|
|
var document any
|
|
if result.Document != nil {
|
|
if doc, ok := result.Document.(string); ok {
|
|
if doc == "" {
|
|
document = info.Documents[result.Index]
|
|
} else {
|
|
document = doc
|
|
}
|
|
} else {
|
|
document = result.Document
|
|
}
|
|
}
|
|
respResult.Document = document
|
|
}
|
|
jinaRespResults[i] = respResult
|
|
}
|
|
jinaResp = dto.RerankResponse{
|
|
Results: jinaRespResults,
|
|
Usage: dto.Usage{
|
|
PromptTokens: info.PromptTokens,
|
|
TotalTokens: info.PromptTokens,
|
|
},
|
|
}
|
|
} else {
|
|
err = common.Unmarshal(responseBody, &jinaResp)
|
|
if err != nil {
|
|
return nil, types.NewOpenAIError(err, types.ErrorCodeBadResponseBody, http.StatusInternalServerError)
|
|
}
|
|
jinaResp.Usage.PromptTokens = jinaResp.Usage.TotalTokens
|
|
}
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
c.JSON(http.StatusOK, jinaResp)
|
|
return &jinaResp.Usage, nil
|
|
}
|