functionManager: use json-schema-to-ts to derive arguments for OpenAIFunctions

This commit is contained in:
Wroclaw 2024-04-24 02:27:31 +02:00
parent d9bee2dcf2
commit b567e13f2a
3 changed files with 52 additions and 24 deletions

38
package-lock.json generated
View file

@ -23,6 +23,7 @@
"@typescript-eslint/eslint-plugin": "^7.7.1", "@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1", "@typescript-eslint/parser": "^7.7.1",
"eslint": "^8.57.0", "eslint": "^8.57.0",
"json-schema-to-ts": "^3.0.1",
"prisma": "5.13.0" "prisma": "5.13.0"
} }
}, },
@ -35,6 +36,18 @@
"node": ">=0.10.0" "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": { "node_modules/@discordjs/builders": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz", "resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz",
@ -1526,6 +1539,19 @@
"integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==",
"dev": true "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": { "node_modules/json-schema-traverse": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "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": { "node_modules/require-directory": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "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", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" "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": { "node_modules/ts-api-utils": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz", "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz",

View file

@ -25,6 +25,7 @@
"@typescript-eslint/eslint-plugin": "^7.7.1", "@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1", "@typescript-eslint/parser": "^7.7.1",
"eslint": "^8.57.0", "eslint": "^8.57.0",
"json-schema-to-ts": "^3.0.1",
"prisma": "5.13.0" "prisma": "5.13.0"
} }
} }

View file

@ -4,17 +4,11 @@ import {
, ChatCompletionMessageToolCall , ChatCompletionMessageToolCall
, ChatCompletionTool , ChatCompletionTool
} from "openai/resources/chat"; } from "openai/resources/chat";
import { type FromSchema, type JSONSchema } from "json-schema-to-ts";
type parameterMap = { type OpenAIFunctionRequestData = (JSONSchema & {
string: string, type: "object"
number: number, });
};
type nameTypeMap = {[name: string]: keyof parameterMap} | Record<string, never>;
type OpenAIFunctionRequestData<T extends nameTypeMap> = {
[name in keyof T]: T[name];
};
type ChatCompletionToolDefinition = ChatCompletionTool; type ChatCompletionToolDefinition = ChatCompletionTool;
type ChatCompletionToolCall = ChatCompletionMessageToolCall; type ChatCompletionToolCall = ChatCompletionMessageToolCall;
@ -24,31 +18,26 @@ type ChatCompletionFunctionDefinition = FunctionDefinition;
/** /**
* Represents the function that can be ran by the OpenAI model * Represents the function that can be ran by the OpenAI model
*/ */
export interface OpenAIFunction<T extends nameTypeMap = nameTypeMap> { export interface OpenAIFunction<
T extends Readonly<OpenAIFunctionRequestData> = Readonly<OpenAIFunctionRequestData>
> {
name: string, name: string,
description?: string, description?: string,
parameters: { parameters: T,
type: "object",
properties: T extends Record<string, never> ? Record<string, never> : {
[name in T[string]]: {
type: T[name],
description?: string,
}
},
required?: Array<keyof T>,
},
} }
export abstract class OpenAIFunction<T extends nameTypeMap = nameTypeMap> { export abstract class OpenAIFunction<
T extends Readonly<OpenAIFunctionRequestData> = Readonly<OpenAIFunctionRequestData>
> {
getSettings(): ChatCompletionFunctionDefinition { getSettings(): ChatCompletionFunctionDefinition {
return { return {
name: this.name, name: this.name,
description: this.description, description: this.description,
parameters: this.parameters, parameters: this.parameters as Record<string, unknown>,
}; };
} }
abstract execute(data: OpenAIFunctionRequestData<T>): Promise<string>; abstract execute(data: FromSchema<T>): Promise<string>;
} }
/* /*