https://github.com/ryoppippi/typiautil
utility functions for typia
https://github.com/ryoppippi/typiautil
typescript typia utility-library
Last synced: 6 months ago
JSON representation
utility functions for typia
- Host: GitHub
- URL: https://github.com/ryoppippi/typiautil
- Owner: ryoppippi
- License: mit
- Created: 2024-08-07T22:06:00.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-19T09:05:28.000Z (8 months ago)
- Last Synced: 2025-04-03T11:48:42.831Z (6 months ago)
- Topics: typescript, typia, utility-library
- Language: TypeScript
- Homepage:
- Size: 34.2 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yaml
- License: LICENSE
Awesome Lists containing this project
README
# typiautil
[](https://jsr.io/@ryoppippi/typiautil)
[](https://jsr.io/@ryoppippi/typiautil)A utility library for [typia](https://typia.io/)
## Funcitons
## Utility for OpenAI
### `typiaResponseFormat`
typia version of [`zodResponseFormat`](https://github.com/openai/openai-node/blob/31e4afd6ca50e8e2560598296c099390c5956e31/src/helpers/zod.ts#L56-L73)
```ts
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(), // or typia.createValidateEquals()
}),
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`](https://github.com/openai/openai-node/blob/31e4afd6ca50e8e2560598296c099390c5956e31/src/helpers/zod.ts#L80-L106)
```ts
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(), // or typia.createValidateEquals()
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](https://platform.openai.com/docs/guides/structured-outputs).
```ts
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(chat.choices.at(0)?.message.content as string)console.log(json);
```## LICENSE
[MIT](./LICENSE)