Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exact LLM schema types even in the generation mode. #1387

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "7.0.0-dev.20241126-2",
"version": "7.0.0-dev.20241127-2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -41,16 +41,15 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "2.0.0-dev.20241126",
"@samchon/openapi": "2.0.0-dev.20241127-2",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
"package-manager-detector": "^0.2.0",
"randexp": "^0.5.3"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0",
"@samchon/openapi": ">=1.2.4 <2.0.0"
"typescript": ">=4.8.0 <5.7.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^26.0.1",
Expand Down
7 changes: 3 additions & 4 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.20241126-2",
"version": "7.0.0-dev.20241127",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -37,11 +37,10 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "7.0.0-dev.20241126-2"
"typia": "7.0.0-dev.20241127"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0",
"@samchon/openapi": ">=1.2.4 <2.0.0"
"typescript": ">=4.8.0 <5.7.0"
},
"stackblitz": {
"startCommand": "npm install && npm run test"
Expand Down
4 changes: 2 additions & 2 deletions src/internal/_llmApplicationFinalize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ILlmApplication } from "@samchon/openapi";
import { ILlmApplication, ILlmSchema } from "@samchon/openapi";
import { HttpLlmConverter } from "@samchon/openapi/lib/converters/HttpLlmConverter";
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";

export const _llmApplicationFinalize = <Model extends ILlmApplication.Model>(
export const _llmApplicationFinalize = <Model extends ILlmSchema.Model>(
app: ILlmApplication<Model>,
options?: Partial<Pick<ILlmApplication.IOptions<Model>, "separate">>,
): void => {
Expand Down
19 changes: 8 additions & 11 deletions src/llm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ILlmApplication } from "@samchon/openapi";
import { ILlmApplication, ILlmSchema } from "@samchon/openapi";

/**
* > You must configure the generic argument `App`.
Expand Down Expand Up @@ -80,10 +80,7 @@ export function application(
* @reference https://platform.openai.com/docs/guides/function-calling
* @author Jeongho Nam - https://github.com/samchon
*/
export function application<
App extends object,
Model extends ILlmApplication.Model,
>(
export function application<App extends object, Model extends ILlmSchema.Model>(
options?: Partial<Pick<ILlmApplication.IOptions<Model>, "separate">>,
): ILlmApplication<Model>;

Expand Down Expand Up @@ -148,8 +145,8 @@ export function parameters(): never;
*/
export function parameters<
Parameters extends object,
Model extends ILlmApplication.Model,
>(): ILlmApplication.ModelSchema[Model];
Model extends ILlmSchema.Model,
>(): ILlmSchema.ModelParameters[Model];

/**
* @internal
Expand Down Expand Up @@ -234,14 +231,14 @@ export function schema(): never;
* @reference https://platform.openai.com/docs/guides/function-calling
* @author Jeongho Nam - https://github.com/samchon
*/
export function schema<T, Model extends ILlmApplication.Model>(
export function schema<T, Model extends ILlmSchema.Model>(
...$defs: Extract<
ILlmApplication.ModelSchema[Model],
ILlmSchema.ModelSchema[Model],
{ $ref: string }
> extends never
? []
: [Record<string, ILlmApplication.ModelSchema[Model]>]
): ILlmApplication.ModelSchema[Model];
: [Record<string, ILlmSchema.ModelSchema[Model]>]
): ILlmSchema.ModelSchema[Model];

/**
* @internal
Expand Down
4 changes: 3 additions & 1 deletion src/programmers/ImportProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ export class ImportProgrammer {
[...asset.instances.values()].map((ins) =>
ts.factory.createImportSpecifier(
ins.type,
undefined,
ins.alias || ins.alias === ins.name
? ts.factory.createIdentifier(ins.name)
: undefined,
ts.factory.createIdentifier(ins.alias ?? ins.name),
),
),
Expand Down
34 changes: 17 additions & 17 deletions src/programmers/llm/LlmApplicationProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ILlmApplication, OpenApi } from "@samchon/openapi";
import { ILlmApplication, ILlmSchema, OpenApi } from "@samchon/openapi";
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";
import { ILlmFunction } from "@samchon/openapi/lib/structures/ILlmFunction";

Expand All @@ -13,7 +13,7 @@ import { JsonApplicationProgrammer } from "../json/JsonApplicationProgrammer";
import { LlmSchemaProgrammer } from "./LlmSchemaProgrammer";

export namespace LlmApplicationProgrammer {
export const validate = (model: ILlmApplication.Model) => {
export const validate = (model: ILlmSchema.Model) => {
let top: Metadata | undefined;
return (
metadata: Metadata,
Expand Down Expand Up @@ -86,7 +86,7 @@ export namespace LlmApplicationProgrammer {
return output;
};

export const write = <Model extends ILlmApplication.Model>(props: {
export const write = <Model extends ILlmSchema.Model>(props: {
model: Model;
metadata: Metadata;
}): ILlmApplication<Model> => {
Expand Down Expand Up @@ -124,14 +124,14 @@ export namespace LlmApplicationProgrammer {
};
};

const writeFunction = <Model extends ILlmApplication.Model>(props: {
const writeFunction = <Model extends ILlmSchema.Model>(props: {
model: Model;
components: OpenApi.IComponents;
function: __IJsonApplication.IFunction<OpenApi.IJsonSchema>;
}): ILlmFunction<ILlmApplication.ModelParameters[Model]> => {
const parameters: ILlmApplication.ModelParameters[Model] =
}): ILlmFunction<Model> => {
const parameters: ILlmSchema.ModelParameters[Model] =
writeParameters(props);
const output: ILlmApplication.ModelSchema[Model] | null = writeOutput({
const output: ILlmSchema.ModelSchema[Model] | null = writeOutput({
model: props.model,
parameters,
components: props.components,
Expand All @@ -147,7 +147,7 @@ export namespace LlmApplicationProgrammer {
name: props.function.name,
parameters,
output: (output ?? undefined) as
| ILlmApplication.ModelParameters[Model]["properties"][string]
| ILlmSchema.ModelSchema[Model]
| undefined,
description: (() => {
if (
Expand All @@ -168,11 +168,11 @@ export namespace LlmApplicationProgrammer {
};
};

const writeParameters = <Model extends ILlmApplication.Model>(props: {
const writeParameters = <Model extends ILlmSchema.Model>(props: {
model: Model;
components: OpenApi.IComponents;
function: __IJsonApplication.IFunction<OpenApi.IJsonSchema>;
}): ILlmApplication.ModelParameters[Model] => {
}): ILlmSchema.ModelParameters[Model] => {
const schema: OpenApi.IJsonSchema.IObject = {
type: "object",
properties: Object.fromEntries(
Expand All @@ -190,31 +190,31 @@ export namespace LlmApplicationProgrammer {
.map((p) => p.name),
additionalProperties: false,
};
const parameters: ILlmApplication.ModelParameters[Model] | null =
const parameters: ILlmSchema.ModelParameters[Model] | null =
LlmSchemaConverter.parameters(props.model)({
config: LlmSchemaConverter.defaultConfig(props.model) as any,
components: props.components,
schema,
}) as ILlmApplication.ModelParameters[Model] | null;
}) as ILlmSchema.ModelParameters[Model] | null;
if (parameters === null)
throw new Error("Failed to write LLM application parameters.");
return parameters;
};

const writeOutput = <Model extends ILlmApplication.Model>(props: {
const writeOutput = <Model extends ILlmSchema.Model>(props: {
model: Model;
parameters: ILlmApplication.ModelParameters[Model];
parameters: ILlmSchema.ModelParameters[Model];
components: OpenApi.IComponents;
schema: OpenApi.IJsonSchema | null;
}): ILlmApplication.ModelSchema[Model] | null => {
}): ILlmSchema.ModelSchema[Model] | null => {
if (props.schema === null) return null;
const output: ILlmApplication.ModelSchema[Model] | null =
const output: ILlmSchema.ModelSchema[Model] | null =
LlmSchemaConverter.schema(props.model)({
config: LlmSchemaConverter.defaultConfig(props.model) as any,
components: props.components,
schema: props.schema,
$defs: (props.parameters as any).$defs,
}) as ILlmApplication.ModelSchema[Model] | null;
}) as ILlmSchema.ModelSchema[Model] | null;
if (output === null)
throw new Error("Failed to write LLM application output.");
return output;
Expand Down
12 changes: 6 additions & 6 deletions src/programmers/llm/LlmParametersProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ILlmApplication, OpenApi, OpenApiTypeChecker } from "@samchon/openapi";
import { ILlmSchema, OpenApi, OpenApiTypeChecker } from "@samchon/openapi";
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";

import { MetadataFactory } from "../../factories/MetadataFactory";
Expand All @@ -10,10 +10,10 @@ import { JsonSchemasProgrammer } from "../json/JsonSchemasProgrammer";
import { LlmSchemaProgrammer } from "./LlmSchemaProgrammer";

export namespace LlmParametersProgrammer {
export const write = <Model extends ILlmApplication.Model>(props: {
export const write = <Model extends ILlmSchema.Model>(props: {
model: Model;
metadata: Metadata;
}): ILlmApplication.ModelParameters[Model] => {
}): ILlmSchema.ModelParameters[Model] => {
const collection: IJsonSchemaCollection<"3.1"> =
JsonSchemasProgrammer.write({
version: "3.1",
Expand All @@ -30,19 +30,19 @@ export namespace LlmParametersProgrammer {
throw new Error("Unreachable code. Failed to find the object schema.");
})();

const parameters: ILlmApplication.ModelParameters[Model] | null =
const parameters: ILlmSchema.ModelParameters[Model] | null =
LlmSchemaConverter.parameters(props.model)({
config: LlmSchemaConverter.defaultConfig(props.model) as any,
components: collection.components,
schema,
}) as ILlmApplication.ModelParameters[Model] | null;
}) as ILlmSchema.ModelParameters[Model] | null;
if (parameters === null)
throw new Error("Failed to convert JSON schema to LLM schema.");
return parameters;
};

export const validate =
(model: ILlmApplication.Model) =>
(model: ILlmSchema.Model) =>
(metadata: Metadata, explore: MetadataFactory.IExplore): string[] => {
const output: string[] = [];
if (explore.top === true) {
Expand Down
18 changes: 9 additions & 9 deletions src/programmers/llm/LlmSchemaProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ILlmApplication } from "@samchon/openapi";
import { ILlmSchema } from "@samchon/openapi";
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";

import { IJsonSchemaCollection } from "../../schemas/json/IJsonSchemaCollection";
Expand All @@ -13,13 +13,13 @@ import { json_schema_string } from "../internal/json_schema_string";
import { JsonSchemasProgrammer } from "../json/JsonSchemasProgrammer";

export namespace LlmSchemaProgrammer {
export interface IOutput<Model extends ILlmApplication.Model> {
export interface IOutput<Model extends ILlmSchema.Model> {
model: Model;
schema: ILlmApplication.ModelSchema[Model];
$defs: Record<string, ILlmApplication.ModelSchema[Model]>;
schema: ILlmSchema.ModelSchema[Model];
$defs: Record<string, ILlmSchema.ModelSchema[Model]>;
}

export const write = <Model extends ILlmApplication.Model>(props: {
export const write = <Model extends ILlmSchema.Model>(props: {
model: Model;
metadata: Metadata;
}): IOutput<Model> => {
Expand All @@ -29,14 +29,14 @@ export namespace LlmSchemaProgrammer {
metadatas: [props.metadata],
});

const $defs: Record<string, ILlmApplication.ModelSchema[Model]> = {};
const schema: ILlmApplication.ModelSchema[Model] | null =
const $defs: Record<string, ILlmSchema.ModelSchema[Model]> = {};
const schema: ILlmSchema.ModelSchema[Model] | null =
LlmSchemaConverter.schema(props.model)({
config: LlmSchemaConverter.defaultConfig(props.model) as any,
components: collection.components,
schema: collection.schemas[0]!,
$defs: $defs as any,
}) as ILlmApplication.ModelSchema[Model] | null;
}) as ILlmSchema.ModelSchema[Model] | null;
if (schema === null)
throw new Error("Failed to convert JSON schema to LLM schema.");
return {
Expand All @@ -47,7 +47,7 @@ export namespace LlmSchemaProgrammer {
};

export const validate =
(model: ILlmApplication.Model) =>
(model: ILlmSchema.Model) =>
(metadata: Metadata): string[] => {
const output: string[] = [];
if (
Expand Down
17 changes: 16 additions & 1 deletion src/transformers/features/json/JsonSchemasTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,22 @@ export namespace JsonSchemasTransformer {
version,
metadatas,
});
return LiteralFactory.write(app);
return ts.factory.createAsExpression(
LiteralFactory.write(app),
ts.factory.createTypeReferenceNode(
props.context.importer.instance({
name: "IJsonSchemaCollection",
file: "typia",
type: true,
alias: "__IJsonSchemaCollection",
}).text,
[
ts.factory.createLiteralTypeNode(
ts.factory.createStringLiteral(version),
),
],
),
);
};

const get_parameter =
Expand Down
Loading