channel.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. if channel.Other == "" {
  192. c.JSON(http.StatusOK, gin.H{
  193. "success": false,
  194. "message": "部署地区不能为空",
  195. })
  196. return
  197. } else {
  198. if common.IsJsonStr(channel.Other) {
  199. // must have default
  200. regionMap := common.StrToMap(channel.Other)
  201. if regionMap["default"] == nil {
  202. c.JSON(http.StatusOK, gin.H{
  203. "success": false,
  204. "message": "部署地区必须包含default字段",
  205. })
  206. return
  207. }
  208. }
  209. }
  210. keys = []string{channel.Key}
  211. }
  212. channels := make([]model.Channel, 0, len(keys))
  213. for _, key := range keys {
  214. if key == "" {
  215. continue
  216. }
  217. localChannel := channel
  218. localChannel.Key = key
  219. channels = append(channels, localChannel)
  220. }
  221. err = model.BatchInsertChannels(channels)
  222. if err != nil {
  223. c.JSON(http.StatusOK, gin.H{
  224. "success": false,
  225. "message": err.Error(),
  226. })
  227. return
  228. }
  229. c.JSON(http.StatusOK, gin.H{
  230. "success": true,
  231. "message": "",
  232. })
  233. return
  234. }
  235. func DeleteChannel(c *gin.Context) {
  236. id, _ := strconv.Atoi(c.Param("id"))
  237. channel := model.Channel{Id: id}
  238. err := channel.Delete()
  239. if err != nil {
  240. c.JSON(http.StatusOK, gin.H{
  241. "success": false,
  242. "message": err.Error(),
  243. })
  244. return
  245. }
  246. c.JSON(http.StatusOK, gin.H{
  247. "success": true,
  248. "message": "",
  249. })
  250. return
  251. }
  252. func DeleteDisabledChannel(c *gin.Context) {
  253. rows, err := model.DeleteDisabledChannel()
  254. if err != nil {
  255. c.JSON(http.StatusOK, gin.H{
  256. "success": false,
  257. "message": err.Error(),
  258. })
  259. return
  260. }
  261. c.JSON(http.StatusOK, gin.H{
  262. "success": true,
  263. "message": "",
  264. "data": rows,
  265. })
  266. return
  267. }
  268. type ChannelBatch struct {
  269. Ids []int `json:"ids"`
  270. }
  271. func DeleteChannelBatch(c *gin.Context) {
  272. channelBatch := ChannelBatch{}
  273. err := c.ShouldBindJSON(&channelBatch)
  274. if err != nil || len(channelBatch.Ids) == 0 {
  275. c.JSON(http.StatusOK, gin.H{
  276. "success": false,
  277. "message": "参数错误",
  278. })
  279. return
  280. }
  281. err = model.BatchDeleteChannels(channelBatch.Ids)
  282. if err != nil {
  283. c.JSON(http.StatusOK, gin.H{
  284. "success": false,
  285. "message": err.Error(),
  286. })
  287. return
  288. }
  289. c.JSON(http.StatusOK, gin.H{
  290. "success": true,
  291. "message": "",
  292. "data": len(channelBatch.Ids),
  293. })
  294. return
  295. }
  296. func UpdateChannel(c *gin.Context) {
  297. channel := model.Channel{}
  298. err := c.ShouldBindJSON(&channel)
  299. if err != nil {
  300. c.JSON(http.StatusOK, gin.H{
  301. "success": false,
  302. "message": err.Error(),
  303. })
  304. return
  305. }
  306. if channel.Type == common.ChannelTypeVertexAi {
  307. if channel.Other == "" {
  308. c.JSON(http.StatusOK, gin.H{
  309. "success": false,
  310. "message": "部署地区不能为空",
  311. })
  312. return
  313. } else {
  314. if common.IsJsonStr(channel.Other) {
  315. // must have default
  316. regionMap := common.StrToMap(channel.Other)
  317. if regionMap["default"] == nil {
  318. c.JSON(http.StatusOK, gin.H{
  319. "success": false,
  320. "message": "部署地区必须包含default字段",
  321. })
  322. return
  323. }
  324. }
  325. }
  326. }
  327. err = channel.Update()
  328. if err != nil {
  329. c.JSON(http.StatusOK, gin.H{
  330. "success": false,
  331. "message": err.Error(),
  332. })
  333. return
  334. }
  335. c.JSON(http.StatusOK, gin.H{
  336. "success": true,
  337. "message": "",
  338. "data": channel,
  339. })
  340. return
  341. }