channel.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 < 0 {
  42. p = 0
  43. }
  44. if pageSize < 0 {
  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. if enableTagMode {
  51. tags, err := model.GetPaginatedTags(p*pageSize, pageSize)
  52. if err != nil {
  53. c.JSON(http.StatusOK, gin.H{
  54. "success": false,
  55. "message": err.Error(),
  56. })
  57. return
  58. }
  59. for _, tag := range tags {
  60. if tag != nil && *tag != "" {
  61. tagChannel, err := model.GetChannelsByTag(*tag, idSort)
  62. if err == nil {
  63. channelData = append(channelData, tagChannel...)
  64. }
  65. }
  66. }
  67. } else {
  68. channels, err := model.GetAllChannels(p*pageSize, pageSize, false, idSort)
  69. if err != nil {
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": false,
  72. "message": err.Error(),
  73. })
  74. return
  75. }
  76. channelData = channels
  77. }
  78. c.JSON(http.StatusOK, gin.H{
  79. "success": true,
  80. "message": "",
  81. "data": channelData,
  82. })
  83. return
  84. }
  85. func FetchUpstreamModels(c *gin.Context) {
  86. id, err := strconv.Atoi(c.Param("id"))
  87. if err != nil {
  88. c.JSON(http.StatusOK, gin.H{
  89. "success": false,
  90. "message": err.Error(),
  91. })
  92. return
  93. }
  94. channel, err := model.GetChannelById(id, true)
  95. if err != nil {
  96. c.JSON(http.StatusOK, gin.H{
  97. "success": false,
  98. "message": err.Error(),
  99. })
  100. return
  101. }
  102. if channel.Type != common.ChannelTypeOpenAI {
  103. c.JSON(http.StatusOK, gin.H{
  104. "success": false,
  105. "message": "仅支持 OpenAI 类型渠道",
  106. })
  107. return
  108. }
  109. url := fmt.Sprintf("%s/v1/models", *channel.BaseURL)
  110. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  111. if err != nil {
  112. c.JSON(http.StatusOK, gin.H{
  113. "success": false,
  114. "message": err.Error(),
  115. })
  116. }
  117. result := OpenAIModelsResponse{}
  118. err = json.Unmarshal(body, &result)
  119. if err != nil {
  120. c.JSON(http.StatusOK, gin.H{
  121. "success": false,
  122. "message": err.Error(),
  123. })
  124. }
  125. if !result.Success {
  126. c.JSON(http.StatusOK, gin.H{
  127. "success": false,
  128. "message": "上游返回错误",
  129. })
  130. }
  131. var ids []string
  132. for _, model := range result.Data {
  133. ids = append(ids, model.ID)
  134. }
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": true,
  137. "message": "",
  138. "data": ids,
  139. })
  140. }
  141. func FixChannelsAbilities(c *gin.Context) {
  142. count, err := model.FixAbility()
  143. if err != nil {
  144. c.JSON(http.StatusOK, gin.H{
  145. "success": false,
  146. "message": err.Error(),
  147. })
  148. return
  149. }
  150. c.JSON(http.StatusOK, gin.H{
  151. "success": true,
  152. "message": "",
  153. "data": count,
  154. })
  155. }
  156. func SearchChannels(c *gin.Context) {
  157. keyword := c.Query("keyword")
  158. group := c.Query("group")
  159. modelKeyword := c.Query("model")
  160. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  161. enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
  162. channelData := make([]*model.Channel, 0)
  163. if enableTagMode {
  164. tags, err := model.SearchTags(keyword, group, modelKeyword, idSort)
  165. if err != nil {
  166. c.JSON(http.StatusOK, gin.H{
  167. "success": false,
  168. "message": err.Error(),
  169. })
  170. return
  171. }
  172. for _, tag := range tags {
  173. if tag != nil && *tag != "" {
  174. tagChannel, err := model.GetChannelsByTag(*tag, idSort)
  175. if err == nil {
  176. channelData = append(channelData, tagChannel...)
  177. }
  178. }
  179. }
  180. } else {
  181. channels, err := model.SearchChannels(keyword, group, modelKeyword, idSort)
  182. if err != nil {
  183. c.JSON(http.StatusOK, gin.H{
  184. "success": false,
  185. "message": err.Error(),
  186. })
  187. return
  188. }
  189. channelData = channels
  190. }
  191. c.JSON(http.StatusOK, gin.H{
  192. "success": true,
  193. "message": "",
  194. "data": channelData,
  195. })
  196. return
  197. }
  198. func GetChannel(c *gin.Context) {
  199. id, err := strconv.Atoi(c.Param("id"))
  200. if err != nil {
  201. c.JSON(http.StatusOK, gin.H{
  202. "success": false,
  203. "message": err.Error(),
  204. })
  205. return
  206. }
  207. channel, err := model.GetChannelById(id, false)
  208. if err != nil {
  209. c.JSON(http.StatusOK, gin.H{
  210. "success": false,
  211. "message": err.Error(),
  212. })
  213. return
  214. }
  215. c.JSON(http.StatusOK, gin.H{
  216. "success": true,
  217. "message": "",
  218. "data": channel,
  219. })
  220. return
  221. }
  222. func AddChannel(c *gin.Context) {
  223. channel := model.Channel{}
  224. err := c.ShouldBindJSON(&channel)
  225. if err != nil {
  226. c.JSON(http.StatusOK, gin.H{
  227. "success": false,
  228. "message": err.Error(),
  229. })
  230. return
  231. }
  232. channel.CreatedTime = common.GetTimestamp()
  233. keys := strings.Split(channel.Key, "\n")
  234. if channel.Type == common.ChannelTypeVertexAi {
  235. if channel.Other == "" {
  236. c.JSON(http.StatusOK, gin.H{
  237. "success": false,
  238. "message": "部署地区不能为空",
  239. })
  240. return
  241. } else {
  242. if common.IsJsonStr(channel.Other) {
  243. // must have default
  244. regionMap := common.StrToMap(channel.Other)
  245. if regionMap["default"] == nil {
  246. c.JSON(http.StatusOK, gin.H{
  247. "success": false,
  248. "message": "部署地区必须包含default字段",
  249. })
  250. return
  251. }
  252. }
  253. }
  254. keys = []string{channel.Key}
  255. }
  256. channels := make([]model.Channel, 0, len(keys))
  257. for _, key := range keys {
  258. if key == "" {
  259. continue
  260. }
  261. localChannel := channel
  262. localChannel.Key = key
  263. channels = append(channels, localChannel)
  264. }
  265. err = model.BatchInsertChannels(channels)
  266. if err != nil {
  267. c.JSON(http.StatusOK, gin.H{
  268. "success": false,
  269. "message": err.Error(),
  270. })
  271. return
  272. }
  273. c.JSON(http.StatusOK, gin.H{
  274. "success": true,
  275. "message": "",
  276. })
  277. return
  278. }
  279. func DeleteChannel(c *gin.Context) {
  280. id, _ := strconv.Atoi(c.Param("id"))
  281. channel := model.Channel{Id: id}
  282. err := channel.Delete()
  283. if err != nil {
  284. c.JSON(http.StatusOK, gin.H{
  285. "success": false,
  286. "message": err.Error(),
  287. })
  288. return
  289. }
  290. c.JSON(http.StatusOK, gin.H{
  291. "success": true,
  292. "message": "",
  293. })
  294. return
  295. }
  296. func DeleteDisabledChannel(c *gin.Context) {
  297. rows, err := model.DeleteDisabledChannel()
  298. if err != nil {
  299. c.JSON(http.StatusOK, gin.H{
  300. "success": false,
  301. "message": err.Error(),
  302. })
  303. return
  304. }
  305. c.JSON(http.StatusOK, gin.H{
  306. "success": true,
  307. "message": "",
  308. "data": rows,
  309. })
  310. return
  311. }
  312. type ChannelTag struct {
  313. Tag string `json:"tag"`
  314. NewTag *string `json:"new_tag"`
  315. Priority *int64 `json:"priority"`
  316. Weight *uint `json:"weight"`
  317. ModelMapping *string `json:"model_mapping"`
  318. Models *string `json:"models"`
  319. Groups *string `json:"groups"`
  320. }
  321. func DisableTagChannels(c *gin.Context) {
  322. channelTag := ChannelTag{}
  323. err := c.ShouldBindJSON(&channelTag)
  324. if err != nil || channelTag.Tag == "" {
  325. c.JSON(http.StatusOK, gin.H{
  326. "success": false,
  327. "message": "参数错误",
  328. })
  329. return
  330. }
  331. err = model.DisableChannelByTag(channelTag.Tag)
  332. if err != nil {
  333. c.JSON(http.StatusOK, gin.H{
  334. "success": false,
  335. "message": err.Error(),
  336. })
  337. return
  338. }
  339. c.JSON(http.StatusOK, gin.H{
  340. "success": true,
  341. "message": "",
  342. })
  343. return
  344. }
  345. func EnableTagChannels(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.EnableChannelByTag(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 EditTagChannels(c *gin.Context) {
  370. channelTag := ChannelTag{}
  371. err := c.ShouldBindJSON(&channelTag)
  372. if err != nil {
  373. c.JSON(http.StatusOK, gin.H{
  374. "success": false,
  375. "message": "参数错误",
  376. })
  377. return
  378. }
  379. if channelTag.Tag == "" {
  380. c.JSON(http.StatusOK, gin.H{
  381. "success": false,
  382. "message": "tag不能为空",
  383. })
  384. return
  385. }
  386. err = model.EditChannelByTag(channelTag.Tag, channelTag.NewTag, channelTag.ModelMapping, channelTag.Models, channelTag.Groups, channelTag.Priority, channelTag.Weight)
  387. if err != nil {
  388. c.JSON(http.StatusOK, gin.H{
  389. "success": false,
  390. "message": err.Error(),
  391. })
  392. return
  393. }
  394. c.JSON(http.StatusOK, gin.H{
  395. "success": true,
  396. "message": "",
  397. })
  398. return
  399. }
  400. type ChannelBatch struct {
  401. Ids []int `json:"ids"`
  402. }
  403. func DeleteChannelBatch(c *gin.Context) {
  404. channelBatch := ChannelBatch{}
  405. err := c.ShouldBindJSON(&channelBatch)
  406. if err != nil || len(channelBatch.Ids) == 0 {
  407. c.JSON(http.StatusOK, gin.H{
  408. "success": false,
  409. "message": "参数错误",
  410. })
  411. return
  412. }
  413. err = model.BatchDeleteChannels(channelBatch.Ids)
  414. if err != nil {
  415. c.JSON(http.StatusOK, gin.H{
  416. "success": false,
  417. "message": err.Error(),
  418. })
  419. return
  420. }
  421. c.JSON(http.StatusOK, gin.H{
  422. "success": true,
  423. "message": "",
  424. "data": len(channelBatch.Ids),
  425. })
  426. return
  427. }
  428. func UpdateChannel(c *gin.Context) {
  429. channel := model.Channel{}
  430. err := c.ShouldBindJSON(&channel)
  431. if err != nil {
  432. c.JSON(http.StatusOK, gin.H{
  433. "success": false,
  434. "message": err.Error(),
  435. })
  436. return
  437. }
  438. if channel.Type == common.ChannelTypeVertexAi {
  439. if channel.Other == "" {
  440. c.JSON(http.StatusOK, gin.H{
  441. "success": false,
  442. "message": "部署地区不能为空",
  443. })
  444. return
  445. } else {
  446. if common.IsJsonStr(channel.Other) {
  447. // must have default
  448. regionMap := common.StrToMap(channel.Other)
  449. if regionMap["default"] == nil {
  450. c.JSON(http.StatusOK, gin.H{
  451. "success": false,
  452. "message": "部署地区必须包含default字段",
  453. })
  454. return
  455. }
  456. }
  457. }
  458. }
  459. err = channel.Update()
  460. if err != nil {
  461. c.JSON(http.StatusOK, gin.H{
  462. "success": false,
  463. "message": err.Error(),
  464. })
  465. return
  466. }
  467. c.JSON(http.StatusOK, gin.H{
  468. "success": true,
  469. "message": "",
  470. "data": channel,
  471. })
  472. return
  473. }