Skip to content

Commit

Permalink
Merge pull request #1159 from samchon/feat/llm-validate
Browse files Browse the repository at this point in the history
Add LLM restrition option
  • Loading branch information
samchon authored Dec 16, 2024
2 parents 077dbb2 + f1b2aab commit 8981e06
Show file tree
Hide file tree
Showing 111 changed files with 2,974 additions and 39 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"reflect-metadata": "^0.2.2",
"tgrid": "^1.1.0",
"tstl": "^3.0.0",
"typia": "^7.4.0"
"typia": "^7.4.1"
},
"devDependencies": {
"@types/autocannon": "^7.9.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@nestia/station",
"version": "4.4.0",
"version": "4.4.1",
"description": "Nestia station",
"scripts": {
"build": "node deploy build",
Expand Down
2 changes: 1 addition & 1 deletion packages/benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ts-patch": "^3.3.0",
"typescript": "~5.7.2",
"typescript-transform-paths": "^3.4.7",
"typia": "^7.4.0",
"typia": "^7.4.1",
"uuid": "^10.0.0"
},
"dependencies": {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "4.4.0-dev.20241216-3",
"version": "4.4.1-dev.20241216",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -39,7 +39,7 @@
"@nestia/fetcher": "workspace:^",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@samchon/openapi": "^2.2.0",
"@samchon/openapi": "^2.2.1",
"detect-ts-node": "^1.0.5",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -48,16 +48,16 @@
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.3",
"tgrid": "^1.1.0",
"typia": "^7.4.0",
"typia": "^7.4.1",
"ws": "^7.5.3"
},
"peerDependencies": {
"@nestia/fetcher": ">=4.4.0-dev.20241216-3",
"@nestia/fetcher": ">=4.4.1-dev.20241216",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.3",
"typia": ">=7.4.0 <8.0.0"
"typia": ">=7.4.1 <8.0.0"
},
"devDependencies": {
"@nestjs/common": "^10.4.13",
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/options/INestiaTransformOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ILlmSchema } from "@samchon/openapi";

export interface INestiaTransformOptions {
validate?: INestiaTransformOptions.Validate;
stringify?: INestiaTransformOptions.Stringify | null;
llm?: INestiaTransformOptions.ILlm<"chatgpt" | "gemini" | "3.0">;
throws?: boolean;
}
export namespace INestiaTransformOptions {
Expand All @@ -26,4 +29,10 @@ export namespace INestiaTransformOptions {
| "is"
| "validate"
| "validate.log";

export interface ILlm<Model extends ILlmSchema.Model> {
model: Model;
strict?: Model extends "chatgpt" ? boolean : never;
recursive?: Model extends "gemini" | "3.0" ? false | number : never;
}
}
11 changes: 11 additions & 0 deletions packages/core/src/programmers/TypedBodyProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JsonMetadataFactory } from "typia/lib/factories/JsonMetadataFactory";
import { AssertProgrammer } from "typia/lib/programmers/AssertProgrammer";
import { IsProgrammer } from "typia/lib/programmers/IsProgrammer";
import { ValidateProgrammer } from "typia/lib/programmers/ValidateProgrammer";
import { LlmSchemaProgrammer } from "typia/lib/programmers/llm/LlmSchemaProgrammer";
import { MiscAssertCloneProgrammer } from "typia/lib/programmers/misc/MiscAssertCloneProgrammer";
import { MiscAssertPruneProgrammer } from "typia/lib/programmers/misc/MiscAssertPruneProgrammer";
import { MiscValidateCloneProgrammer } from "typia/lib/programmers/misc/MiscValidateCloneProgrammer";
Expand All @@ -11,6 +12,7 @@ import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";

import { INestiaTransformContext } from "../options/INestiaTransformProject";
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
import { LlmValidatePredicator } from "./internal/LlmValidatePredicator";

export namespace TypedBodyProgrammer {
export const generate = (props: {
Expand All @@ -24,6 +26,15 @@ export namespace TypedBodyProgrammer {
checker: props.context.checker,
transformer: props.context.transformer,
type: props.type,
validate: LlmValidatePredicator.is(props.context.options.llm)
? LlmSchemaProgrammer.validate({
model: props.context.options.llm.model,
config: {
strict: props.context.options.llm.strict,
recursive: props.context.options.llm.recursive,
},
})
: undefined,
});

// GENERATE VALIDATION PLAN
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/programmers/TypedQueryBodyProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
import ts from "typescript";
import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
import { HttpAssertQueryProgrammer } from "typia/lib/programmers/http/HttpAssertQueryProgrammer";
import { HttpIsQueryProgrammer } from "typia/lib/programmers/http/HttpIsQueryProgrammer";
import { HttpQueryProgrammer } from "typia/lib/programmers/http/HttpQueryProgrammer";
import { HttpValidateQueryProgrammer } from "typia/lib/programmers/http/HttpValidateQueryProgrammer";
import { LlmSchemaProgrammer } from "typia/lib/programmers/llm/LlmSchemaProgrammer";
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
import { TransformerError } from "typia/lib/transformers/TransformerError";

import { INestiaTransformContext } from "../options/INestiaTransformProject";
import { IRequestQueryValidator } from "../options/IRequestQueryValidator";
import { LlmValidatePredicator } from "./internal/LlmValidatePredicator";

export namespace TypedQueryBodyProgrammer {
export const generate = (props: {
context: INestiaTransformContext;
modulo: ts.LeftHandSideExpression;
type: ts.Type;
}): ts.ObjectLiteralExpression => {
// VALIDATE TYPE
if (LlmValidatePredicator.is(props.context.options.llm)) {
const result = MetadataFactory.analyze({
checker: props.context.checker,
transformer: props.context.transformer,
options: {
escape: false,
constant: true,
absorb: true,
validate: (meta, explore) => {
const errors: string[] = HttpQueryProgrammer.validate(
meta,
explore,
true,
);
errors.push(
...LlmSchemaProgrammer.validate({
model: props.context.options.llm!.model,
config: {
strict: props.context.options.llm!.strict,
recursive: props.context.options.llm!.recursive,
},
})(meta),
);
return errors;
},
},
collection: new MetadataCollection(),
type: props.type,
});
if (result.success === false)
throw TransformerError.from({
code: "@nestia.core.TypedQuery.Body",
errors: result.errors,
});
}

// GENERATE VALIDATION PLAN
const parameter =
(key: IRequestQueryValidator<any>["type"]) =>
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/programmers/TypedQueryProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
import ts from "typescript";
import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
import { HttpAssertQueryProgrammer } from "typia/lib/programmers/http/HttpAssertQueryProgrammer";
import { HttpIsQueryProgrammer } from "typia/lib/programmers/http/HttpIsQueryProgrammer";
import { HttpQueryProgrammer } from "typia/lib/programmers/http/HttpQueryProgrammer";
import { HttpValidateQueryProgrammer } from "typia/lib/programmers/http/HttpValidateQueryProgrammer";
import { LlmSchemaProgrammer } from "typia/lib/programmers/llm/LlmSchemaProgrammer";
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
import { TransformerError } from "typia/lib/transformers/TransformerError";

import { INestiaTransformContext } from "../options/INestiaTransformProject";
import { IRequestQueryValidator } from "../options/IRequestQueryValidator";
import { LlmValidatePredicator } from "./internal/LlmValidatePredicator";

export namespace TypedQueryProgrammer {
export const generate = (props: {
context: INestiaTransformContext;
modulo: ts.LeftHandSideExpression;
type: ts.Type;
}): ts.Expression => {
// VALIDATE TYPE
if (LlmValidatePredicator.is(props.context.options.llm)) {
const result = MetadataFactory.analyze({
checker: props.context.checker,
transformer: props.context.transformer,
options: {
escape: false,
constant: true,
absorb: true,
validate: (meta, explore) => {
const errors: string[] = HttpQueryProgrammer.validate(
meta,
explore,
true,
);
errors.push(
...LlmSchemaProgrammer.validate({
model: props.context.options.llm!.model,
config: {
strict: props.context.options.llm!.strict,
recursive: props.context.options.llm!.recursive,
},
})(meta),
);
return errors;
},
},
collection: new MetadataCollection(),
type: props.type,
});
if (result.success === false)
throw TransformerError.from({
code: "@nestia.core.TypedQuery",
errors: result.errors,
});
}

// GENERATE VALIDATION PLAN
const parameter =
(key: IRequestQueryValidator<any>["type"]) =>
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/programmers/TypedQueryRouteProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
import ts from "typescript";
import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
import { HttpQueryProgrammer } from "typia/lib/programmers/http/HttpQueryProgrammer";
import { LlmSchemaProgrammer } from "typia/lib/programmers/llm/LlmSchemaProgrammer";
import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
import { TransformerError } from "typia/lib/transformers/TransformerError";

import { INestiaTransformContext } from "../options/INestiaTransformProject";
import { HttpAssertQuerifyProgrammer } from "./http/HttpAssertQuerifyProgrammer";
import { HttpIsQuerifyProgrammer } from "./http/HttpIsQuerifyProgrammer";
import { HttpQuerifyProgrammer } from "./http/HttpQuerifyProgrammer";
import { HttpValidateQuerifyProgrammer } from "./http/HttpValidateQuerifyProgrammer";
import { LlmValidatePredicator } from "./internal/LlmValidatePredicator";

export namespace TypedQueryRouteProgrammer {
export const generate = (props: {
context: INestiaTransformContext;
modulo: ts.LeftHandSideExpression;
type: ts.Type;
}): ts.Expression => {
// VALIDATE TYPE
if (LlmValidatePredicator.is(props.context.options.llm)) {
const result = MetadataFactory.analyze({
checker: props.context.checker,
transformer: props.context.transformer,
options: {
escape: false,
constant: true,
absorb: true,
validate: (meta, explore) => {
const errors: string[] = HttpQueryProgrammer.validate(
meta,
explore,
true,
);
errors.push(
...LlmSchemaProgrammer.validate({
model: props.context.options.llm!.model,
config: {
strict: props.context.options.llm!.strict,
recursive: props.context.options.llm!.recursive,
},
})(meta),
);
return errors;
},
},
collection: new MetadataCollection(),
type: props.type,
});
if (result.success === false)
throw TransformerError.from({
code: props.modulo.getText(),
errors: result.errors,
});
}

// GENERATE STRINGIFY PLAN
const parameter = (
key: string,
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/programmers/TypedRouteProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import ts from "typescript";
import { JsonMetadataFactory } from "typia/lib/factories/JsonMetadataFactory";
import { JsonAssertStringifyProgrammer } from "typia/lib/programmers/json/JsonAssertStringifyProgrammer";
import { JsonIsStringifyProgrammer } from "typia/lib/programmers/json/JsonIsStringifyProgrammer";
import { JsonStringifyProgrammer } from "typia/lib/programmers/json/JsonStringifyProgrammer";
import { JsonValidateStringifyProgrammer } from "typia/lib/programmers/json/JsonValidateStringifyProgrammer";
import { LlmSchemaProgrammer } from "typia/lib/programmers/llm/LlmSchemaProgrammer";
import { IProgrammerProps } from "typia/lib/transformers/IProgrammerProps";

import { INestiaTransformContext } from "../options/INestiaTransformProject";
import { LlmValidatePredicator } from "./internal/LlmValidatePredicator";

export namespace TypedRouteProgrammer {
export const generate = (props: {
context: INestiaTransformContext;
modulo: ts.LeftHandSideExpression;
type: ts.Type;
}): ts.Expression => {
// VALIDATE TYPE
if (LlmValidatePredicator.is(props.context.options.llm))
JsonMetadataFactory.analyze({
method: "@nestia.core.TypedBody",
checker: props.context.checker,
transformer: props.context.transformer,
type: props.type,
validate: LlmSchemaProgrammer.validate({
model: props.context.options.llm.model,
config: {
strict: props.context.options.llm.strict,
recursive: props.context.options.llm.recursive,
},
}),
});

// GENERATE STRINGIFY PLAN
const parameter = (next: {
type: string;
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/programmers/internal/LlmValidatePredicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ILlmSchema } from "@samchon/openapi";

import { INestiaTransformOptions } from "../../options/INestiaTransformOptions";

export namespace LlmValidatePredicator {
export const is = <Model extends ILlmSchema.Model>(
llm: INestiaTransformOptions.ILlm<Model> | undefined,
): llm is INestiaTransformOptions.ILlm<Model> => {
if (llm?.model === undefined) return false;
else if (llm.model === "chatgpt") return llm.strict === true;
else if (llm.model === "gemini") return true;
else if (llm.model === "3.0")
return llm.recursive === false || llm.recursive === 0;
return false;
};
}
2 changes: 1 addition & 1 deletion packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ts-patch": "^3.3.0",
"typescript": "~5.7.2",
"typescript-transform-paths": "^3.4.7",
"typia": "^7.4.0"
"typia": "^7.4.1"
},
"files": [
"lib",
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"dependencies": {
"@mui/material": "^5.15.6",
"@nestia/migrate": "workspace:^",
"@samchon/openapi": "^2.2.0",
"@samchon/openapi": "^2.2.1",
"@stackblitz/sdk": "^1.11.0",
"js-yaml": "^4.1.0",
"prettier": "3.3.3",
"react-mui-fileuploader": "^0.5.2",
"typia": "^7.4.0"
"typia": "^7.4.1"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
Expand Down
Loading

0 comments on commit 8981e06

Please sign in to comment.