Skip to main content

Built and signed on GitHub Actions

A utility library for typia

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.3.1)

typiautil

JSR JSR

A utility library for typia

Funcitons

Utility for OpenAI

typiaResponseFormat

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]>(),
    validate: typia.createValidate<Output>(), // or typia.createValidateEquals<Output>()
  }),
  messages: [
    {
      role: "system",
      content: "Extract information and return as the structured data following schema",
    },
  ],
 });

console.log(completion.choices[0].message.parsed);

typiaFunction

typia version of zodFunction

import { typiaFunction } from "@ryoppippi/typiautil/openai";
import typia, { tags } from "typia";
import OpenAI from "openai";

/** add description as a JSDoc */
type Params = {
  id: string & tags.Type<'uint32'>;
  name: string & tags.MinLength<1>;
}

function myFunction(args: Params) {
  return args;
}

const client = new OpenAI({})

const completion = await client.beta.chat.completions.parse({
  model: "gpt-4o-mini",
  tool: [typiaFunction({
    jsonSchema: typia.json.application<[Params]>(),
    validate: typia.createValidate<Params>(), // or typia.createValidateEquals<Params>()
    name: "dummy", // you can specify the name of the function, otherwise it will be the name of the type (in this case, "Params")
    description: "dummy function", // you can specify the description of the function, otherwise it will be the JSDoc of the type (in this case, "add description as a JSDoc")
    function: myFunction, // the function to be called (optional)
   })],
  messages: [
    {
      role: "system",
      content: "use the dummy function",
    },
  ],
});

console.log(completion.choices[0].message.tool_calls[0].function.parsed_arguments);

typiaJsonToOpenAIJsonSchema

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);

LICENSE

MIT

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@ryoppippi/typiautil

Import symbol

import * as mod from "@ryoppippi/typiautil";

---- OR ----

Import directly with a jsr specifier

import * as mod from "jsr:@ryoppippi/typiautil";

Add Package

npx jsr add @ryoppippi/typiautil

Import symbol

import * as mod from "@ryoppippi/typiautil";

Add Package

yarn dlx jsr add @ryoppippi/typiautil

Import symbol

import * as mod from "@ryoppippi/typiautil";

Add Package

pnpm dlx jsr add @ryoppippi/typiautil

Import symbol

import * as mod from "@ryoppippi/typiautil";

Add Package

bunx jsr add @ryoppippi/typiautil

Import symbol

import * as mod from "@ryoppippi/typiautil";