channel.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strconv"
  9. "strings"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type OpenAIModel struct {
  13. ID string `json:"id"`
  14. Object string `json:"object"`
  15. Created int64 `json:"created"`
  16. OwnedBy string `json:"owned_by"`
  17. Permission []struct {
  18. ID string `json:"id"`
  19. Object string `json:"object"`
  20. Created int64 `json:"created"`
  21. AllowCreateEngine bool `json:"allow_create_engine"`
  22. AllowSampling bool `json:"allow_sampling"`
  23. AllowLogprobs bool `json:"allow_logprobs"`
  24. AllowSearchIndices bool `json:"allow_search_indices"`
  25. AllowView bool `json:"allow_view"`
  26. AllowFineTuning bool `json:"allow_fine_tuning"`
  27. Organization string `json:"organization"`
  28. Group string `json:"group"`
  29. IsBlocking bool `json:"is_blocking"`
  30. } `json:"permission"`
  31. Root string `json:"root"`
  32. Parent string `json:"parent"`
  33. }
  34. type OpenAIModelsResponse struct {
  35. Data []OpenAIModel `json:"data"`
  36. Success bool `json:"success"`
  37. }
  38. func GetAllChannels(c *gin.Context) {
  39. p, _ := strconv.Atoi(c.Query("p"))
  40. pageSize, _ := strconv.Atoi(c.Query("page_size"))
  41. if p < 1 {
  42. p = 1
  43. }
  44. if pageSize < 1 {
  45. pageSize = common.ItemsPerPage
  46. }
  47. channelData := make([]*model.Channel, 0)
  48. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  49. enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
  50. // type filter
  51. typeStr := c.Query("type")
  52. typeFilter := -1
  53. if typeStr != "" {
  54. if t, err := strconv.Atoi(typeStr); err == nil {
  55. typeFilter = t
  56. }
  57. }
  58. var total int64
  59. if enableTagMode {
  60. // tag 分页:先分页 tag,再取各 tag 下 channels
  61. tags, err := model.GetPaginatedTags((p-1)*pageSize, pageSize)
  62. if err != nil {
  63. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  64. return
  65. }
  66. for _, tag := range tags {
  67. if tag != nil && *tag != "" {
  68. tagChannel, err := model.GetChannelsByTag(*tag, idSort)
  69. if err == nil {
  70. channelData = append(channelData, tagChannel...)
  71. }
  72. }
  73. }
  74. // 计算 tag 总数用于分页
  75. total, _ = model.CountAllTags()
  76. } else if typeFilter >= 0 {
  77. channels, err := model.GetChannelsByType((p-1)*pageSize, pageSize, idSort, typeFilter)
  78. if err != nil {
  79. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  80. return
  81. }
  82. channelData = channels
  83. total, _ = model.CountChannelsByType(typeFilter)
  84. } else {
  85. channels, err := model.GetAllChannels((p-1)*pageSize, pageSize, false, idSort)
  86. if err != nil {
  87. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  88. return
  89. }
  90. channelData = channels
  91. total, _ = model.CountAllChannels()
  92. }
  93. // calculate type counts
  94. typeCounts, _ := model.CountChannelsGroupByType()
  95. c.JSON(http.StatusOK, gin.H{
  96. "success": true,
  97. "message": "",
  98. "data": gin.H{
  99. "items": channelData,
  100. "total": total,
  101. "page": p,
  102. "page_size": pageSize,
  103. "type_counts": typeCounts,
  104. },
  105. })
  106. return
  107. }
  108. func FetchUpstreamModels(c *gin.Context) {
  109. id, err := strconv.Atoi(c.Param("id"))
  110. if err != nil {
  111. c.JSON(http.StatusOK, gin.H{
  112. "success": false,
  113. "message": err.Error(),
  114. })
  115. return
  116. }
  117. channel, err := model.GetChannelById(id, true)
  118. if err != nil {
  119. c.JSON(http.StatusOK, gin.H{
  120. "success": false,
  121. "message": err.Error(),
  122. })
  123. return
  124. }
  125. //if channel.Type != common.ChannelTypeOpenAI {
  126. // c.JSON(http.StatusOK, gin.H{
  127. // "success": false,
  128. // "message": "仅支持 OpenAI 类型渠道",
  129. // })
  130. // return
  131. //}
  132. baseURL := common.ChannelBaseURLs[channel.Type]
  133. if channel.GetBaseURL() != "" {
  134. baseURL = channel.GetBaseURL()
  135. }
  136. url := fmt.Sprintf("%s/v1/models", baseURL)
  137. switch channel.Type {
  138. case common.ChannelTypeGemini:
  139. url = fmt.Sprintf("%s/v1beta/openai/models", baseURL)
  140. case common.ChannelTypeAli:
  141. url = fmt.Sprintf("%s/compatible-mode/v1/models", baseURL)
  142. }
  143. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  144. if err != nil {
  145. c.JSON(http.StatusOK, gin.H{
  146. "success": false,
  147. "message": err.Error(),
  148. })
  149. return
  150. }
  151. var result OpenAIModelsResponse
  152. if err = json.Unmarshal(body, &result); err != nil {
  153. c.JSON(http.StatusOK, gin.H{
  154. "success": false,
  155. "message": fmt.Sprintf("解析响应失败: %s", err.Error()),
  156. })
  157. return
  158. }
  159. var ids []string
  160. for _, model := range result.Data {
  161. id := model.ID
  162. if channel.Type == common.ChannelTypeGemini {
  163. id = strings.TrimPrefix(id, "models/")
  164. }
  165. ids = append(ids, id)
  166. }
  167. c.JSON(http.StatusOK, gin.H{
  168. "success": true,
  169. "message": "",
  170. "data": ids,
  171. })
  172. }
  173. func FixChannelsAbilities(c *gin.Context) {
  174. count, err := model.FixAbility()
  175. if err != nil {
  176. c.JSON(http.StatusOK, gin.H{
  177. "success": false,
  178. "message": err.Error(),
  179. })
  180. return
  181. }
  182. c.JSON(http.StatusOK, gin.H{
  183. "success": true,
  184. "message": "",
  185. "data": count,
  186. })
  187. }
  188. func SearchChannels(c *gin.Context) {
  189. keyword := c.Query("keyword")
  190. group := c.Query("group")
  191. modelKeyword := c.Query("model")
  192. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  193. enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
  194. channelData := make([]*model.Channel, 0)
  195. if enableTagMode {
  196. tags, err := model.SearchTags(keyword, group, modelKeyword, idSort)
  197. if err != nil {
  198. c.JSON(http.StatusOK, gin.H{
  199. "success": false,
  200. "message": err.Error(),
  201. })
  202. return
  203. }
  204. for _, tag := range tags {
  205. if tag != nil && *tag != "" {
  206. tagChannel, err := model.GetChannelsByTag(*tag, idSort)
  207. if err == nil {
  208. channelData = append(channelData, tagChannel...)
  209. }
  210. }
  211. }
  212. } else {
  213. channels, err := model.SearchChannels(keyword, group, modelKeyword, idSort)
  214. if err != nil {
  215. c.JSON(http.StatusOK, gin.H{
  216. "success": false,
  217. "message": err.Error(),
  218. })
  219. return
  220. }
  221. channelData = channels
  222. }
  223. // calculate type counts for search results
  224. typeCounts := make(map[int64]int64)
  225. for _, channel := range channelData {
  226. typeCounts[int64(channel.Type)]++
  227. }
  228. c.JSON(http.StatusOK, gin.H{
  229. "success": true,
  230. "message": "",
  231. "data": gin.H{
  232. "items": channelData,
  233. "type_counts": typeCounts,
  234. },
  235. })
  236. return
  237. }
  238. func GetChannel(c *gin.Context) {
  239. id, err := strconv.Atoi(c.Param("id"))
  240. if err != nil {
  241. c.JSON(http.StatusOK, gin.H{
  242. "success": false,
  243. "message": err.Error(),
  244. })
  245. return
  246. }
  247. channel, err := model.GetChannelById(id, false)
  248. if err != nil {
  249. c.JSON(http.StatusOK, gin.H{
  250. "success": false,
  251. "message": err.Error(),
  252. })
  253. return
  254. }
  255. c.JSON(http.StatusOK, gin.H{
  256. "success": true,
  257. "message": "",
  258. "data": channel,
  259. })
  260. return
  261. }
  262. func AddChannel(c *gin.Context) {
  263. channel := model.Channel{}
  264. err := c.ShouldBindJSON(&channel)
  265. if err != nil {
  266. c.JSON(http.StatusOK, gin.H{
  267. "success": false,
  268. "message": err.Error(),
  269. })
  270. return
  271. }
  272. channel.CreatedTime = common.GetTimestamp()
  273. keys := strings.Split(channel.Key, "\n")
  274. if channel.Type == common.ChannelTypeVertexAi {
  275. if channel.Other == "" {
  276. c.JSON(http.StatusOK, gin.H{
  277. "success": false,
  278. "message": "部署地区不能为空",
  279. })
  280. return
  281. } else {
  282. if common.IsJsonStr(channel.Other) {
  283. // must have default
  284. regionMap := common.StrToMap(channel.Other)
  285. if regionMap["default"] == nil {
  286. c.JSON(http.StatusOK, gin.H{
  287. "success": false,
  288. "message": "部署地区必须包含default字段",
  289. })
  290. return
  291. }
  292. }
  293. }
  294. keys = []string{channel.Key}
  295. }
  296. channels := make([]model.Channel, 0, len(keys))
  297. for _, key := range keys {
  298. if key == "" {
  299. continue
  300. }
  301. localChannel := channel
  302. localChannel.Key = key
  303. // Validate the length of the model name
  304. models := strings.Split(localChannel.Models, ",")
  305. for _, model := range models {
  306. if len(model) > 255 {
  307. c.JSON(http.StatusOK, gin.H{
  308. "success": false,
  309. "message": fmt.Sprintf("模型名称过长: %s", model),
  310. })
  311. return
  312. }
  313. }
  314. channels = append(channels, localChannel)
  315. }
  316. err = model.BatchInsertChannels(channels)
  317. if err != nil {
  318. c.JSON(http.StatusOK, gin.H{
  319. "success": false,
  320. "message": err.Error(),
  321. })
  322. return
  323. }
  324. c.JSON(http.StatusOK, gin.H{
  325. "success": true,
  326. "message": "",
  327. })
  328. return
  329. }
  330. func DeleteChannel(c *gin.Context) {
  331. id, _ := strconv.Atoi(c.Param("id"))
  332. channel := model.Channel{Id: id}
  333. err := channel.Delete()
  334. if err != nil {
  335. c.JSON(http.StatusOK, gin.H{
  336. "success": false,
  337. "message": err.Error(),
  338. })
  339. return
  340. }
  341. c.JSON(http.StatusOK, gin.H{
  342. "success": true,
  343. "message": "",
  344. })
  345. return
  346. }
  347. func DeleteDisabledChannel(c *gin.Context) {
  348. rows, err := model.DeleteDisabledChannel()
  349. if err != nil {
  350. c.JSON(http.StatusOK, gin.H{
  351. "success": false,
  352. "message": err.Error(),
  353. })
  354. return
  355. }
  356. c.JSON(http.StatusOK, gin.H{
  357. "success": true,
  358. "message": "",
  359. "data": rows,
  360. })
  361. return
  362. }
  363. type ChannelTag struct {
  364. Tag string `json:"tag"`
  365. NewTag *string `json:"new_tag"`
  366. Priority *int64 `json:"priority"`
  367. Weight *uint `json:"weight"`
  368. ModelMapping *string `json:"model_mapping"`
  369. Models *string `json:"models"`
  370. Groups *string `json:"groups"`
  371. }
  372. func DisableTagChannels(c *gin.Context) {
  373. channelTag := ChannelTag{}
  374. err := c.ShouldBindJSON(&channelTag)
  375. if err != nil || channelTag.Tag == "" {
  376. c.JSON(http.StatusOK, gin.H{
  377. "success": false,
  378. "message": "参数错误",
  379. })
  380. return
  381. }
  382. err = model.DisableChannelByTag(channelTag.Tag)
  383. if err != nil {
  384. c.JSON(http.StatusOK, gin.H{
  385. "success": false,
  386. "message": err.Error(),
  387. })
  388. return
  389. }
  390. c.JSON(http.StatusOK, gin.H{
  391. "success": true,
  392. "message": "",
  393. })
  394. return
  395. }
  396. func EnableTagChannels(c *gin.Context) {
  397. channelTag := ChannelTag{}
  398. err := c.ShouldBindJSON(&channelTag)
  399. if err != nil || channelTag.Tag == "" {
  400. c.JSON(http.StatusOK, gin.H{
  401. "success": false,
  402. "message": "参数错误",
  403. })
  404. return
  405. }
  406. err = model.EnableChannelByTag(channelTag.Tag)
  407. if err != nil {
  408. c.JSON(http.StatusOK, gin.H{
  409. "success": false,
  410. "message": err.Error(),
  411. })
  412. return
  413. }
  414. c.JSON(http.StatusOK, gin.H{
  415. "success": true,
  416. "message": "",
  417. })
  418. return
  419. }
  420. func EditTagChannels(c *gin.Context) {
  421. channelTag := ChannelTag{}
  422. err := c.ShouldBindJSON(&channelTag)
  423. if err != nil {
  424. c.JSON(http.StatusOK, gin.H{
  425. "success": false,
  426. "message": "参数错误",
  427. })
  428. return
  429. }
  430. if channelTag.Tag == "" {
  431. c.JSON(http.StatusOK, gin.H{
  432. "success": false,
  433. "message": "tag不能为空",
  434. })
  435. return
  436. }
  437. err = model.EditChannelByTag(channelTag.Tag, channelTag.NewTag, channelTag.ModelMapping, channelTag.Models, channelTag.Groups, channelTag.Priority, channelTag.Weight)
  438. if err != nil {
  439. c.JSON(http.StatusOK, gin.H{
  440. "success": false,
  441. "message": err.Error(),
  442. })
  443. return
  444. }
  445. c.JSON(http.StatusOK, gin.H{
  446. "success": true,
  447. "message": "",
  448. })
  449. return
  450. }
  451. type ChannelBatch struct {
  452. Ids []int `json:"ids"`
  453. Tag *string `json:"tag"`
  454. }
  455. func DeleteChannelBatch(c *gin.Context) {
  456. channelBatch := ChannelBatch{}
  457. err := c.ShouldBindJSON(&channelBatch)
  458. if err != nil || len(channelBatch.Ids) == 0 {
  459. c.JSON(http.StatusOK, gin.H{
  460. "success": false,
  461. "message": "参数错误",
  462. })
  463. return
  464. }
  465. err = model.BatchDeleteChannels(channelBatch.Ids)
  466. if err != nil {
  467. c.JSON(http.StatusOK, gin.H{
  468. "success": false,
  469. "message": err.Error(),
  470. })
  471. return
  472. }
  473. c.JSON(http.StatusOK, gin.H{
  474. "success": true,
  475. "message": "",
  476. "data": len(channelBatch.Ids),
  477. })
  478. return
  479. }
  480. func UpdateChannel(c *gin.Context) {
  481. channel := model.Channel{}
  482. err := c.ShouldBindJSON(&channel)
  483. if err != nil {
  484. c.JSON(http.StatusOK, gin.H{
  485. "success": false,
  486. "message": err.Error(),
  487. })
  488. return
  489. }
  490. if channel.Type == common.ChannelTypeVertexAi {
  491. if channel.Other == "" {
  492. c.JSON(http.StatusOK, gin.H{
  493. "success": false,
  494. "message": "部署地区不能为空",
  495. })
  496. return
  497. } else {
  498. if common.IsJsonStr(channel.Other) {
  499. // must have default
  500. regionMap := common.StrToMap(channel.Other)
  501. if regionMap["default"] == nil {
  502. c.JSON(http.StatusOK, gin.H{
  503. "success": false,
  504. "message": "部署地区必须包含default字段",
  505. })
  506. return
  507. }
  508. }
  509. }
  510. }
  511. err = channel.Update()
  512. if err != nil {
  513. c.JSON(http.StatusOK, gin.H{
  514. "success": false,
  515. "message": err.Error(),
  516. })
  517. return
  518. }
  519. c.JSON(http.StatusOK, gin.H{
  520. "success": true,
  521. "message": "",
  522. "data": channel,
  523. })
  524. return
  525. }
  526. func FetchModels(c *gin.Context) {
  527. var req struct {
  528. BaseURL string `json:"base_url"`
  529. Type int `json:"type"`
  530. Key string `json:"key"`
  531. }
  532. if err := c.ShouldBindJSON(&req); err != nil {
  533. c.JSON(http.StatusBadRequest, gin.H{
  534. "success": false,
  535. "message": "Invalid request",
  536. })
  537. return
  538. }
  539. baseURL := req.BaseURL
  540. if baseURL == "" {
  541. baseURL = common.ChannelBaseURLs[req.Type]
  542. }
  543. client := &http.Client{}
  544. url := fmt.Sprintf("%s/v1/models", baseURL)
  545. request, err := http.NewRequest("GET", url, nil)
  546. if err != nil {
  547. c.JSON(http.StatusInternalServerError, gin.H{
  548. "success": false,
  549. "message": err.Error(),
  550. })
  551. return
  552. }
  553. // remove line breaks and extra spaces.
  554. key := strings.TrimSpace(req.Key)
  555. // If the key contains a line break, only take the first part.
  556. key = strings.Split(key, "\n")[0]
  557. request.Header.Set("Authorization", "Bearer "+key)
  558. response, err := client.Do(request)
  559. if err != nil {
  560. c.JSON(http.StatusInternalServerError, gin.H{
  561. "success": false,
  562. "message": err.Error(),
  563. })
  564. return
  565. }
  566. //check status code
  567. if response.StatusCode != http.StatusOK {
  568. c.JSON(http.StatusInternalServerError, gin.H{
  569. "success": false,
  570. "message": "Failed to fetch models",
  571. })
  572. return
  573. }
  574. defer response.Body.Close()
  575. var result struct {
  576. Data []struct {
  577. ID string `json:"id"`
  578. } `json:"data"`
  579. }
  580. if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
  581. c.JSON(http.StatusInternalServerError, gin.H{
  582. "success": false,
  583. "message": err.Error(),
  584. })
  585. return
  586. }
  587. var models []string
  588. for _, model := range result.Data {
  589. models = append(models, model.ID)
  590. }
  591. c.JSON(http.StatusOK, gin.H{
  592. "success": true,
  593. "data": models,
  594. })
  595. }
  596. func BatchSetChannelTag(c *gin.Context) {
  597. channelBatch := ChannelBatch{}
  598. err := c.ShouldBindJSON(&channelBatch)
  599. if err != nil || len(channelBatch.Ids) == 0 {
  600. c.JSON(http.StatusOK, gin.H{
  601. "success": false,
  602. "message": "参数错误",
  603. })
  604. return
  605. }
  606. err = model.BatchSetChannelTag(channelBatch.Ids, channelBatch.Tag)
  607. if err != nil {
  608. c.JSON(http.StatusOK, gin.H{
  609. "success": false,
  610. "message": err.Error(),
  611. })
  612. return
  613. }
  614. c.JSON(http.StatusOK, gin.H{
  615. "success": true,
  616. "message": "",
  617. "data": len(channelBatch.Ids),
  618. })
  619. return
  620. }
  621. func GetTagModels(c *gin.Context) {
  622. tag := c.Query("tag")
  623. if tag == "" {
  624. c.JSON(http.StatusBadRequest, gin.H{
  625. "success": false,
  626. "message": "tag不能为空",
  627. })
  628. return
  629. }
  630. channels, err := model.GetChannelsByTag(tag, false) // Assuming false for idSort is fine here
  631. if err != nil {
  632. c.JSON(http.StatusInternalServerError, gin.H{
  633. "success": false,
  634. "message": err.Error(),
  635. })
  636. return
  637. }
  638. var longestModels string
  639. maxLength := 0
  640. // Find the longest models string among all channels with the given tag
  641. for _, channel := range channels {
  642. if channel.Models != "" {
  643. currentModels := strings.Split(channel.Models, ",")
  644. if len(currentModels) > maxLength {
  645. maxLength = len(currentModels)
  646. longestModels = channel.Models
  647. }
  648. }
  649. }
  650. c.JSON(http.StatusOK, gin.H{
  651. "success": true,
  652. "message": "",
  653. "data": longestModels,
  654. })
  655. return
  656. }