3 ms·
In my experience, using TypeScript for prompt management alongside something like zod or TypeBox is super useful for getting rid of the stringly-typed prompt pr
by reissbaker 3y ago
In my experience, using TypeScript for prompt management alongside something like zod or TypeBox is super useful for getting rid of the stringly-typed prompt problem. Write a function that wraps template strings for the English bits, and inside the templates for your examples do:
JSON.stringify({ ... } satisfies MyOutputType)
which ensures your examples always exactly match what you expect the LLM to return, even as you iterate on the prompt and output types. Then just call the LLM and validate the output using zod/TypeBox. Once you're done writing the wrapper function, now instead of managing a bunch of string interpolation when you want to have the LLM analyze something, you have a function that takes some input data, and returns your output type, guaranteed.
With OpenAI tool calling, you can even convert the zod/TypeBox type into JSON Schema, give that to OpenAI, and have a fairly good chance the LLM doesn't make any mistakes without even needing retries. (Although you should still retry if the zod/TypeBox/whatever validator fails.)
- throwup238 3y agoIf you're using llama.cpp there is also a tool to convert Typescript interfaces to the BNF grammar used to restrict the output tokens: https://grammar.intrinsiclabs.ai/ https://grammar.intrinsiclabs.ai/