UNPKG

ai-utils.js

Version:

Build AI applications, chatbots, and agents with JavaScript and TypeScript.

136 lines (135 loc) 4.15 kB
import SecureJSON from "secure-json-parse"; import { zodToJsonSchema } from "zod-to-json-schema"; export const OpenAIChatFunctionPrompt = { forOpenAIFunctionDescription(options) { return new OpenAIChatSingleFunctionPrompt(options); }, forSchema({ messages, schemaDefinition, }) { return this.forOpenAIFunctionDescription({ messages, fn: { name: schemaDefinition.name, description: schemaDefinition.description, parameters: schemaDefinition.schema, }, }); }, forSchemaCurried(messages) { return (schemaDefinition) => this.forSchema({ messages, schemaDefinition, }); }, forTool({ messages, tool, }) { return this.forSchema({ messages, schemaDefinition: tool.inputSchemaDefinition, }); }, forToolCurried(messages) { return (tool) => this.forTool({ messages, tool }); }, forOpenAIFunctionDescriptions(options) { return new OpenAIChatAutoFunctionPrompt(options); }, forSchemas({ messages, schemaDefinitions, }) { return this.forOpenAIFunctionDescriptions({ messages, fns: schemaDefinitions.map((schemaDefinition) => ({ name: schemaDefinition.name, description: schemaDefinition.description, parameters: schemaDefinition.schema, })), }); }, forSchemasCurried(messages) { return (schemaDefinitions) => this.forSchemas({ messages, schemaDefinitions }); }, forTools({ messages, tools, }) { return this.forSchemas({ messages, schemaDefinitions: tools.map((tool) => tool.inputSchemaDefinition), }); }, forToolsCurried(messages) { return (tools) => this.forTools({ messages, tools }); }, }; export class OpenAIChatSingleFunctionPrompt { constructor({ messages, fn, }) { Object.defineProperty(this, "messages", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "fn", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.messages = messages; this.fn = fn; } extractJson(response) { const jsonText = response.choices[0].message.function_call.arguments; return SecureJSON.parse(jsonText); } get functionCall() { return { name: this.fn.name }; } get functions() { return [ { name: this.fn.name, description: this.fn.description, parameters: zodToJsonSchema(this.fn.parameters), }, ]; } } export class OpenAIChatAutoFunctionPrompt { constructor({ messages, fns, }) { Object.defineProperty(this, "messages", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "fns", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.messages = messages; this.fns = fns; } extractJsonAndText(response) { const message = response.choices[0].message; const content = message.content; const functionCall = message.function_call; return functionCall == null ? { schema: null, value: null, text: content ?? "", } : { schema: functionCall.name, value: SecureJSON.parse(functionCall.arguments), text: content, }; } get functionCall() { return "auto"; } get functions() { return this.fns.map((fn) => ({ name: fn.name, description: fn.description, parameters: zodToJsonSchema(fn.parameters), })); } }