channel.go 9.5 KB

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