UNPKG

@raphtlw/hyperfunc

Version:

Streamlined Function Calling for LLMs

27 lines (26 loc) 1.27 kB
import OpenAI from "openai"; import { z } from "zod"; export type HyperFunctionData<Context> = { _description: string; _schema: z.AnyZodObject; _handler: (args: unknown, context: Context) => Promise<unknown> | unknown; }; export declare const hyperFunctionToTool: <Context>(name: string, data: HyperFunctionData<Context>) => OpenAI.Chat.Completions.ChatCompletionTool; /** * Storage for HyperFunction(s) with associated methods */ export declare const hyperStore: <Context>(functions: Record<string, HyperFunctionData<Context>>) => { functions: Map<string, HyperFunctionData<Context>>; set(name: string, hf: HyperFunctionData<Context>): void; callTool(data: OpenAI.Chat.Completions.ChatCompletionMessageToolCall, context: Context): Promise<any>; asTools(): OpenAI.Chat.Completions.ChatCompletionTool[]; }; export type HyperStore<Context> = ReturnType<typeof hyperStore<Context>>; /** * Define new HyperFunction */ export declare const hyper: <Args extends Record<string, z.ZodFirstPartySchemaTypes>, Context, Return>({ description, args, handler, }: { description: string; args: Args; handler: (args: { [K in keyof Args]: z.TypeOf<Args[K]>; }, context: Context) => Promise<Return> | Return; }) => HyperFunctionData<Context>;