From 92889df8b0a1d390457c834f7cd05cec4b6e20c5 Mon Sep 17 00:00:00 2001 From: wood chen Date: Tue, 18 Mar 2025 02:33:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A8=A1=E5=9E=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=A8=A1=E5=9E=8B=E6=95=B0=E6=8D=AE=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E5=92=8C=E9=80=89=E6=8B=A9=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 UpdateOtherPrices 函数中新增模型数据映射,按作者和模型名称分类整理模型数据 - 实现优先选择非free版本的逻辑,确保处理模型数据的准确性 - 增强代码可读性,修复部分逻辑问题,提升整体性能 --- .../cron/openrouter-api/update-other-price.go | 57 +++++++++++++++++-- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/backend/cron/openrouter-api/update-other-price.go b/backend/cron/openrouter-api/update-other-price.go index ecc4915..0d21941 100644 --- a/backend/cron/openrouter-api/update-other-price.go +++ b/backend/cron/openrouter-api/update-other-price.go @@ -59,18 +59,66 @@ func UpdateOtherPrices() error { // 处理每个模型的价格数据 processedCount := 0 skippedCount := 0 + + // 创建一个映射,用于按作者和模型名称存储模型数据 + // 键:作者/模型名称基础部分 + // 值:带有free标识和不带free标识的模型数据 + modelDataMap := make(map[string]map[bool]*ModelData) + + // 第一遍遍历,分类整理模型数据 for _, modelData := range resp.Data { // 提取模型名称(slug中/后面的部分) parts := strings.Split(modelData.Slug, "/") if len(parts) < 2 { log.Printf("跳过无效的模型名称: %s", modelData.Slug) + continue + } + + author := parts[0] + fullModelName := parts[1] + + // 判断是否带有":free"后缀 + isFree := strings.HasSuffix(fullModelName, ":free") + + // 提取基础模型名称(不带":free"后缀) + baseModelName := fullModelName + if isFree { + baseModelName = strings.TrimSuffix(fullModelName, ":free") + } + + // 创建模型的唯一键 + modelKey := author + "/" + baseModelName + + // 如果需要,为这个模型键初始化一个条目 + if _, exists := modelDataMap[modelKey]; !exists { + modelDataMap[modelKey] = make(map[bool]*ModelData) + } + + // 存储模型数据 + modelDataMap[modelKey][isFree] = &modelData + } + + // 第二遍遍历,根据处理规则选择合适的模型数据 + for modelKey, variants := range modelDataMap { + var modelData *ModelData + + // 优先选择非free版本 + if nonFreeData, hasNonFree := variants[false]; hasNonFree { + modelData = nonFreeData + } else if freeData, hasFree := variants[true]; hasFree { + // 如果只有free版本,则使用free版本 + modelData = freeData + } else { + // 不应该发生,但为了安全 + log.Printf("处理模型数据异常: %s", modelKey) skippedCount++ continue } - // 获取模型名称并去除":free"后缀 - modelName := parts[1] - modelName = strings.Split(modelName, ":")[0] + // 提取模型名称 + parts := strings.Split(modelData.Slug, "/") + modelName := strings.Split(parts[1], ":")[0] // 移除":free"后缀 + author := parts[0] // 检查是否在黑名单中 if isInBlacklist(modelName) { @@ -79,9 +127,6 @@ func UpdateOtherPrices() error { continue } - // 获取作者名称 - author := parts[0] - // 检查是否支持的厂商 channelType, ok := authorToChannelType[author] if !ok {