genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[Docs] Missing structured output instructions in the generate content docs

Open chrisraygill opened this issue 1 year ago • 0 comments

Generating structured output is a core value of Genkit and is currently not documented in the generate content docs.

Example:

import { generate } from "@genkit-ai/ai";
import { gemini15Flash } from "@genkit-ai/vertexai";
import { z } from "zod";

const CreatureSchema = z.object({
  name: z.string().describe('the name of the creature'),
  hitPoints: z.number().describe('hit points, between 5 and 100'),
  attacks: z.array(z.object({
    name: z.string(),
    damage: z.number().describe('amount of damage, between 2 and 25'),
  })).describe('3 attacks the creature can use')
});

const createCreature = defineFlow({
    name: "createCreature",
    inputSchema: z.string(),
    outputSchema: CreatureSchema,
  },
  (habitat) => {
    const result = await generate({
      model: gemini15Flash,
      prompt: `You are a brilliant RPG designer. Generate a creature that lives in ${habitat}.`,
      output: {schema: CreatureSchema}
    });
    // strongly typed and ready to go
    return result.output();
  }
)

console.log(await createCreature("a developer conference"));

chrisraygill avatar Aug 06 '24 18:08 chrisraygill