-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path5-model-discovery.js
More file actions
124 lines (104 loc) · 3.62 KB
/
Copy path5-model-discovery.js
File metadata and controls
124 lines (104 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Example 5: Model Discovery - Find Available Models
*
* This example shows how to:
* - List available models from a provider
* - See context window limits
* - Check budget usage percentage
* - Choose the right model for your needs
*/
const {
listModels,
createBudgetGuard,
patchGlobalFetch,
getBudgetStatus
} = require("../dist/index.js");
async function discoverModels() {
console.log("🔥 TokenFirewall - Model Discovery Example\n");
console.log("=".repeat(60) + "\n");
// Set up budget tracking (optional for discovery)
createBudgetGuard({
monthlyLimit: 100,
mode: "warn"
});
patchGlobalFetch();
// Example 1: Discover OpenAI models
if (process.env.OPENAI_API_KEY) {
console.log("🔍 Discovering OpenAI models...\n");
const models = await listModels({
provider: "openai",
apiKey: process.env.OPENAI_API_KEY,
includeBudgetUsage: true // Include current budget usage %
});
// Filter to GPT models only
const gptModels = models
.filter(m => m.model.includes("gpt"))
.slice(0, 5);
gptModels.forEach(model => {
console.log(`📦 ${model.model}`);
if (model.contextLimit) {
console.log(` Context: ${model.contextLimit.toLocaleString()} tokens`);
}
if (model.budgetUsagePercentage !== undefined) {
console.log(` Budget Used: ${model.budgetUsagePercentage.toFixed(2)}%`);
}
console.log();
});
}
// Example 2: Discover Gemini models
if (process.env.GEMINI_API_KEY) {
console.log("=".repeat(60) + "\n");
console.log("🔍 Discovering Gemini models...\n");
const models = await listModels({
provider: "gemini",
apiKey: process.env.GEMINI_API_KEY
});
models.slice(0, 5).forEach(model => {
console.log(`📦 ${model.model}`);
if (model.contextLimit) {
console.log(` Context: ${model.contextLimit.toLocaleString()} tokens`);
}
console.log();
});
}
// Example 3: Compare context limits across providers
console.log("=".repeat(60) + "\n");
console.log("📊 Context Limit Comparison:\n");
const providers = [
{ name: "openai", key: process.env.OPENAI_API_KEY },
{ name: "gemini", key: process.env.GEMINI_API_KEY },
{ name: "grok", key: process.env.XAI_API_KEY }
];
for (const provider of providers) {
if (!provider.key) continue;
try {
const models = await listModels({
provider: provider.name,
apiKey: provider.key
});
const modelsWithContext = models.filter(m => m.contextLimit);
if (modelsWithContext.length > 0) {
const maxContext = Math.max(...modelsWithContext.map(m => m.contextLimit));
const maxModel = modelsWithContext.find(m => m.contextLimit === maxContext);
console.log(`${provider.name.toUpperCase()}:`);
console.log(` Largest: ${maxModel.model}`);
console.log(` Context: ${maxContext.toLocaleString()} tokens\n`);
}
} catch (error) {
console.log(`${provider.name.toUpperCase()}: Failed to fetch models\n`);
}
}
console.log("=".repeat(60));
}
discoverModels();
// 💡 Key Points:
// - listModels() fetches real-time model availability
// - Context limits help you choose the right model for your task
// - includeBudgetUsage shows how much of your budget is used
// - Works with OpenAI, Anthropic, Gemini, Grok, and Kimi
// - Anthropic returns a static list (no API endpoint available)
// 🎯 Use Cases:
// - Choose models based on context window needs
// - Find the most cost-effective model
// - Check which models are available in your region
// - Monitor budget usage while selecting models