ai-gateway/controller/channel.go

256 lines
5.2 KiB
Go
Raw Normal View History

2023-04-22 12:39:27 +00:00
package controller
import (
2023-05-15 02:48:52 +00:00
"bytes"
"encoding/json"
"errors"
"fmt"
2023-04-22 12:39:27 +00:00
"github.com/gin-gonic/gin"
"net/http"
2023-04-22 13:14:09 +00:00
"one-api/common"
"one-api/model"
2023-04-22 12:39:27 +00:00
"strconv"
"strings"
2023-05-15 02:48:52 +00:00
"time"
2023-04-22 12:39:27 +00:00
)
2023-04-22 13:41:16 +00:00
func GetAllChannels(c *gin.Context) {
2023-04-22 12:39:27 +00:00
p, _ := strconv.Atoi(c.Query("p"))
if p < 0 {
p = 0
}
2023-04-22 14:02:59 +00:00
channels, err := model.GetAllChannels(p*common.ItemsPerPage, common.ItemsPerPage)
2023-04-22 12:39:27 +00:00
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
2023-04-22 14:02:59 +00:00
"data": channels,
2023-04-22 12:39:27 +00:00
})
return
}
2023-04-22 13:41:16 +00:00
func SearchChannels(c *gin.Context) {
2023-04-22 12:39:27 +00:00
keyword := c.Query("keyword")
2023-04-22 14:02:59 +00:00
channels, err := model.SearchChannels(keyword)
2023-04-22 12:39:27 +00:00
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
2023-04-22 14:02:59 +00:00
"data": channels,
2023-04-22 12:39:27 +00:00
})
return
}
2023-04-22 14:02:59 +00:00
func GetChannel(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
2023-04-22 12:39:27 +00:00
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
2023-04-23 10:24:11 +00:00
channel, err := model.GetChannelById(id, false)
2023-04-22 14:02:59 +00:00
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
2023-04-22 12:39:27 +00:00
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
2023-04-22 14:02:59 +00:00
"data": channel,
2023-04-22 12:39:27 +00:00
})
return
}
2023-04-22 14:02:59 +00:00
func AddChannel(c *gin.Context) {
channel := model.Channel{}
err := c.ShouldBindJSON(&channel)
if err != nil {
c.JSON(http.StatusOK, gin.H{
2023-04-22 12:39:27 +00:00
"success": false,
2023-04-22 14:02:59 +00:00
"message": err.Error(),
2023-04-22 12:39:27 +00:00
})
return
}
2023-04-23 07:42:23 +00:00
channel.CreatedTime = common.GetTimestamp()
keys := strings.Split(channel.Key, "\n")
channels := make([]model.Channel, 0)
for _, key := range keys {
if key == "" {
continue
}
localChannel := channel
localChannel.Key = key
channels = append(channels, localChannel)
}
err = model.BatchInsertChannels(channels)
2023-04-22 14:02:59 +00:00
if err != nil {
2023-04-22 12:39:27 +00:00
c.JSON(http.StatusOK, gin.H{
"success": false,
2023-04-22 14:02:59 +00:00
"message": err.Error(),
2023-04-22 12:39:27 +00:00
})
return
}
2023-04-22 14:02:59 +00:00
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()
2023-04-22 12:39:27 +00:00
if err != nil {
c.JSON(http.StatusOK, gin.H{
2023-04-22 14:02:59 +00:00
"success": false,
2023-04-22 12:39:27 +00:00
"message": err.Error(),
})
return
}
2023-04-22 14:02:59 +00:00
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
return
2023-04-22 12:39:27 +00:00
}
2023-04-22 14:02:59 +00:00
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(),
})
2023-04-22 12:39:27 +00:00
return
}
2023-04-22 14:02:59 +00:00
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": "",
2023-04-23 07:42:23 +00:00
"data": channel,
2023-04-22 14:02:59 +00:00
})
return
2023-04-22 12:39:27 +00:00
}
2023-05-15 02:48:52 +00:00
func testChannel(channel *model.Channel, request *ChatRequest) error {
if request.Model == "" {
request.Model = "gpt-3.5-turbo"
if channel.Type == common.ChannelTypeAzure {
request.Model = "gpt-35-turbo"
}
}
requestURL := common.ChannelBaseURLs[channel.Type]
if channel.Type == common.ChannelTypeAzure {
requestURL = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", channel.BaseURL, request.Model)
} else {
if channel.Type == common.ChannelTypeCustom {
requestURL = channel.BaseURL
}
requestURL += "/v1/chat/completions"
}
jsonData, err := json.Marshal(request)
if err != nil {
return err
}
req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
if channel.Type == common.ChannelTypeAzure {
req.Header.Set("api-key", channel.Key)
} else {
req.Header.Set("Authorization", "Bearer "+channel.Key)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var response TextResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return err
}
if response.Error.Type != "" {
return errors.New(fmt.Sprintf("type %s, code %s, message %s", response.Error.Type, response.Error.Code, response.Error.Message))
}
return nil
}
func TestChannel(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
}
model_ := c.Query("model")
chatRequest := &ChatRequest{
Model: model_,
}
testMessage := Message{
Role: "user",
Content: "echo hi",
}
chatRequest.Messages = append(chatRequest.Messages, testMessage)
tik := time.Now()
err = testChannel(channel, chatRequest)
tok := time.Now()
milliseconds := tok.Sub(tik).Milliseconds()
go channel.UpdateResponseTime(milliseconds)
consumedTime := float64(milliseconds) / 1000.0
2023-05-15 02:48:52 +00:00
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
"time": consumedTime,
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"time": consumedTime,
})
return
}