-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use the newly launched Structured Outputs feature of OpenAI, response_format: { type: "json_schema", json_schema: {"strict": true, "schema": ...} }
#6477
Comments
Hey @ianva! I'm here to help you with any bugs, questions, or contributions you have for LangChain.js. Let's tackle this issue together while we wait for a human maintainer to join in. To use the newly launched Structured Outputs feature of OpenAI with the import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
// Define your Zod schema
const TranslationSchema = z.object({
translatedText: z.string(),
entities: z.array(
z.object({
ner: z.string(),
label: z.string(),
translated: z.string().optional(),
})
),
});
// Create the structured output model
const model = new ChatOpenAI({ modelName: "gpt-4-turbo-preview", temperature: 0 });
const modelWithStructuredOutput = model.withStructuredOutput({
schema: zodToJsonSchema(TranslationSchema),
name: "translation",
});
const prompt = ChatPromptTemplate.fromMessages([
"system",
`You are a helpful assistant. Translate the following text and extract named entities.`,
"human",
"Translate this text: {textToTranslate}",
]);
const chain = prompt.pipe(modelWithStructuredOutput);
const result = await chain.invoke({
textToTranslate: "Hello, world!",
});
console.log(result); In this example:
This setup ensures that the output conforms to the specified JSON schema or Zod schema, leveraging the Note: If using |
I bet the answer is not correct. It should be smth like And again with latest 3.1 update and tooling it seems to me functionality is broken somehow. I cant make model to generate appropriate content |
Hey, we're implementing |
Is the encapsulation of the ChatOpenAI class too complex, making it difficult to expand fields? |
Updating the {
type: "json_schema",
json_schema: { ... }
} is simple, however OpenAI added a new beta endpoint for making requests with this new json schema response format, as well as some utils for converting Zod schemas to their expected JSON schema format. Due to this, it's not as simple as expending the type, and actual code logic changes are required to make it work as intended. |
Thanks for your work, looking forward to this update arriving soon |
I am curious shouldn't |
@david8z We can't make |
Checked other resources
Example Code
The text was updated successfully, but these errors were encountered: