Skip to content

Commit

Permalink
ILlmSchema.IParameters.additionalProperties to be false.
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Nov 29, 2024
1 parent 1700b31 commit b918d01
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samchon/openapi",
"version": "2.0.0-dev.20241129-4",
"version": "2.0.0-dev.20241129-8",
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
Expand Down
12 changes: 10 additions & 2 deletions src/converters/LlmConverterV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ export namespace LlmConverterV3 {
const entity: OpenApi.IJsonSchema.IObject | null =
LlmParametersFinder.find(props);
if (entity === null) return null;
return schema({
else if (!!entity.additionalProperties) {
if (props.errors)
props.errors.push(
`${props.accessor ?? "$input"}.additionalProperties: LLM does not allow additional properties on parameters.`,
);
return null;
}
const res = schema({
...props,
schema: entity,
}) as ILlmSchemaV3.IParameters | null;
if (res !== null) res.additionalProperties = false;
return res;
};

export const schema = (props: {
Expand Down Expand Up @@ -87,7 +96,6 @@ export namespace LlmConverterV3 {
else if (LlmTypeCheckerV3.isObject(next)) {
next.properties ??= {};
next.required ??= [];
next.additionalProperties = false;
} else if (
props.config.constraint === false &&
(LlmTypeCheckerV3.isInteger(next) || LlmTypeCheckerV3.isNumber(next))
Expand Down
8 changes: 8 additions & 0 deletions src/converters/LlmConverterV3_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export namespace LlmConverterV3_1 {
const entity: OpenApi.IJsonSchema.IObject | null =
LlmParametersFinder.find(props);
if (entity === null) return null;
else if (!!entity.additionalProperties) {
if (props.errors)
props.errors.push(
`${props.accessor ?? "$input"}.additionalProperties: LLM does not allow additional properties on parameters.`,
);
return null;
}

const $defs: Record<string, ILlmSchemaV3_1> = {};
const res: ILlmSchemaV3_1.IParameters | null = schema({
Expand All @@ -28,6 +35,7 @@ export namespace LlmConverterV3_1 {
}) as ILlmSchemaV3_1.IParameters | null;
if (res === null) return null;
res.$defs = $defs;
res.additionalProperties = false;
return res;
};

Expand Down
24 changes: 24 additions & 0 deletions test/features/llm/v3.1/test_llm_v30_parameters_dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TestValidator } from "@nestia/e2e";
import { ILlmSchema, OpenApi } from "@samchon/openapi";
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";
import typia, { IJsonSchemaCollection } from "typia";

export const test_llm_v30_parameters_dynamic = (): void => {
const errors: string[] = [];
const collection: IJsonSchemaCollection =
typia.json.schemas<[Record<string, number>]>();
const parameters: ILlmSchema.IParameters<"3.0"> | null =
LlmSchemaConverter.parameters("3.0")({
errors,
accessor: "$input",
config: LlmSchemaConverter.defaultConfig("3.0"),
components: collection.components,
schema: typia.assert<
OpenApi.IJsonSchema.IReference | OpenApi.IJsonSchema.IObject
>(collection.schemas[0]),
});
TestValidator.equals("parameters")(parameters)(null);
TestValidator.equals("errors")(errors.map((e) => e.split(":")[0]).sort())(
["$input.additionalProperties"].sort(),
);
};
24 changes: 24 additions & 0 deletions test/features/llm/v3.1/test_llm_v31_parameters_dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TestValidator } from "@nestia/e2e";
import { ILlmSchema, OpenApi } from "@samchon/openapi";
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";
import typia, { IJsonSchemaCollection } from "typia";

export const test_llm_v31_parameters_dynamic = (): void => {
const errors: string[] = [];
const collection: IJsonSchemaCollection =
typia.json.schemas<[Record<string, number>]>();
const parameters: ILlmSchema.IParameters<"3.1"> | null =
LlmSchemaConverter.parameters("3.1")({
errors,
accessor: "$input",
config: LlmSchemaConverter.defaultConfig("3.1"),
components: collection.components,
schema: typia.assert<
OpenApi.IJsonSchema.IReference | OpenApi.IJsonSchema.IObject
>(collection.schemas[0]),
});
TestValidator.equals("parameters")(parameters)(null);
TestValidator.equals("errors")(errors.map((e) => e.split(":")[0]).sort())(
["$input.additionalProperties"].sort(),
);
};

0 comments on commit b918d01

Please sign in to comment.