A utility library for typia
typia version of zodResponseFormat
import { typiaResponseFormat } from "@ryoppippi/typiautil/openai";
import typia, { tags } from "typia";
import OpenAI from "openai";
/** add description as a JSDoc */
type Output = {
/** id of the entity */
id: string & tags.Type<'uint32'>;
/** name of the entity */
name: string & tags.MinLength<1>;
}
const client = new OpenAI({})
const completion = await client.beta.chat.completions.parse({
model: "gpt-4o-mini",
response_format: typiaResponseFormat({
jsonSchema: typia.json.application<[Output]>(),
assertParse: typia.json.createAssertParse<Output>(),
}),
messages: [
{
role: "system",
content: "Extract information and return as the structured data following schema",
},
],
});
console.log(completion.choices[0].message.parsed);
Converts JSON Schema generated by Typia to OpenAI ResponseFormat for Structured Outputs.
import { typiaJsonToOpenAIResponse } from "@ryoppippi/typiautil/openai";
import typia, { tags } from "typia";
import OpenAI from "openai";
/** add description as a JSDoc */
type Output = {
/** id of the entity */
id: string & tags.Type<'uint32'>;
/** name of the entity */
name: string & tags.MinLength<1>;
}
const client = new OpenAI({})
const chat = await client.chat.completions.create({
model: "gpt-4o-mini",
response_format: typiaJsonToOpenAIResponse({
jsonSchema: typia.json.application<[Output]>(),
}),
messages: [
{
role: "system",
content: "Extract information and return as the structured data following schema",
},
],
});
/** parse res as JSON */
const json = typia.json.validateParse<Output>(chat.choices.at(0)?.message.content as string)
console.log(json);