docker 时间问题修复,bug修复
continuous-integration/drone/push Build is passing Details

main
lvxiu_ext 3 years ago
parent 4b54bde677
commit ad5c4c43bc

@ -1,6 +1,10 @@
FROM alpine:latest FROM alpine:latest
WORKDIR /app WORKDIR /app
RUN apk add --no-cache tzdata
ENV TZ Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN adduser -S -D -H -h /app -s /sbin/nologin -u 1000 app RUN adduser -S -D -H -h /app -s /sbin/nologin -u 1000 app
ADD api2gpt-mid /app/ ADD api2gpt-mid /app/

@ -321,6 +321,12 @@ func completions(c *gin.Context) {
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)
if resp.StatusCode != http.StatusOK {
//退回预扣除的余额
checkBlanceReturn(key, chatRequest.Model)
return nil
}
chatRequestId := "" chatRequestId := ""
reqContent := "" reqContent := ""
reader := bufio.NewReader(resp.Body) reader := bufio.NewReader(resp.Body)
@ -512,7 +518,7 @@ func main() {
//添加reids测试数据 //添加reids测试数据
//var serverInfo ServerInfo = ServerInfo{ //var serverInfo ServerInfo = ServerInfo{
// ServerAddress: "https://gptp.any-door.cn", // ServerAddress: "https://gptp.any-door.cn",
// AvailableKey: "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,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,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", // AvailableKey: "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",
//} //}
// var serverInfo2 ServerInfo = ServerInfo{ // var serverInfo2 ServerInfo = ServerInfo{
// ServerAddress: "https://azure.any-door.cn", // ServerAddress: "https://azure.any-door.cn",

@ -87,7 +87,7 @@ func checkBlance(key string, model string) (ServerInfo, error) {
if err != nil { if err != nil {
return serverInfo, errors.New("余额计算失败") return serverInfo, errors.New("余额计算失败")
} }
log.Printf("用户余额 %f key: %v", balance, key) log.Printf("用户余额 %f key: %v 预扣了:%f", balance, key, (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
if balance < 0 { if balance < 0 {
Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice).Result() Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice).Result()
return serverInfo, errors.New("用户余额不足") return serverInfo, errors.New("用户余额不足")
@ -96,6 +96,36 @@ func checkBlance(key string, model string) (ServerInfo, error) {
return serverInfo, nil return serverInfo, nil
} }
func checkBlanceReturn(key string, model string) error {
var serverInfo ServerInfo
//获取用户信息
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
var userInfo UserInfo
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
//获取服务器信息
serverInfoStr, err := Redis.Get(context.Background(), "server:"+userInfo.SID).Result()
if err != nil {
return errors.New("服务器信息不存在")
}
err = json.Unmarshal([]byte(serverInfoStr), &serverInfo)
if err != nil {
return errors.New("服务器信息解析失败")
}
//获取模型价格
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
if err != nil {
return errors.New("模型信息不存在")
}
var modelInfo ModelInfo
err = json.Unmarshal([]byte(modelPriceStr), &modelInfo)
if err != nil {
return errors.New("模型信息解析失败")
}
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice)).Result()
log.Printf("用户余额 %f key: %v 返还预扣:%f", balance, key, (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
return nil
}
// 余额消费 // 余额消费
func consumption(key string, model string, prompt_tokens int, completion_tokens int, total_tokens int, msg_id string) (string, error) { func consumption(key string, model string, prompt_tokens int, completion_tokens int, total_tokens int, msg_id string) (string, error) {
//获取用户信息 //获取用户信息

Loading…
Cancel
Save