mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
🔨 feat: Use x-strict attribute in OpenAPI Actions for Strict Function Definition (#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 <olivier.schiavo@wengo.com>
This commit is contained in:
parent
aea055b597
commit
d844e56c50
2 changed files with 18 additions and 3 deletions
|
|
@ -17,6 +17,7 @@ export type ParametersSchema = {
|
|||
type: string;
|
||||
properties: Record<string, Reference | Schema>;
|
||||
required: string[];
|
||||
additionalProperties?: boolean;
|
||||
};
|
||||
|
||||
export type OpenAPISchema = OpenAPIV3.SchemaObject &
|
||||
|
|
@ -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,
|
||||
|
|
@ -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',
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue