first commit
This commit is contained in:
60
examples/weather.js
Normal file
60
examples/weather.js
Normal file
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Example: Weather lookup
|
||||
*
|
||||
* Demonstrates basic tool-calling with llmorbiter.
|
||||
* The LLM is given a weather tool and asked about the weather in a city.
|
||||
*
|
||||
* Usage:
|
||||
* OPENROUTER_API_KEY=sk-or-... node examples/weather.js
|
||||
* node examples/weather.js --key sk-or-... --city Tokyo
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
import { LLMOrbiter, OpenRouterProvider } from '../src/index.js';
|
||||
import { weatherTool } from './tools/weather.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
dotenv.config({ path: path.resolve(__dirname, '.env') });
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
function getArg(flag) {
|
||||
const idx = args.indexOf(flag);
|
||||
return idx !== -1 && args[idx + 1] ? args[idx + 1] : null;
|
||||
}
|
||||
|
||||
const apiKey = getArg('--key') || process.env.OPENROUTER_API_KEY;
|
||||
const model = getArg('--model') || 'deepseek/deepseek-v4-flash-0731';
|
||||
const city = getArg('--city') || 'Berlin';
|
||||
|
||||
if (!apiKey) {
|
||||
console.error('Error: Missing API key. Set OPENROUTER_API_KEY in examples/.env or pass --key <api-key>');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const provider = new OpenRouterProvider({ apiKey });
|
||||
|
||||
const orbiter = new LLMOrbiter(provider, {
|
||||
onLLMCall: (call) => console.log(`🤖 LLM call: model="${call.model}" tools=${call.toolCount} promptSize=${call.promptSize} messages=${call.messages}`),
|
||||
onStatus: (status) => console.log(`⚡ ${status}`),
|
||||
onToolCall: (call) => console.log(`🔧 Tool called: ${call.name}(${JSON.stringify(call.args)})`),
|
||||
onToolError: (err) => console.log(`❌ Tool error:`, err.error),
|
||||
onToolResult: (res) => console.log(`📦 Tool result:`, res.result),
|
||||
onUsage: (usage) => console.log(`📊 LLM call usage:`, usage),
|
||||
});
|
||||
|
||||
const result = await orbiter.run({
|
||||
prompt: `What's the weather like in ${city}?`,
|
||||
model,
|
||||
tools: [weatherTool],
|
||||
});
|
||||
|
||||
console.log('\n💬 Response:', result.content);
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user