channel.go 10 KB

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