Эх сурвалжийг харах

fix:model改为根据response算

jimmyyem 1 өдөр өмнө
parent
commit
b920825482

+ 14 - 2
ent/custom_types/types.go

@@ -26,8 +26,9 @@ type LabelDist struct {
 }
 
 type OriginalData struct {
-	Request  interface{} `json:"request"`
-	Response interface{} `json:"response"`
+	Request   interface{} `json:"request"`
+	Response  interface{} `json:"response"`
+	VResponse *VResponse  `json:"vResponse"`
 }
 
 type ContactFieldTemplate struct {
@@ -42,3 +43,14 @@ type ContactFieldTemplateOptions struct {
 	Label *string `json:"type,omitempty"`
 	Value *string `json:"value,omitempty"`
 }
+
+type VResponse struct {
+	ID           string          `json:"id"`
+	Model        string          `json:"model"`
+	ResponseData []VResponseData `json:"responseData"`
+}
+
+type VResponseData struct {
+	ID    string `json:"id"`
+	Model string `json:"model,omitempty"`
+}

+ 7 - 5
hook/credit/credit.go

@@ -20,15 +20,17 @@ func AddCreditUsage(tx *ent.Tx, ctx context.Context,
 	question *string, answer *string,
 	originalData *custom_types.OriginalData, chatData *Usage, model string) error {
 	// 积分明细表记录使用量
-	modelName, price := GetModelPrice(model)
+	//modelName, price := GetModelPrice(model)
+	modelName, price := ComputeModelPrice(originalData.VResponse)
 	number := ComputePrice(price, chatData.TotalTokens)
-	
+	originalData.VResponse = nil
+
 	// 记录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).

+ 77 - 0
hook/credit/models.go

@@ -1,8 +1,13 @@
 package credit
 
 import (
+	"fmt"
 	"math"
+	"regexp"
+	"sort"
 	"strings"
+	"wechat-api/ent/custom_types"
+	"wechat-api/hook/dify"
 )
 
 var modelArray = []string{
@@ -81,3 +86,75 @@ func Subtraction(number1, number2 float64) float64 {
 	res := math.Floor(d1-d2) / 1000000
 	return res
 }
+
+func ComputeModelPrice(response interface{}) (model string, price float64) {
+	fmt.Printf("response=%v  \n", response)
+
+	// 先获取所有本次响应里所有的model
+	modelInputArray := make([]string, 0)
+
+	// 如果是 gpt/chat/submit 处调用这里会是非正常的结构,所以要提前处理
+	if _, ok := response.(dify.ChatResp); ok {
+		return modelArray[0], priceArray[0]
+	}
+
+	// 如果是标准 VResponse 结构
+	if _, ok := response.(custom_types.VResponse); ok {
+		if modelName := response.(custom_types.VResponse).Model; modelName != "" {
+			modelInputArray = append(modelInputArray, modelName)
+		}
+
+		// 获取 responseData 下所有model
+		responseDataVal := response.(custom_types.VResponse).ResponseData
+		if responseDataVal != nil {
+			for _, res := range responseDataVal {
+				fmt.Printf("model=%v  \n", res.Model)
+				if res.Model == "" {
+					continue
+				} else {
+					modelInputArray = append(modelInputArray, strings.ToLower(res.Model))
+				}
+			}
+		}
+	}
+
+	fmt.Printf("modelInputArray=%v \n", modelInputArray)
+	// 如果精确搜索能找到model,返回model及其价格
+	if len(modelInputArray) > 0 {
+		for i, providedModelName := range modelInputArray {
+			for _, modelName := range modelArray {
+				if modelName == providedModelName {
+					return modelName, priceArray[i]
+				}
+			}
+		}
+	}
+
+	// 如果精确搜索搜不到,则把所有model按从长倒短,挨个匹配下,看有无符合条件
+	// 如果模型里带日期,这时候要在筛选一次,比如 gpt-4.1-mini-2024-07-18 其实应该匹配到 gpt-4.1-mini
+	// 对模型名数组按名字长度排序,然后按长到短匹配
+	sortedModelArray := make([]string, len(modelArray))
+	copy(sortedModelArray, modelArray)
+
+	// 使用長度降序排序模型陣列
+	sort.Slice(sortedModelArray, func(i, j int) bool {
+		return len(sortedModelArray[i]) > len(sortedModelArray[j])
+	})
+
+	// 排序后按长到短挨个匹配合适的model
+	if len(sortedModelArray) > 0 {
+		for _, model := range sortedModelArray {
+			for _, modelName := range modelInputArray {
+				re := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(model))
+				if re.MatchString(modelName) {
+					fmt.Printf("In models.go the given model_name: %s match: %s\n", modelName, model)
+					model, price := GetModelPrice(model)
+					return model, price
+				}
+			}
+		}
+	}
+
+	// 最后返回最高的价格
+	return modelArray[0], priceArray[0]
+}