ratio_sync.go 19 KB

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