Skip to content

Commit

Permalink
🔨 feat: Use x-strict attribute in OpenAPI Actions for Strict Functi…
Browse files Browse the repository at this point in the history
…on Definition (danny-avila#4639)

* feat: manage an 'x-strict': true attribute in openapi specs for assistants which generates function calls with stric attribute

* fix typo and lint errors

---------

Co-authored-by: Olivier Schiavo <[email protected]>
  • Loading branch information
owengo and olivierhub authored Feb 10, 2025
1 parent aea055b commit d844e56
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/data-provider/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ParametersSchema = {
type: string;
properties: Record<string, Reference | Schema>;
required: string[];
additionalProperties?: boolean;
};

export type OpenAPISchema = OpenAPIV3.SchemaObject &
Expand Down Expand Up @@ -128,24 +129,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,
Expand Down Expand Up @@ -383,12 +393,15 @@ 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',
Expand Down Expand Up @@ -428,7 +441,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(
Expand Down
2 changes: 2 additions & 0 deletions packages/data-provider/src/types/assistants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export type FunctionTool = {
description: string;
name: string;
parameters: Record<string, unknown>;
strict?: boolean;
additionalProperties?: boolean; // must be false if strict is true https://platform.openai.com/docs/guides/structured-outputs/some-type-specific-keywords-are-not-yet-supported
};
};

Expand Down

0 comments on commit d844e56

Please sign in to comment.