import { initChatModel } from "langchain";// Follow the steps here to configure your credentials:// https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started.htmlconst model = await initChatModel("bedrock:gpt-4.1");
Copy
const response = await model.invoke("Why do parrots talk?");
const conversation = [ { role: "system", content: "You are a helpful assistant that translates English to French." }, { role: "user", content: "Translate: I love programming." }, { role: "assistant", content: "J'adore la programmation." }, { role: "user", content: "Translate: I love building applications." },];const response = await model.invoke(conversation);console.log(response); // AIMessage("J'adore créer des applications.")
Message objects
Copy
import { HumanMessage, AIMessage, SystemMessage } from "langchain";const conversation = [ new SystemMessage("You are a helpful assistant that translates English to French."), new HumanMessage("Translate: I love programming."), new AIMessage("J'adore la programmation."), new HumanMessage("Translate: I love building applications."),];const response = await model.invoke(conversation);console.log(response); // AIMessage("J'adore créer des applications.")
let full: AIMessageChunk | null = null;for await (const chunk of stream) { full = full ? full.concat(chunk) : chunk; console.log(full.text);}// The// The sky// The sky is// The sky is typically// The sky is typically blue// ...console.log(full.contentBlocks);// [{"type": "text", "text": "The sky is typically blue..."}]
const responses = await model.batch([ "Why do parrots have colorful feathers?", "How do airplanes fly?", "What is quantum computing?", "Why do parrots have colorful feathers?", "How do airplanes fly?", "What is quantum computing?",]);for (const response of responses) { console.log(response);}
import { tool } from "langchain";import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";const getWeather = tool( (input) => `It's sunny in ${input.location}.`, { name: "get_weather", description: "Get the weather at a location.", schema: z.object({ location: z.string().describe("The location to get the weather for"), }), },);const model = new ChatOpenAI({ model: "gpt-4o" });const modelWithTools = model.bindTools([getWeather]); const response = await modelWithTools.invoke("What's the weather like in Boston?");const toolCalls = response.tool_calls || [];for (const tool_call of toolCalls) { // View tool calls made by the model console.log(`Tool: ${tool_call.name}`); console.log(`Args: ${tool_call.args}`);}
// Bind (potentially multiple) tools to the modelconst modelWithTools = model.bindTools([get_weather])// Step 1: Model generates tool callsconst messages = [{"role": "user", "content": "What's the weather in Boston?"}]const ai_msg = await modelWithTools.invoke(messages)messages.push(ai_msg)// Step 2: Execute tools and collect resultsfor (const tool_call of ai_msg.tool_calls) { // Execute the tool with the generated arguments const tool_result = await get_weather.invoke(tool_call) messages.push(tool_result)}// Step 3: Pass results back to model for final responseconst final_response = await modelWithTools.invoke(messages)console.log(final_response.text)// "The current weather in Boston is 72°F and sunny."
const stream = await modelWithTools.stream( "What's the weather in Boston and Tokyo?")for await (const chunk of stream) { // Tool call chunks arrive progressively if (chunk.tool_call_chunks) { for (const tool_chunk of chunk.tool_call_chunks) { console.log(`Tool: ${tool_chunk.get('name', '')}`) console.log(`Args: ${tool_chunk.get('args', '')}`) } }}// Output:// Tool: get_weather// Args:// Tool:// Args: {"loc// Tool:// Args: ation": "BOS"}// Tool: get_time// Args:// Tool:// Args: {"timezone": "Tokyo"}
您可以累积块以构建完整的工具调用:
Accumulate tool calls
Copy
let full: AIMessageChunk | null = nullconst stream = await modelWithTools.stream("What's the weather in Boston?")for await (const chunk of stream) { full = full ? full.concat(chunk) : chunk console.log(full.contentBlocks)}
import * as z from "zod";const Movie = 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 modelWithStructure = model.withStructuredOutput(Movie);const response = await modelWithStructure.invoke("Provide details about the movie Inception");console.log(response);// {// title: "Inception",// year: 2010,// director: "Christopher Nolan",// rating: 8.8,// }
为了最大限度地控制或实现互操作性,您可以提供原始的 JSON Schema。
Copy
const jsonSchema = { "title": "Movie", "description": "A movie with details", "type": "object", "properties": { "title": { "type": "string", "description": "The title of the movie", }, "year": { "type": "integer", "description": "The year the movie was released", }, "director": { "type": "string", "description": "The director of the movie", }, "rating": { "type": "number", "description": "The movie's rating out of 10", }, }, "required": ["title", "year", "director", "rating"],}const modelWithStructure = model.withStructuredOutput( jsonSchema, { method: "jsonSchema" },)const response = await modelWithStructure.invoke("Provide details about the movie Inception")console.log(response) // {'title': 'Inception', 'year': 2010, ...}
import * as z from "zod";const Movie = 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"), 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 modelWithStructure = model.withStructuredOutput(Movie, { includeRaw: true });const response = await modelWithStructure.invoke("Provide details about the movie Inception");console.log(response);// {// raw: AIMessage { ... },// parsed: { title: "Inception", ... }// }
示例:嵌套结构
模式可以是嵌套的:
Copy
import * as z from "zod";const Actor = z.object({ name: str role: z.string(),});const MovieDetails = z.object({ title: z.string(), year: z.number(), cast: z.array(Actor), genres: z.array(z.string()), budget: z.number().nullable().describe("Budget in millions USD"),});const modelWithStructure = model.withStructuredOutput(MovieDetails);
import { initChatModel } from "langchain";const model = await initChatModel("openai:gpt-4.1-mini");const modelWithTools = model.bindTools([{ type: "web_search" }])const message = await modelWithTools.invoke("What was a positive news story from today?");console.log(message.contentBlocks);