优化代码
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"opencatd-open/store"
|
"opencatd-open/store"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Sakurasan/to"
|
"github.com/Sakurasan/to"
|
||||||
@@ -297,29 +298,7 @@ func GenerateToken() string {
|
|||||||
return token.String()
|
return token.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// type Tokens struct {
|
func getHttpClient() *http.Client {
|
||||||
// UserID int
|
|
||||||
// PromptCount int
|
|
||||||
// CompletionCount int
|
|
||||||
// TotalTokens int
|
|
||||||
// Model string
|
|
||||||
// PromptHash string
|
|
||||||
// }
|
|
||||||
|
|
||||||
func HandleProy(c *gin.Context) {
|
|
||||||
var (
|
|
||||||
localuser bool
|
|
||||||
isStream bool
|
|
||||||
chatreq = openai.ChatCompletionRequest{}
|
|
||||||
chatres = openai.ChatCompletionResponse{}
|
|
||||||
chatlog store.Tokens
|
|
||||||
pre_prompt string
|
|
||||||
)
|
|
||||||
auth := c.Request.Header.Get("Authorization")
|
|
||||||
if len(auth) > 7 && auth[:7] == "Bearer " {
|
|
||||||
localuser = store.IsExistAuthCache(auth[7:])
|
|
||||||
}
|
|
||||||
client := http.DefaultClient
|
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
||||||
Proxy: http.ProxyFromEnvironment,
|
Proxy: http.ProxyFromEnvironment,
|
||||||
DialContext: (&net.Dialer{
|
DialContext: (&net.Dialer{
|
||||||
@@ -333,7 +312,23 @@ func HandleProy(c *gin.Context) {
|
|||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
}
|
}
|
||||||
client.Transport = tr
|
return &http.Client{Transport: tr}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleProy(c *gin.Context) {
|
||||||
|
var (
|
||||||
|
localuser bool
|
||||||
|
isStream bool
|
||||||
|
chatreq = openai.ChatCompletionRequest{}
|
||||||
|
chatres = openai.ChatCompletionResponse{}
|
||||||
|
chatlog store.Tokens
|
||||||
|
pre_prompt string
|
||||||
|
wg sync.WaitGroup
|
||||||
|
)
|
||||||
|
auth := c.Request.Header.Get("Authorization")
|
||||||
|
if len(auth) > 7 && auth[:7] == "Bearer " {
|
||||||
|
localuser = store.IsExistAuthCache(auth[7:])
|
||||||
|
}
|
||||||
|
|
||||||
if c.Request.URL.Path == "/v1/chat/completions" && localuser {
|
if c.Request.URL.Path == "/v1/chat/completions" && localuser {
|
||||||
|
|
||||||
@@ -367,7 +362,7 @@ func HandleProy(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem()))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", store.FromKeyCacheRandomItem()))
|
||||||
}
|
}
|
||||||
|
client := getHttpClient()
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@@ -396,13 +391,22 @@ func HandleProy(c *gin.Context) {
|
|||||||
resp.Header.Del("content-security-policy-report-only")
|
resp.Header.Del("content-security-policy-report-only")
|
||||||
resp.Header.Del("clear-site-data")
|
resp.Header.Del("clear-site-data")
|
||||||
|
|
||||||
|
c.Writer.WriteHeader(resp.StatusCode)
|
||||||
|
writer := bufio.NewWriter(c.Writer)
|
||||||
|
defer writer.Flush()
|
||||||
|
|
||||||
reader := bufio.NewReader(resp.Body)
|
reader := bufio.NewReader(resp.Body)
|
||||||
var resbuf = bytes.NewBuffer(nil)
|
var resbuf = bytes.NewBuffer(nil)
|
||||||
|
|
||||||
if resp.StatusCode == 200 && localuser {
|
if resp.StatusCode == 200 && localuser {
|
||||||
|
wg.Add(1)
|
||||||
if isStream {
|
if isStream {
|
||||||
chatdata := <-fetchResponseContent(resbuf, reader)
|
contentCh := fetchResponseContent(resbuf, reader)
|
||||||
chatlog.CompletionCount = NumTokensFromStr(chatdata, chatreq.Model)
|
var buffer bytes.Buffer
|
||||||
|
for content := range contentCh {
|
||||||
|
buffer.WriteString(content)
|
||||||
|
}
|
||||||
|
chatlog.CompletionCount = NumTokensFromStr(buffer.String(), chatreq.Model)
|
||||||
chatlog.TotalTokens = chatlog.PromptCount + chatlog.CompletionCount
|
chatlog.TotalTokens = chatlog.PromptCount + chatlog.CompletionCount
|
||||||
} else {
|
} else {
|
||||||
reader.WriteTo(resbuf)
|
reader.WriteTo(resbuf)
|
||||||
@@ -418,19 +422,24 @@ func HandleProy(c *gin.Context) {
|
|||||||
if err := store.SumDaily(chatlog.UserID); err != nil {
|
if err := store.SumDaily(chatlog.UserID); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
c.Writer.WriteHeader(resp.StatusCode)
|
|
||||||
if localuser {
|
|
||||||
// 返回 API 响应主体
|
// 返回 API 响应主体
|
||||||
if _, err := io.Copy(c.Writer, resbuf); err != nil {
|
if _, err := io.Copy(writer, resbuf); err != nil {
|
||||||
log.Println(err)
|
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
_, err = io.Copy(c.Writer, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// 返回 API 响应主体
|
// 返回 API 响应主体
|
||||||
if _, err := io.Copy(c.Writer, io.NopCloser(reader)); err != nil {
|
if _, err := io.Copy(writer, reader); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func QueryUsage(from, to string) ([]CalcUsage, error) {
|
|||||||
--SUM(prompt_units) AS prompt_units,
|
--SUM(prompt_units) AS prompt_units,
|
||||||
-- SUM(completion_units) AS completion_units,
|
-- SUM(completion_units) AS completion_units,
|
||||||
SUM(total_unit) AS total_unit,
|
SUM(total_unit) AS total_unit,
|
||||||
SUM(cost) AS cost`).
|
printf('%.6f', SUM(cost)) AS cost`).
|
||||||
Group("user_id").
|
Group("user_id").
|
||||||
Where("date >= ? AND date < ?", from, to).
|
Where("date >= ? AND date < ?", from, to).
|
||||||
Find(&results).Error
|
Find(&results).Error
|
||||||
|
|||||||
Reference in New Issue
Block a user