Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: manage an 'x-strict': true attribute in openapi specs for assis… #4639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/data-provider/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ParametersSchema = {
type: string;
properties: Record<string, Reference | Schema>;
required: string[];
additionalProperties?: boolean;
};

export type OpenAPISchema = OpenAPIV3.SchemaObject &
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: {},
Expand Down Expand Up @@ -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(
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;
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
};
};

Expand Down