relay_info.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. package common
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/QuantumNous/new-api/common"
  9. "github.com/QuantumNous/new-api/constant"
  10. "github.com/QuantumNous/new-api/dto"
  11. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  12. "github.com/QuantumNous/new-api/setting/model_setting"
  13. "github.com/QuantumNous/new-api/types"
  14. "github.com/gin-gonic/gin"
  15. "github.com/gorilla/websocket"
  16. )
  17. type ThinkingContentInfo struct {
  18. IsFirstThinkingContent bool
  19. SendLastThinkingContent bool
  20. HasSentThinkingContent bool
  21. }
  22. const (
  23. LastMessageTypeNone = "none"
  24. LastMessageTypeText = "text"
  25. LastMessageTypeTools = "tools"
  26. LastMessageTypeThinking = "thinking"
  27. )
  28. type ClaudeConvertInfo struct {
  29. LastMessagesType string
  30. Index int
  31. Usage *dto.Usage
  32. FinishReason string
  33. Done bool
  34. ToolCallBaseIndex int
  35. ToolCallMaxIndexOffset int
  36. }
  37. type RerankerInfo struct {
  38. Documents []any
  39. ReturnDocuments bool
  40. }
  41. type BuildInToolInfo struct {
  42. ToolName string
  43. CallCount int
  44. SearchContextSize string
  45. }
  46. type ResponsesUsageInfo struct {
  47. BuiltInTools map[string]*BuildInToolInfo
  48. }
  49. type ChannelMeta struct {
  50. ChannelType int
  51. ChannelId int
  52. ChannelIsMultiKey bool
  53. ChannelMultiKeyIndex int
  54. ChannelBaseUrl string
  55. ApiType int
  56. ApiVersion string
  57. ApiKey string
  58. Organization string
  59. ChannelCreateTime int64
  60. ParamOverride map[string]interface{}
  61. HeadersOverride map[string]interface{}
  62. ChannelSetting dto.ChannelSettings
  63. ChannelOtherSettings dto.ChannelOtherSettings
  64. UpstreamModelName string
  65. IsModelMapped bool
  66. SupportStreamOptions bool // 是否支持流式选项
  67. }
  68. type TokenCountMeta struct {
  69. //promptTokens int
  70. estimatePromptTokens int
  71. }
  72. type RelayInfo struct {
  73. TokenId int
  74. TokenKey string
  75. TokenGroup string
  76. UserId int
  77. UsingGroup string // 使用的分组,当auto跨分组重试时,会变动
  78. UserGroup string // 用户所在分组
  79. TokenUnlimited bool
  80. StartTime time.Time
  81. FirstResponseTime time.Time
  82. isFirstResponse bool
  83. //SendLastReasoningResponse bool
  84. IsStream bool
  85. IsGeminiBatchEmbedding bool
  86. IsPlayground bool
  87. UsePrice bool
  88. RelayMode int
  89. OriginModelName string
  90. RequestURLPath string
  91. RequestHeaders map[string]string
  92. ShouldIncludeUsage bool
  93. DisablePing bool // 是否禁止向下游发送自定义 Ping
  94. ClientWs *websocket.Conn
  95. TargetWs *websocket.Conn
  96. InputAudioFormat string
  97. OutputAudioFormat string
  98. RealtimeTools []dto.RealTimeTool
  99. IsFirstRequest bool
  100. AudioUsage bool
  101. ReasoningEffort string
  102. UserSetting dto.UserSetting
  103. UserEmail string
  104. UserQuota int
  105. RelayFormat types.RelayFormat
  106. SendResponseCount int
  107. ReceivedResponseCount int
  108. FinalPreConsumedQuota int // 最终预消耗的配额
  109. // Billing 是计费会话,封装了预扣费/结算/退款的统一生命周期。
  110. // 免费模型和按次计费(MJ/Task)时为 nil。
  111. Billing BillingSettler
  112. // BillingSource indicates whether this request is billed from wallet quota or subscription.
  113. // "" or "wallet" => wallet; "subscription" => subscription
  114. BillingSource string
  115. // SubscriptionId is the user_subscriptions.id used when BillingSource == "subscription"
  116. SubscriptionId int
  117. // SubscriptionPreConsumed is the amount pre-consumed on subscription item (quota units or 1)
  118. SubscriptionPreConsumed int64
  119. // SubscriptionPostDelta is the post-consume delta applied to amount_used (quota units; can be negative).
  120. SubscriptionPostDelta int64
  121. // SubscriptionPlanId / SubscriptionPlanTitle are used for logging/UI display.
  122. SubscriptionPlanId int
  123. SubscriptionPlanTitle string
  124. // RequestId is used for idempotent pre-consume/refund
  125. RequestId string
  126. // SubscriptionAmountTotal / SubscriptionAmountUsedAfterPreConsume are used to compute remaining in logs.
  127. SubscriptionAmountTotal int64
  128. SubscriptionAmountUsedAfterPreConsume int64
  129. IsClaudeBetaQuery bool // /v1/messages?beta=true
  130. IsChannelTest bool // channel test request
  131. RetryIndex int
  132. LastError *types.NewAPIError
  133. RuntimeHeadersOverride map[string]interface{}
  134. UseRuntimeHeadersOverride bool
  135. PriceData types.PriceData
  136. Request dto.Request
  137. // RequestConversionChain records request format conversions in order, e.g.
  138. // ["openai", "openai_responses"] or ["openai", "claude"].
  139. RequestConversionChain []types.RelayFormat
  140. // 最终请求到上游的格式 TODO: 当前仅设置了Claude
  141. FinalRequestRelayFormat types.RelayFormat
  142. ThinkingContentInfo
  143. TokenCountMeta
  144. *ClaudeConvertInfo
  145. *RerankerInfo
  146. *ResponsesUsageInfo
  147. *ChannelMeta
  148. *TaskRelayInfo
  149. }
  150. func (info *RelayInfo) InitChannelMeta(c *gin.Context) {
  151. channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  152. paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  153. headerOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelHeaderOverride)
  154. apiType, _ := common.ChannelType2APIType(channelType)
  155. channelMeta := &ChannelMeta{
  156. ChannelType: channelType,
  157. ChannelId: common.GetContextKeyInt(c, constant.ContextKeyChannelId),
  158. ChannelIsMultiKey: common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey),
  159. ChannelMultiKeyIndex: common.GetContextKeyInt(c, constant.ContextKeyChannelMultiKeyIndex),
  160. ChannelBaseUrl: common.GetContextKeyString(c, constant.ContextKeyChannelBaseUrl),
  161. ApiType: apiType,
  162. ApiVersion: c.GetString("api_version"),
  163. ApiKey: common.GetContextKeyString(c, constant.ContextKeyChannelKey),
  164. Organization: c.GetString("channel_organization"),
  165. ChannelCreateTime: c.GetInt64("channel_create_time"),
  166. ParamOverride: paramOverride,
  167. HeadersOverride: headerOverride,
  168. UpstreamModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  169. IsModelMapped: false,
  170. SupportStreamOptions: false,
  171. }
  172. if channelType == constant.ChannelTypeAzure {
  173. channelMeta.ApiVersion = GetAPIVersion(c)
  174. }
  175. if channelType == constant.ChannelTypeVertexAi {
  176. channelMeta.ApiVersion = c.GetString("region")
  177. }
  178. channelSetting, ok := common.GetContextKeyType[dto.ChannelSettings](c, constant.ContextKeyChannelSetting)
  179. if ok {
  180. channelMeta.ChannelSetting = channelSetting
  181. }
  182. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  183. if ok {
  184. channelMeta.ChannelOtherSettings = channelOtherSettings
  185. }
  186. if streamSupportedChannels[channelMeta.ChannelType] {
  187. channelMeta.SupportStreamOptions = true
  188. }
  189. info.ChannelMeta = channelMeta
  190. // reset some fields based on channel meta
  191. // 重置某些字段,例如模型名称等
  192. if info.Request != nil {
  193. info.Request.SetModelName(info.OriginModelName)
  194. }
  195. }
  196. func (info *RelayInfo) ToString() string {
  197. if info == nil {
  198. return "RelayInfo<nil>"
  199. }
  200. // Basic info
  201. b := &strings.Builder{}
  202. fmt.Fprintf(b, "RelayInfo{ ")
  203. fmt.Fprintf(b, "RelayFormat: %s, ", info.RelayFormat)
  204. fmt.Fprintf(b, "RelayMode: %d, ", info.RelayMode)
  205. fmt.Fprintf(b, "IsStream: %t, ", info.IsStream)
  206. fmt.Fprintf(b, "IsPlayground: %t, ", info.IsPlayground)
  207. fmt.Fprintf(b, "RequestURLPath: %q, ", info.RequestURLPath)
  208. fmt.Fprintf(b, "OriginModelName: %q, ", info.OriginModelName)
  209. fmt.Fprintf(b, "EstimatePromptTokens: %d, ", info.estimatePromptTokens)
  210. fmt.Fprintf(b, "ShouldIncludeUsage: %t, ", info.ShouldIncludeUsage)
  211. fmt.Fprintf(b, "DisablePing: %t, ", info.DisablePing)
  212. fmt.Fprintf(b, "SendResponseCount: %d, ", info.SendResponseCount)
  213. fmt.Fprintf(b, "FinalPreConsumedQuota: %d, ", info.FinalPreConsumedQuota)
  214. // User & token info (mask secrets)
  215. fmt.Fprintf(b, "User{ Id: %d, Email: %q, Group: %q, UsingGroup: %q, Quota: %d }, ",
  216. info.UserId, common.MaskEmail(info.UserEmail), info.UserGroup, info.UsingGroup, info.UserQuota)
  217. fmt.Fprintf(b, "Token{ Id: %d, Unlimited: %t, Key: ***masked*** }, ", info.TokenId, info.TokenUnlimited)
  218. // Time info
  219. latencyMs := info.FirstResponseTime.Sub(info.StartTime).Milliseconds()
  220. fmt.Fprintf(b, "Timing{ Start: %s, FirstResponse: %s, LatencyMs: %d }, ",
  221. info.StartTime.Format(time.RFC3339Nano), info.FirstResponseTime.Format(time.RFC3339Nano), latencyMs)
  222. // Audio / realtime
  223. if info.InputAudioFormat != "" || info.OutputAudioFormat != "" || len(info.RealtimeTools) > 0 || info.AudioUsage {
  224. fmt.Fprintf(b, "Realtime{ AudioUsage: %t, InFmt: %q, OutFmt: %q, Tools: %d }, ",
  225. info.AudioUsage, info.InputAudioFormat, info.OutputAudioFormat, len(info.RealtimeTools))
  226. }
  227. // Reasoning
  228. if info.ReasoningEffort != "" {
  229. fmt.Fprintf(b, "ReasoningEffort: %q, ", info.ReasoningEffort)
  230. }
  231. // Price data (non-sensitive)
  232. if info.PriceData.UsePrice {
  233. fmt.Fprintf(b, "PriceData{ %s }, ", info.PriceData.ToSetting())
  234. }
  235. // Channel metadata (mask ApiKey)
  236. if info.ChannelMeta != nil {
  237. cm := info.ChannelMeta
  238. fmt.Fprintf(b, "ChannelMeta{ Type: %d, Id: %d, IsMultiKey: %t, MultiKeyIndex: %d, BaseURL: %q, ApiType: %d, ApiVersion: %q, Organization: %q, CreateTime: %d, UpstreamModelName: %q, IsModelMapped: %t, SupportStreamOptions: %t, ApiKey: ***masked*** }, ",
  239. cm.ChannelType, cm.ChannelId, cm.ChannelIsMultiKey, cm.ChannelMultiKeyIndex, cm.ChannelBaseUrl, cm.ApiType, cm.ApiVersion, cm.Organization, cm.ChannelCreateTime, cm.UpstreamModelName, cm.IsModelMapped, cm.SupportStreamOptions)
  240. }
  241. // Responses usage info (non-sensitive)
  242. if info.ResponsesUsageInfo != nil && len(info.ResponsesUsageInfo.BuiltInTools) > 0 {
  243. fmt.Fprintf(b, "ResponsesTools{ ")
  244. first := true
  245. for name, tool := range info.ResponsesUsageInfo.BuiltInTools {
  246. if !first {
  247. fmt.Fprintf(b, ", ")
  248. }
  249. first = false
  250. if tool != nil {
  251. fmt.Fprintf(b, "%s: calls=%d", name, tool.CallCount)
  252. } else {
  253. fmt.Fprintf(b, "%s: calls=0", name)
  254. }
  255. }
  256. fmt.Fprintf(b, " }, ")
  257. }
  258. fmt.Fprintf(b, "}")
  259. return b.String()
  260. }
  261. // 定义支持流式选项的通道类型
  262. var streamSupportedChannels = map[int]bool{
  263. constant.ChannelTypeOpenAI: true,
  264. constant.ChannelTypeAnthropic: true,
  265. constant.ChannelTypeAws: true,
  266. constant.ChannelTypeGemini: true,
  267. constant.ChannelCloudflare: true,
  268. constant.ChannelTypeAzure: true,
  269. constant.ChannelTypeVolcEngine: true,
  270. constant.ChannelTypeOllama: true,
  271. constant.ChannelTypeXai: true,
  272. constant.ChannelTypeDeepSeek: true,
  273. constant.ChannelTypeBaiduV2: true,
  274. constant.ChannelTypeZhipu_v4: true,
  275. constant.ChannelTypeAli: true,
  276. constant.ChannelTypeSubmodel: true,
  277. constant.ChannelTypeCodex: true,
  278. constant.ChannelTypeMoonshot: true,
  279. constant.ChannelTypeMiniMax: true,
  280. constant.ChannelTypeSiliconFlow: true,
  281. }
  282. func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
  283. info := genBaseRelayInfo(c, nil)
  284. info.RelayFormat = types.RelayFormatOpenAIRealtime
  285. info.ClientWs = ws
  286. info.InputAudioFormat = "pcm16"
  287. info.OutputAudioFormat = "pcm16"
  288. info.IsFirstRequest = true
  289. return info
  290. }
  291. func GenRelayInfoClaude(c *gin.Context, request dto.Request) *RelayInfo {
  292. info := genBaseRelayInfo(c, request)
  293. info.RelayFormat = types.RelayFormatClaude
  294. info.ShouldIncludeUsage = false
  295. info.ClaudeConvertInfo = &ClaudeConvertInfo{
  296. LastMessagesType: LastMessageTypeNone,
  297. }
  298. info.IsClaudeBetaQuery = c.Query("beta") == "true" || isClaudeBetaForced(c)
  299. return info
  300. }
  301. func isClaudeBetaForced(c *gin.Context) bool {
  302. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  303. return ok && channelOtherSettings.ClaudeBetaQuery
  304. }
  305. func GenRelayInfoRerank(c *gin.Context, request *dto.RerankRequest) *RelayInfo {
  306. info := genBaseRelayInfo(c, request)
  307. info.RelayMode = relayconstant.RelayModeRerank
  308. info.RelayFormat = types.RelayFormatRerank
  309. info.RerankerInfo = &RerankerInfo{
  310. Documents: request.Documents,
  311. ReturnDocuments: request.GetReturnDocuments(),
  312. }
  313. return info
  314. }
  315. func GenRelayInfoOpenAIAudio(c *gin.Context, request dto.Request) *RelayInfo {
  316. info := genBaseRelayInfo(c, request)
  317. info.RelayFormat = types.RelayFormatOpenAIAudio
  318. return info
  319. }
  320. func GenRelayInfoEmbedding(c *gin.Context, request dto.Request) *RelayInfo {
  321. info := genBaseRelayInfo(c, request)
  322. info.RelayFormat = types.RelayFormatEmbedding
  323. return info
  324. }
  325. func GenRelayInfoResponses(c *gin.Context, request *dto.OpenAIResponsesRequest) *RelayInfo {
  326. info := genBaseRelayInfo(c, request)
  327. info.RelayMode = relayconstant.RelayModeResponses
  328. info.RelayFormat = types.RelayFormatOpenAIResponses
  329. info.ResponsesUsageInfo = &ResponsesUsageInfo{
  330. BuiltInTools: make(map[string]*BuildInToolInfo),
  331. }
  332. if len(request.Tools) > 0 {
  333. for _, tool := range request.GetToolsMap() {
  334. toolType := common.Interface2String(tool["type"])
  335. info.ResponsesUsageInfo.BuiltInTools[toolType] = &BuildInToolInfo{
  336. ToolName: toolType,
  337. CallCount: 0,
  338. }
  339. switch toolType {
  340. case dto.BuildInToolWebSearchPreview:
  341. searchContextSize := common.Interface2String(tool["search_context_size"])
  342. if searchContextSize == "" {
  343. searchContextSize = "medium"
  344. }
  345. info.ResponsesUsageInfo.BuiltInTools[toolType].SearchContextSize = searchContextSize
  346. }
  347. }
  348. }
  349. return info
  350. }
  351. func GenRelayInfoGemini(c *gin.Context, request dto.Request) *RelayInfo {
  352. info := genBaseRelayInfo(c, request)
  353. info.RelayFormat = types.RelayFormatGemini
  354. info.ShouldIncludeUsage = false
  355. return info
  356. }
  357. func GenRelayInfoImage(c *gin.Context, request dto.Request) *RelayInfo {
  358. info := genBaseRelayInfo(c, request)
  359. info.RelayFormat = types.RelayFormatOpenAIImage
  360. return info
  361. }
  362. func GenRelayInfoOpenAI(c *gin.Context, request dto.Request) *RelayInfo {
  363. info := genBaseRelayInfo(c, request)
  364. info.RelayFormat = types.RelayFormatOpenAI
  365. return info
  366. }
  367. func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
  368. //channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  369. //channelId := common.GetContextKeyInt(c, constant.ContextKeyChannelId)
  370. //paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  371. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  372. // 当令牌分组为空时,表示使用用户分组
  373. if tokenGroup == "" {
  374. tokenGroup = common.GetContextKeyString(c, constant.ContextKeyUserGroup)
  375. }
  376. startTime := common.GetContextKeyTime(c, constant.ContextKeyRequestStartTime)
  377. if startTime.IsZero() {
  378. startTime = time.Now()
  379. }
  380. isStream := false
  381. if request != nil {
  382. isStream = request.IsStream(c)
  383. }
  384. // firstResponseTime = time.Now() - 1 second
  385. reqId := common.GetContextKeyString(c, common.RequestIdKey)
  386. if reqId == "" {
  387. reqId = common.GetTimeString() + common.GetRandomString(8)
  388. }
  389. info := &RelayInfo{
  390. Request: request,
  391. RequestId: reqId,
  392. UserId: common.GetContextKeyInt(c, constant.ContextKeyUserId),
  393. UsingGroup: common.GetContextKeyString(c, constant.ContextKeyUsingGroup),
  394. UserGroup: common.GetContextKeyString(c, constant.ContextKeyUserGroup),
  395. UserQuota: common.GetContextKeyInt(c, constant.ContextKeyUserQuota),
  396. UserEmail: common.GetContextKeyString(c, constant.ContextKeyUserEmail),
  397. OriginModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  398. TokenId: common.GetContextKeyInt(c, constant.ContextKeyTokenId),
  399. TokenKey: common.GetContextKeyString(c, constant.ContextKeyTokenKey),
  400. TokenUnlimited: common.GetContextKeyBool(c, constant.ContextKeyTokenUnlimited),
  401. TokenGroup: tokenGroup,
  402. isFirstResponse: true,
  403. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  404. RequestURLPath: c.Request.URL.String(),
  405. RequestHeaders: cloneRequestHeaders(c),
  406. IsStream: isStream,
  407. StartTime: startTime,
  408. FirstResponseTime: startTime.Add(-time.Second),
  409. ThinkingContentInfo: ThinkingContentInfo{
  410. IsFirstThinkingContent: true,
  411. SendLastThinkingContent: false,
  412. },
  413. TokenCountMeta: TokenCountMeta{
  414. //promptTokens: common.GetContextKeyInt(c, constant.ContextKeyPromptTokens),
  415. estimatePromptTokens: common.GetContextKeyInt(c, constant.ContextKeyEstimatedTokens),
  416. },
  417. }
  418. if info.RelayMode == relayconstant.RelayModeUnknown {
  419. info.RelayMode = c.GetInt("relay_mode")
  420. }
  421. if strings.HasPrefix(c.Request.URL.Path, "/pg") {
  422. info.IsPlayground = true
  423. info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
  424. info.RequestURLPath = "/v1" + info.RequestURLPath
  425. }
  426. userSetting, ok := common.GetContextKeyType[dto.UserSetting](c, constant.ContextKeyUserSetting)
  427. if ok {
  428. info.UserSetting = userSetting
  429. }
  430. return info
  431. }
  432. func cloneRequestHeaders(c *gin.Context) map[string]string {
  433. if c == nil || c.Request == nil {
  434. return nil
  435. }
  436. if len(c.Request.Header) == 0 {
  437. return nil
  438. }
  439. headers := make(map[string]string, len(c.Request.Header))
  440. for key := range c.Request.Header {
  441. value := strings.TrimSpace(c.Request.Header.Get(key))
  442. if value == "" {
  443. continue
  444. }
  445. headers[key] = value
  446. }
  447. if len(headers) == 0 {
  448. return nil
  449. }
  450. return headers
  451. }
  452. func GenRelayInfo(c *gin.Context, relayFormat types.RelayFormat, request dto.Request, ws *websocket.Conn) (*RelayInfo, error) {
  453. var info *RelayInfo
  454. var err error
  455. switch relayFormat {
  456. case types.RelayFormatOpenAI:
  457. info = GenRelayInfoOpenAI(c, request)
  458. case types.RelayFormatOpenAIAudio:
  459. info = GenRelayInfoOpenAIAudio(c, request)
  460. case types.RelayFormatOpenAIImage:
  461. info = GenRelayInfoImage(c, request)
  462. case types.RelayFormatOpenAIRealtime:
  463. info = GenRelayInfoWs(c, ws)
  464. case types.RelayFormatClaude:
  465. info = GenRelayInfoClaude(c, request)
  466. case types.RelayFormatRerank:
  467. if request, ok := request.(*dto.RerankRequest); ok {
  468. info = GenRelayInfoRerank(c, request)
  469. break
  470. }
  471. err = errors.New("request is not a RerankRequest")
  472. case types.RelayFormatGemini:
  473. info = GenRelayInfoGemini(c, request)
  474. case types.RelayFormatEmbedding:
  475. info = GenRelayInfoEmbedding(c, request)
  476. case types.RelayFormatOpenAIResponses:
  477. if request, ok := request.(*dto.OpenAIResponsesRequest); ok {
  478. info = GenRelayInfoResponses(c, request)
  479. break
  480. }
  481. err = errors.New("request is not a OpenAIResponsesRequest")
  482. case types.RelayFormatOpenAIResponsesCompaction:
  483. if request, ok := request.(*dto.OpenAIResponsesCompactionRequest); ok {
  484. return GenRelayInfoResponsesCompaction(c, request), nil
  485. }
  486. return nil, errors.New("request is not a OpenAIResponsesCompactionRequest")
  487. case types.RelayFormatTask:
  488. info = genBaseRelayInfo(c, nil)
  489. case types.RelayFormatMjProxy:
  490. info = genBaseRelayInfo(c, nil)
  491. default:
  492. err = errors.New("invalid relay format")
  493. }
  494. if err != nil {
  495. return nil, err
  496. }
  497. if info == nil {
  498. return nil, errors.New("failed to build relay info")
  499. }
  500. info.InitRequestConversionChain()
  501. return info, nil
  502. }
  503. func (info *RelayInfo) InitRequestConversionChain() {
  504. if info == nil {
  505. return
  506. }
  507. if len(info.RequestConversionChain) > 0 {
  508. return
  509. }
  510. if info.RelayFormat == "" {
  511. return
  512. }
  513. info.RequestConversionChain = []types.RelayFormat{info.RelayFormat}
  514. }
  515. func (info *RelayInfo) AppendRequestConversion(format types.RelayFormat) {
  516. if info == nil {
  517. return
  518. }
  519. if format == "" {
  520. return
  521. }
  522. if len(info.RequestConversionChain) == 0 {
  523. info.RequestConversionChain = []types.RelayFormat{format}
  524. return
  525. }
  526. last := info.RequestConversionChain[len(info.RequestConversionChain)-1]
  527. if last == format {
  528. return
  529. }
  530. info.RequestConversionChain = append(info.RequestConversionChain, format)
  531. }
  532. func GenRelayInfoResponsesCompaction(c *gin.Context, request *dto.OpenAIResponsesCompactionRequest) *RelayInfo {
  533. info := genBaseRelayInfo(c, request)
  534. if info.RelayMode == relayconstant.RelayModeUnknown {
  535. info.RelayMode = relayconstant.RelayModeResponsesCompact
  536. }
  537. info.RelayFormat = types.RelayFormatOpenAIResponsesCompaction
  538. return info
  539. }
  540. //func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  541. // info.promptTokens = promptTokens
  542. //}
  543. func (info *RelayInfo) SetEstimatePromptTokens(promptTokens int) {
  544. info.estimatePromptTokens = promptTokens
  545. }
  546. func (info *RelayInfo) GetEstimatePromptTokens() int {
  547. return info.estimatePromptTokens
  548. }
  549. func (info *RelayInfo) SetFirstResponseTime() {
  550. if info.isFirstResponse {
  551. info.FirstResponseTime = time.Now()
  552. info.isFirstResponse = false
  553. }
  554. }
  555. func (info *RelayInfo) HasSendResponse() bool {
  556. return info.FirstResponseTime.After(info.StartTime)
  557. }
  558. type TaskRelayInfo struct {
  559. Action string
  560. OriginTaskID string
  561. ConsumeQuota bool
  562. }
  563. type TaskSubmitReq struct {
  564. Prompt string `json:"prompt"`
  565. Model string `json:"model,omitempty"`
  566. Mode string `json:"mode,omitempty"`
  567. Image string `json:"image,omitempty"`
  568. Images []string `json:"images,omitempty"`
  569. Size string `json:"size,omitempty"`
  570. Duration int `json:"duration,omitempty"`
  571. Seconds string `json:"seconds,omitempty"`
  572. InputReference string `json:"input_reference,omitempty"`
  573. Metadata map[string]interface{} `json:"metadata,omitempty"`
  574. }
  575. func (t *TaskSubmitReq) GetPrompt() string {
  576. return t.Prompt
  577. }
  578. func (t *TaskSubmitReq) HasImage() bool {
  579. return len(t.Images) > 0
  580. }
  581. func (t *TaskSubmitReq) UnmarshalJSON(data []byte) error {
  582. type Alias TaskSubmitReq
  583. aux := &struct {
  584. Metadata json.RawMessage `json:"metadata,omitempty"`
  585. *Alias
  586. }{
  587. Alias: (*Alias)(t),
  588. }
  589. if err := common.Unmarshal(data, &aux); err != nil {
  590. return err
  591. }
  592. if len(aux.Metadata) > 0 {
  593. var metadataStr string
  594. if err := common.Unmarshal(aux.Metadata, &metadataStr); err == nil && metadataStr != "" {
  595. var metadataObj map[string]interface{}
  596. if err := common.Unmarshal([]byte(metadataStr), &metadataObj); err == nil {
  597. t.Metadata = metadataObj
  598. return nil
  599. }
  600. }
  601. var metadataObj map[string]interface{}
  602. if err := common.Unmarshal(aux.Metadata, &metadataObj); err == nil {
  603. t.Metadata = metadataObj
  604. }
  605. }
  606. return nil
  607. }
  608. func (t *TaskSubmitReq) UnmarshalMetadata(v any) error {
  609. metadata := t.Metadata
  610. if metadata != nil {
  611. metadataBytes, err := json.Marshal(metadata)
  612. if err != nil {
  613. return fmt.Errorf("marshal metadata failed: %w", err)
  614. }
  615. err = json.Unmarshal(metadataBytes, v)
  616. if err != nil {
  617. return fmt.Errorf("unmarshal metadata to target failed: %w", err)
  618. }
  619. }
  620. return nil
  621. }
  622. type TaskInfo struct {
  623. Code int `json:"code"`
  624. TaskID string `json:"task_id"`
  625. Status string `json:"status"`
  626. Reason string `json:"reason,omitempty"`
  627. Url string `json:"url,omitempty"`
  628. RemoteUrl string `json:"remote_url,omitempty"`
  629. Progress string `json:"progress,omitempty"`
  630. CompletionTokens int `json:"completion_tokens,omitempty"` // 用于按倍率计费
  631. TotalTokens int `json:"total_tokens,omitempty"` // 用于按倍率计费
  632. }
  633. func FailTaskInfo(reason string) *TaskInfo {
  634. return &TaskInfo{
  635. Status: "FAILURE",
  636. Reason: reason,
  637. }
  638. }
  639. // RemoveDisabledFields 从请求 JSON 数据中移除渠道设置中禁用的字段
  640. // service_tier: 服务层级字段,可能导致额外计费(OpenAI、Claude、Responses API 支持)
  641. // store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
  642. // safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
  643. func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings) ([]byte, error) {
  644. var data map[string]interface{}
  645. if err := common.Unmarshal(jsonData, &data); err != nil {
  646. common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
  647. return jsonData, nil
  648. }
  649. // 默认移除 service_tier,除非明确允许(避免额外计费风险)
  650. if !channelOtherSettings.AllowServiceTier {
  651. if _, exists := data["service_tier"]; exists {
  652. delete(data, "service_tier")
  653. }
  654. }
  655. // 默认允许 store 透传,除非明确禁用(禁用可能影响 Codex 使用)
  656. if channelOtherSettings.DisableStore {
  657. if _, exists := data["store"]; exists {
  658. delete(data, "store")
  659. }
  660. }
  661. // 默认移除 safety_identifier,除非明确允许(保护用户隐私,避免向 OpenAI 报告用户信息)
  662. if !channelOtherSettings.AllowSafetyIdentifier {
  663. if _, exists := data["safety_identifier"]; exists {
  664. delete(data, "safety_identifier")
  665. }
  666. }
  667. jsonDataAfter, err := common.Marshal(data)
  668. if err != nil {
  669. common.SysError("RemoveDisabledFields Marshal error :" + err.Error())
  670. return jsonData, nil
  671. }
  672. return jsonDataAfter, nil
  673. }
  674. // RemoveGeminiDisabledFields removes disabled fields from Gemini request JSON data
  675. // Currently supports removing functionResponse.id field which Vertex AI does not support
  676. func RemoveGeminiDisabledFields(jsonData []byte) ([]byte, error) {
  677. if !model_setting.GetGeminiSettings().RemoveFunctionResponseIdEnabled {
  678. return jsonData, nil
  679. }
  680. var data map[string]interface{}
  681. if err := common.Unmarshal(jsonData, &data); err != nil {
  682. common.SysError("RemoveGeminiDisabledFields Unmarshal error: " + err.Error())
  683. return jsonData, nil
  684. }
  685. // Process contents array
  686. // Handle both camelCase (functionResponse) and snake_case (function_response)
  687. if contents, ok := data["contents"].([]interface{}); ok {
  688. for _, content := range contents {
  689. if contentMap, ok := content.(map[string]interface{}); ok {
  690. if parts, ok := contentMap["parts"].([]interface{}); ok {
  691. for _, part := range parts {
  692. if partMap, ok := part.(map[string]interface{}); ok {
  693. // Check functionResponse (camelCase)
  694. if funcResp, ok := partMap["functionResponse"].(map[string]interface{}); ok {
  695. delete(funcResp, "id")
  696. }
  697. // Check function_response (snake_case)
  698. if funcResp, ok := partMap["function_response"].(map[string]interface{}); ok {
  699. delete(funcResp, "id")
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. jsonDataAfter, err := common.Marshal(data)
  708. if err != nil {
  709. common.SysError("RemoveGeminiDisabledFields Marshal error: " + err.Error())
  710. return jsonData, nil
  711. }
  712. return jsonDataAfter, nil
  713. }