Skip to content

Commit

Permalink
feat: manage an 'x-strict': true attribute in openapi specs for assis…
Browse files Browse the repository at this point in the history
…tants which generates function calls with stric attribute
  • Loading branch information
olivierhub committed Nov 5, 2024
1 parent 3428c3c commit 055568d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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

0 comments on commit 055568d

Please sign in to comment.