- install
npm install @openrouter/ai-sdk-provider ai zod tsx
- start
npx tsx example.ts
| import { createOpenRouter } from '@openrouter/ai-sdk-provider'; | |
| import { generateText, tool, stepCountIs } from 'ai'; | |
| import { z } from 'zod'; | |
| const OPENROUTER_API_KEY="your-key"; | |
| const MODEL_NAME = "openai/gpt-oss-120b"; | |
| export const getWeather = async (modelName: string) => { | |
| const openrouter = createOpenRouter({ | |
| apiKey: OPENROUTER_API_KEY, | |
| }); | |
| const { text } = await generateText({ | |
| model: openrouter(modelName), | |
| prompt: 'Tokyo の天気は ?', | |
| tools: { | |
| getWeather: tool({ | |
| description: 'Get the current weather in a given city.', | |
| parameters: z.object({ | |
| city: z.string().describe('The city name'), | |
| }), | |
| execute: async ({ city }) => { | |
| return `The weather in ${city} is sunny.`; | |
| }, | |
| }), | |
| }, | |
| stopWhen: stepCountIs(5), | |
| }); | |
| console.log(text); | |
| return text; | |
| }; | |
| const main = async function(){ | |
| await getWeather(MODEL_NAME); | |
| } | |
| main(); |
| { | |
| "name": "ai_test1", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "type": "module", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "@openrouter/ai-sdk-provider": "^2.10.0", | |
| "ai": "^6.0.217", | |
| "tsx": "^4.22.4", | |
| "zod": "^4.4.3" | |
| } | |
| } |