up
This commit is contained in:
@@ -2,7 +2,9 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -18,6 +20,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
// "github.com/pkoukk/tiktoken-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -292,6 +295,12 @@ func GenerateToken() string {
|
|||||||
return token.String()
|
return token.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type _ struct {
|
||||||
|
model string
|
||||||
|
promptCount int
|
||||||
|
completionCount int
|
||||||
|
}
|
||||||
|
|
||||||
func HandleProy(c *gin.Context) {
|
func HandleProy(c *gin.Context) {
|
||||||
var localuser bool
|
var localuser bool
|
||||||
var isStream bool
|
var isStream bool
|
||||||
@@ -376,6 +385,7 @@ func HandleProy(c *gin.Context) {
|
|||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
reader := bufio.NewReader(resp.Body)
|
reader := bufio.NewReader(resp.Body)
|
||||||
|
// var resbuf = bytes.NewBuffer(nil)
|
||||||
|
|
||||||
if resp.StatusCode == 200 && isStream {
|
if resp.StatusCode == 200 && isStream {
|
||||||
//todo
|
//todo
|
||||||
@@ -437,6 +447,18 @@ func HandleReverseProxy(c *gin.Context) {
|
|||||||
proxy.ServeHTTP(c.Writer, req)
|
proxy.ServeHTTP(c.Writer, req)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func Cost(model string, promptCount, completionCount int) float64 {
|
||||||
|
var cost float64
|
||||||
|
switch model {
|
||||||
|
case "gpt-3.5":
|
||||||
|
cost = 0.002 * float64((promptCount+completionCount)/1000)
|
||||||
|
case "gpt-4-32k":
|
||||||
|
cost = 0.06*float64(promptCount/1000) + 0.12*float64(completionCount/1000)
|
||||||
|
case "gpt-4":
|
||||||
|
cost = 0.03*float64(promptCount/1000) + 0.06*float64(completionCount/1000)
|
||||||
|
}
|
||||||
|
return cost
|
||||||
|
}
|
||||||
|
|
||||||
func HandleUsage(c *gin.Context) {
|
func HandleUsage(c *gin.Context) {
|
||||||
fromStr := c.Query("from")
|
fromStr := c.Query("from")
|
||||||
@@ -463,39 +485,46 @@ func HandleUsage(c *gin.Context) {
|
|||||||
c.JSON(200, usage)
|
c.JSON(200, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo
|
func fetchResponseContent(buf *bytes.Buffer, responseBody *bufio.Reader) <-chan string {
|
||||||
func streamEvent(reader *bufio.Reader) error {
|
contentCh := make(chan string)
|
||||||
|
|
||||||
lineChan := make(chan string, 1)
|
|
||||||
|
|
||||||
timeout := time.AfterFunc(60*time.Second, func() {
|
|
||||||
lineChan <- ""
|
|
||||||
})
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
line, err := reader.ReadString('\n')
|
defer close(contentCh)
|
||||||
if err == nil {
|
for {
|
||||||
lineChan <- line
|
line, err := responseBody.ReadString('\n')
|
||||||
|
if err == nil {
|
||||||
|
buf.WriteString(line)
|
||||||
|
if line == "\n" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(line, "data:") {
|
||||||
|
line = strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
||||||
|
if strings.HasSuffix(line, "[DONE]") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
dec := json.NewDecoder(strings.NewReader(line))
|
||||||
|
var data map[string]interface{}
|
||||||
|
if err := dec.Decode(&data); err == io.EOF {
|
||||||
|
log.Println("EOF:", err)
|
||||||
|
break
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Println("Error decoding response:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if choices, ok := data["choices"].([]interface{}); ok {
|
||||||
|
for _, choice := range choices {
|
||||||
|
choiceMap := choice.(map[string]interface{})
|
||||||
|
if content, ok := choiceMap["delta"].(map[string]interface{})["content"]; ok {
|
||||||
|
contentCh <- content.(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
return contentCh
|
||||||
line := <-lineChan
|
|
||||||
|
|
||||||
timeout.Stop()
|
|
||||||
if line == "" {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(line, "data:") {
|
|
||||||
line = strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
|
||||||
//log.Println("Received data:", line)
|
|
||||||
|
|
||||||
if line == "[DONE]" {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user