Generative APIJSON Generation

JSON Generation API

The .generate.json() method creates structured JSON content based on prompts, messages, or schemas. Use this for generating API responses, structured records, or documents with optional real-time streaming.

Usage

Example 1 - Prompt with Plain Schema

Define a schema and generate structured JSON:

unbody.generate.json("Provide user details:", {
  schema: {
    type: "object",
    properties: {
      name: { type: "string" },
      age: { type: "number" },
      address: { type: "string" },
    },
  },
});

Example 2 - Prompt with Zod Schema

Use Zod for schema validation:

import { z } from "zod";
 
unbody.generate.json("Provide user details:", {
  schema: z.object({
    name: z.string(),
    age: z.number(),
    address: z.string(),
  }),
});

Example 3 - Message-Based Input with Schema

Use messages and a schema to guide the generation:

import { z } from "zod";
 
unbody.generate.json(
  [
    { role: "user", content: "What is the structure of a business entity?" },
    { role: "system", content: "Explain in detail with examples." },
  ],
  {
    schema: z.object({
      companyName: z.string(),
      employees: z.number(),
      headquarters: z.string(),
    }),
  }
);

Example 4 - Streaming JSON Generation

Generate structured JSON with real-time streaming:

import { z } from "zod";
 
const stream = await unbody.generate.json(
  "Generate a detailed product catalog entry:",
  {
    schema: z.object({
      productName: z.string(),
      price: z.number(),
      description: z.string(),
      features: z.array(z.string()),
    }),
    stream: true,
  }
);
 
for await (const chunk of stream) {
  if (chunk.finished) {
    console.log('Stream finished');
    console.log('Final response:', chunk.content);
    break;
  } else {
    console.log(chunk.content);
  }
}

Options

  • schema: Defines the structure of the output (plain JSON schema or Zod schema).
  • model: AI model to use (e.g., gpt-4).
  • topP: Diversity control via nucleus sampling (0 to 1).
  • maxTokens: Maximum length of the response.
  • temperature: Creativity adjustment (0 to 1).
  • presencePenalty: Penalize new tokens based on input presence (-2.0 to 2.0).
  • frequencyPenalty: Penalize tokens based on frequency of occurrence (-2.0 to 2.0).
  • stream: Enable streaming for real-time content generation (boolean).
©2024 Unbody