| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- package controller
- import (
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "net/http"
- "one-api/common"
- "one-api/model"
- "strconv"
- "strings"
- )
- type OpenAIModel struct {
- ID string `json:"id"`
- Object string `json:"object"`
- Created int64 `json:"created"`
- OwnedBy string `json:"owned_by"`
- Permission []struct {
- ID string `json:"id"`
- Object string `json:"object"`
- Created int64 `json:"created"`
- AllowCreateEngine bool `json:"allow_create_engine"`
- AllowSampling bool `json:"allow_sampling"`
- AllowLogprobs bool `json:"allow_logprobs"`
- AllowSearchIndices bool `json:"allow_search_indices"`
- AllowView bool `json:"allow_view"`
- AllowFineTuning bool `json:"allow_fine_tuning"`
- Organization string `json:"organization"`
- Group string `json:"group"`
- IsBlocking bool `json:"is_blocking"`
- } `json:"permission"`
- Root string `json:"root"`
- Parent string `json:"parent"`
- }
- type OpenAIModelsResponse struct {
- Data []OpenAIModel `json:"data"`
- Success bool `json:"success"`
- }
- func GetAllChannels(c *gin.Context) {
- p, _ := strconv.Atoi(c.Query("p"))
- pageSize, _ := strconv.Atoi(c.Query("page_size"))
- if p < 0 {
- p = 0
- }
- if pageSize < 0 {
- pageSize = common.ItemsPerPage
- }
- idSort, _ := strconv.ParseBool(c.Query("id_sort"))
- channels, err := model.GetAllChannels(p*pageSize, pageSize, false, idSort)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": channels,
- })
- return
- }
- func FetchUpstreamModels(c *gin.Context) {
- id, err := strconv.Atoi(c.Param("id"))
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel, err := model.GetChannelById(id, true)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- if channel.Type != common.ChannelTypeOpenAI {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "仅支持 OpenAI 类型渠道",
- })
- return
- }
- url := fmt.Sprintf("%s/v1/models", *channel.BaseURL)
- body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- }
- result := OpenAIModelsResponse{}
- err = json.Unmarshal(body, &result)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- }
- if !result.Success {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "上游返回错误",
- })
- }
- var ids []string
- for _, model := range result.Data {
- ids = append(ids, model.ID)
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": ids,
- })
- }
- func FixChannelsAbilities(c *gin.Context) {
- count, err := model.FixAbility()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": count,
- })
- }
- func SearchChannels(c *gin.Context) {
- keyword := c.Query("keyword")
- group := c.Query("group")
- modelKeyword := c.Query("model")
- //idSort, _ := strconv.ParseBool(c.Query("id_sort"))
- channels, err := model.SearchChannels(keyword, group, modelKeyword)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": channels,
- })
- return
- }
- func GetChannel(c *gin.Context) {
- id, err := strconv.Atoi(c.Param("id"))
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel, err := model.GetChannelById(id, false)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": channel,
- })
- return
- }
- func AddChannel(c *gin.Context) {
- channel := model.Channel{}
- err := c.ShouldBindJSON(&channel)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel.CreatedTime = common.GetTimestamp()
- keys := strings.Split(channel.Key, "\n")
- channels := make([]model.Channel, 0, len(keys))
- for _, key := range keys {
- if key == "" {
- continue
- }
- localChannel := channel
- localChannel.Key = key
- channels = append(channels, localChannel)
- }
- err = model.BatchInsertChannels(channels)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func DeleteChannel(c *gin.Context) {
- id, _ := strconv.Atoi(c.Param("id"))
- channel := model.Channel{Id: id}
- err := channel.Delete()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func DeleteDisabledChannel(c *gin.Context) {
- rows, err := model.DeleteDisabledChannel()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": rows,
- })
- return
- }
- type ChannelBatch struct {
- Ids []int `json:"ids"`
- }
- func DeleteChannelBatch(c *gin.Context) {
- channelBatch := ChannelBatch{}
- err := c.ShouldBindJSON(&channelBatch)
- if err != nil || len(channelBatch.Ids) == 0 {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "参数错误",
- })
- return
- }
- err = model.BatchDeleteChannels(channelBatch.Ids)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": len(channelBatch.Ids),
- })
- return
- }
- func UpdateChannel(c *gin.Context) {
- channel := model.Channel{}
- err := c.ShouldBindJSON(&channel)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- err = channel.Update()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": channel,
- })
- return
- }
|