channel.go 14 KB

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