示例工程 ch25-provider-models

ch25-provider-models/01-model-metadata/index.ts

typescript · 3.3 KB

// 01-model-metadata —— 模型元数据决定了三件事:能不能装、能跑多长、要花多少钱。
//
// 运行:node --experimental-strip-types index.ts
//
// 三个观察点:
//   1. 每个模型都带 contextWindow / maxTokens / cost / reasoning
//   2. 成本按「每百万 token」计价,输入/输出/缓存读写四个价格不同
//   3. reasoning 决定了可用的 thinking 档位
 
import type { Model } from "@earendil-works/pi-ai";
import { getSupportedThinkingLevels } from "@earendil-works/pi-ai";
 
const models: Model<any>[] = [
	{
		id: "cheap-fast",
		name: "Cheap Fast",
		api: "stub",
		provider: "acme",
		baseUrl: "https://stub.invalid",
		input: ["text"],
		cost: { input: 0.15, output: 0.6, cacheRead: 0.03, cacheWrite: 0.15 },
		contextWindow: 128_000,
		maxTokens: 8_192,
		reasoning: false,
	} as unknown as Model<any>,
	{
		id: "smart-reasoning",
		name: "Smart Reasoning",
		api: "stub",
		provider: "acme",
		baseUrl: "https://stub.invalid",
		input: ["text", "image"],
		cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
		contextWindow: 200_000,
		maxTokens: 32_000,
		reasoning: true,
	} as unknown as Model<any>,
	{
		id: "reasoning-limited",
		name: "Reasoning Limited",
		api: "stub",
		provider: "acme",
		baseUrl: "https://stub.invalid",
		input: ["text"],
		cost: { input: 1, output: 4, cacheRead: 0.1, cacheWrite: 1 },
		contextWindow: 32_000,
		maxTokens: 4_096,
		reasoning: true,
		thinkingLevelMap: { minimal: null, low: null, medium: null, high: "high" },
	} as unknown as Model<any>,
];
 
/** 估算一次调用的成本(美元):输入 + 输出,按每百万 token 计价。 */
function estimateCost(model: Model<any>, inputTokens: number, outputTokens: number) {
	return (model.cost.input * inputTokens + model.cost.output * outputTokens) / 1_000_000;
}
 
console.log("模型                   窗口      最大输出   reasoning  档位");
for (const model of models) {
	const levels = getSupportedThinkingLevels(model).join("/");
	console.log(`${model.id.padEnd(20)} ${String(model.contextWindow).padEnd(9)} ${String(model.maxTokens).padEnd(9)} ${String(model.reasoning).padEnd(10)} ${levels}`);
}
 
const sample = estimateCost(models[0] as Model<any>, 20_000, 1_000);
const sample2 = estimateCost(models[1] as Model<any>, 20_000, 1_000);
console.log(`20k 输入 + 1k 输出成本:cheap=$${sample.toFixed(6)},smart=$${sample2.toFixed(6)}(相差 ${(sample2 / sample).toFixed(1)} 倍)`);
 
const ok = (label: string, pass: boolean): void => {
	console.log(`${pass ? "✓" : "✗"} ${label}`);
	if (!pass) process.exitCode = 1;
};
 
ok("非 reasoning 模型只有 off 档", getSupportedThinkingLevels(models[0] as Model<any>).join(",") === "off");
ok("reasoning 模型支持多档", getSupportedThinkingLevels(models[1] as Model<any>).length > 1);
ok("thinkingLevelMap 为 null 的档位被排除", getSupportedThinkingLevels(models[2] as Model<any>).join(",") === "off,high");
ok("成本差异体现在估算里(20 倍以上)", sample2 / sample > 20);
ok("窗口与最大输出是独立字段", models[1]?.contextWindow !== models[1]?.maxTokens);
ok("成本四项齐全", ["input", "output", "cacheRead", "cacheWrite"].every((key) => typeof (models[0]?.cost as unknown as Record<string, unknown>)[key] === "number"));
 
console.log("✓ OK");