From 055568d1500c387fc92c4e444cb94306001a9f5d Mon Sep 17 00:00:00 2001 From: Olivier Schiavo Date: Tue, 5 Nov 2024 12:04:43 +0100 Subject: [PATCH] feat: manage an 'x-strict': true attribute in openapi specs for assistants which generates function calls with stric attribute --- packages/data-provider/src/actions.ts | 21 +++++++++++++++---- .../data-provider/src/types/assistants.ts | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/data-provider/src/actions.ts b/packages/data-provider/src/actions.ts index 386ca34e74f..dedf7c47140 100644 --- a/packages/data-provider/src/actions.ts +++ b/packages/data-provider/src/actions.ts @@ -11,6 +11,7 @@ export type ParametersSchema = { type: string; properties: Record; required: string[]; + additionalProperties?: boolean; }; export type OpenAPISchema = OpenAPIV3.SchemaObject & @@ -122,24 +123,33 @@ export class FunctionSignature { name: string; description: string; parameters: ParametersSchema; + strict: boolean; - constructor(name: string, description: string, parameters: ParametersSchema) { + constructor(name: string, description: string, parameters: ParametersSchema, strict?: boolean) { this.name = name; this.description = description; this.parameters = parameters; + this.strict = strict || false; } toObjectTool(): FunctionTool { + const parameters = { + ...this.parameters, + additionalProperties: this.strict ? false : undefined + }; + return { type: Tools.function, function: { name: this.name, description: this.description, - parameters: this.parameters, + parameters, + strict: this.strict }, }; } } + class RequestConfig { constructor( readonly domain: string, @@ -366,13 +376,16 @@ export function openapiToFunction( for (const [method, operation] of Object.entries(methods as OpenAPIV3.PathsObject)) { const operationObj = operation as OpenAPIV3.OperationObject & { 'x-openai-isConsequential'?: boolean; + } & { + 'x-strict'?: boolean }; // Operation ID is used as the function name const defaultOperationId = `${method}_${path}`; const operationId = operationObj.operationId || sanitizeOperationId(defaultOperationId); const description = operationObj.summary || operationObj.description || ''; - + const isStrict = operationObj['x-strict'] || false; + const parametersSchema: OpenAPISchema = { type: 'object', properties: {}, @@ -411,7 +424,7 @@ export function openapiToFunction( } } - const functionSignature = new FunctionSignature(operationId, description, parametersSchema); + const functionSignature = new FunctionSignature(operationId, description, parametersSchema, isStrict); functionSignatures.push(functionSignature); const actionRequest = new ActionRequest( diff --git a/packages/data-provider/src/types/assistants.ts b/packages/data-provider/src/types/assistants.ts index 6e21c0b5b27..7e21b59c3b1 100644 --- a/packages/data-provider/src/types/assistants.ts +++ b/packages/data-provider/src/types/assistants.ts @@ -38,6 +38,8 @@ export type FunctionTool = { description: string; name: string; parameters: Record; + strict?: boolean; + additionnalProperties?: boolean; // must be false if strict is true https://platform.openai.com/docs/guides/structured-outputs/some-type-specific-keywords-are-not-yet-supported }; };