| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- package relay
- import (
- "bytes"
- "fmt"
- "io"
- "net/http"
- "strings"
- "time"
- "github.com/QuantumNous/new-api/common"
- "github.com/QuantumNous/new-api/constant"
- "github.com/QuantumNous/new-api/dto"
- "github.com/QuantumNous/new-api/logger"
- "github.com/QuantumNous/new-api/model"
- "github.com/QuantumNous/new-api/pkg/billingexpr"
- relaycommon "github.com/QuantumNous/new-api/relay/common"
- relayconstant "github.com/QuantumNous/new-api/relay/constant"
- "github.com/QuantumNous/new-api/relay/helper"
- "github.com/QuantumNous/new-api/service"
- "github.com/QuantumNous/new-api/setting/model_setting"
- "github.com/QuantumNous/new-api/setting/operation_setting"
- "github.com/QuantumNous/new-api/setting/ratio_setting"
- "github.com/QuantumNous/new-api/types"
- "github.com/samber/lo"
- "github.com/shopspring/decimal"
- "github.com/gin-gonic/gin"
- )
- func TextHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
- info.InitChannelMeta(c)
- textReq, ok := info.Request.(*dto.GeneralOpenAIRequest)
- if !ok {
- return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected dto.GeneralOpenAIRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
- }
- request, err := common.DeepCopy(textReq)
- if err != nil {
- return types.NewError(fmt.Errorf("failed to copy request to GeneralOpenAIRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
- }
- if request.WebSearchOptions != nil {
- c.Set("chat_completion_web_search_context_size", request.WebSearchOptions.SearchContextSize)
- }
- err = helper.ModelMappedHelper(c, info, request)
- if err != nil {
- return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
- }
- includeUsage := true
- // 判断用户是否需要返回使用情况
- if request.StreamOptions != nil {
- includeUsage = request.StreamOptions.IncludeUsage
- }
- // 如果不支持StreamOptions,将StreamOptions设置为nil
- if !info.SupportStreamOptions || !lo.FromPtrOr(request.Stream, false) {
- request.StreamOptions = nil
- } else {
- // 如果支持StreamOptions,且请求中没有设置StreamOptions,根据配置文件设置StreamOptions
- if constant.ForceStreamOption {
- request.StreamOptions = &dto.StreamOptions{
- IncludeUsage: true,
- }
- }
- }
- info.ShouldIncludeUsage = includeUsage
- adaptor := GetAdaptor(info.ApiType)
- if adaptor == nil {
- return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
- }
- adaptor.Init(info)
- passThroughGlobal := model_setting.GetGlobalSettings().PassThroughRequestEnabled
- if info.RelayMode == relayconstant.RelayModeChatCompletions &&
- !passThroughGlobal &&
- !info.ChannelSetting.PassThroughBodyEnabled &&
- service.ShouldChatCompletionsUseResponsesGlobal(info.ChannelId, info.ChannelType, info.OriginModelName) {
- applySystemPromptIfNeeded(c, info, request)
- usage, newApiErr := chatCompletionsViaResponses(c, info, adaptor, request)
- if newApiErr != nil {
- return newApiErr
- }
- var containAudioTokens = usage.CompletionTokenDetails.AudioTokens > 0 || usage.PromptTokensDetails.AudioTokens > 0
- var containsAudioRatios = ratio_setting.ContainsAudioRatio(info.OriginModelName) || ratio_setting.ContainsAudioCompletionRatio(info.OriginModelName)
- if containAudioTokens && containsAudioRatios {
- service.PostAudioConsumeQuota(c, info, usage, "")
- } else {
- postConsumeQuota(c, info, usage)
- }
- return nil
- }
- var requestBody io.Reader
- if passThroughGlobal || info.ChannelSetting.PassThroughBodyEnabled {
- storage, err := common.GetBodyStorage(c)
- if err != nil {
- return types.NewErrorWithStatusCode(err, types.ErrorCodeReadRequestBodyFailed, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
- }
- if common.DebugEnabled {
- if debugBytes, bErr := storage.Bytes(); bErr == nil {
- println("requestBody: ", string(debugBytes))
- }
- }
- requestBody = common.ReaderOnly(storage)
- } else {
- convertedRequest, err := adaptor.ConvertOpenAIRequest(c, info, request)
- if err != nil {
- return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
- }
- relaycommon.AppendRequestConversionFromRequest(info, convertedRequest)
- if info.ChannelSetting.SystemPrompt != "" {
- // 如果有系统提示,则将其添加到请求中
- request, ok := convertedRequest.(*dto.GeneralOpenAIRequest)
- if ok {
- containSystemPrompt := false
- for _, message := range request.Messages {
- if message.Role == request.GetSystemRoleName() {
- containSystemPrompt = true
- break
- }
- }
- if !containSystemPrompt {
- // 如果没有系统提示,则添加系统提示
- systemMessage := dto.Message{
- Role: request.GetSystemRoleName(),
- Content: info.ChannelSetting.SystemPrompt,
- }
- request.Messages = append([]dto.Message{systemMessage}, request.Messages...)
- } else if info.ChannelSetting.SystemPromptOverride {
- common.SetContextKey(c, constant.ContextKeySystemPromptOverride, true)
- // 如果有系统提示,且允许覆盖,则拼接到前面
- for i, message := range request.Messages {
- if message.Role == request.GetSystemRoleName() {
- if message.IsStringContent() {
- request.Messages[i].SetStringContent(info.ChannelSetting.SystemPrompt + "\n" + message.StringContent())
- } else {
- contents := message.ParseContent()
- contents = append([]dto.MediaContent{
- {
- Type: dto.ContentTypeText,
- Text: info.ChannelSetting.SystemPrompt,
- },
- }, contents...)
- request.Messages[i].Content = contents
- }
- break
- }
- }
- }
- }
- }
- jsonData, err := common.Marshal(convertedRequest)
- if err != nil {
- return types.NewError(err, types.ErrorCodeJsonMarshalFailed, types.ErrOptionWithSkipRetry())
- }
- // remove disabled fields for OpenAI API
- jsonData, err = relaycommon.RemoveDisabledFields(jsonData, info.ChannelOtherSettings, info.ChannelSetting.PassThroughBodyEnabled)
- if err != nil {
- return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
- }
- // apply param override
- if len(info.ParamOverride) > 0 {
- jsonData, err = relaycommon.ApplyParamOverrideWithRelayInfo(jsonData, info)
- if err != nil {
- return newAPIErrorFromParamOverride(err)
- }
- }
- logger.LogDebug(c, fmt.Sprintf("text request body: %s", string(jsonData)))
- requestBody = bytes.NewBuffer(jsonData)
- }
- var httpResp *http.Response
- resp, err := adaptor.DoRequest(c, info, requestBody)
- if err != nil {
- return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
- }
- statusCodeMappingStr := c.GetString("status_code_mapping")
- if resp != nil {
- httpResp = resp.(*http.Response)
- info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
- if httpResp.StatusCode != http.StatusOK {
- newApiErr := service.RelayErrorHandler(c.Request.Context(), httpResp, false)
- // reset status code 重置状态码
- service.ResetStatusCode(newApiErr, statusCodeMappingStr)
- return newApiErr
- }
- }
- usage, newApiErr := adaptor.DoResponse(c, httpResp, info)
- if newApiErr != nil {
- // reset status code 重置状态码
- service.ResetStatusCode(newApiErr, statusCodeMappingStr)
- return newApiErr
- }
- var containAudioTokens = usage.(*dto.Usage).CompletionTokenDetails.AudioTokens > 0 || usage.(*dto.Usage).PromptTokensDetails.AudioTokens > 0
- var containsAudioRatios = ratio_setting.ContainsAudioRatio(info.OriginModelName) || ratio_setting.ContainsAudioCompletionRatio(info.OriginModelName)
- if containAudioTokens && containsAudioRatios {
- service.PostAudioConsumeQuota(c, info, usage.(*dto.Usage), "")
- } else {
- postConsumeQuota(c, info, usage.(*dto.Usage))
- }
- return nil
- }
- func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage *dto.Usage, extraContent ...string) {
- originUsage := usage
- if usage == nil {
- usage = &dto.Usage{
- PromptTokens: relayInfo.GetEstimatePromptTokens(),
- CompletionTokens: 0,
- TotalTokens: relayInfo.GetEstimatePromptTokens(),
- }
- extraContent = append(extraContent, "上游无计费信息")
- }
- if originUsage != nil {
- service.ObserveChannelAffinityUsageCacheByRelayFormat(ctx, usage, relayInfo.GetFinalRequestRelayFormat())
- }
- // Tiered billing: only determines quota, logging continues through normal path
- isClaudeUsageSemantic := relayInfo.GetFinalRequestRelayFormat() == types.RelayFormatClaude
- var tieredUsedVars map[string]bool
- if snap := relayInfo.TieredBillingSnapshot; snap != nil {
- tieredUsedVars = billingexpr.UsedVars(snap.ExprString)
- }
- var tieredResult *billingexpr.TieredResult
- tieredOk, tieredQuota, tieredRes := service.TryTieredSettle(relayInfo, service.BuildTieredTokenParams(usage, isClaudeUsageSemantic, tieredUsedVars))
- if tieredOk {
- tieredResult = tieredRes
- }
- adminRejectReason := common.GetContextKeyString(ctx, constant.ContextKeyAdminRejectReason)
- useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
- promptTokens := usage.PromptTokens
- cacheTokens := usage.PromptTokensDetails.CachedTokens
- imageTokens := usage.PromptTokensDetails.ImageTokens
- audioTokens := usage.PromptTokensDetails.AudioTokens
- completionTokens := usage.CompletionTokens
- cachedCreationTokens := usage.PromptTokensDetails.CachedCreationTokens
- modelName := relayInfo.OriginModelName
- tokenName := ctx.GetString("token_name")
- completionRatio := relayInfo.PriceData.CompletionRatio
- cacheRatio := relayInfo.PriceData.CacheRatio
- imageRatio := relayInfo.PriceData.ImageRatio
- modelRatio := relayInfo.PriceData.ModelRatio
- groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio
- modelPrice := relayInfo.PriceData.ModelPrice
- cachedCreationRatio := relayInfo.PriceData.CacheCreationRatio
- // Convert values to decimal for precise calculation
- dPromptTokens := decimal.NewFromInt(int64(promptTokens))
- dCacheTokens := decimal.NewFromInt(int64(cacheTokens))
- dImageTokens := decimal.NewFromInt(int64(imageTokens))
- dAudioTokens := decimal.NewFromInt(int64(audioTokens))
- dCompletionTokens := decimal.NewFromInt(int64(completionTokens))
- dCachedCreationTokens := decimal.NewFromInt(int64(cachedCreationTokens))
- dCompletionRatio := decimal.NewFromFloat(completionRatio)
- dCacheRatio := decimal.NewFromFloat(cacheRatio)
- dImageRatio := decimal.NewFromFloat(imageRatio)
- dModelRatio := decimal.NewFromFloat(modelRatio)
- dGroupRatio := decimal.NewFromFloat(groupRatio)
- dModelPrice := decimal.NewFromFloat(modelPrice)
- dCachedCreationRatio := decimal.NewFromFloat(cachedCreationRatio)
- dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit)
- ratio := dModelRatio.Mul(dGroupRatio)
- // Collect tool call usage from context and relayInfo
- toolUsage := service.ToolCallUsage{
- ModelName: modelName,
- ImageGenerationCall: ctx.GetBool("image_generation_call"),
- ImageGenerationQuality: ctx.GetString("image_generation_call_quality"),
- ImageGenerationSize: ctx.GetString("image_generation_call_size"),
- }
- if relayInfo.ResponsesUsageInfo != nil {
- if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists && webSearchTool.CallCount > 0 {
- toolUsage.WebSearchCalls = webSearchTool.CallCount
- toolUsage.WebSearchToolName = dto.BuildInToolWebSearchPreview
- }
- if fileSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolFileSearch]; exists {
- toolUsage.FileSearchCalls = fileSearchTool.CallCount
- }
- } else if strings.HasSuffix(modelName, "search-preview") {
- toolUsage.WebSearchCalls = 1
- toolUsage.WebSearchToolName = dto.BuildInToolWebSearchPreview
- }
- if claudeSearchCalls := ctx.GetInt("claude_web_search_requests"); claudeSearchCalls > 0 {
- toolUsage.WebSearchCalls = claudeSearchCalls
- toolUsage.WebSearchToolName = "web_search"
- }
- toolResult := service.ComputeToolCallQuota(toolUsage, groupRatio)
- for _, item := range toolResult.Items {
- extraContent = append(extraContent, fmt.Sprintf("%s 调用 %d 次,花费 %d", item.Name, item.CallCount, item.Quota))
- }
- var quotaCalculateDecimal decimal.Decimal
- var audioInputQuota decimal.Decimal
- var audioInputPrice float64
- if !relayInfo.PriceData.UsePrice {
- baseTokens := dPromptTokens
- // 减去 cached tokens
- // Anthropic API 的 input_tokens 已经不包含缓存 tokens,不需要减去
- // OpenAI/OpenRouter 等 API 的 prompt_tokens 包含缓存 tokens,需要减去
- var cachedTokensWithRatio decimal.Decimal
- if !dCacheTokens.IsZero() {
- if !isClaudeUsageSemantic {
- baseTokens = baseTokens.Sub(dCacheTokens)
- }
- cachedTokensWithRatio = dCacheTokens.Mul(dCacheRatio)
- }
- var dCachedCreationTokensWithRatio decimal.Decimal
- if !dCachedCreationTokens.IsZero() {
- if !isClaudeUsageSemantic {
- baseTokens = baseTokens.Sub(dCachedCreationTokens)
- }
- dCachedCreationTokensWithRatio = dCachedCreationTokens.Mul(dCachedCreationRatio)
- }
- // 减去 image tokens
- var imageTokensWithRatio decimal.Decimal
- if !dImageTokens.IsZero() {
- baseTokens = baseTokens.Sub(dImageTokens)
- imageTokensWithRatio = dImageTokens.Mul(dImageRatio)
- }
- // 减去 Gemini audio tokens
- if !dAudioTokens.IsZero() {
- audioInputPrice = operation_setting.GetGeminiInputAudioPricePerMillionTokens(modelName)
- if audioInputPrice > 0 {
- // 重新计算 base tokens
- baseTokens = baseTokens.Sub(dAudioTokens)
- audioInputQuota = decimal.NewFromFloat(audioInputPrice).Div(decimal.NewFromInt(1000000)).Mul(dAudioTokens).Mul(dGroupRatio).Mul(dQuotaPerUnit)
- extraContent = append(extraContent, fmt.Sprintf("Audio Input 花费 %s", audioInputQuota.String()))
- }
- }
- promptQuota := baseTokens.Add(cachedTokensWithRatio).
- Add(imageTokensWithRatio).
- Add(dCachedCreationTokensWithRatio)
- completionQuota := dCompletionTokens.Mul(dCompletionRatio)
- quotaCalculateDecimal = promptQuota.Add(completionQuota).Mul(ratio)
- if !ratio.IsZero() && quotaCalculateDecimal.LessThanOrEqual(decimal.Zero) {
- quotaCalculateDecimal = decimal.NewFromInt(1)
- }
- } else {
- quotaCalculateDecimal = dModelPrice.Mul(dQuotaPerUnit).Mul(dGroupRatio)
- }
- // 添加 audio input 独立计费(Gemini 音频按 token 计价,不属于工具调用)
- quotaCalculateDecimal = quotaCalculateDecimal.Add(audioInputQuota)
- if len(relayInfo.PriceData.OtherRatios) > 0 {
- for key, otherRatio := range relayInfo.PriceData.OtherRatios {
- dOtherRatio := decimal.NewFromFloat(otherRatio)
- quotaCalculateDecimal = quotaCalculateDecimal.Mul(dOtherRatio)
- extraContent = append(extraContent, fmt.Sprintf("其他倍率 %s: %f", key, otherRatio))
- }
- }
- quota := int(quotaCalculateDecimal.Round(0).IntPart())
- if tieredOk {
- quota = tieredQuota
- }
- // Tool call fees: add for per-token and tiered billing; skip for per-call (price includes everything)
- if !relayInfo.PriceData.UsePrice && toolResult.TotalQuota > 0 {
- quota += toolResult.TotalQuota
- }
- totalTokens := promptTokens + completionTokens
- // record all the consume log even if quota is 0
- if totalTokens == 0 {
- // in this case, must be some error happened
- // we cannot just return, because we may have to return the pre-consumed quota
- quota = 0
- extraContent = append(extraContent, "上游没有返回计费信息,无法扣费(可能是上游超时)")
- logger.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+
- "tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, modelName, relayInfo.FinalPreConsumedQuota))
- } else {
- if !ratio.IsZero() && quota == 0 {
- quota = 1
- }
- model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota)
- model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota)
- }
- if err := service.SettleBilling(ctx, relayInfo, quota); err != nil {
- logger.LogError(ctx, "error settling billing: "+err.Error())
- }
- logModel := modelName
- if strings.HasPrefix(logModel, "gpt-4-gizmo") {
- logModel = "gpt-4-gizmo-*"
- extraContent = append(extraContent, fmt.Sprintf("模型 %s", modelName))
- }
- if strings.HasPrefix(logModel, "gpt-4o-gizmo") {
- logModel = "gpt-4o-gizmo-*"
- extraContent = append(extraContent, fmt.Sprintf("模型 %s", modelName))
- }
- logContent := strings.Join(extraContent, ", ")
- other := service.GenerateTextOtherInfo(ctx, relayInfo, modelRatio, groupRatio, completionRatio, cacheTokens, cacheRatio, modelPrice, relayInfo.PriceData.GroupRatioInfo.GroupSpecialRatio)
- if adminRejectReason != "" {
- other["reject_reason"] = adminRejectReason
- }
- // For chat-based calls to the Claude model, tagging is required. Using Claude's rendering logs, the two approaches handle input rendering differently.
- if isClaudeUsageSemantic {
- other["claude"] = true
- other["usage_semantic"] = "anthropic"
- }
- if imageTokens != 0 {
- other["image"] = true
- other["image_ratio"] = imageRatio
- other["image_output"] = imageTokens
- }
- if cachedCreationTokens != 0 {
- other["cache_creation_tokens"] = cachedCreationTokens
- other["cache_creation_ratio"] = cachedCreationRatio
- }
- for _, item := range toolResult.Items {
- switch item.Name {
- case "web_search", "claude_web_search":
- other["web_search"] = true
- other["web_search_call_count"] = item.CallCount
- other["web_search_price"] = item.PricePer1K
- case "file_search":
- other["file_search"] = true
- other["file_search_call_count"] = item.CallCount
- other["file_search_price"] = item.PricePer1K
- case "image_generation":
- other["image_generation_call"] = true
- other["image_generation_call_price"] = item.TotalPrice
- }
- }
- if !audioInputQuota.IsZero() {
- other["audio_input_seperate_price"] = true
- other["audio_input_token_count"] = audioTokens
- other["audio_input_price"] = audioInputPrice
- }
- if tieredResult != nil {
- service.InjectTieredBillingInfo(other, relayInfo, tieredResult)
- }
- model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{
- ChannelId: relayInfo.ChannelId,
- PromptTokens: promptTokens,
- CompletionTokens: completionTokens,
- ModelName: logModel,
- TokenName: tokenName,
- Quota: quota,
- Content: logContent,
- TokenId: relayInfo.TokenId,
- UseTimeSeconds: int(useTimeSeconds),
- IsStream: relayInfo.IsStream,
- Group: relayInfo.UsingGroup,
- Other: other,
- })
- }
|