Skip to content

Commit

Permalink
Merge pull request #1372 from samchon/feat/llm
Browse files Browse the repository at this point in the history
Strict JSON/LLM schema of object type
  • Loading branch information
samchon authored Nov 22, 2024
2 parents e5b50e0 + 3e591ee commit 4cfc667
Show file tree
Hide file tree
Showing 203 changed files with 663 additions and 10,403 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export namespace json {
// LLM FUNCTION CALLING APPLICATION
export namespace llm {
// application from a class or interface type
export function application<App, Model>(): ILlmApplication;
export function schema<T, Model>(): ILlmSchema; // LLM type schema
export function application<App, Model>(): ILlmApplication<Model>;
export function schema<T, Model>(): ILlmSchema<Model>; // type schema
}

// PROTOCOL BUFFER
Expand Down
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-7.0.0-dev.20241122.tgz"
"typia": "../typia-7.0.0-dev.20241123.tgz"
}
}
3 changes: 1 addition & 2 deletions benchmark/src/structures/zod/ZodUltimateUnion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ const ObjectDef: z.ZodType<OpenApi.IJsonSchema.IObject> = z.lazy(() =>
z.object({
$id: z.string(),
type: z.literal("object"),

properties: z.record(Schema),
patternProperties: z.union([z.undefined(), z.record(Schema)]),
required: z.union([z.undefined(), z.array(z.string())]),
required: z.array(z.string()),
description: z.union([z.string(), z.undefined()]),
}),
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "7.0.0-dev.20241122",
"version": "7.0.0-dev.20241123",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -42,7 +42,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "2.0.0-dev.20241122-2",
"@samchon/openapi": "^2.0.0-dev.20241123-3",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export namespace json {
// LLM FUNCTION CALLING APPLICATION
export namespace llm {
// application from a class or interface type
export function application<App, Model>(): ILlmApplication;
export function schema<T, Model>(): ILlmSchema; // LLM type schema
export function application<App, Model>(): ILlmApplication<Model>;
export function schema<T, Model>(): ILlmSchema<Model>; // type schema
}

// PROTOCOL BUFFER
Expand Down
4 changes: 2 additions & 2 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.0.0-dev.20241122",
"version": "7.0.0-dev.20241123",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "7.0.0-dev.20241122"
"typia": "7.0.0-dev.20241123"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0"
Expand Down
1 change: 1 addition & 0 deletions src/programmers/internal/json_schema_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const json_schema_native = (props: {
props.components.schemas[props.native.name] ??= {
type: "object",
properties: {},
required: [],
};
}
return json_schema_plugin({
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/internal/json_schema_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const create_object_schema = (props: {
return {
type: "object",
properties,
required: required.length ? required : undefined,
required,
title: (() => {
const info: IJsDocTagInfo | undefined = props.object.type.jsDocTags.find(
(tag) => tag.name === "title",
Expand Down
8 changes: 8 additions & 0 deletions src/programmers/llm/LlmSchemaProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export namespace LlmSchemaProgrammer {
metadata.constants.some((c) => c.type === "bigint")
)
output.push("LLM schema does not support bigint type.");
if (
metadata.objects.some((o) =>
o.type.properties.some(
(p) => p.key.isSoleLiteral() === false && p.value.size() !== 0,
),
)
)
output.push("LLM schema does not support dynamic property in object.");
if (
metadata.tuples.some((t) =>
t.type.elements.some((e) => e.isRequired() === false),
Expand Down
2 changes: 1 addition & 1 deletion test-error/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"typescript": "^5.3.2"
},
"dependencies": {
"typia": "../typia-7.0.0-dev.20241122.tgz"
"typia": "../typia-7.0.0-dev.20241123.tgz"
}
}
2 changes: 1 addition & 1 deletion test-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"typescript": "^5.4.5"
},
"dependencies": {
"typia": "../typia-7.0.0-dev.20241122.tgz"
"typia": "../typia-7.0.0-dev.20241123.tgz"
}
}
10 changes: 10 additions & 0 deletions test/build/internal/TestLlmApplicationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export namespace TestLlmApplicationGenerator {
) === false
)
continue;

const v31: string = await fs.promises.readFile(
`${__dirname}/../../schemas/json.schemas/v3_1/${s.name}.json`,
"utf8",
);
if (
v31.includes(`"additionalProperties": {`) === true ||
v31.includes(`"additionalProperties": true`)
)
continue;
else if (model === "chatgpt") {
// CHATGPT DOES NOT SUPPORT TUPLE TYPE
const json: string = await fs.promises.readFile(
Expand Down
16 changes: 11 additions & 5 deletions test/build/internal/TestLlmSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ export namespace TestLlmSchemaGenerator {
) === false
)
continue;

const v31: string = await fs.promises.readFile(
`${__dirname}/../../schemas/json.schemas/v3_1/${s.name}.json`,
"utf8",
);
if (
v31.includes(`"additionalProperties": {`) === true ||
v31.includes(`"additionalProperties": true`)
)
continue;
else if (model === "chatgpt") {
// CHATGPT DOES NOT SUPPORT TUPLE TYPE
const json: string = await fs.promises.readFile(
`${__dirname}/../../schemas/json.schemas/v3_1/${s.name}.json`,
"utf8",
);
if (json.includes(`"prefixItems":`) === true) continue;
if (v31.includes(`"prefixItems":`) === true) continue;
} else if (model === "gemini") {
// GEMINI DOES NOT SUPPORT UNION TYPE
const json: string = await fs.promises.readFile(
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-7.0.0-dev.20241122.tgz"
"typia": "../typia-7.0.0-dev.20241123.tgz"
}
}
2 changes: 2 additions & 0 deletions test/schemas/json.application/v3_0/DynamicArray.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"value": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"type": "array",
"items": {
Expand All @@ -26,6 +27,7 @@
"value": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"type": "array",
"items": {
Expand Down
6 changes: 4 additions & 2 deletions test/schemas/json.application/v3_0/DynamicEnumeration.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"ru": {
"type": "string"
}
}
},
"required": []
}
},
"required": [
Expand Down Expand Up @@ -81,7 +82,8 @@
"ru": {
"type": "string"
}
}
},
"required": []
}
},
"required": [
Expand Down
4 changes: 3 additions & 1 deletion test/schemas/json.application/v3_0/DynamicNever.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"schemas": {
"DynamicNever": {
"type": "object",
"properties": {}
"properties": {},
"required": []
},
"DynamicNever.Nullable": {
"type": "object",
"properties": {},
"required": [],
"nullable": true
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/schemas/json.application/v3_0/DynamicSimple.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"value": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"type": "number"
}
Expand All @@ -23,6 +24,7 @@
"value": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"type": "number"
}
Expand Down
2 changes: 2 additions & 0 deletions test/schemas/json.application/v3_0/DynamicTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"DynamicTemplate": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"oneOf": [
{
Expand All @@ -22,6 +23,7 @@
"DynamicTemplate.Nullable": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"oneOf": [
{
Expand Down
1 change: 1 addition & 0 deletions test/schemas/json.application/v3_0/DynamicTree.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"RecordstringDynamicTree": {
"type": "object",
"properties": {},
"required": [],
"description": "Construct a type with a set of properties K of type T",
"additionalProperties": {
"$ref": "#/components/schemas/DynamicTree"
Expand Down
4 changes: 3 additions & 1 deletion test/schemas/json.application/v3_0/DynamicUndefined.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"schemas": {
"DynamicUndefined": {
"type": "object",
"properties": {}
"properties": {},
"required": []
},
"DynamicUndefined.Nullable": {
"type": "object",
"properties": {},
"required": [],
"nullable": true
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/schemas/json.application/v3_0/DynamicUnion.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"DynamicUnion": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"oneOf": [
{
Expand All @@ -19,6 +20,7 @@
"DynamicUnion.Nullable": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"oneOf": [
{
Expand Down
2 changes: 2 additions & 0 deletions test/schemas/json.application/v3_0/ObjectDynamic.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"ObjectDynamic": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"oneOf": [
{
Expand All @@ -22,6 +23,7 @@
"ObjectDynamic.Nullable": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"oneOf": [
{
Expand Down
4 changes: 3 additions & 1 deletion test/schemas/json.application/v3_0/ObjectOptional.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"sequence": {
"type": "number"
}
}
},
"required": []
},
"ObjectOptional.Nullable": {
"type": "object",
Expand All @@ -35,6 +36,7 @@
"type": "number"
}
},
"required": [],
"nullable": true
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/schemas/json.application/v3_0/ObjectPartial.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"$ref": "#/components/schemas/ObjectPartial.IBase.Nullable"
}
},
"required": [],
"description": "Make all properties in T optional"
},
"ObjectPartial.IBase": {
Expand Down Expand Up @@ -109,6 +110,7 @@
"$ref": "#/components/schemas/ObjectPartial.IBase.Nullable"
}
},
"required": [],
"description": "Make all properties in T optional",
"nullable": true
}
Expand Down
4 changes: 3 additions & 1 deletion test/schemas/json.application/v3_0/ObjectRequired.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"$ref": "#/components/schemas/ObjectRequired.IBase.Nullable"
}
},
"required": [],
"nullable": true
},
"RequiredObjectRequired.IBase": {
Expand Down Expand Up @@ -78,7 +79,8 @@
"object": {
"$ref": "#/components/schemas/ObjectRequired.IBase.Nullable"
}
}
},
"required": []
},
"RequiredObjectRequired.IBase.Nullable": {
"type": "object",
Expand Down
Loading

0 comments on commit 4cfc667

Please sign in to comment.