UNPKG

json-gpt

Version:

Json-GPT permite interactuar de forma sencilla con el modelo GPT-3.5-turbo en formato JSON

186 lines (178 loc) 7.68 kB
import { ChatCompletionRequestMessage, CreateChatCompletionRequest } from 'openai'; import { z } from 'zod'; /** * Module for solving requests using the OpenAI GPT-3 API. * @module solve */ /** * Type definition for the input request to be passed to the solve function. * It can be either a string or an array of ChatCompletionRequestMessage objects. */ type SolveRequest = string | Array<ChatCompletionRequestMessage>; /** * Type definition for the options that can be passed to the solve function. * It includes optional parameters for model configuration, exponential backoff and verbosity. * @property {number} [initial_delay=4000] - Exponential backoff initial delay. * @property {number} [max_retries=4] - Exponential backoff max retires. * @property {number} [delay_exponential=2] - Exponential backoff delay exponential. * @property {boolean} [verbose=false] - Verbose configuration. */ type SolveRequestOptions = Partial<Omit<CreateChatCompletionRequest, 'model' | 'messages'>> & { initial_delay?: number; max_retries?: number; delay_exponential?: number; verbose?: boolean; }; /** * Interface for the response returned by the solve function. * It includes the status code and the data (string) generated by GPT-3.5-tubo. * @property {number} status - Response status * @property {string} data - Response data */ interface SolveResponse { status: number; data: string; } /** * Function for solving a request using the OpenAI GPT-3.5-turbo API. * @function * @async * @param {SolveRequest} request - The input request to be processed by GPT-3.5-turbo. It can be either a string or an array of ChatCompletionRequestMessage objects. * @param {SolveRequestOptions} options - Optional parameters for model configuration, exponential backoff and verbosity.. * @returns {Promise<SolveResponse>} - Promise that resolves to a SolveResponse object containing the status code and the data (string) generated by GPT-3.5-turbo. */ declare function solve(request: SolveRequest, options?: SolveRequestOptions): Promise<SolveResponse>; /** * Module for solving requests using the OpenAI GPT-3 API. * @module solveJson */ /** * Interface for a JSON solving request. * @interface * @template Output - The expected output type of the solved JSON. * @property {string} instructions - Instructions for solving the JSON. * @property {object} target - The target object to solve within the JSON. * @property {string} target.key - The key of the target object. * @property {string} target.value - The value of the target object. * @property {string} safeKey? - The key name of the return strinc content in case of json parse fail. * @property {z.ZodType<Output>} zodSchema - The Zod schema for indicating the model the output format and validating it. * @property {object} data - Additional data to be passed to the solving function. */ interface SolveJsonRequest<Output extends object> { instructions: string; target: { key: string; value: string; }; safeKey?: string; zodSchema: z.ZodType<Output>; data: { [key: string]: any; }; } /** * Type for the response of a JSON solving request. * @typedef {Object} SolveJsonResponse * @property {number} status - The status code of the response. * @property {Output} data - The data returned by the solving function, if successful. * @property {Object} data.error - The error object returned by the solving function, if unsuccessful. * @property {string} data.text - The original text input to the solving function. */ type SolveJsonResponse<Output extends object> = { status: number; data: Output; } | { status: number; data: { error: any; text: string; }; }; /** * Function for solving a request from a JSON object using the OpenAI GPT-3.5-turbo API. Solves the target based on the provided instructions and schema. * @function * @async * @template Output - The expected output type of the solved JSON. * @param {SolveJsonRequest<T>} json - The JSON request object containing instructions, target, schema, and data. * @param {SolveRequestOptions} options - Optional options for the solving process. * @returns {Promise<SolveJsonResponse<T>>} - A promise that resolves to the solved JSON response. */ declare function solveJson<Output extends object>(json: SolveJsonRequest<Output>, options?: SolveRequestOptions): Promise<SolveJsonResponse<Output>>; /** * Module for solving requests using the OpenAI GPT-3 API. * @module solveChat */ /** * Represents a system message in a conversation. * @interface SystemMessage * @property {string} role - The role of the message, which should be set to 'system'. * @property {any} [key] - Additional key-value pairs for custom data. */ interface SystemMessage { role: 'system'; [key: string]: any; } /** * Represents a user message in a conversation. * @interface UserMessage * @property {string} [role='user'] - The role of the message, which should be set to 'user'. * @property {string} message - The content of the message from the user. * @property {Object} data - Additional key-value pairs for custom data. */ interface UserMessage { role: 'user'; message: string; data: { [key: string]: any; }; } /** * Represents an assistant message in a conversation. * @interface AIMessage * @property {string} [role='assistant'] - The role of the message, which should be set to 'assistant'. * @property {string} message - The content of the message from the assistant, if any. * @property {Object} action - Additional key-value pairs for custom data, if any. */ type AIMessage = { role: 'assistant'; } & ({ message: string; } | { action?: { [key: string]: any; }; }); /** * Represents a conversation in the form of an array of system, user, and assistant messages. * @typedef {Array<SystemMessage | UserMessage | AIMessage>} Conversation */ type Conversation = Array<UserMessage | AIMessage | SystemMessage>; /** * Represents a request for solving a chat-based language model. * @interface SolveChatRequest * @template Output - The expected output schema for the assistant's response. * @property {string} instructions - Instructions for the assistant on how to respond to the conversation. * @property {Conversation} messages - The conversation in the form of an array of system, user, and assistant messages. * @property {z.ZodType<Output>} zodSchema - The Zod schema for validating the output of the assistant's response. * @property {Object} custom - Additional custom data to be included in the request. * @property {string} [safeKey] - An optional safe key for safe content filtering. */ interface SolveChatRequest<Output extends object> { instructions: string; messages: Conversation; zodSchema: z.ZodType<Output>; custom: { [key: string]: any; }; safeKey?: string; } /** * Solves a chat-based language model. * @async * @function solveChat * @param {SolveChatRequest<Output>} json - The request object for solving the chat-based language model. * @param {SolveRequestOptions} [options] - Additional options for the request, such as verbosity and safe key. * @returns {SolveChatResponse<Output>} - The response object containing the assistant's response. */ declare function solveChat<Output extends object>(json: SolveChatRequest<Output>, options?: SolveRequestOptions): Promise<SolveJsonResponse<Output>>; export { AIMessage, Conversation, SolveChatRequest, SolveJsonRequest, SolveJsonResponse, SolveRequest, SolveRequestOptions, SolveResponse, SystemMessage, UserMessage, solve, solveChat, solveJson };