functionManager: use json-schema-to-ts to derive arguments for OpenAIFunctions
This commit is contained in:
parent
d9bee2dcf2
commit
b567e13f2a
3 changed files with 52 additions and 24 deletions
|
@ -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<string, never>;
|
||||
|
||||
type OpenAIFunctionRequestData<T extends nameTypeMap> = {
|
||||
[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<T extends nameTypeMap = nameTypeMap> {
|
||||
export interface OpenAIFunction<
|
||||
T extends Readonly<OpenAIFunctionRequestData> = Readonly<OpenAIFunctionRequestData>
|
||||
> {
|
||||
name: string,
|
||||
description?: string,
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: T extends Record<string, never> ? Record<string, never> : {
|
||||
[name in T[string]]: {
|
||||
type: T[name],
|
||||
description?: string,
|
||||
}
|
||||
},
|
||||
required?: Array<keyof T>,
|
||||
},
|
||||
parameters: T,
|
||||
}
|
||||
|
||||
export abstract class OpenAIFunction<T extends nameTypeMap = nameTypeMap> {
|
||||
export abstract class OpenAIFunction<
|
||||
T extends Readonly<OpenAIFunctionRequestData> = Readonly<OpenAIFunctionRequestData>
|
||||
> {
|
||||
getSettings(): ChatCompletionFunctionDefinition {
|
||||
return {
|
||||
name: this.name,
|
||||
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>;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue