Skip to content

Commit

Permalink
typia.llm.schema<T>() function.
Browse files Browse the repository at this point in the history
Added `typia.llm.schema<T>()` function which generates type schema information of the LLM function callimg by TypeScript type.
  • Loading branch information
samchon committed Sep 6, 2024
1 parent cd6ed78 commit fe886ad
Show file tree
Hide file tree
Showing 239 changed files with 8,266 additions and 31 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-6.10.0-dev.20240823.tgz"
"typia": "../typia-6.10.0-dev.20240906.tgz"
}
}
2 changes: 1 addition & 1 deletion debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"tstl": "^3.0.0",
"typia": "../typia-6.8.0.tgz",
"typia": "../typia-6.10.0-dev.20240823.tgz",
"uuid": "^10.0.0"
}
}
26 changes: 26 additions & 0 deletions debug/src/llm-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ILlmSchema } from "@samchon/openapi";
import typia from "typia";

class Parent {
public toJSON(): Child {
return new Child();
}
}
class Child {
public readonly id: number = 1;
public readonly flag: boolean = true;

public toJSON(): IBrand {
return {
code: "code",
name: "name",
};
}
}
interface IBrand {
code: string;
name: string;
}

const schema: ILlmSchema = typia.llm.schema<Parent>();
console.log(schema);
6 changes: 0 additions & 6 deletions debug/src/pattern.ts

This file was deleted.

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-6.10.0-dev.20240823.tgz"
"typia": "../typia-6.10.0-dev.20240906.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": "6.10.0-dev.20240823",
"version": "6.10.0-dev.20240906",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -67,7 +67,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^0.4.9",
"@samchon/openapi": "^0.5.0-dev.20240906-2",
"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": "6.10.0-dev.20240823",
"version": "6.10.0-dev.20240906",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -63,7 +63,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "6.10.0-dev.20240823"
"typia": "6.10.0-dev.20240906"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.6.0"
Expand Down
4 changes: 2 additions & 2 deletions src/factories/internal/metadata/emplace_metadata_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export const emplace_metadata_object =
): MetadataProperty => {
// COMMENTS AND TAGS
const description: string | null = symbol
? CommentFactory.description(symbol) ?? null
? (CommentFactory.description(symbol) ?? null)
: null;
const jsDocTags: ts.JSDocTagInfo[] = (
symbol?.getJsDocTags() ?? []
).filter(filter ?? (() => true));

// THE PROPERTY
const property = MetadataProperty.create({
const property: MetadataProperty = MetadataProperty.create({
key,
value,
description,
Expand Down
20 changes: 20 additions & 0 deletions src/llm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ILlmSchema } from "@samchon/openapi";

export function schema(): never;
export function schema<T>(): ILlmSchema;

/**
* @internal
*/
export function schema(): never {
halt("schema");
}

/**
* @internal
*/
function halt(name: string): never {
throw new Error(
`Error on typia.llm.${name}(): no transform has been configured. Read and follow https://typia.io/docs/setup please.`,
);
}
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TypeGuardError } from "./TypeGuardError";

export * as functional from "./functional";
export * as http from "./http";
export * as llm from "./llm";
export * as json from "./json";
export * as misc from "./misc";
export * as notations from "./notations";
Expand Down
10 changes: 4 additions & 6 deletions src/programmers/internal/application_escaped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export const application_escaped =
<Version extends "3.0" | "3.1">(
generator: (meta: Metadata) => Schema<Version>,
) =>
(resolved: MetadataEscaped): Schema<Version>[] => {
const output: Schema<Version> | null = generator(resolved.returns);
(escaped: MetadataEscaped): Schema<Version>[] => {
const output: Schema<Version> | null = generator(escaped.returns);
if (output === null) return [];

if (is_date(new Set())(resolved.original)) {
if (is_date(new Set())(escaped.original)) {
const string: StringSchema<Version> | undefined = is_string(output)
? output
: is_one_of(output)
Expand All @@ -27,9 +27,7 @@ export const application_escaped =
)
string.format = "date-time";
}
return is_one_of(output)
? (output.oneOf as OneOfSchema<Version>[])
: [output];
return is_one_of(output) ? (output.oneOf as Schema<Version>[]) : [output];
};

/**
Expand Down
22 changes: 22 additions & 0 deletions src/programmers/internal/llm_schema_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ILlmSchema } from "@samchon/openapi";

import { MetadataArray } from "../../schemas/metadata/MetadataArray";

import { application_plugin } from "./application_plugin";
import { llm_schema_station } from "./llm_schema_station";

/**
* @internal
*/
export const llm_schema_array = (array: MetadataArray): ILlmSchema.IArray[] =>
application_plugin(
{
type: "array",
items: llm_schema_station({
metadata: array.type.value,
blockNever: false,
attribute: {},
}),
},
array.tags,
);
61 changes: 61 additions & 0 deletions src/programmers/internal/llm_schema_escaped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ILlmSchema } from "@samchon/openapi";

import { Metadata } from "../../schemas/metadata/Metadata";
import { MetadataEscaped } from "../../schemas/metadata/MetadataEscaped";

import { llm_schema_station } from "./llm_schema_station";

/**
* @internal
*/
export const llm_schema_escaped = (escaped: MetadataEscaped): ILlmSchema[] => {
const output: ILlmSchema | null = llm_schema_station({
metadata: escaped.returns,
blockNever: false,
attribute: {},
});
if (output === null) return [];
else if (is_date(new Set())(escaped.original)) {
const string: ILlmSchema.IString | undefined = is_string(output)
? output
: is_one_of(output)
? (output.oneOf.find(is_string) as ILlmSchema.IString)
: undefined;
if (
string !== undefined &&
string.format !== "date" &&
string.format !== "date-time"
)
string.format = "date-time";
}
return is_one_of(output) ? (output.oneOf as ILlmSchema[]) : [output];
};

/**
* @internal
*/
const is_string = (elem: ILlmSchema): elem is ILlmSchema.IString =>
(elem as ILlmSchema.IString).type === "string";

/**
* @internal
*/
const is_one_of = (elem: ILlmSchema): elem is ILlmSchema.IOneOf =>
Array.isArray((elem as ILlmSchema.IOneOf).oneOf);

/**
* @internal
*/
const is_date =
(visited: Set<Metadata>) =>
(meta: Metadata): boolean => {
if (visited.has(meta)) return false;
visited.add(meta);

return (
meta.natives.some((name) => name === "Date") ||
meta.arrays.some((array) => is_date(visited)(array.type.value)) ||
meta.tuples.some((tuple) => tuple.type.elements.some(is_date(visited))) ||
meta.aliases.some((alias) => is_date(visited)(alias.value))
);
};
17 changes: 17 additions & 0 deletions src/programmers/internal/llm_schema_native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ILlmSchema } from "@samchon/openapi";

/**
* @internal
*/
export const llm_schema_native = (
name: string,
): ILlmSchema.IString | ILlmSchema.IObject =>
name === "Blob" || name === "File"
? {
type: "string",
format: "binary",
}
: {
type: "object",
properties: {},
};
Loading

0 comments on commit fe886ad

Please sign in to comment.