Skip to main content
Sometimes you want the generated output to be valid JSON with predefined fields. You can use GrammarConfig to manually specify a JSON schema, or define the schema using Zod.
1

Make a new project folder

mkdir demo && cd demo
2

Initialize with pnpm

pnpm init
3

Install dependencies

pnpm add typescript ts-node @types/node -D
pnpm add @trymirai/uzu
4

Initialize a tsconfig.json

{
    "compilerOptions": {
        "target": "es2020",
        "module": "commonjs",
        "moduleResolution": "node",
        "strict": true,
        "esModuleInterop": true,
        "outDir": "dist",
        "types": [
            "node"
        ]
    },
    "include": [
        "*.ts"
    ]
}
5

Get an API key

Go to Platform and follow this guide.
6

Create main.ts

Don’t forget to add your API key.
import Engine, { GrammarConfig } from '@trymirai/uzu';
import * as z from "zod";

const CountryType = z.object({
    name: z.string(),
    capital: z.string(),
});
const CountryListType = z.array(CountryType);

async function main() {
    const output = await Engine.create('API_KEY')
        .chatModel('Qwen/Qwen3-0.6B')
        .download((update) => {
            console.log('Progress:', update.progress);
        })
        .session()
        .enableThinking(false)
        .grammarConfig(GrammarConfig.fromType(CountryListType))
        .reply(
            "Give me a JSON object containing a list of 3 countries, where each country has name and capital fields",
            (partialOutput) => {
                return true;
            },
        );

    const countries = output.text.parsed.structuredResponse(CountryListType);
    console.log(countries);
}

main().catch((error) => {
    console.error(error);
});
7

Run the snippet

pnpm ts-node main.ts
Now that we’ve tried the simplest snippet, let’s take a look at the step-by-step integration guide.