GuidesRecipes
DiscordDashboard
DiscordDashboard

generateWithType

WARNING: Probabilistic functions
Theses functions cannot be guaranteed to work every time. Complex inputs can sometimes confuse the LLMs and lead them to throw an Error.

To define the schema we expose a modified version of io-ts that adds a way to describe your types.

For the example here, we want to describe a function given in input and extract its name, the name of its arguments and its return type.

import { t } from "polyfire";

// In React: const { models: { generateWithType } } = usePolyfire();
// In other environments: const { models: { generateWithType } } = polyfire;

const functionDescriptionType = t.type({
  functionName: t.string,
  description: t.string.description("A 50 word description of what the function does"),
  args: t.array(t.string).description("The name of the arguments"),
  returnType: t.string
});

const { functionName, description, args, returnType } = await generateWithType(
  "function add(a, b, c) { return a + b + c }",
  functionDescriptionType,
);

console.log(functionName) // add
console.log(description) // Adds three numbers together.
console.log(args) // ["a", "b", "c"]
console.log(returnType) // number