From b567e13f2a8b4bdbbb7bebe57a95196e5f93f186 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Wed, 24 Apr 2024 02:27:31 +0200 Subject: [PATCH] functionManager: use json-schema-to-ts to derive arguments for OpenAIFunctions --- package-lock.json | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/funcitonManager.ts | 37 +++++++++++++------------------------ 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 85b46e3..ec40cc9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "@typescript-eslint/eslint-plugin": "^7.7.1", "@typescript-eslint/parser": "^7.7.1", "eslint": "^8.57.0", + "json-schema-to-ts": "^3.0.1", "prisma": "5.13.0" } }, @@ -35,6 +36,18 @@ "node": ">=0.10.0" } }, + "node_modules/@babel/runtime": { + "version": "7.24.4", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.24.4.tgz", + "integrity": "sha512-dkxf7+hn8mFBwKjs9bvBlArzLVxVbS8usaPUDd5p2a9JCL9tB8OaOVN1isD4+Xyk4ns89/xeOmbQvgdK7IIVdA==", + "dev": true, + "dependencies": { + "regenerator-runtime": "^0.14.0" + }, + "engines": { + "node": ">=6.9.0" + } + }, "node_modules/@discordjs/builders": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz", @@ -1526,6 +1539,19 @@ "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", "dev": true }, + "node_modules/json-schema-to-ts": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-3.0.1.tgz", + "integrity": "sha512-ANphQxnKbzLWPeYDmdoci8C9g9ttpfMx8etTlJJ8UCEmNXH9jxGkn3AAbMe+lR4N5OG/01nYxPrDyugLdsRt+A==", + "dev": true, + "dependencies": { + "@babel/runtime": "^7.18.3", + "ts-algebra": "^1.2.2" + }, + "engines": { + "node": ">=16" + } + }, "node_modules/json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -1908,6 +1934,12 @@ } ] }, + "node_modules/regenerator-runtime": { + "version": "0.14.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz", + "integrity": "sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==", + "dev": true + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -2077,6 +2109,12 @@ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/ts-algebra": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-1.2.2.tgz", + "integrity": "sha512-kloPhf1hq3JbCPOTYoOWDKxebWjNb2o/LKnNfkWhxVVisFFmMJPPdJeGoGmM+iRLyoXAR61e08Pb+vUXINg8aA==", + "dev": true + }, "node_modules/ts-api-utils": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz", diff --git a/package.json b/package.json index af0fdf1..870f9c7 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@typescript-eslint/eslint-plugin": "^7.7.1", "@typescript-eslint/parser": "^7.7.1", "eslint": "^8.57.0", + "json-schema-to-ts": "^3.0.1", "prisma": "5.13.0" } } diff --git a/src/funcitonManager.ts b/src/funcitonManager.ts index 6ab27d0..4860c0c 100644 --- a/src/funcitonManager.ts +++ b/src/funcitonManager.ts @@ -4,17 +4,11 @@ import { , ChatCompletionMessageToolCall , ChatCompletionTool } from "openai/resources/chat"; +import { type FromSchema, type JSONSchema } from "json-schema-to-ts"; -type parameterMap = { - string: string, - number: number, -}; - -type nameTypeMap = {[name: string]: keyof parameterMap} | Record; - -type OpenAIFunctionRequestData = { - [name in keyof T]: T[name]; -}; +type OpenAIFunctionRequestData = (JSONSchema & { + type: "object" +}); type ChatCompletionToolDefinition = ChatCompletionTool; type ChatCompletionToolCall = ChatCompletionMessageToolCall; @@ -24,31 +18,26 @@ type ChatCompletionFunctionDefinition = FunctionDefinition; /** * Represents the function that can be ran by the OpenAI model */ -export interface OpenAIFunction { +export interface OpenAIFunction< + T extends Readonly = Readonly +> { name: string, description?: string, - parameters: { - type: "object", - properties: T extends Record ? Record : { - [name in T[string]]: { - type: T[name], - description?: string, - } - }, - required?: Array, - }, + parameters: T, } -export abstract class OpenAIFunction { +export abstract class OpenAIFunction< + T extends Readonly = Readonly +> { getSettings(): ChatCompletionFunctionDefinition { return { name: this.name, description: this.description, - parameters: this.parameters, + parameters: this.parameters as Record, }; } - abstract execute(data: OpenAIFunctionRequestData): Promise; + abstract execute(data: FromSchema): Promise; } /*