|
|
|
@ -10,7 +10,6 @@ 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"
|
|
|
|
@ -32,11 +31,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()})
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, modelStr, err)
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, modelStr, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, modelStr)
|
|
|
|
fmt.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 {
|
|
|
|
@ -47,7 +46,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 {
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
fmt.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -74,7 +73,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)
|
|
|
|
log.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
fmt.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 {
|
|
|
|
@ -89,16 +88,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 {
|
|
|
|
log.Printf("读取返回数据出错: %v", err)
|
|
|
|
fmt.Printf("读取返回数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &imagesResponse)
|
|
|
|
err = json.Unmarshal(body, &imagesResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("json解析数据出错: %v", err)
|
|
|
|
fmt.Printf("json解析数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
log.Printf("image size: %v", len(imagesResponse.Data))
|
|
|
|
fmt.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)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
@ -127,11 +126,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()})
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
fmt.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 {
|
|
|
|
@ -142,7 +141,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 {
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
fmt.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -169,26 +168,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)
|
|
|
|
log.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
fmt.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 {
|
|
|
|
log.Printf("读取返回数据出错: %v", err)
|
|
|
|
fmt.Printf("读取返回数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("json解析数据出错: %v", err)
|
|
|
|
fmt.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))
|
|
|
|
log.Printf("prompt_tokens: %v complate_tokens: %v total_tokens: %v", promptTokens, complateTokens, totalTokens)
|
|
|
|
fmt.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)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
@ -216,11 +215,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()})
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
fmt.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 {
|
|
|
|
@ -231,7 +230,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 {
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
fmt.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -258,25 +257,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)
|
|
|
|
log.Printf("开始处理返回逻辑 %d", string(sss))
|
|
|
|
fmt.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 {
|
|
|
|
log.Printf("读取返回数据出错: %v", err)
|
|
|
|
fmt.Printf("读取返回数据出错: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("json解析数据出错: %v", err)
|
|
|
|
fmt.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))
|
|
|
|
log.Printf("prompt_tokens: %v total_tokens: %v", promptTokens, totalTokens)
|
|
|
|
fmt.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)
|
|
|
|
//消费余额
|
|
|
|
//消费余额
|
|
|
|
@ -306,11 +305,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()})
|
|
|
|
log.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
fmt.Printf("请求出错 KEY: %v Model: %v ERROR: %v", key, chatRequest.Model, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("请求的KEY: %v Model: %v", key, chatRequest.Model)
|
|
|
|
fmt.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 {
|
|
|
|
@ -321,7 +320,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 {
|
|
|
|
log.Printf("http request err: %v", err)
|
|
|
|
fmt.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -348,13 +347,13 @@ func Completions(c *gin.Context) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
sss, err := json.Marshal(chatRequest)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("chatRequest 转化出错 %v", err)
|
|
|
|
fmt.Printf("chatRequest 转化出错 %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("开始处理返回逻辑: %v", string(sss))
|
|
|
|
fmt.Printf("开始处理返回逻辑: %v", string(sss))
|
|
|
|
if chatRequest.Stream {
|
|
|
|
if chatRequest.Stream {
|
|
|
|
// 流式回应,处理
|
|
|
|
// 流式回应,处理
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
|
log.Printf("流式回应 http status code: %v", resp.StatusCode)
|
|
|
|
fmt.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)
|
|
|
|
@ -377,7 +376,7 @@ func Completions(c *gin.Context) {
|
|
|
|
if err == io.EOF {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("流式回应,处理 err %v:", err.Error())
|
|
|
|
fmt.Printf("流式回应,处理 err %v:", err.Error())
|
|
|
|
break
|
|
|
|
break
|
|
|
|
//return err
|
|
|
|
//return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -402,7 +401,7 @@ func Completions(c *gin.Context) {
|
|
|
|
// 写回数据
|
|
|
|
// 写回数据
|
|
|
|
_, err = c.Writer.Write([]byte(string(chunk) + "\n"))
|
|
|
|
_, err = c.Writer.Write([]byte(string(chunk) + "\n"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("写回数据 err: %v", err.Error())
|
|
|
|
fmt.Printf("写回数据 err: %v", err.Error())
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Writer.(http.Flusher).Flush()
|
|
|
|
c.Writer.(http.Flusher).Flush()
|
|
|
|
@ -414,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)
|
|
|
|
log.Printf("返回内容:%v", reqContent)
|
|
|
|
fmt.Printf("返回内容:%v", reqContent)
|
|
|
|
totalTokens = promptTokens + completionTokens
|
|
|
|
totalTokens = promptTokens + completionTokens
|
|
|
|
log.Printf("prompt_tokens: %v completion_tokens: %v total_tokens: %v", promptTokens, completionTokens, totalTokens)
|
|
|
|
fmt.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 {
|
|
|
|
@ -431,7 +430,7 @@ 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 {
|
|
|
|
log.Printf("非流式回应,处理 err: %v", err)
|
|
|
|
fmt.Printf("非流式回应,处理 err: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
err = json.Unmarshal(body, &chatResponse)
|
|
|
|
@ -442,7 +441,7 @@ func Completions(c *gin.Context) {
|
|
|
|
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))
|
|
|
|
log.Printf("prompt_tokens: %v completion_tokens: %v total_tokens: %v", promptTokens, completionTokens, totalTokens)
|
|
|
|
fmt.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 {
|
|
|
|
|