Skip to content

Commit

Permalink
Copy full JSON schema to OpenAI function
Browse files Browse the repository at this point in the history
This works around the bug where array types get parsed as object types
  • Loading branch information
programmarchy committed Aug 30, 2023
1 parent 511b45d commit 5259940
Showing 1 changed file with 6 additions and 47 deletions.
53 changes: 6 additions & 47 deletions langchain/src/chains/openai_functions/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ function convertOpenAPIParamsToJSONSchema(
if (param.schema) {
schema = spec.getSchema(param.schema);
// eslint-disable-next-line no-param-reassign
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(
schema,
spec
);
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(schema);
} else if (param.content) {
const mediaTypeSchema = Object.values(param.content)[0].schema;
if (mediaTypeSchema) {
Expand All @@ -123,10 +120,7 @@ function convertOpenAPIParamsToJSONSchema(
schema.description = param.description ?? "";
}
// eslint-disable-next-line no-param-reassign
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(
schema,
spec
);
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(schema);
} else {
return jsonSchema;
}
Expand All @@ -152,41 +146,9 @@ function convertOpenAPIParamsToJSONSchema(
* @returns The JSON schema representation of the OpenAPI schema.
*/
function convertOpenAPISchemaToJSONSchema(
schema: OpenAPIV3_1.SchemaObject,
spec: OpenAPISpec
) {
if (schema.type !== "object" && schema.type !== "array") {
return {
type: schema.type ?? "string",
} as JsonSchema7Type;
}
return Object.keys(schema.properties ?? {}).reduce(
(jsonSchema: JsonSchema7ObjectType, propertyName) => {
if (!schema.properties) {
return jsonSchema;
}
const openAPIProperty = spec.getSchema(schema.properties[propertyName]);
if (openAPIProperty.type === undefined) {
return jsonSchema;
}
// eslint-disable-next-line no-param-reassign
jsonSchema.properties[propertyName] = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type: openAPIProperty.type as any,
description: openAPIProperty.description,
};
if (openAPIProperty.required && jsonSchema.required !== undefined) {
jsonSchema.required.push(propertyName);
}
return jsonSchema;
},
{
type: "object",
properties: {},
required: [],
additionalProperties: {},
}
);
schema: OpenAPIV3_1.SchemaObject
): JsonSchema7Type {
return schema;
}

/**
Expand Down Expand Up @@ -258,10 +220,7 @@ function convertOpenAPISpecToOpenAIFunctions(spec: OpenAPISpec): {
)) {
if (mediaTypeObject.schema !== undefined) {
const schema = spec.getSchema(mediaTypeObject.schema);
requestBodySchemas[mediaType] = convertOpenAPISchemaToJSONSchema(
schema,
spec
) as JsonSchema7ObjectType;
requestBodySchemas[mediaType] = convertOpenAPISchemaToJSONSchema(schema) as JsonSchema7ObjectType;
}
}
const mediaTypes = Object.keys(requestBodySchemas);
Expand Down

0 comments on commit 5259940

Please sign in to comment.