(rawData: OneAPIRawData, rateBaseline: number = 0.002)
| 39 | } |
| 40 | |
| 41 | parse(rawData: OneAPIRawData, rateBaseline: number = 0.002): StandardizedPricingRecord { |
| 42 | if (!this.canParse(rawData)) { |
| 43 | throw new Error("Invalid OneAPI data format"); |
| 44 | } |
| 45 | |
| 46 | const models: StandardizedModel[] = []; |
| 47 | const groups = new Map<string, StandardizedGroup>(); |
| 48 | |
| 49 | const { model_group, model_completion_ratio, group_special } = rawData.data; |
| 50 | |
| 51 | // 1. 创建所有用户组 |
| 52 | for (const [groupId, groupData] of Object.entries(model_group)) { |
| 53 | const group: StandardizedGroup = { |
| 54 | id: groupId, |
| 55 | name: groupId, |
| 56 | rate: groupData.GroupRatio, |
| 57 | }; |
| 58 | groups.set(groupId, group); |
| 59 | } |
| 60 | |
| 61 | // 2. 收集所有模型的定价信息 |
| 62 | const modelPricingMap = new Map<string, { groupId: string; price: number; isPerCall: boolean }[]>(); |
| 63 | |
| 64 | for (const [groupId, groupData] of Object.entries(model_group)) { |
| 65 | for (const [modelName, priceData] of Object.entries(groupData.ModelPrice)) { |
| 66 | if (!modelPricingMap.has(modelName)) { |
| 67 | modelPricingMap.set(modelName, []); |
| 68 | } |
| 69 | |
| 70 | modelPricingMap.get(modelName)!.push({ |
| 71 | groupId, |
| 72 | price: priceData.price, |
| 73 | isPerCall: priceData.isPrice, |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // 3. 为每个模型创建StandardizedModel,确保价格一致性 |
| 79 | for (const [modelName, pricingList] of modelPricingMap) { |
| 80 | const completeRate = model_completion_ratio[modelName] ?? 1; |
| 81 | const supportedGroups = group_special[modelName] || pricingList.map((p) => p.groupId); |
| 82 | |
| 83 | // 检查该模型在所有支持的分组中是否有一致的基准价格和计费类型 |
| 84 | const relevantPricing = pricingList.filter((p) => supportedGroups.includes(p.groupId)); |
| 85 | |
| 86 | if (relevantPricing.length === 0) { |
| 87 | console.warn(`模型 ${modelName} 没有找到相关分组的定价信息,跳过`); |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | // 检查计费类型是否一致(全部按token计费或全部按次计费) |
| 92 | const firstType = relevantPricing[0].isPerCall; |
| 93 | const hasConsistentType = relevantPricing.every((p) => p.isPerCall === firstType); |
| 94 | |
| 95 | if (!hasConsistentType) { |
| 96 | console.warn(`模型 ${modelName} 在不同分组中有不一致的计费类型,跳过`); |
| 97 | continue; |
| 98 | } |
nothing calls this directly
no test coverage detected