Browse Source

AI 通用接口增加积分记录

boweniac 12 giờ trước cách đây
mục cha
commit
28e1b56d40

+ 6 - 6
hook/credit/credit.go

@@ -18,17 +18,17 @@ type Usage struct {
 func AddCreditUsage(tx *ent.Tx, ctx context.Context,
 	agentId string, userId string, departmentId uint64,
 	question *string, answer *string,
-	originalData *custom_types.OriginalData, chatData *Usage) error {
+	originalData *custom_types.OriginalData, chatData *Usage, model string) error {
 	// 积分明细表记录使用量
-	modelName, price := GetModelPrice()
+	modelName, price := GetModelPrice(model)
 	number := ComputePrice(price, chatData.TotalTokens)
-
+	
 	// 记录Token使用信息
 	usageDetailItem, err := tx.UsageDetail.Create().
-		SetType(3).            //1-微信 2-名片 3-智能体
-		SetBotID(agentId).     //智能体ID
+		SetType(3). //1-微信 2-名片 3-智能体
+		SetBotID(agentId). //智能体ID
 		SetReceiverID(userId). //接收者userID
-		SetApp(8).             //8-智能体
+		SetApp(8). //8-智能体
 		SetSessionID(0).
 		SetRequest(*question).
 		SetResponse(*answer).

+ 9 - 5
hook/credit/models.go

@@ -1,6 +1,9 @@
 package credit
 
-import "math"
+import (
+	"math"
+	"strings"
+)
 
 var modelArray = []string{
 	"o1",
@@ -50,12 +53,13 @@ var priceArray = []float64{
 	0.000014,
 }
 
-func getModelName() string {
-	return "gpt-4o"
+func getModelName(modelName string) string {
+	// 将字符串转换为小写
+	return strings.ToLower(modelName)
 }
 
-func GetModelPrice() (model string, price float64) {
-	difyModelName := getModelName()
+func GetModelPrice(modelName string) (model string, price float64) {
+	difyModelName := getModelName(modelName)
 	for i, v := range modelArray {
 		if v == difyModelName {
 			return v, priceArray[i]

+ 1 - 1
internal/logic/chatrecords/gpts_submit_api_chat_logic.go

@@ -215,7 +215,7 @@ func (l *GptsSubmitApiChatLogic) GptsSubmitApiChat(tokenStr string, req *types.G
 						err = credit.AddCreditUsage(tx, l.ctx,
 							agentId, userId, *userInfo.DepartmentId,
 							req.Content, &answer,
-							&originalData, &usage,
+							&originalData, &usage, "gpt-4o-mini",
 						)
 						if err != nil {
 							_ = tx.Rollback()

+ 1 - 1
internal/utils/compapi/func.go

@@ -55,7 +55,7 @@ func AppendUsageDetailLog(ctx context.Context, svcCtx *ServiceContext,
 		err = credit.AddCreditUsage(tx, ctx,
 			authToken, req.EventType, orgId,
 			&msgContent, &resp.Choices[0].Message.Content,
-			&rawReqResp, &usage,
+			&rawReqResp, &usage, req.Model,
 		)
 		if err != nil {
 			_ = tx.Rollback()