Skip to content

Commit

Permalink
Enhance test programs for the LLM schemas.
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Nov 10, 2024
1 parent a5ad9d7 commit 57e375f
Show file tree
Hide file tree
Showing 909 changed files with 10,165 additions and 5,130 deletions.
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.20241110.tgz"
"typia": "../typia-7.0.0-dev.20241111.tgz"
}
}
2 changes: 1 addition & 1 deletion errors/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.20241110.tgz"
"typia": "../typia-7.0.0-dev.20241111.tgz"
}
}
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.20241110",
"version": "7.0.0-dev.20241111",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^2.0.0-dev.20241110-2",
"@samchon/openapi": "2.0.0-dev.20241111",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
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.20241110",
"version": "7.0.0-dev.20241111",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "7.0.0-dev.20241110"
"typia": "7.0.0-dev.20241111"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0"
Expand Down
16 changes: 8 additions & 8 deletions src/factories/MetadataCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ export class MetadataCollection {
const oldbie = this.objects_.get(type);
if (oldbie !== undefined) return [oldbie, false];

const $id: string = this.getName(checker, type);
const id: string = this.getName(checker, type);
const obj: MetadataObjectType = MetadataObjectType.create({
name: $id,
name: id,
properties: [],
description:
(type.aliasSymbol && CommentFactory.description(type.aliasSymbol)) ??
Expand All @@ -150,9 +150,9 @@ export class MetadataCollection {
const oldbie = this.aliases_.get(type);
if (oldbie !== undefined) return [oldbie, false, () => {}];

const $id: string = this.getName(checker, type);
const id: string = this.getName(checker, type);
const alias: MetadataAliasType = MetadataAliasType.create({
name: $id,
name: id,
value: null!,
description: CommentFactory.description(symbol) ?? null,
recursive: null!,
Expand All @@ -170,9 +170,9 @@ export class MetadataCollection {
const oldbie = this.arrays_.get(type);
if (oldbie !== undefined) return [oldbie, false, () => {}];

const $id = this.getName(checker, type);
const id = this.getName(checker, type);
const array: MetadataArrayType = MetadataArrayType.create({
name: $id,
name: id,
value: null!,
index: null,
recursive: null!,
Expand All @@ -189,9 +189,9 @@ export class MetadataCollection {
const oldbie = this.tuples_.get(type);
if (oldbie !== undefined) return [oldbie, false, () => {}];

const $id = this.getName(checker, type);
const id = this.getName(checker, type);
const tuple: MetadataTupleType = MetadataTupleType.create({
name: $id,
name: id,
elements: null!,
index: null,
recursive: null!,
Expand Down
14 changes: 9 additions & 5 deletions src/programmers/internal/json_schema_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ export const json_schema_array = (props: {
tags: props.array.tags,
});
if (props.array.type.recursive === true) {
const out = () => [{ $ref }];
const $ref: string = `#/components/schemas/${props.array.type.name}`;
if (props.components.schemas?.[$ref] !== undefined) return out();
const out = () => [
{
$ref: `#/components/schemas/${props.array.type.name}`,
},
];
if (props.components.schemas?.[props.array.type.name] !== undefined)
return out();

props.components.schemas ??= {};
props.components.schemas[$ref] ??= {};
props.components.schemas[props.array.type.name] ??= {};

const oneOf: OpenApi.IJsonSchema.IArray[] = factory();
Object.assign(
props.components.schemas[$ref]!,
props.components.schemas[props.array.type.name]!,
oneOf.length === 1 ? oneOf[0] : { oneOf },
);
return out();
Expand Down
6 changes: 3 additions & 3 deletions src/programmers/llm/LlmApplicationProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { MetadataObjectType } from "../../schemas/metadata/MetadataObjectType";
import { LlmSchemaProgrammer } from "./LlmSchemaProgrammer";

export namespace LlmApplicationProgrammer {
export const validate = () => {
export const validate = (model: ILlmApplication.Model) => {
let top: Metadata | undefined;
return (
metadata: Metadata,
Expand All @@ -28,7 +28,7 @@ export namespace LlmApplicationProgrammer {
metadata.functions.length === 1
)
return validateFunction(metadata.functions[0]!);
else return LlmSchemaProgrammer.validate(metadata);
else return LlmSchemaProgrammer.validate(model)(metadata);

const output: string[] = [];
const valid: boolean =
Expand Down Expand Up @@ -88,7 +88,7 @@ export namespace LlmApplicationProgrammer {
model: Model;
metadata: Metadata;
}): ILlmApplication<Model> => {
const errors: string[] = validate()(props.metadata, {
const errors: string[] = validate(props.model)(props.metadata, {
top: true,
object: null,
property: null,
Expand Down
81 changes: 48 additions & 33 deletions src/programmers/llm/LlmSchemaProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ILlmApplication } from "@samchon/openapi";
import { HttpLlmConverter } from "@samchon/openapi/lib/converters/HttpLlmConverter";
import { OpenApiV3Converter } from "@samchon/openapi/lib/converters/OpenApiV3Converter";

import { IJsonSchemaCollection } from "../../schemas/json/IJsonSchemaCollection";
import { Metadata } from "../../schemas/metadata/Metadata";
Expand All @@ -8,41 +9,55 @@ import { AtomicPredicator } from "../helpers/AtomicPredicator";
import { JsonSchemasProgrammer } from "../json/JsonSchemasProgrammer";

export namespace LlmSchemaProgrammer {
export const validate = (metadata: Metadata): string[] => {
const output: string[] = [];
if (
metadata.atomics.some((a) => a.type === "bigint") ||
metadata.constants.some((c) => c.type === "bigint")
)
output.push("LLM schema does not support bigint type.");
if (
metadata.tuples.some((t) =>
t.type.elements.some((e) => e.isRequired() === false),
) ||
metadata.arrays.some((a) => a.type.value.isRequired() === false)
)
output.push("LLM schema does not support undefined type in array.");
if (metadata.maps.length)
output.push("LLM schema does not support Map type.");
if (metadata.sets.length)
output.push("LLM schema does not support Set type.");
for (const native of metadata.natives)
export const validate =
(model: ILlmApplication.Model) =>
(metadata: Metadata): string[] => {
const output: string[] = [];
if (
AtomicPredicator.native(native.name) === false &&
native.name !== "Date" &&
native.name !== "Blob" &&
native.name !== "File"
metadata.atomics.some((a) => a.type === "bigint") ||
metadata.constants.some((c) => c.type === "bigint")
)
output.push(`LLM schema does not support ${native.name} type.`);
// if (
// metadata.aliases.some((a) => a.type.recursive) ||
// metadata.arrays.some((a) => a.type.recursive) ||
// metadata.objects.some((o) => o.type.recursive) ||
// metadata.tuples.some((t) => t.type.recursive)
// )
// output.push("LLM schema does not support recursive type.");
return output;
};
output.push("LLM schema does not support bigint type.");
if (
metadata.tuples.some((t) =>
t.type.elements.some((e) => e.isRequired() === false),
) ||
metadata.arrays.some((a) => a.type.value.isRequired() === false)
)
output.push("LLM schema does not support undefined type in array.");
if (metadata.maps.length)
output.push("LLM schema does not support Map type.");
if (metadata.sets.length)
output.push("LLM schema does not support Set type.");
for (const native of metadata.natives)
if (
AtomicPredicator.native(native.name) === false &&
native.name !== "Date" &&
native.name !== "Blob" &&
native.name !== "File"
)
output.push(`LLM schema does not support ${native.name} type.`);
if (model === "gemini") {
try {
const {
schemas: [schema],
} = JsonSchemasProgrammer.write({
version: "3.0",
metadatas: [metadata],
});
if (OpenApiV3Converter.TypeChecker.isOneOf(schema!))
output.push("Gemini model does not support the union type.");
} catch {}
}
// if (
// metadata.aliases.some((a) => a.type.recursive) ||
// metadata.arrays.some((a) => a.type.recursive) ||
// metadata.objects.some((o) => o.type.recursive) ||
// metadata.tuples.some((t) => t.type.recursive)
// )
// output.push("LLM schema does not support recursive type.");
return output;
};

export const write = <Model extends ILlmApplication.Model>(props: {
model: Model;
Expand Down
26 changes: 14 additions & 12 deletions src/transformers/features/llm/LlmApplicationTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export namespace LlmApplicationTransformer {
const top: ts.Node = props.expression.typeArguments[0]!;
if (ts.isTypeNode(top) === false) return props.expression;

// GET MODEL
const model: ILlmApplication.Model = get_parameter<ILlmApplication.Model>({
checker: props.context.checker,
name: "Model",
is: (value) =>
value === "3.1" ||
value === "3.0" ||
value === "chatgpt" ||
value === "gemini",
cast: (value) => value as ILlmApplication.Model,
default: () => "3.1",
})(props.expression.typeArguments[1]);

// GET TYPE
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
const collection: MetadataCollection = new MetadataCollection({
Expand All @@ -42,7 +55,7 @@ export namespace LlmApplicationTransformer {
constant: true,
absorb: false,
functional: true,
validate: LlmApplicationProgrammer.validate(),
validate: LlmApplicationProgrammer.validate(model),
},
collection,
type,
Expand All @@ -54,17 +67,6 @@ export namespace LlmApplicationTransformer {
});

// GENERATE LLM APPLICATION
const model: ILlmApplication.Model = get_parameter<ILlmApplication.Model>({
checker: props.context.checker,
name: "Model",
is: (value) =>
value === "3.1" ||
value === "3.0" ||
value === "chatgpt" ||
value === "gemini",
cast: (value) => value as ILlmApplication.Model,
default: () => "3.1",
})(props.expression.typeArguments[1]);
const schema: ILlmApplication<ILlmApplication.Model> =
LlmApplicationProgrammer.write({
model,
Expand Down
26 changes: 14 additions & 12 deletions src/transformers/features/llm/LlmSchemaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export namespace LlmSchemaTransformer {
const top: ts.Node = props.expression.typeArguments[0]!;
if (ts.isTypeNode(top) === false) return props.expression;

// GET MODEL
const model: ILlmApplication.Model = get_parameter<ILlmApplication.Model>({
checker: props.context.checker,
name: "Model",
is: (value) =>
value === "3.1" ||
value === "3.0" ||
value === "chatgpt" ||
value === "gemini",
cast: (value) => value as ILlmApplication.Model,
default: () => "3.1",
})(props.expression.typeArguments[1]);

// GET TYPE
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
const collection: MetadataCollection = new MetadataCollection({
Expand All @@ -41,7 +54,7 @@ export namespace LlmSchemaTransformer {
escape: true,
constant: true,
absorb: false,
validate: LlmSchemaProgrammer.validate,
validate: LlmSchemaProgrammer.validate(model),
},
collection,
type,
Expand All @@ -53,17 +66,6 @@ export namespace LlmSchemaTransformer {
});

// GENERATE LLM SCHEMA
const model: ILlmApplication.Model = get_parameter<ILlmApplication.Model>({
checker: props.context.checker,
name: "Model",
is: (value) =>
value === "3.1" ||
value === "3.0" ||
value === "chatgpt" ||
value === "gemini",
cast: (value) => value as ILlmApplication.Model,
default: () => "3.1",
})(props.expression.typeArguments[1]);
const schema: ILlmApplication.ModelSchema[ILlmApplication.Model] =
LlmSchemaProgrammer.write({
model,
Expand Down
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.20241110.tgz"
"typia": "../typia-7.0.0-dev.20241111.tgz"
}
}
13 changes: 9 additions & 4 deletions test/build/internal/TestLlmSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ export namespace TestLlmSchemaGenerator {
): Promise<void> {
for (const s of structures) {
if (s.JSONABLE === false) continue;
else if (s.RECURSIVE === true) continue;

else if (model !== "chatgpt" && s.RECURSIVE === true) continue;
else if (model === "gemini") {
const json: string = await fs.promises.readFile(
`${__dirname}/../../schemas/json/v3_0/${s.name}.json`,
"utf8",
);
if (json.includes(`"oneOf":`) === true) continue;
}
const content: string[] = [
`import typia from "typia";`,
`import { ${s.name} } from "../../../structures/${s.name}";`,
Expand Down Expand Up @@ -69,7 +75,6 @@ export namespace TestLlmSchemaGenerator {
`test_llm_schema_${model.replace(".", "_")}_`.length,
file.length - 3,
);
console.log(name);
const location: string =
__dirname +
`/../../bin/features/llm.schema/${model}/${file.slice(0, -3)}.js`;
Expand All @@ -90,4 +95,4 @@ export namespace TestLlmSchemaGenerator {
}
}

const MODELS = ["3.0", "3.1", "chatgpt"];
const MODELS = ["3.0", "3.1", "chatgpt", "gemini"];
Loading

0 comments on commit 57e375f

Please sign in to comment.