| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- package controller
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "one-api/common"
- "one-api/model"
- "strconv"
- "strings"
- "github.com/gin-gonic/gin"
- )
- 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
- }
- channelData := make([]*model.Channel, 0)
- idSort, _ := strconv.ParseBool(c.Query("id_sort"))
- enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
- if enableTagMode {
- tags, err := model.GetPaginatedTags(p*pageSize, pageSize)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- for _, tag := range tags {
- if tag != nil && *tag != "" {
- tagChannel, err := model.GetChannelsByTag(*tag, idSort)
- if err == nil {
- channelData = append(channelData, tagChannel...)
- }
- }
- }
- } else {
- channels, err := model.GetAllChannels(p*pageSize, pageSize, false, idSort)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channelData = channels
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": channelData,
- })
- 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"))
- enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
- channelData := make([]*model.Channel, 0)
- if enableTagMode {
- tags, err := model.SearchTags(keyword, group, modelKeyword, idSort)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- for _, tag := range tags {
- if tag != nil && *tag != "" {
- tagChannel, err := model.GetChannelsByTag(*tag, idSort)
- if err == nil {
- channelData = append(channelData, tagChannel...)
- }
- }
- }
- } else {
- channels, err := model.SearchChannels(keyword, group, modelKeyword, idSort)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channelData = channels
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": channelData,
- })
- 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")
- if channel.Type == common.ChannelTypeVertexAi {
- if channel.Other == "" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "部署地区不能为空",
- })
- return
- } else {
- if common.IsJsonStr(channel.Other) {
- // must have default
- regionMap := common.StrToMap(channel.Other)
- if regionMap["default"] == nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "部署地区必须包含default字段",
- })
- return
- }
- }
- }
- keys = []string{channel.Key}
- }
- 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 ChannelTag struct {
- Tag string `json:"tag"`
- NewTag *string `json:"new_tag"`
- Priority *int64 `json:"priority"`
- Weight *uint `json:"weight"`
- ModelMapping *string `json:"model_mapping"`
- Models *string `json:"models"`
- Groups *string `json:"groups"`
- }
- func DisableTagChannels(c *gin.Context) {
- channelTag := ChannelTag{}
- err := c.ShouldBindJSON(&channelTag)
- if err != nil || channelTag.Tag == "" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "参数错误",
- })
- return
- }
- err = model.DisableChannelByTag(channelTag.Tag)
- 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 EnableTagChannels(c *gin.Context) {
- channelTag := ChannelTag{}
- err := c.ShouldBindJSON(&channelTag)
- if err != nil || channelTag.Tag == "" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "参数错误",
- })
- return
- }
- err = model.EnableChannelByTag(channelTag.Tag)
- 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 EditTagChannels(c *gin.Context) {
- channelTag := ChannelTag{}
- err := c.ShouldBindJSON(&channelTag)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "参数错误",
- })
- return
- }
- if channelTag.Tag == "" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "tag不能为空",
- })
- return
- }
- err = model.EditChannelByTag(channelTag.Tag, channelTag.NewTag, channelTag.ModelMapping, channelTag.Models, channelTag.Groups, channelTag.Priority, channelTag.Weight)
- 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
- }
- 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
- }
- if channel.Type == common.ChannelTypeVertexAi {
- if channel.Other == "" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "部署地区不能为空",
- })
- return
- } else {
- if common.IsJsonStr(channel.Other) {
- // must have default
- regionMap := common.StrToMap(channel.Other)
- if regionMap["default"] == nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "部署地区必须包含default字段",
- })
- 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
- }
|