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

IChatGptSchema.IOneOf.discriminator #64

Merged
merged 1 commit into from
Nov 11, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samchon/openapi",
"version": "2.0.0-dev.20241111",
"version": "2.0.0-dev.20241111-2",
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
Expand Down
15 changes: 15 additions & 0 deletions src/converters/ChatGptConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ export namespace ChatGptConverter {
return {
...props.schema,
oneOf: oneOf.filter((v) => v !== null),
discriminator: props.schema.discriminator
? {
propertyName: props.schema.discriminator.propertyName,
mapping: props.schema.discriminator.mapping
? Object.fromEntries(
Object.entries(props.schema.discriminator.mapping).map(
([key, value]) => [
key,
value.replace("#/components/schemas/", "#/$defs/"),
],
),
)
: undefined,
}
: undefined,
};
}
return props.schema;
Expand Down
16 changes: 8 additions & 8 deletions src/structures/IChatGptSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export namespace IChatGptSchema {
* Exclusive minimum value restriction.
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined the `exclusiveMinimum` value as `number`, {@link OpenApi}
* defined the `exclusiveMinimum` value as `number`, {@link IChatGptSchema}
* forcibly converts it to `boolean` type, and assign the numeric value to
* the {@link minimum} property.
*/
Expand All @@ -77,7 +77,7 @@ export namespace IChatGptSchema {
* Exclusive maximum value restriction.
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined the `exclusiveMaximum` value as `number`, {@link OpenApi}
* defined the `exclusiveMaximum` value as `number`, {@link IChatGptSchema}
* forcibly converts it to `boolean` type, and assign the numeric value to
* the {@link maximum} property.
*/
Expand Down Expand Up @@ -375,11 +375,11 @@ export namespace IChatGptSchema {
*
* The `ref` is a reference to the named schema. Format of the `$ref` is
* following the JSON Pointer specification. In the OpenAPI, the `$ref`
* starts with `#/components/schemas/` which means the type is stored in
* the {@link OpenApi.IComponents.schemas} object.
* starts with `#/$defs/` which means the type is stored in
* the {@link IChatGptSchema.ITop.$defs} object.
*
* - `#/components/schemas/SomeObject`
* - `#/components/schemas/AnotherObject`
* - `#/$defs/SomeObject`
* - `#/$defs/AnotherObject`
*/
$ref: Key;
}
Expand All @@ -390,7 +390,7 @@ export namespace IChatGptSchema {
* IOneOf` represents an union type of the TypeScript (`A | B | C`).
*
* For reference, even though your Swagger (or OpenAPI) document has
* defined `anyOf` instead of the `oneOf`, {@link OpenApi} forcibly
* defined `anyOf` instead of the `oneOf`, {@link IChatGptSchema} forcibly
* converts it to `oneOf` type.
*/
export interface IOneOf extends __IAttribute {
Expand Down Expand Up @@ -420,7 +420,7 @@ export namespace IChatGptSchema {
* This property is valid only for {@link IReference} typed
* {@link IOneOf.oneof} elements. Therefore, `key` of `mapping` is
* the discriminator value, and `value` of `mapping` is the
* schema name like `#/components/schemas/SomeObject`.
* schema name like `#/$defs/SomeObject`.
*/
mapping?: Record<string, string>;
}
Expand Down
6 changes: 3 additions & 3 deletions src/structures/IHttpLlmApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface IHttpLlmApplication<
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema = IHttpLlmApplication.ModelSchema[Model],
Operation extends OpenApi.IOperation = OpenApi.IOperation,
Route extends IHttpMigrateRoute = IHttpMigrateRoute,
Expand Down Expand Up @@ -112,7 +112,7 @@ export namespace IHttpLlmApplication {
export type ModelSchema = {
"3.0": ILlmSchemaV3;
"3.1": ILlmSchemaV3_1;
chatgpt: IChatGptSchema;
chatgpt: IChatGptSchema.ITop;
gemini: IGeminiSchema;
};

Expand Down Expand Up @@ -166,7 +166,7 @@ export namespace IHttpLlmApplication {
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema = IHttpLlmApplication.ModelSchema[Model],
> {
/**
Expand Down
10 changes: 7 additions & 3 deletions src/structures/IHttpLlmFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ import { ILlmSchemaV3_1 } from "./ILlmSchemaV3_1";
* @author Jeongho Nam - https://github.com/samchon
*/
export interface IHttpLlmFunction<
Schema extends ILlmSchemaV3 | ILlmSchemaV3_1 | IChatGptSchema | IGeminiSchema,
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema.ITop
| IGeminiSchema,
Operation extends OpenApi.IOperation = OpenApi.IOperation,
Route extends IHttpMigrateRoute = IHttpMigrateRoute,
> {
Expand Down Expand Up @@ -231,7 +235,7 @@ export namespace IHttpLlmFunction {
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema,
> {
/**
Expand All @@ -252,7 +256,7 @@ export namespace IHttpLlmFunction {
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema,
> {
/**
Expand Down
6 changes: 3 additions & 3 deletions src/structures/ILlmApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ILlmApplication<
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema = ILlmApplication.ModelSchema[Model],
> {
/**
Expand All @@ -64,7 +64,7 @@ export namespace ILlmApplication {
export type ModelSchema = {
"3.0": ILlmSchemaV3;
"3.1": ILlmSchemaV3_1;
chatgpt: IChatGptSchema;
chatgpt: IChatGptSchema.ITop;
gemini: IGeminiSchema;
};

Expand All @@ -76,7 +76,7 @@ export namespace ILlmApplication {
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema = ILlmApplication.ModelSchema[Model],
> {
/**
Expand Down
10 changes: 7 additions & 3 deletions src/structures/ILlmFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import { ILlmSchemaV3_1 } from "./ILlmSchemaV3_1";
* @author Jeongho Nam - https://github.com/samchon
*/
export interface ILlmFunction<
Schema extends ILlmSchemaV3 | ILlmSchemaV3_1 | IChatGptSchema | IGeminiSchema,
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema.ITop
| IGeminiSchema,
> {
/**
* Representative name of the function.
Expand Down Expand Up @@ -88,7 +92,7 @@ export namespace ILlmFunction {
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema,
> {
/**
Expand All @@ -109,7 +113,7 @@ export namespace ILlmFunction {
Schema extends
| ILlmSchemaV3
| ILlmSchemaV3_1
| IChatGptSchema
| IChatGptSchema.ITop
| IGeminiSchema,
> {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { TestValidator } from "@nestia/e2e";
import { IChatGptSchema } from "@samchon/openapi";
import { ChatGptConverter } from "@samchon/openapi/lib/converters/ChatGptConverter";
import typia, { IJsonApplication } from "typia";

export const test_chatgpt_schema_oneof_discriminator = (): void => {
const collection: IJsonApplication =
typia.json.application<[IPoint | ILine | ITriangle | IRectangle]>();
const schema = ChatGptConverter.schema({
components: collection.components,
schema: collection.schemas[0],
});
TestValidator.equals("discriminator")({
oneOf: [
{
$ref: "#/$defs/IPoint",
},
{
$ref: "#/$defs/ILine",
},
{
$ref: "#/$defs/ITriangle",
},
{
$ref: "#/$defs/IRectangle",
},
],
discriminator: {
propertyName: "type",
mapping: {
point: "#/$defs/IPoint",
line: "#/$defs/ILine",
triangle: "#/$defs/ITriangle",
rectangle: "#/$defs/IRectangle",
},
},
} satisfies IChatGptSchema as IChatGptSchema)(schema as IChatGptSchema);
};

interface IPoint {
type: "point";
x: number;
y: number;
}
interface ILine {
type: "line";
p1: IPoint;
p2: IPoint;
}
interface ITriangle {
type: "triangle";
p1: IPoint;
p2: IPoint;
p3: IPoint;
}
interface IRectangle {
type: "rectangle";
p1: IPoint;
p2: IPoint;
p3: IPoint;
p4: IPoint;
}
19 changes: 19 additions & 0 deletions test/features/llm/v3.1/test_llm_schema_v31_ultimate_union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { OpenApi } from "@samchon/openapi";
import { HttpLlmConverter } from "@samchon/openapi/lib/converters/HttpLlmConverter";
import typia from "typia";

export const test_llm_schema_v31_ultimate_union = (): void => {
const collection = typia.json.application<[IJsonSchemaCollection[]]>();
HttpLlmConverter.schema({
model: "3.1",
components: collection.components,
schema: collection.schemas[0],
recursive: 3,
});
};

interface IJsonSchemaCollection {
version: "3.1";
components: OpenApi.IComponents;
schemas: OpenApi.IJsonSchema[];
}