ratio_sync.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net"
  8. "net/http"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/QuantumNous/new-api/common"
  13. "github.com/QuantumNous/new-api/logger"
  14. "github.com/QuantumNous/new-api/dto"
  15. "github.com/QuantumNous/new-api/model"
  16. "github.com/QuantumNous/new-api/setting/ratio_setting"
  17. "github.com/gin-gonic/gin"
  18. )
  19. const (
  20. defaultTimeoutSeconds = 10
  21. defaultEndpoint = "/api/ratio_config"
  22. maxConcurrentFetches = 8
  23. maxRatioConfigBytes = 10 << 20 // 10MB
  24. floatEpsilon = 1e-9
  25. )
  26. func nearlyEqual(a, b float64) bool {
  27. if a > b {
  28. return a-b < floatEpsilon
  29. }
  30. return b-a < floatEpsilon
  31. }
  32. func valuesEqual(a, b interface{}) bool {
  33. af, aok := a.(float64)
  34. bf, bok := b.(float64)
  35. if aok && bok {
  36. return nearlyEqual(af, bf)
  37. }
  38. return a == b
  39. }
  40. var ratioTypes = []string{"model_ratio", "completion_ratio", "cache_ratio", "model_price"}
  41. type upstreamResult struct {
  42. Name string `json:"name"`
  43. Data map[string]any `json:"data,omitempty"`
  44. Err string `json:"err,omitempty"`
  45. }
  46. func FetchUpstreamRatios(c *gin.Context) {
  47. var req dto.UpstreamRequest
  48. if err := c.ShouldBindJSON(&req); err != nil {
  49. common.SysError("failed to bind upstream request: " + err.Error())
  50. c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "请求参数格式错误"})
  51. return
  52. }
  53. if req.Timeout <= 0 {
  54. req.Timeout = defaultTimeoutSeconds
  55. }
  56. var upstreams []dto.UpstreamDTO
  57. if len(req.Upstreams) > 0 {
  58. for _, u := range req.Upstreams {
  59. if strings.HasPrefix(u.BaseURL, "http") {
  60. if u.Endpoint == "" {
  61. u.Endpoint = defaultEndpoint
  62. }
  63. u.BaseURL = strings.TrimRight(u.BaseURL, "/")
  64. upstreams = append(upstreams, u)
  65. }
  66. }
  67. } else if len(req.ChannelIDs) > 0 {
  68. intIds := make([]int, 0, len(req.ChannelIDs))
  69. for _, id64 := range req.ChannelIDs {
  70. intIds = append(intIds, int(id64))
  71. }
  72. dbChannels, err := model.GetChannelsByIds(intIds)
  73. if err != nil {
  74. logger.LogError(c.Request.Context(), "failed to query channels: "+err.Error())
  75. c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "查询渠道失败"})
  76. return
  77. }
  78. for _, ch := range dbChannels {
  79. if base := ch.GetBaseURL(); strings.HasPrefix(base, "http") {
  80. upstreams = append(upstreams, dto.UpstreamDTO{
  81. ID: ch.Id,
  82. Name: ch.Name,
  83. BaseURL: strings.TrimRight(base, "/"),
  84. Endpoint: "",
  85. })
  86. }
  87. }
  88. }
  89. if len(upstreams) == 0 {
  90. c.JSON(http.StatusOK, gin.H{"success": false, "message": "无有效上游渠道"})
  91. return
  92. }
  93. var wg sync.WaitGroup
  94. ch := make(chan upstreamResult, len(upstreams))
  95. sem := make(chan struct{}, maxConcurrentFetches)
  96. dialer := &net.Dialer{Timeout: 10 * time.Second}
  97. transport := &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, ResponseHeaderTimeout: 10 * time.Second}
  98. if common.TLSInsecureSkipVerify {
  99. transport.TLSClientConfig = common.InsecureTLSConfig
  100. }
  101. transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
  102. host, _, err := net.SplitHostPort(addr)
  103. if err != nil {
  104. host = addr
  105. }
  106. // 对 github.io 优先尝试 IPv4,失败则回退 IPv6
  107. if strings.HasSuffix(host, "github.io") {
  108. if conn, err := dialer.DialContext(ctx, "tcp4", addr); err == nil {
  109. return conn, nil
  110. }
  111. return dialer.DialContext(ctx, "tcp6", addr)
  112. }
  113. return dialer.DialContext(ctx, network, addr)
  114. }
  115. client := &http.Client{Transport: transport}
  116. for _, chn := range upstreams {
  117. wg.Add(1)
  118. go func(chItem dto.UpstreamDTO) {
  119. defer wg.Done()
  120. sem <- struct{}{}
  121. defer func() { <-sem }()
  122. endpoint := chItem.Endpoint
  123. var fullURL string
  124. if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
  125. fullURL = endpoint
  126. } else {
  127. if endpoint == "" {
  128. endpoint = defaultEndpoint
  129. } else if !strings.HasPrefix(endpoint, "/") {
  130. endpoint = "/" + endpoint
  131. }
  132. fullURL = chItem.BaseURL + endpoint
  133. }
  134. uniqueName := chItem.Name
  135. if chItem.ID != 0 {
  136. uniqueName = fmt.Sprintf("%s(%d)", chItem.Name, chItem.ID)
  137. }
  138. ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(req.Timeout)*time.Second)
  139. defer cancel()
  140. httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil)
  141. if err != nil {
  142. logger.LogWarn(c.Request.Context(), "build request failed: "+err.Error())
  143. ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
  144. return
  145. }
  146. // 简单重试:最多 3 次,指数退避
  147. var resp *http.Response
  148. var lastErr error
  149. for attempt := 0; attempt < 3; attempt++ {
  150. resp, lastErr = client.Do(httpReq)
  151. if lastErr == nil {
  152. break
  153. }
  154. time.Sleep(time.Duration(200*(1<<attempt)) * time.Millisecond)
  155. }
  156. if lastErr != nil {
  157. logger.LogWarn(c.Request.Context(), "http error on "+chItem.Name+": "+lastErr.Error())
  158. ch <- upstreamResult{Name: uniqueName, Err: lastErr.Error()}
  159. return
  160. }
  161. defer resp.Body.Close()
  162. if resp.StatusCode != http.StatusOK {
  163. logger.LogWarn(c.Request.Context(), "non-200 from "+chItem.Name+": "+resp.Status)
  164. ch <- upstreamResult{Name: uniqueName, Err: resp.Status}
  165. return
  166. }
  167. // Content-Type 和响应体大小校验
  168. if ct := resp.Header.Get("Content-Type"); ct != "" && !strings.Contains(strings.ToLower(ct), "application/json") {
  169. logger.LogWarn(c.Request.Context(), "unexpected content-type from "+chItem.Name+": "+ct)
  170. }
  171. limited := io.LimitReader(resp.Body, maxRatioConfigBytes)
  172. // 兼容两种上游接口格式:
  173. // type1: /api/ratio_config -> data 为 map[string]any,包含 model_ratio/completion_ratio/cache_ratio/model_price
  174. // type2: /api/pricing -> data 为 []Pricing 列表,需要转换为与 type1 相同的 map 格式
  175. var body struct {
  176. Success bool `json:"success"`
  177. Data json.RawMessage `json:"data"`
  178. Message string `json:"message"`
  179. }
  180. if err := json.NewDecoder(limited).Decode(&body); err != nil {
  181. logger.LogWarn(c.Request.Context(), "json decode failed from "+chItem.Name+": "+err.Error())
  182. ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
  183. return
  184. }
  185. if !body.Success {
  186. ch <- upstreamResult{Name: uniqueName, Err: body.Message}
  187. return
  188. }
  189. // 若 Data 为空,将继续按 type1 尝试解析(与多数静态 ratio_config 兼容)
  190. // 尝试按 type1 解析
  191. var type1Data map[string]any
  192. if err := json.Unmarshal(body.Data, &type1Data); err == nil {
  193. // 如果包含至少一个 ratioTypes 字段,则认为是 type1
  194. isType1 := false
  195. for _, rt := range ratioTypes {
  196. if _, ok := type1Data[rt]; ok {
  197. isType1 = true
  198. break
  199. }
  200. }
  201. if isType1 {
  202. ch <- upstreamResult{Name: uniqueName, Data: type1Data}
  203. return
  204. }
  205. }
  206. // 如果不是 type1,则尝试按 type2 (/api/pricing) 解析
  207. var pricingItems []struct {
  208. ModelName string `json:"model_name"`
  209. QuotaType int `json:"quota_type"`
  210. ModelRatio float64 `json:"model_ratio"`
  211. ModelPrice float64 `json:"model_price"`
  212. CompletionRatio float64 `json:"completion_ratio"`
  213. }
  214. if err := json.Unmarshal(body.Data, &pricingItems); err != nil {
  215. logger.LogWarn(c.Request.Context(), "unrecognized data format from "+chItem.Name+": "+err.Error())
  216. ch <- upstreamResult{Name: uniqueName, Err: "无法解析上游返回数据"}
  217. return
  218. }
  219. modelRatioMap := make(map[string]float64)
  220. completionRatioMap := make(map[string]float64)
  221. modelPriceMap := make(map[string]float64)
  222. for _, item := range pricingItems {
  223. if item.QuotaType == 1 {
  224. modelPriceMap[item.ModelName] = item.ModelPrice
  225. } else {
  226. modelRatioMap[item.ModelName] = item.ModelRatio
  227. // completionRatio 可能为 0,此时也直接赋值,保持与上游一致
  228. completionRatioMap[item.ModelName] = item.CompletionRatio
  229. }
  230. }
  231. converted := make(map[string]any)
  232. if len(modelRatioMap) > 0 {
  233. ratioAny := make(map[string]any, len(modelRatioMap))
  234. for k, v := range modelRatioMap {
  235. ratioAny[k] = v
  236. }
  237. converted["model_ratio"] = ratioAny
  238. }
  239. if len(completionRatioMap) > 0 {
  240. compAny := make(map[string]any, len(completionRatioMap))
  241. for k, v := range completionRatioMap {
  242. compAny[k] = v
  243. }
  244. converted["completion_ratio"] = compAny
  245. }
  246. if len(modelPriceMap) > 0 {
  247. priceAny := make(map[string]any, len(modelPriceMap))
  248. for k, v := range modelPriceMap {
  249. priceAny[k] = v
  250. }
  251. converted["model_price"] = priceAny
  252. }
  253. ch <- upstreamResult{Name: uniqueName, Data: converted}
  254. }(chn)
  255. }
  256. wg.Wait()
  257. close(ch)
  258. localData := ratio_setting.GetExposedData()
  259. var testResults []dto.TestResult
  260. var successfulChannels []struct {
  261. name string
  262. data map[string]any
  263. }
  264. for r := range ch {
  265. if r.Err != "" {
  266. testResults = append(testResults, dto.TestResult{
  267. Name: r.Name,
  268. Status: "error",
  269. Error: r.Err,
  270. })
  271. } else {
  272. testResults = append(testResults, dto.TestResult{
  273. Name: r.Name,
  274. Status: "success",
  275. })
  276. successfulChannels = append(successfulChannels, struct {
  277. name string
  278. data map[string]any
  279. }{name: r.Name, data: r.Data})
  280. }
  281. }
  282. differences := buildDifferences(localData, successfulChannels)
  283. c.JSON(http.StatusOK, gin.H{
  284. "success": true,
  285. "data": gin.H{
  286. "differences": differences,
  287. "test_results": testResults,
  288. },
  289. })
  290. }
  291. func buildDifferences(localData map[string]any, successfulChannels []struct {
  292. name string
  293. data map[string]any
  294. }) map[string]map[string]dto.DifferenceItem {
  295. differences := make(map[string]map[string]dto.DifferenceItem)
  296. allModels := make(map[string]struct{})
  297. for _, ratioType := range ratioTypes {
  298. if localRatioAny, ok := localData[ratioType]; ok {
  299. if localRatio, ok := localRatioAny.(map[string]float64); ok {
  300. for modelName := range localRatio {
  301. allModels[modelName] = struct{}{}
  302. }
  303. }
  304. }
  305. }
  306. for _, channel := range successfulChannels {
  307. for _, ratioType := range ratioTypes {
  308. if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
  309. for modelName := range upstreamRatio {
  310. allModels[modelName] = struct{}{}
  311. }
  312. }
  313. }
  314. }
  315. confidenceMap := make(map[string]map[string]bool)
  316. // 预处理阶段:检查pricing接口的可信度
  317. for _, channel := range successfulChannels {
  318. confidenceMap[channel.name] = make(map[string]bool)
  319. modelRatios, hasModelRatio := channel.data["model_ratio"].(map[string]any)
  320. completionRatios, hasCompletionRatio := channel.data["completion_ratio"].(map[string]any)
  321. if hasModelRatio && hasCompletionRatio {
  322. // 遍历所有模型,检查是否满足不可信条件
  323. for modelName := range allModels {
  324. // 默认为可信
  325. confidenceMap[channel.name][modelName] = true
  326. // 检查是否满足不可信条件:model_ratio为37.5且completion_ratio为1
  327. if modelRatioVal, ok := modelRatios[modelName]; ok {
  328. if completionRatioVal, ok := completionRatios[modelName]; ok {
  329. // 转换为float64进行比较
  330. if modelRatioFloat, ok := modelRatioVal.(float64); ok {
  331. if completionRatioFloat, ok := completionRatioVal.(float64); ok {
  332. if modelRatioFloat == 37.5 && completionRatioFloat == 1.0 {
  333. confidenceMap[channel.name][modelName] = false
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. } else {
  341. // 如果不是从pricing接口获取的数据,则全部标记为可信
  342. for modelName := range allModels {
  343. confidenceMap[channel.name][modelName] = true
  344. }
  345. }
  346. }
  347. for modelName := range allModels {
  348. for _, ratioType := range ratioTypes {
  349. var localValue interface{} = nil
  350. if localRatioAny, ok := localData[ratioType]; ok {
  351. if localRatio, ok := localRatioAny.(map[string]float64); ok {
  352. if val, exists := localRatio[modelName]; exists {
  353. localValue = val
  354. }
  355. }
  356. }
  357. upstreamValues := make(map[string]interface{})
  358. confidenceValues := make(map[string]bool)
  359. hasUpstreamValue := false
  360. hasDifference := false
  361. for _, channel := range successfulChannels {
  362. var upstreamValue interface{} = nil
  363. if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
  364. if val, exists := upstreamRatio[modelName]; exists {
  365. upstreamValue = val
  366. hasUpstreamValue = true
  367. if localValue != nil && !valuesEqual(localValue, val) {
  368. hasDifference = true
  369. } else if valuesEqual(localValue, val) {
  370. upstreamValue = "same"
  371. }
  372. }
  373. }
  374. if upstreamValue == nil && localValue == nil {
  375. upstreamValue = "same"
  376. }
  377. if localValue == nil && upstreamValue != nil && upstreamValue != "same" {
  378. hasDifference = true
  379. }
  380. upstreamValues[channel.name] = upstreamValue
  381. confidenceValues[channel.name] = confidenceMap[channel.name][modelName]
  382. }
  383. shouldInclude := false
  384. if localValue != nil {
  385. if hasDifference {
  386. shouldInclude = true
  387. }
  388. } else {
  389. if hasUpstreamValue {
  390. shouldInclude = true
  391. }
  392. }
  393. if shouldInclude {
  394. if differences[modelName] == nil {
  395. differences[modelName] = make(map[string]dto.DifferenceItem)
  396. }
  397. differences[modelName][ratioType] = dto.DifferenceItem{
  398. Current: localValue,
  399. Upstreams: upstreamValues,
  400. Confidence: confidenceValues,
  401. }
  402. }
  403. }
  404. }
  405. channelHasDiff := make(map[string]bool)
  406. for _, ratioMap := range differences {
  407. for _, item := range ratioMap {
  408. for chName, val := range item.Upstreams {
  409. if val != nil && val != "same" {
  410. channelHasDiff[chName] = true
  411. }
  412. }
  413. }
  414. }
  415. for modelName, ratioMap := range differences {
  416. for ratioType, item := range ratioMap {
  417. for chName := range item.Upstreams {
  418. if !channelHasDiff[chName] {
  419. delete(item.Upstreams, chName)
  420. delete(item.Confidence, chName)
  421. }
  422. }
  423. allSame := true
  424. for _, v := range item.Upstreams {
  425. if v != "same" {
  426. allSame = false
  427. break
  428. }
  429. }
  430. if len(item.Upstreams) == 0 || allSame {
  431. delete(ratioMap, ratioType)
  432. } else {
  433. differences[modelName][ratioType] = item
  434. }
  435. }
  436. if len(ratioMap) == 0 {
  437. delete(differences, modelName)
  438. }
  439. }
  440. return differences
  441. }
  442. func GetSyncableChannels(c *gin.Context) {
  443. channels, err := model.GetAllChannels(0, 0, true, false)
  444. if err != nil {
  445. c.JSON(http.StatusOK, gin.H{
  446. "success": false,
  447. "message": err.Error(),
  448. })
  449. return
  450. }
  451. var syncableChannels []dto.SyncableChannel
  452. for _, channel := range channels {
  453. if channel.GetBaseURL() != "" {
  454. syncableChannels = append(syncableChannels, dto.SyncableChannel{
  455. ID: channel.Id,
  456. Name: channel.Name,
  457. BaseURL: channel.GetBaseURL(),
  458. Status: channel.Status,
  459. })
  460. }
  461. }
  462. syncableChannels = append(syncableChannels, dto.SyncableChannel{
  463. ID: -100,
  464. Name: "官方倍率预设",
  465. BaseURL: "https://basellm.github.io",
  466. Status: 1,
  467. })
  468. c.JSON(http.StatusOK, gin.H{
  469. "success": true,
  470. "message": "",
  471. "data": syncableChannels,
  472. })
  473. }