|
|
|
@ -10,6 +10,7 @@ import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/http/httputil"
|
|
|
|
@ -31,11 +32,11 @@ func Images(c *gin.Context) {
|
|
|
|
serverInfo, err := service.CheckBlanceForImages(key, modelStr, imagesRequest.N)
|
|
|
|
serverInfo, err := service.CheckBlanceForImages(key, modelStr, imagesRequest.N)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, modelStr, err)
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, modelStr, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("请求的KEY: %v Model: %v", key, modelStr)
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, modelStr)
|
|
|
|
|
|
|
|
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -46,7 +47,7 @@ func Images(c *gin.Context) {
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
newReqBody, err := json.Marshal(imagesRequest)
|
|
|
|
newReqBody, err := json.Marshal(imagesRequest)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("http request err: %v", err)
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -73,7 +74,7 @@ func Images(c *gin.Context) {
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(newReqBody))
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(newReqBody))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sss, err := json.Marshal(imagesRequest)
|
|
|
|
sss, err := json.Marshal(imagesRequest)
|
|
|
|
fmt.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
log.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
|
|
|
|
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
@ -88,16 +89,16 @@ func Images(c *gin.Context) {
|
|
|
|
var imagesResponse model.ImagesResponse
|
|
|
|
var imagesResponse model.ImagesResponse
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("读取返回数据出错: %v", err)
|
|
|
|
log.Printf("读取返回数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &imagesResponse)
|
|
|
|
err = json.Unmarshal(body, &imagesResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("json解析数据出错: %v", err)
|
|
|
|
log.Printf("json解析数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
fmt.Printf("image size: %v", len(imagesResponse.Data))
|
|
|
|
log.Printf("image size: %v", len(imagesResponse.Data))
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
timestampID := "img-" + fmt.Sprintf("%d", timestamp)
|
|
|
|
timestampID := "img-" + fmt.Sprintf("%d", timestamp)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
@ -126,11 +127,11 @@ func Edit(c *gin.Context) {
|
|
|
|
serverInfo, err := service.CheckBlance(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
serverInfo, err := service.CheckBlance(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
|
|
|
|
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -141,7 +142,7 @@ func Edit(c *gin.Context) {
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
newReqBody, err := json.Marshal(chatRequest)
|
|
|
|
newReqBody, err := json.Marshal(chatRequest)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("http request err: %v", err)
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -168,26 +169,26 @@ func Edit(c *gin.Context) {
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(newReqBody))
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(newReqBody))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
fmt.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
log.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
|
|
|
|
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
resp.Header.Set("Openai-Organization", "api2gpt")
|
|
|
|
resp.Header.Set("Openai-Organization", "api2gpt")
|
|
|
|
var chatResponse model.ChatResponse
|
|
|
|
var chatResponse model.ChatResponse
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("读取返回数据出错: %v", err)
|
|
|
|
log.Printf("读取返回数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("json解析数据出错: %v", err)
|
|
|
|
log.Printf("json解析数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
promptTokens = chatResponse.Usage.PromptTokens
|
|
|
|
promptTokens = chatResponse.Usage.PromptTokens
|
|
|
|
complateTokens = chatResponse.Usage.CompletionTokens
|
|
|
|
complateTokens = chatResponse.Usage.CompletionTokens
|
|
|
|
totalTokens = chatResponse.Usage.TotalTokens
|
|
|
|
totalTokens = chatResponse.Usage.TotalTokens
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
fmt.Printf("prompt_tokens: %v complate_tokens: %v total_tokens: %v", promptTokens, complateTokens, totalTokens)
|
|
|
|
log.Printf("prompt_tokens: %v complate_tokens: %v total_tokens: %v", promptTokens, complateTokens, totalTokens)
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
timestampID := "edit-" + fmt.Sprintf("%d", timestamp)
|
|
|
|
timestampID := "edit-" + fmt.Sprintf("%d", timestamp)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
@ -215,11 +216,11 @@ func Embeddings(c *gin.Context) {
|
|
|
|
serverInfo, err := service.CheckBlance(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
serverInfo, err := service.CheckBlance(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
|
|
|
|
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -230,7 +231,7 @@ func Embeddings(c *gin.Context) {
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
newReqBody, err := json.Marshal(chatRequest)
|
|
|
|
newReqBody, err := json.Marshal(chatRequest)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("http request err: %v", err)
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -257,25 +258,25 @@ func Embeddings(c *gin.Context) {
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(newReqBody))
|
|
|
|
req.Body = io.NopCloser(bytes.NewReader(newReqBody))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
fmt.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
log.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
|
|
|
|
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
resp.Header.Set("Openai-Organization", "api2gpt")
|
|
|
|
resp.Header.Set("Openai-Organization", "api2gpt")
|
|
|
|
var chatResponse model.ChatResponse
|
|
|
|
var chatResponse model.ChatResponse
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("读取返回数据出错: %v", err)
|
|
|
|
log.Printf("读取返回数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("json解析数据出错: %v", err)
|
|
|
|
log.Printf("json解析数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
promptTokens = chatResponse.Usage.PromptTokens
|
|
|
|
promptTokens = chatResponse.Usage.PromptTokens
|
|
|
|
totalTokens = chatResponse.Usage.TotalTokens
|
|
|
|
totalTokens = chatResponse.Usage.TotalTokens
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
fmt.Printf("prompt_tokens: %v total_tokens: %v", promptTokens, totalTokens)
|
|
|
|
log.Printf("prompt_tokens: %v total_tokens: %v", promptTokens, totalTokens)
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
timestampID := "emb-" + fmt.Sprintf("%d", timestamp)
|
|
|
|
timestampID := "emb-" + fmt.Sprintf("%d", timestamp)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
@ -305,11 +306,11 @@ func Completions(c *gin.Context) {
|
|
|
|
serverInfo, err := service.CheckBlance(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
serverInfo, err := service.CheckBlance(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(403, gin.H{"error": err.Error()})
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
|
|
|
|
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
remote, err := url.Parse(serverInfo.ServerAddress)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -320,7 +321,7 @@ func Completions(c *gin.Context) {
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(remote)
|
|
|
|
newReqBody, err := json.Marshal(chatRequest)
|
|
|
|
newReqBody, err := json.Marshal(chatRequest)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("http request err: %v", err)
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -347,13 +348,13 @@ func Completions(c *gin.Context) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("chatRequest 转化出错 %v", err)
|
|
|
|
log.Printf("chatRequest 转化出错 %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("开始处理返回逻辑: %v", string(sss))
|
|
|
|
log.Printf("开始处理返回逻辑: %v", string(sss))
|
|
|
|
if chatRequest.Stream {
|
|
|
|
if chatRequest.Stream {
|
|
|
|
// 流式回应,处理
|
|
|
|
// 流式回应,处理
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
fmt.Printf("流式回应 http status code: %v", resp.StatusCode)
|
|
|
|
log.Printf("流式回应 http status code: %v", resp.StatusCode)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
//退回预扣除的余额
|
|
|
|
//退回预扣除的余额
|
|
|
|
err = service.CheckBlanceReturn(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
err = service.CheckBlanceReturn(key, chatRequest.Model, chatRequest.MaxTokens)
|
|
|
|
@ -376,7 +377,7 @@ func Completions(c *gin.Context) {
|
|
|
|
if err == io.EOF {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("流式回应,处理 err %v:", err.Error())
|
|
|
|
log.Printf("流式回应,处理 err %v:", err.Error())
|
|
|
|
break
|
|
|
|
break
|
|
|
|
//return err
|
|
|
|
//return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -386,7 +387,7 @@ func Completions(c *gin.Context) {
|
|
|
|
if trimStr != "\n" {
|
|
|
|
if trimStr != "\n" {
|
|
|
|
err := json.Unmarshal([]byte(trimStr), &chatResponse)
|
|
|
|
err := json.Unmarshal([]byte(trimStr), &chatResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
log.Printf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if chatResponse.Choices != nil {
|
|
|
|
if chatResponse.Choices != nil {
|
|
|
|
if chatResponse.Choices[0].Text != "" {
|
|
|
|
if chatResponse.Choices[0].Text != "" {
|
|
|
|
@ -397,11 +398,10 @@ func Completions(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
|
|
chatRequestId = chatResponse.Id
|
|
|
|
chatRequestId = chatResponse.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 写回数据
|
|
|
|
// 写回数据
|
|
|
|
_, err = c.Writer.Write([]byte(string(chunk) + "\n"))
|
|
|
|
_, err = c.Writer.Write([]byte(string(chunk) + "\n"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("写回数据 err: %v", err.Error())
|
|
|
|
log.Printf("写回数据 err: %v", err.Error())
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Writer.(http.Flusher).Flush()
|
|
|
|
c.Writer.(http.Flusher).Flush()
|
|
|
|
@ -413,9 +413,9 @@ func Completions(c *gin.Context) {
|
|
|
|
promptTokens = common.NumTokensFromMessages(chatRequest.Messages, chatRequest.Model)
|
|
|
|
promptTokens = common.NumTokensFromMessages(chatRequest.Messages, chatRequest.Model)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completionTokens = common.NumTokensFromString(reqContent, chatRequest.Model)
|
|
|
|
completionTokens = common.NumTokensFromString(reqContent, chatRequest.Model)
|
|
|
|
fmt.Printf("返回内容:%v", reqContent)
|
|
|
|
log.Printf("返回内容:%v", reqContent)
|
|
|
|
totalTokens = promptTokens + completionTokens
|
|
|
|
totalTokens = promptTokens + completionTokens
|
|
|
|
fmt.Printf("prompt_tokens: %v completion_tokens: %v total_tokens: %v", promptTokens, completionTokens, totalTokens)
|
|
|
|
log.Printf("prompt_tokens: %v completion_tokens: %v total_tokens: %v", promptTokens, completionTokens, totalTokens)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
_, err := service.Consumption(key, chatRequest.Model, promptTokens, completionTokens, totalTokens, chatRequestId)
|
|
|
|
_, err := service.Consumption(key, chatRequest.Model, promptTokens, completionTokens, totalTokens, chatRequestId)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -430,18 +430,18 @@ func Completions(c *gin.Context) {
|
|
|
|
var chatResponse model.ChatResponse
|
|
|
|
var chatResponse model.ChatResponse
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("非流式回应,处理 err: %v", err)
|
|
|
|
log.Printf("非流式回应,处理 err: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
log.Printf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
promptTokens = chatResponse.Usage.PromptTokens
|
|
|
|
promptTokens = chatResponse.Usage.PromptTokens
|
|
|
|
completionTokens = chatResponse.Usage.CompletionTokens
|
|
|
|
completionTokens = chatResponse.Usage.CompletionTokens
|
|
|
|
totalTokens = chatResponse.Usage.TotalTokens
|
|
|
|
totalTokens = chatResponse.Usage.TotalTokens
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
fmt.Printf("prompt_tokens: %v completion_tokens: %v total_tokens: %v", promptTokens, completionTokens, totalTokens)
|
|
|
|
log.Printf("prompt_tokens: %v completion_tokens: %v total_tokens: %v", promptTokens, completionTokens, totalTokens)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
_, err = service.Consumption(key, chatRequest.Model, promptTokens, completionTokens, totalTokens, chatResponse.Id)
|
|
|
|
_, err = service.Consumption(key, chatRequest.Model, promptTokens, completionTokens, totalTokens, chatResponse.Id)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|