channel.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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)
  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:"newTag"`
  285. Priority *int64 `json:"priority"`
  286. Weight *uint `json:"weight"`
  287. }
  288. func DisableTagChannels(c *gin.Context) {
  289. channelTag := ChannelTag{}
  290. err := c.ShouldBindJSON(&channelTag)
  291. if err != nil || channelTag.Tag == "" {
  292. c.JSON(http.StatusOK, gin.H{
  293. "success": false,
  294. "message": "参数错误",
  295. })
  296. return
  297. }
  298. err = model.DisableChannelByTag(channelTag.Tag)
  299. if err != nil {
  300. c.JSON(http.StatusOK, gin.H{
  301. "success": false,
  302. "message": err.Error(),
  303. })
  304. return
  305. }
  306. c.JSON(http.StatusOK, gin.H{
  307. "success": true,
  308. "message": "",
  309. })
  310. return
  311. }
  312. func EnableTagChannels(c *gin.Context) {
  313. channelTag := ChannelTag{}
  314. err := c.ShouldBindJSON(&channelTag)
  315. if err != nil || channelTag.Tag == "" {
  316. c.JSON(http.StatusOK, gin.H{
  317. "success": false,
  318. "message": "参数错误",
  319. })
  320. return
  321. }
  322. err = model.EnableChannelByTag(channelTag.Tag)
  323. if err != nil {
  324. c.JSON(http.StatusOK, gin.H{
  325. "success": false,
  326. "message": err.Error(),
  327. })
  328. return
  329. }
  330. c.JSON(http.StatusOK, gin.H{
  331. "success": true,
  332. "message": "",
  333. })
  334. return
  335. }
  336. func EditTagChannels(c *gin.Context) {
  337. channelTag := ChannelTag{}
  338. err := c.ShouldBindJSON(&channelTag)
  339. if err != nil || channelTag.Tag == "" {
  340. c.JSON(http.StatusOK, gin.H{
  341. "success": false,
  342. "message": "参数错误",
  343. })
  344. return
  345. }
  346. err = model.EditChannelByTag(channelTag.Tag, channelTag.NewTag, channelTag.Priority, channelTag.Weight)
  347. if err != nil {
  348. c.JSON(http.StatusOK, gin.H{
  349. "success": false,
  350. "message": err.Error(),
  351. })
  352. return
  353. }
  354. c.JSON(http.StatusOK, gin.H{
  355. "success": true,
  356. "message": "",
  357. })
  358. return
  359. }
  360. type ChannelBatch struct {
  361. Ids []int `json:"ids"`
  362. }
  363. func DeleteChannelBatch(c *gin.Context) {
  364. channelBatch := ChannelBatch{}
  365. err := c.ShouldBindJSON(&channelBatch)
  366. if err != nil || len(channelBatch.Ids) == 0 {
  367. c.JSON(http.StatusOK, gin.H{
  368. "success": false,
  369. "message": "参数错误",
  370. })
  371. return
  372. }
  373. err = model.BatchDeleteChannels(channelBatch.Ids)
  374. if err != nil {
  375. c.JSON(http.StatusOK, gin.H{
  376. "success": false,
  377. "message": err.Error(),
  378. })
  379. return
  380. }
  381. c.JSON(http.StatusOK, gin.H{
  382. "success": true,
  383. "message": "",
  384. "data": len(channelBatch.Ids),
  385. })
  386. return
  387. }
  388. func UpdateChannel(c *gin.Context) {
  389. channel := model.Channel{}
  390. err := c.ShouldBindJSON(&channel)
  391. if err != nil {
  392. c.JSON(http.StatusOK, gin.H{
  393. "success": false,
  394. "message": err.Error(),
  395. })
  396. return
  397. }
  398. if channel.Type == common.ChannelTypeVertexAi {
  399. if channel.Other == "" {
  400. c.JSON(http.StatusOK, gin.H{
  401. "success": false,
  402. "message": "部署地区不能为空",
  403. })
  404. return
  405. } else {
  406. if common.IsJsonStr(channel.Other) {
  407. // must have default
  408. regionMap := common.StrToMap(channel.Other)
  409. if regionMap["default"] == nil {
  410. c.JSON(http.StatusOK, gin.H{
  411. "success": false,
  412. "message": "部署地区必须包含default字段",
  413. })
  414. return
  415. }
  416. }
  417. }
  418. }
  419. err = channel.Update()
  420. if err != nil {
  421. c.JSON(http.StatusOK, gin.H{
  422. "success": false,
  423. "message": err.Error(),
  424. })
  425. return
  426. }
  427. c.JSON(http.StatusOK, gin.H{
  428. "success": true,
  429. "message": "",
  430. "data": channel,
  431. })
  432. return
  433. }