| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package controller
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "net/http"
- "one-api/common"
- "one-api/model"
- "strconv"
- "strings"
- "time"
- )
- func GetAllChannels(c *gin.Context) {
- p, _ := strconv.Atoi(c.Query("p"))
- if p < 0 {
- p = 0
- }
- channels, err := model.GetAllChannels(p*common.ItemsPerPage, common.ItemsPerPage)
- 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 SearchChannels(c *gin.Context) {
- keyword := c.Query("keyword")
- channels, err := model.SearchChannels(keyword)
- 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()
- channel.AccessedTime = 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)
- 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 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
- }
- 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()
- consumedTime := float64(tok.Sub(tik).Milliseconds()) / 1000.0
- 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
- }
|