-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added `typia.llm.schema<T>()` function which generates type schema information of the LLM function callimg by TypeScript type.
- Loading branch information
Showing
239 changed files
with
8,266 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
}; |
Oops, something went wrong.