Replies: 1 comment
-
To define the response type for structured output similar to what OpenAI offers natively in LangChain, you can use the import { z } from "zod";
import { ChatOpenAI } from "@langchain/openai";
import {
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
} from "@langchain/core/prompts";
import { createStructuredOutputChainFromZod } from "../structured_output.js";
const chain = createStructuredOutputChainFromZod(
z.object({
name: z.string().describe("Human name"),
surname: z.string().describe("Human surname"),
age: z.number().describe("Human age"),
appearance: z.string().describe("Human appearance description"),
shortBio: z.string().describe("Short bio description"),
university: z.string().optional().describe("University name if attended"),
gender: z.string().describe("Gender of the human"),
interests: z
.array(z.string())
.describe("json array of strings human interests"),
}),
{
prompt: new ChatPromptTemplate({
promptMessages: [
SystemMessagePromptTemplate.fromTemplate(
"Generate details of a hypothetical person."
),
HumanMessagePromptTemplate.fromTemplate(
"Person description: {inputText}"
),
],
inputVariables: ["inputText"],
}),
llm: new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }),
outputKey: "person",
}
);
const response = await chain.call({ inputText: "A man, living in Poland." }); For streaming capabilities similar to the const stream = await chain.stream({});
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
} This approach lets you handle the response as it is being generated, similar to streaming objects in other SDKs [1]. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
How can I define the response type to achieve a structured output similar to what OpenAI offers natively?
This is not clear to me. Additionally, is there something similar to the streamObject in Vercel's SDK?
https://sdk.vercel.ai/examples/node/streaming-structured-data/stream-object
System Info
langchain@npm:0.2.20
Beta Was this translation helpful? Give feedback.
All reactions