ratio_sync.go 15 KB

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