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

Fix parameters separator for empty object case. #108

Merged
merged 1 commit into from
Dec 12, 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.3",
"version": "2.0.4",
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
Expand Down
8 changes: 8 additions & 0 deletions src/composers/llm/ChatGptSchemaComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ export namespace ChatGptSchemaComposer {
predicate: (schema: IChatGptSchema) => boolean;
schema: IChatGptSchema.IObject;
}): [IChatGptSchema.IObject | null, IChatGptSchema.IObject | null] => {
// EMPTY OBJECT
if (
Object.keys(props.schema.properties ?? {}).length === 0 &&
!!props.schema.additionalProperties === false
)
return [props.schema, null];

const llm = {
...props.schema,
properties: {} as Record<string, IChatGptSchema>,
Expand All @@ -268,6 +275,7 @@ export namespace ChatGptSchemaComposer {
...props.schema,
properties: {} as Record<string, IChatGptSchema>,
} satisfies IChatGptSchema.IObject;

for (const [key, value] of Object.entries(props.schema.properties ?? {})) {
const [x, y] = separateStation({
$defs: props.$defs,
Expand Down
8 changes: 8 additions & 0 deletions src/composers/llm/LlmSchemaV3Composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ export namespace LlmSchemaV3Composer {
predicate: (schema: ILlmSchemaV3) => boolean;
schema: ILlmSchemaV3.IObject;
}): [ILlmSchemaV3.IObject | null, ILlmSchemaV3.IObject | null] => {
// EMPTY OBJECT
if (
Object.keys(props.schema.properties ?? {}).length === 0 &&
!!props.schema.additionalProperties === false
)
return [props.schema, null];

const llm = {
...props.schema,
properties: {} as Record<string, ILlmSchemaV3>,
Expand All @@ -229,6 +236,7 @@ export namespace LlmSchemaV3Composer {
properties: {} as Record<string, ILlmSchemaV3>,
additionalProperties: props.schema.additionalProperties,
} satisfies ILlmSchemaV3.IObject;

for (const [key, value] of Object.entries(props.schema.properties ?? {})) {
const [x, y] = separateStation({
predicate: props.predicate,
Expand Down
8 changes: 8 additions & 0 deletions src/composers/llm/LlmSchemaV3_1Composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ export namespace LlmSchemaV3_1Composer {
predicate: (schema: ILlmSchemaV3_1) => boolean;
schema: ILlmSchemaV3_1.IObject;
}): [ILlmSchemaV3_1.IObject | null, ILlmSchemaV3_1.IObject | null] => {
// EMPTY OBJECT
if (
Object.keys(props.schema.properties ?? {}).length === 0 &&
!!props.schema.additionalProperties === false
)
return [props.schema, null];

const llm = {
...props.schema,
properties: {} as Record<string, ILlmSchemaV3_1>,
Expand All @@ -433,6 +440,7 @@ export namespace LlmSchemaV3_1Composer {
...props.schema,
properties: {} as Record<string, ILlmSchemaV3_1>,
} satisfies ILlmSchemaV3_1.IObject;

for (const [key, value] of Object.entries(props.schema.properties ?? {})) {
const [x, y] = separateStation({
$defs: props.$defs,
Expand Down
64 changes: 64 additions & 0 deletions test/features/llm/validate_llm_schema_separate_object_empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { TestValidator } from "@nestia/e2e";
import {
ILlmSchema,
IOpenApiSchemaError,
IResult,
OpenApi,
OpenApiTypeChecker,
} from "@samchon/openapi";
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer";
import typia, { IJsonSchemaCollection } from "typia";

export const test_chatgpt_schema_separate_object_empty = (): void =>
validate_llm_schema_separate_object_empty("chatgpt");

export const test_claude_schema_separate_object_empty = (): void =>
validate_llm_schema_separate_object_empty("claude");

export const test_gemini_schema_separate_object_empty = (): void =>
validate_llm_schema_separate_object_empty("gemini");

export const test_llama_schema_separate_object_empty = (): void =>
validate_llm_schema_separate_object_empty("llama");

export const test_llm_v30_schema_separate_object_empty = (): void =>
validate_llm_schema_separate_object_empty("3.0");

export const test_llm_v31_schema_separate_object_empty = (): void =>
validate_llm_schema_separate_object_empty("3.1");

const validate_llm_schema_separate_object_empty = <
Model extends ILlmSchema.Model,
>(
model: Model,
): void => {
TestValidator.equals("separated")(
LlmSchemaComposer.separateParameters(model)({
predicate: ((schema: OpenApi.IJsonSchema) =>
OpenApiTypeChecker.isInteger(schema)) as any,
parameters: schema(model)(typia.json.schemas<[{}]>()) as any,
}),
)({
llm: schema(model)(typia.json.schemas<[{}]>()) as any,
human: null,
});
};

const schema =
<Model extends ILlmSchema.Model>(model: Model) =>
(collection: IJsonSchemaCollection): ILlmSchema.IParameters<Model> => {
const result: IResult<
ILlmSchema.IParameters<Model>,
IOpenApiSchemaError
> = LlmSchemaComposer.parameters(model)({
components: collection.components,
schema: typia.assert<
OpenApi.IJsonSchema.IObject | OpenApi.IJsonSchema.IReference
>(collection.schemas[0]),
config: LlmSchemaComposer.defaultConfig(
model,
) satisfies ILlmSchema.IConfig<Model> as any,
}) as IResult<ILlmSchema.IParameters<Model>, IOpenApiSchemaError>;
if (result.success === false) throw new Error("Invalid schema");
return result.value;
};
3 changes: 2 additions & 1 deletion test/utils/ChatGptFunctionCaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ export namespace ChatGptFunctionCaller {
name: props.name,
description: props.description,
parameters: parameters.value as Record<string, any>,
strict: true,
},
},
],
tool_choice: "required",
parallel_tool_calls: false,
});

const toolCalls: OpenAI.ChatCompletionMessageToolCall[] =
Expand Down
4 changes: 4 additions & 0 deletions test/utils/ClaudeFunctionCaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export namespace ClaudeFunctionCaller {
input_schema: parameters.value as any,
},
],
tool_choice: {
type: "any",
disable_parallel_tool_use: true,
},
});

const toolCalls: Anthropic.ToolUseBlock[] = completion.content.filter(
Expand Down
Loading