|
@@ -10,8 +10,8 @@ import (
|
|
|
"regexp"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
-
|
|
|
"wechat-api/ent"
|
|
|
+ "wechat-api/hook/credit"
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
|
"wechat-api/internal/utils/compapi"
|
|
@@ -184,44 +184,76 @@ func (l *ChatCompletionsLogic) AppendAsyncRequest(apiKeyObj *ent.ApiKey, req *ty
|
|
|
}
|
|
|
|
|
|
func (l *ChatCompletionsLogic) AppendUsageDetailLog(authToken string, req *types.CompApiReq, resp *types.CompOpenApiResp) error {
|
|
|
-
|
|
|
- logType := 5
|
|
|
rawReqResp := custom_types.OriginalData{Request: req, Response: resp}
|
|
|
- tmpId := 0
|
|
|
- tmpId, _ = strconv.Atoi(resp.ID)
|
|
|
- sessionId := uint64(tmpId)
|
|
|
orgId := uint64(0)
|
|
|
apiKeyObj, ok := contextkey.AuthTokenInfoKey.GetValue(l.ctx)
|
|
|
if ok {
|
|
|
orgId = apiKeyObj.OrganizationID
|
|
|
}
|
|
|
- promptTokens := uint64(resp.Usage.PromptTokens)
|
|
|
- completionToken := uint64(resp.Usage.CompletionTokens)
|
|
|
- totalTokens := promptTokens + completionToken
|
|
|
-
|
|
|
msgContent := getMessageContentStr(req.Messages[0].Content)
|
|
|
-
|
|
|
- _, _, _ = logType, sessionId, totalTokens
|
|
|
- res, err := l.svcCtx.DB.UsageDetail.Create().
|
|
|
- SetNotNilType(&logType).
|
|
|
- SetNotNilBotID(&authToken).
|
|
|
- SetNotNilReceiverID(&req.EventType).
|
|
|
- SetNotNilSessionID(&sessionId).
|
|
|
- SetNillableRequest(&msgContent).
|
|
|
- SetNillableResponse(&resp.Choices[0].Message.Content).
|
|
|
- SetNillableOrganizationID(&orgId).
|
|
|
- SetOriginalData(rawReqResp).
|
|
|
- SetNillablePromptTokens(&promptTokens).
|
|
|
- SetNillableCompletionTokens(&completionToken).
|
|
|
- SetNillableTotalTokens(&totalTokens).
|
|
|
- Save(l.ctx)
|
|
|
-
|
|
|
- if err == nil { //插入UsageDetai之后再统计UsageTotal
|
|
|
- l.updateUsageTotal(authToken, res.ID, orgId)
|
|
|
+ tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ l.Logger.Errorf("start transaction error:%v\n", err)
|
|
|
+ } else {
|
|
|
+ usage := credit.Usage{
|
|
|
+ CompletionTokens: uint64(resp.Usage.CompletionTokens),
|
|
|
+ PromptTokens: uint64(resp.Usage.PromptTokens),
|
|
|
+ TotalTokens: uint64(resp.Usage.TotalTokens),
|
|
|
+ }
|
|
|
+ err = credit.AddCreditUsage(tx, l.ctx,
|
|
|
+ authToken, req.EventType, orgId,
|
|
|
+ &msgContent, &resp.Choices[0].Message.Content,
|
|
|
+ &rawReqResp, &usage,
|
|
|
+ )
|
|
|
+ if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
+ l.Logger.Errorf("save credits info failed:%v\n", err)
|
|
|
+ } else {
|
|
|
+ _ = tx.Commit()
|
|
|
+ }
|
|
|
}
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+//func (l *ChatCompletionsLogic) AppendUsageDetailLog(authToken string, req *types.CompApiReq, resp *types.CompOpenApiResp) error {
|
|
|
+//
|
|
|
+// logType := 5
|
|
|
+// rawReqResp := custom_types.OriginalData{Request: req, Response: resp}
|
|
|
+// tmpId := 0
|
|
|
+// tmpId, _ = strconv.Atoi(resp.ID)
|
|
|
+// sessionId := uint64(tmpId)
|
|
|
+// orgId := uint64(0)
|
|
|
+// apiKeyObj, ok := contextkey.AuthTokenInfoKey.GetValue(l.ctx)
|
|
|
+// if ok {
|
|
|
+// orgId = apiKeyObj.OrganizationID
|
|
|
+// }
|
|
|
+// promptTokens := uint64(resp.Usage.PromptTokens)
|
|
|
+// completionToken := uint64(resp.Usage.CompletionTokens)
|
|
|
+// totalTokens := promptTokens + completionToken
|
|
|
+//
|
|
|
+// msgContent := getMessageContentStr(req.Messages[0].Content)
|
|
|
+//
|
|
|
+// _, _, _ = logType, sessionId, totalTokens
|
|
|
+// res, err := l.svcCtx.DB.UsageDetail.Create().
|
|
|
+// SetNotNilType(&logType).
|
|
|
+// SetNotNilBotID(&authToken).
|
|
|
+// SetNotNilReceiverID(&req.EventType).
|
|
|
+// SetNotNilSessionID(&sessionId).
|
|
|
+// SetNillableRequest(&msgContent).
|
|
|
+// SetNillableResponse(&resp.Choices[0].Message.Content).
|
|
|
+// SetNillableOrganizationID(&orgId).
|
|
|
+// SetOriginalData(rawReqResp).
|
|
|
+// SetNillablePromptTokens(&promptTokens).
|
|
|
+// SetNillableCompletionTokens(&completionToken).
|
|
|
+// SetNillableTotalTokens(&totalTokens).
|
|
|
+// Save(l.ctx)
|
|
|
+//
|
|
|
+// if err == nil { //插入UsageDetai之后再统计UsageTotal
|
|
|
+// l.updateUsageTotal(authToken, res.ID, orgId)
|
|
|
+// }
|
|
|
+// return err
|
|
|
+//}
|
|
|
+
|
|
|
func (l *ChatCompletionsLogic) getUsagetotalIdByToken(authToken string) (uint64, error) {
|
|
|
|
|
|
var predicates []predicate.UsageTotal
|