channel.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. c.JSON(http.StatusOK, gin.H{
  57. "success": true,
  58. "message": "",
  59. "data": channels,
  60. })
  61. return
  62. }
  63. func FetchUpstreamModels(c *gin.Context) {
  64. id, err := strconv.Atoi(c.Param("id"))
  65. if err != nil {
  66. c.JSON(http.StatusOK, gin.H{
  67. "success": false,
  68. "message": err.Error(),
  69. })
  70. return
  71. }
  72. channel, err := model.GetChannelById(id, true)
  73. if err != nil {
  74. c.JSON(http.StatusOK, gin.H{
  75. "success": false,
  76. "message": err.Error(),
  77. })
  78. return
  79. }
  80. if channel.Type != common.ChannelTypeOpenAI {
  81. c.JSON(http.StatusOK, gin.H{
  82. "success": false,
  83. "message": "仅支持 OpenAI 类型渠道",
  84. })
  85. return
  86. }
  87. url := fmt.Sprintf("%s/v1/models", *channel.BaseURL)
  88. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  89. if err != nil {
  90. c.JSON(http.StatusOK, gin.H{
  91. "success": false,
  92. "message": err.Error(),
  93. })
  94. }
  95. result := OpenAIModelsResponse{}
  96. err = json.Unmarshal(body, &result)
  97. if err != nil {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": false,
  100. "message": err.Error(),
  101. })
  102. }
  103. if !result.Success {
  104. c.JSON(http.StatusOK, gin.H{
  105. "success": false,
  106. "message": "上游返回错误",
  107. })
  108. }
  109. var ids []string
  110. for _, model := range result.Data {
  111. ids = append(ids, model.ID)
  112. }
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": true,
  115. "message": "",
  116. "data": ids,
  117. })
  118. }
  119. func FixChannelsAbilities(c *gin.Context) {
  120. count, err := model.FixAbility()
  121. if err != nil {
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": false,
  124. "message": err.Error(),
  125. })
  126. return
  127. }
  128. c.JSON(http.StatusOK, gin.H{
  129. "success": true,
  130. "message": "",
  131. "data": count,
  132. })
  133. }
  134. func SearchChannels(c *gin.Context) {
  135. keyword := c.Query("keyword")
  136. group := c.Query("group")
  137. modelKeyword := c.Query("model")
  138. //idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  139. channels, err := model.SearchChannels(keyword, group, modelKeyword)
  140. if err != nil {
  141. c.JSON(http.StatusOK, gin.H{
  142. "success": false,
  143. "message": err.Error(),
  144. })
  145. return
  146. }
  147. c.JSON(http.StatusOK, gin.H{
  148. "success": true,
  149. "message": "",
  150. "data": channels,
  151. })
  152. return
  153. }
  154. func GetChannel(c *gin.Context) {
  155. id, err := strconv.Atoi(c.Param("id"))
  156. if err != nil {
  157. c.JSON(http.StatusOK, gin.H{
  158. "success": false,
  159. "message": err.Error(),
  160. })
  161. return
  162. }
  163. channel, err := model.GetChannelById(id, false)
  164. if err != nil {
  165. c.JSON(http.StatusOK, gin.H{
  166. "success": false,
  167. "message": err.Error(),
  168. })
  169. return
  170. }
  171. c.JSON(http.StatusOK, gin.H{
  172. "success": true,
  173. "message": "",
  174. "data": channel,
  175. })
  176. return
  177. }
  178. func AddChannel(c *gin.Context) {
  179. channel := model.Channel{}
  180. err := c.ShouldBindJSON(&channel)
  181. if err != nil {
  182. c.JSON(http.StatusOK, gin.H{
  183. "success": false,
  184. "message": err.Error(),
  185. })
  186. return
  187. }
  188. channel.CreatedTime = common.GetTimestamp()
  189. keys := strings.Split(channel.Key, "\n")
  190. if channel.Type == common.ChannelTypeVertexAi {
  191. keys = []string{channel.Key}
  192. }
  193. channels := make([]model.Channel, 0, len(keys))
  194. for _, key := range keys {
  195. if key == "" {
  196. continue
  197. }
  198. localChannel := channel
  199. localChannel.Key = key
  200. channels = append(channels, localChannel)
  201. }
  202. err = model.BatchInsertChannels(channels)
  203. if err != nil {
  204. c.JSON(http.StatusOK, gin.H{
  205. "success": false,
  206. "message": err.Error(),
  207. })
  208. return
  209. }
  210. c.JSON(http.StatusOK, gin.H{
  211. "success": true,
  212. "message": "",
  213. })
  214. return
  215. }
  216. func DeleteChannel(c *gin.Context) {
  217. id, _ := strconv.Atoi(c.Param("id"))
  218. channel := model.Channel{Id: id}
  219. err := channel.Delete()
  220. if err != nil {
  221. c.JSON(http.StatusOK, gin.H{
  222. "success": false,
  223. "message": err.Error(),
  224. })
  225. return
  226. }
  227. c.JSON(http.StatusOK, gin.H{
  228. "success": true,
  229. "message": "",
  230. })
  231. return
  232. }
  233. func DeleteDisabledChannel(c *gin.Context) {
  234. rows, err := model.DeleteDisabledChannel()
  235. if err != nil {
  236. c.JSON(http.StatusOK, gin.H{
  237. "success": false,
  238. "message": err.Error(),
  239. })
  240. return
  241. }
  242. c.JSON(http.StatusOK, gin.H{
  243. "success": true,
  244. "message": "",
  245. "data": rows,
  246. })
  247. return
  248. }
  249. type ChannelBatch struct {
  250. Ids []int `json:"ids"`
  251. }
  252. func DeleteChannelBatch(c *gin.Context) {
  253. channelBatch := ChannelBatch{}
  254. err := c.ShouldBindJSON(&channelBatch)
  255. if err != nil || len(channelBatch.Ids) == 0 {
  256. c.JSON(http.StatusOK, gin.H{
  257. "success": false,
  258. "message": "参数错误",
  259. })
  260. return
  261. }
  262. err = model.BatchDeleteChannels(channelBatch.Ids)
  263. if err != nil {
  264. c.JSON(http.StatusOK, gin.H{
  265. "success": false,
  266. "message": err.Error(),
  267. })
  268. return
  269. }
  270. c.JSON(http.StatusOK, gin.H{
  271. "success": true,
  272. "message": "",
  273. "data": len(channelBatch.Ids),
  274. })
  275. return
  276. }
  277. func UpdateChannel(c *gin.Context) {
  278. channel := model.Channel{}
  279. err := c.ShouldBindJSON(&channel)
  280. if err != nil {
  281. c.JSON(http.StatusOK, gin.H{
  282. "success": false,
  283. "message": err.Error(),
  284. })
  285. return
  286. }
  287. err = channel.Update()
  288. if err != nil {
  289. c.JSON(http.StatusOK, gin.H{
  290. "success": false,
  291. "message": err.Error(),
  292. })
  293. return
  294. }
  295. c.JSON(http.StatusOK, gin.H{
  296. "success": true,
  297. "message": "",
  298. "data": channel,
  299. })
  300. return
  301. }