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-5.4");
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.")
消息对象
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..."}]
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("提供关于电影《盗梦空间》的详情");console.log(response);// {// title: "Inception",// year: 2010,// director: "Christopher Nolan",// rating: 8.8,// }
为了最大程度的控制或互操作性,你可以提供原始 JSON Schema。
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("提供关于电影《盗梦空间》的详情")console.log(response) // {'title': 'Inception', 'year': 2010, ...}
任何实现 Standard Schema 规范的库的模式也是支持的。Standard Schema 对象在运行时通过模式的 ~standard.validate() 方法进行验证。
import * as v from "valibot";import { toStandardJsonSchema } from "@valibot/to-json-schema";const Movie = toStandardJsonSchema( v.object({ title: v.pipe(v.string(), v.description("The title of the movie")), year: v.pipe(v.number(), v.description("The year the movie was released")), director: v.pipe(v.string(), v.description("The director of the movie")), rating: v.pipe(v.number(), v.description("The movie's rating out of 10")), }));const modelWithStructure = model.withStructuredOutput(Movie);const response = await modelWithStructure.invoke("提供关于电影《盗梦空间》的详情");console.log(response);// {// title: "Inception",// year: 2010,// director: "Christopher Nolan",// rating: 8.8,// }
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("提供关于电影《盗梦空间》的详情");console.log(response);// {// raw: AIMessage { ... },// parsed: { title: "Inception", ... }// }
示例:嵌套结构
模式可以嵌套:
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);