import { ChatOpenRouter } from "@langchain/openrouter";const model = new ChatOpenRouter({ model: "anthropic/claude-sonnet-4.5", temperature: 0, maxTokens: 1024, // other params...});
const aiMsg = await model.invoke([ { role: "system", content: "You are a helpful assistant that translates English to French. Translate the user sentence.", }, { role: "user", content: "I love programming.", },]);console.log(aiMsg.content);
const stream = await model.stream("Write a short poem about the sea.");for await (const chunk of stream) { process.stdout.write(typeof chunk.content === "string" ? chunk.content : "");}
import { ChatOpenRouter } from "@langchain/openrouter";import { tool } from "@langchain/core/tools";import { z } from "zod";const getWeather = tool(async ({ location }) => `Sunny in ${location}`, { name: "get_weather", description: "Get the current weather in a given location", schema: z.object({ location: z .string() .describe("The city and state, e.g. San Francisco, CA"), }),});const modelWithTools = new ChatOpenRouter({ model: "openai/gpt-4o",}).bindTools([getWeather]);const aiMsg = await modelWithTools.invoke( "What is the weather like in San Francisco?");console.log(aiMsg.tool_calls);
import { ChatOpenRouter } from "@langchain/openrouter";import { z } from "zod";const model = new ChatOpenRouter({ model: "openai/gpt-4.1" });const movieSchema = z.object({ title: z.string().describe("The title of the movie"), year: z.number().describe("The year the movie was released"), director: z.string().describe("The director of the movie"), rating: z.number().describe("The movie's rating out of 10"),});const structuredModel = model.withStructuredOutput(movieSchema, { name: "movie", method: "jsonSchema",});const response = await structuredModel.invoke( "Provide details about the movie Inception");console.log(response);