Skip to content

Commit

Permalink
Merge pull request #1438 from samchon/feat/json-validate
Browse files Browse the repository at this point in the history
Fix LLM schema recursive type detector.
  • Loading branch information
samchon authored Dec 16, 2024
2 parents 51d7531 + 5f8f3d8 commit f7ea8ac
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 7 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "7.4.0",
"version": "7.4.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -41,7 +41,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^2.2.0",
"@samchon/openapi": "^2.2.1",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
Expand All @@ -50,7 +50,7 @@
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.8.0",
"@samchon/openapi": ">=2.2.0 <3.0.0"
"@samchon/openapi": ">=2.2.1 <3.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^26.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "7.4.0-dev.20241215",
"version": "7.4.1-dev.20241216-2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -37,11 +37,11 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "7.4.0-dev.20241215"
"typia": "7.4.1-dev.20241216-2"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.8.0",
"@samchon/openapi": ">=2.2.0 <3.0.0"
"@samchon/openapi": ">=2.2.1 <3.0.0"
},
"stackblitz": {
"startCommand": "npm install && npm run test"
Expand Down
9 changes: 8 additions & 1 deletion src/factories/JsonMetadataFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export namespace JsonMetadataFactory {
checker: ts.TypeChecker;
transformer?: ts.TransformationContext;
type: ts.Type;
validate?: MetadataFactory.Validator;
}
export interface IOutput {
collection: MetadataCollection;
Expand All @@ -33,7 +34,13 @@ export namespace JsonMetadataFactory {
escape: true,
constant: true,
absorb: true,
validate,
validate: props.validate
? (meta, explore) => {
const errors: string[] = validate(meta);
errors.push(...props.validate!(meta, explore));
return errors;
}
: validate,
},
collection,
type: props.type,
Expand Down
19 changes: 19 additions & 0 deletions src/programmers/llm/LlmSchemaProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
IChatGptSchema,
IGeminiSchema,
ILlmSchema,
ILlmSchemaV3,
IOpenApiSchemaError,
IResult,
} from "@samchon/openapi";
Expand Down Expand Up @@ -105,6 +107,23 @@ export namespace LlmSchemaProgrammer {
if (props.model === "gemini" && size(metadata) > 1)
output.push("Gemini model does not support the union type.");

// no recursive rule of Gemini and V3
if (
(props.model === "gemini" || props.model === "3.0") &&
((props.config as IGeminiSchema.IConfig | undefined)?.recursive ===
false ||
(props.config as ILlmSchemaV3.IConfig | undefined)?.recursive === 0)
) {
if (metadata.objects.some((o) => o.type.recursive))
output.push(
`LLM schema of "${props.model}" does not support recursive object.`,
);
if (metadata.arrays.some((a) => a.type.recursive))
output.push(
`LLM schema of "${props.model}" does not support recursive array.`,
);
}

// just JSON rule
if (
metadata.atomics.some((a) => a.type === "bigint") ||
Expand Down
114 changes: 114 additions & 0 deletions test-error/src/llm/llm.gemini.recursive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import typia from "typia";

interface IHierarchical {
children: IHierarchical[];
}
interface IReference {
parent: IReference | null;
}

// SCHEMA
typia.llm.schema<
IHierarchical,
"gemini",
{
recursive: false;
}
>();
typia.llm.schema<
IReference,
"gemini",
{
recursive: false;
}
>();
typia.llm.schema<
IHierarchical,
"gemini",
{
recursive: 0;
}
>();
typia.llm.schema<
IReference,
"gemini",
{
recursive: 0;
}
>();

// PARAMETERS
typia.llm.parameters<
{
x: IHierarchical;
},
"gemini",
{
recursive: false;
}
>();
typia.llm.parameters<
{
x: IReference;
},
"gemini",
{
recursive: false;
}
>();
typia.llm.parameters<
{
x: IHierarchical;
},
"gemini",
{
recursive: 0;
}
>();
typia.llm.parameters<
{
x: IReference;
},
"gemini",
{
recursive: 0;
}
>();

// APPLICATION
typia.llm.application<
{
insert(props: { x: IHierarchical }): void;
},
"gemini",
{
recursive: false;
}
>();
typia.llm.application<
{
insert(props: { x: IReference }): void;
},
"gemini",
{
recursive: false;
}
>();
typia.llm.application<
{
insert(props: { x: IHierarchical }): void;
},
"gemini",
{
recursive: 0;
}
>();
typia.llm.application<
{
insert(props: { x: IReference }): void;
},
"gemini",
{
recursive: 0;
}
>();
114 changes: 114 additions & 0 deletions test-error/src/llm/llm.v30.recursive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import typia from "typia";

interface IHierarchical {
children: IHierarchical[];
}
interface IReference {
parent: IReference | null;
}

// SCHEMA
typia.llm.schema<
IHierarchical,
"3.0",
{
recursive: false;
}
>();
typia.llm.schema<
IReference,
"3.0",
{
recursive: false;
}
>();
typia.llm.schema<
IHierarchical,
"3.0",
{
recursive: 0;
}
>();
typia.llm.schema<
IReference,
"3.0",
{
recursive: 0;
}
>();

// PARAMETERS
typia.llm.parameters<
{
x: IHierarchical;
},
"3.0",
{
recursive: false;
}
>();
typia.llm.parameters<
{
x: IReference;
},
"3.0",
{
recursive: false;
}
>();
typia.llm.parameters<
{
x: IHierarchical;
},
"3.0",
{
recursive: 0;
}
>();
typia.llm.parameters<
{
x: IReference;
},
"3.0",
{
recursive: 0;
}
>();

// APPLICATION
typia.llm.application<
{
insert(props: { x: IHierarchical }): void;
},
"3.0",
{
recursive: false;
}
>();
typia.llm.application<
{
insert(props: { x: IReference }): void;
},
"3.0",
{
recursive: false;
}
>();
typia.llm.application<
{
insert(props: { x: IHierarchical }): void;
},
"3.0",
{
recursive: 0;
}
>();
typia.llm.application<
{
insert(props: { x: IReference }): void;
},
"3.0",
{
recursive: 0;
}
>();

0 comments on commit f7ea8ac

Please sign in to comment.