đź“– Guide Documents
LLM Function Calling
schema() function

schema() function

typia
export namespace llm {
  // Individual type schema
  export function schema<T>(): ILlmSchema;
 
  // LLM function calling application schema
  export function application<App extends object>(
    options?: ILlmApplication.IOptions
  ): ILlmApplication;
}

Type schema in the LLM function calling application.

typia.llm.schema<T>() is a function generating type schema which is used in the LLM (Large Language Model) function calling application schema, especially in the typia.llm.application<App>() function.

Return value type ILlmSchema is similar with the JSON schema definition of OpenAPI v3.0 specification, but it does not have the reference type of OpenApi.IJsonSchema.IReference type which has the $ref property.

LLM Function Calling

LLM selects proper function and fill arguments.

In nowadays, most LLM (Large Language Model) like OpenAI are supporting "function calling" feature. The "LLM function calling" means that LLM automatically selects a proper function and fills parameter values from conversation with the user (may by chatting text).

https://platform.openai.com/docs/guides/function-calling (opens in a new tab)

Specialization

You can utilize type tags (or validator's comment tags) to constructing special fields of JSON schema.

If you write any comment on a property, it would fill the IJsonSchema.description value. Also, there're special comment tags only for JSON schema definition that are different with validator's comment tags like below.

  • @deprecated
  • @hidden
  • @internal
  • @title {string}
  • @default {value}

Let's see how those type tags, comment tags and description comments are working with example code.

examples/src/llm-schema.ts
import { ILlmSchema } from "@samchon/openapi";
import typia, { tags } from "typia";
 
export const schema: ILlmSchema = typia.llm.schema<Special>();
 
interface Special {
  /**
   * Deprecated tags are just used for marking.
   *
   * @title Unsigned integer
   * @deprecated
   */
  type: number & tags.Type<"int32">;
 
  /**
   * Internal tagged property never be shown in JSON schema.
   *
   * It even doesn't be shown in other `typia` functions like `assert<T>()`.
   *
   * @internal
   */
  internal: number[];
 
  /**
   * Hidden tagged property never be shown in JSON schema.
   *
   * However, it would be shown in other `typia` functions like `stringify<T>()`.
   *
   * @hidden
   */
  hidden: boolean;
 
  /**
   * You can limit the range of number.
   *
   * @exclusiveMinimum 19
   * @maximum 100
   * @default 30
   */
  number?: number;
 
  /**
   * You can limit the length of string.
   *
   * Also, multiple range conditions are also possible.
   */
  string: string &
    (
      | (tags.MinLength<3> & tags.MaxLength<24>)
      | (tags.MinLength<40> & tags.MaxLength<100>)
    );
 
  /**
   * You can limit the pattern of string.
   *
   * @pattern ^[a-z]+$
   */
  pattern: string;
 
  /**
   * You can limit the format of string.
   *
   * @format date-time
   */
  format: string | null;
 
  /**
   * In the Array case, possible to restrict its elements.
   */
  array: Array<string & tags.Format<"uuid">> & tags.MinItems<3>;
}

Customziation

If what you want is not just filling regular properties of LLM schema specification, but to adding custom properties into the JSON schema definition, you can do it through the tags.TagBase.schema property type or tags.JsonSchemaPlugin type.

For reference, the custom property must be started with x- prefix. It's a rule of LLM schema.

examples/src/llm-schema-custom.ts
import typia, { tags } from "typia";
 
type Monetary<Value extends string> = tags.TagBase<{
  target: "number";
  kind: "monetary";
  value: Value;
  schema: {
    "x-monetary": Value;
  };
}>;
 
type Placeholder<Value extends string> = tags.JsonSchemaPlugin<{
  "x-placeholder": Value;
}>;
 
interface IAccount {
  code: string & Placeholder<"Write you account code please">;
  balance: number & Monetary<"dollar">;
};
 
typia.llm.schema<IAccount>();

Restrictions

LLM schema does not support bigint type, and recursive type either.

LLM schema is based on the JSON schema definition of the OpenAPI v3.0 specification. Therefore, limitations of the JSON schema is also applied to the LLM schema, and the bigint type is not supported in the LLM function calling schema composition.

example/src/llm.schema.recursive.ts
import typia from "typia";
 
typia.llm.schema<bigint>();

Also, LLM schema does not support reference type that is embodied by the OpenApi.IJsonSchema.IReference type with $ref property. Therefore, if recursive type comes, no way to express it in the LLM schema, and it would be compilation error in the typia.llm.application<App>() function.

example/src/llm.schema.recursive.ts
import typia from "typia";
 
typia.llm.schema<Recursive>();
 
interface Recursive {
  id: number;
  children: Recursive[];
}

And if you put any type of native classes like Map or Uint8Array, it would also be error, either. By the way, only Date class type is exceptional, and it would be considered as string & Format<"date-time"> type like below.

example/src/llm.llm.date.ts
import typia from "typia";
 
typia.llm.schema<Date>();