UNPKG

langschema

Version:

one-line LLM output parsers for JS/TS

93 lines (92 loc) 4.4 kB
import { z } from 'zod'; interface GenericPromptOptions { gpt4?: boolean; } type AtLeastOne<T> = [T, ...T[]]; /** * This function parses the given input into a given Zod type using the OpenAI API. The * input type can be ANY Zod type, not just an object - a boolean, a number, an enum, * etc. are all valid inputs. * * @export * @param {string} prompt - The input to parse * @param zodType - The Zod type to parse the response into. * @param {GenericPromptOptions} [promptOptions] - Optional settings for the prompt. * @returns {Promise<T>} A promise that resolves to the parsed value. * * @throws {ZodError} If the parsed response does not match the expected structure. * * @async */ export declare function asZodType<T>(prompt: string, zodType: z.ZodType<T>, promptOptions?: GenericPromptOptions): Promise<T>; /** * Asynchronously handles a binary prompt to return a boolean answer. * * This function creates a Large Language Model (LLM) from the provided options * and prompts the user with a message. It then returns a boolean value based on the * user's answer. * * @export * @param {string} prompt - The prompt message to display to the user. * @param {GenericPromptOptions} [promptOptions] - Optional settings for the prompt. * @returns {Promise<boolean>} A promise that resolves to a boolean indicating the user's response. * * @throws {ZodError} If the parsed response does not match the expected structure. * * @async */ export declare function bool(prompt: string, promptOptions?: GenericPromptOptions): Promise<boolean>; /** * Asynchronously handles a categorical prompt and returns the classified category * * This function creates a Large Language Model (LLM) from the provided options * and prompts the user with a message. It then returns the selected category, * which must be one of the provided allowed values. * * @export * @param {string} prompt - The user's question to classify * @param {AtLeastOne<string>} allowedValues - Array of allowable categorical values. * @param {GenericPromptOptions} [promptOptions] - Optional settings for the prompt. * @returns {Promise<string>} A promise that resolves to a string indicating the user's selected category. * * @throws {Error} If no prompt is provided. * @throws {ZodError} If the parsed response does not match the expected structure or is not one of the allowed values. * * @async */ export declare function categorize(prompt: string, allowedValues: AtLeastOne<string>, promptOptions?: GenericPromptOptions): Promise<string>; /** * Asynchronously handles a list prompt and returns an array of selected values. * * This function creates a Large Language Model (LLM) from the provided options * and prompts the user with a message. The user is expected to select a minimum * and maximum number of values from the allowed list, and the function returns an array * of these values. * * @export * @param {string} prompt - The prompt message to display to the user. * @param {null | AtLeastOne<string>} allowedValues - Array of allowable values. Null indicates that any string is allowed. * @param {number} [minValues=1] - The minimum number of values the user must select. * @param {number} [maxValues=5] - The maximum number of values the user can select. * @param {GenericPromptOptions} [promptOptions] - Optional settings for the prompt. * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the user's selected values. * * @throws {Error} If no prompt is provided, if minValues is not less than maxValues, or if minValues is not greater than zero. * @throws {ZodError} If the parsed response does not match the expected structure or is not one of the allowed values. * * @async */ export declare function list(prompt: string, allowedValues: null | AtLeastOne<string>, minValues?: number, maxValues?: number, promptOptions?: GenericPromptOptions): Promise<string[]>; /** * Asynchronously handles a string prompt and returns any string response. No ouptut validation is performed. * * @export * @param {string} prompt - The prompt message to display to the user. * @returns {Promise<string>} A promise that resolves to a string indicating the content of the user's response. * * @throws {Error} If the LLM fails to create or call. * * @async */ export declare function string(prompt: string): Promise<string>; export {};