channel.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. channels := make([]model.Channel, 0, len(keys))
  191. for _, key := range keys {
  192. if key == "" {
  193. continue
  194. }
  195. localChannel := channel
  196. localChannel.Key = key
  197. channels = append(channels, localChannel)
  198. }
  199. err = model.BatchInsertChannels(channels)
  200. if err != nil {
  201. c.JSON(http.StatusOK, gin.H{
  202. "success": false,
  203. "message": err.Error(),
  204. })
  205. return
  206. }
  207. c.JSON(http.StatusOK, gin.H{
  208. "success": true,
  209. "message": "",
  210. })
  211. return
  212. }
  213. func DeleteChannel(c *gin.Context) {
  214. id, _ := strconv.Atoi(c.Param("id"))
  215. channel := model.Channel{Id: id}
  216. err := channel.Delete()
  217. if err != nil {
  218. c.JSON(http.StatusOK, gin.H{
  219. "success": false,
  220. "message": err.Error(),
  221. })
  222. return
  223. }
  224. c.JSON(http.StatusOK, gin.H{
  225. "success": true,
  226. "message": "",
  227. })
  228. return
  229. }
  230. func DeleteDisabledChannel(c *gin.Context) {
  231. rows, err := model.DeleteDisabledChannel()
  232. if err != nil {
  233. c.JSON(http.StatusOK, gin.H{
  234. "success": false,
  235. "message": err.Error(),
  236. })
  237. return
  238. }
  239. c.JSON(http.StatusOK, gin.H{
  240. "success": true,
  241. "message": "",
  242. "data": rows,
  243. })
  244. return
  245. }
  246. type ChannelBatch struct {
  247. Ids []int `json:"ids"`
  248. }
  249. func DeleteChannelBatch(c *gin.Context) {
  250. channelBatch := ChannelBatch{}
  251. err := c.ShouldBindJSON(&channelBatch)
  252. if err != nil || len(channelBatch.Ids) == 0 {
  253. c.JSON(http.StatusOK, gin.H{
  254. "success": false,
  255. "message": "参数错误",
  256. })
  257. return
  258. }
  259. err = model.BatchDeleteChannels(channelBatch.Ids)
  260. if err != nil {
  261. c.JSON(http.StatusOK, gin.H{
  262. "success": false,
  263. "message": err.Error(),
  264. })
  265. return
  266. }
  267. c.JSON(http.StatusOK, gin.H{
  268. "success": true,
  269. "message": "",
  270. "data": len(channelBatch.Ids),
  271. })
  272. return
  273. }
  274. func UpdateChannel(c *gin.Context) {
  275. channel := model.Channel{}
  276. err := c.ShouldBindJSON(&channel)
  277. if err != nil {
  278. c.JSON(http.StatusOK, gin.H{
  279. "success": false,
  280. "message": err.Error(),
  281. })
  282. return
  283. }
  284. err = channel.Update()
  285. if err != nil {
  286. c.JSON(http.StatusOK, gin.H{
  287. "success": false,
  288. "message": err.Error(),
  289. })
  290. return
  291. }
  292. c.JSON(http.StatusOK, gin.H{
  293. "success": true,
  294. "message": "",
  295. "data": channel,
  296. })
  297. return
  298. }