Skip to content

Commit 166da4b

Browse files
authored
ChatGptSchemaComposer.separateParameters added new property convention (#146)
1 parent 2930f4d commit 166da4b

6 files changed

+94
-54
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@samchon/openapi",
3-
"version": "2.5.2",
3+
"version": "2.5.3",
44
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.",
55
"main": "./lib/index.js",
66
"module": "./lib/index.mjs",

src/composers/llm/ChatGptSchemaComposer.ts

+42-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IOpenApiSchemaError } from "../../structures/IOpenApiSchemaError";
66
import { IResult } from "../../typings/IResult";
77
import { ChatGptTypeChecker } from "../../utils/ChatGptTypeChecker";
88
import { LlmTypeCheckerV3_1 } from "../../utils/LlmTypeCheckerV3_1";
9+
import { NamingConvention } from "../../utils/NamingConvention";
910
import { OpenApiTypeChecker } from "../../utils/OpenApiTypeChecker";
1011
import { LlmDescriptionInverter } from "./LlmDescriptionInverter";
1112
import { LlmSchemaV3_1Composer } from "./LlmSchemaV3_1Composer";
@@ -223,12 +224,17 @@ export namespace ChatGptSchemaComposer {
223224
SEPARATORS
224225
----------------------------------------------------------- */
225226
export const separateParameters = (props: {
226-
predicate: (schema: IChatGptSchema) => boolean;
227227
parameters: IChatGptSchema.IParameters;
228+
predicate: (schema: IChatGptSchema) => boolean;
229+
convention?: (key: string, type: "llm" | "human") => string;
228230
}): ILlmFunction.ISeparated<"chatgpt"> => {
231+
const convention =
232+
props.convention ??
233+
((key, type) => `${key}.${NamingConvention.capitalize(type)}`);
229234
const [llm, human] = separateObject({
230-
$defs: props.parameters.$defs,
231235
predicate: props.predicate,
236+
convention,
237+
$defs: props.parameters.$defs,
232238
schema: props.parameters,
233239
});
234240
if (llm === null || human === null)
@@ -269,8 +275,9 @@ export namespace ChatGptSchemaComposer {
269275
};
270276

271277
const separateStation = (props: {
272-
$defs: Record<string, IChatGptSchema>;
273278
predicate: (schema: IChatGptSchema) => boolean;
279+
convention: (key: string, type: "llm" | "human") => string;
280+
$defs: Record<string, IChatGptSchema>;
274281
schema: IChatGptSchema;
275282
}): [IChatGptSchema | null, IChatGptSchema | null] => {
276283
if (props.predicate(props.schema) === true) return [null, props.schema];
@@ -281,33 +288,38 @@ export namespace ChatGptSchemaComposer {
281288
return [props.schema, null];
282289
else if (ChatGptTypeChecker.isObject(props.schema))
283290
return separateObject({
284-
$defs: props.$defs,
285291
predicate: props.predicate,
292+
convention: props.convention,
293+
$defs: props.$defs,
286294
schema: props.schema,
287295
});
288296
else if (ChatGptTypeChecker.isArray(props.schema))
289297
return separateArray({
290-
$defs: props.$defs,
291298
predicate: props.predicate,
299+
convention: props.convention,
300+
$defs: props.$defs,
292301
schema: props.schema,
293302
});
294303
else if (ChatGptTypeChecker.isReference(props.schema))
295304
return separateReference({
296-
$defs: props.$defs,
297305
predicate: props.predicate,
306+
convention: props.convention,
307+
$defs: props.$defs,
298308
schema: props.schema,
299309
});
300310
return [props.schema, null];
301311
};
302312

303313
const separateArray = (props: {
304-
$defs: Record<string, IChatGptSchema>;
305314
predicate: (schema: IChatGptSchema) => boolean;
315+
convention: (key: string, type: "llm" | "human") => string;
316+
$defs: Record<string, IChatGptSchema>;
306317
schema: IChatGptSchema.IArray;
307318
}): [IChatGptSchema.IArray | null, IChatGptSchema.IArray | null] => {
308319
const [x, y] = separateStation({
309-
$defs: props.$defs,
310320
predicate: props.predicate,
321+
convention: props.convention,
322+
$defs: props.$defs,
311323
schema: props.schema.items,
312324
});
313325
return [
@@ -329,6 +341,7 @@ export namespace ChatGptSchemaComposer {
329341
const separateObject = (props: {
330342
$defs: Record<string, IChatGptSchema>;
331343
predicate: (schema: IChatGptSchema) => boolean;
344+
convention: (key: string, type: "llm" | "human") => string;
332345
schema: IChatGptSchema.IObject;
333346
}): [IChatGptSchema.IObject | null, IChatGptSchema.IObject | null] => {
334347
// EMPTY OBJECT
@@ -350,8 +363,9 @@ export namespace ChatGptSchemaComposer {
350363

351364
for (const [key, value] of Object.entries(props.schema.properties ?? {})) {
352365
const [x, y] = separateStation({
353-
$defs: props.$defs,
354366
predicate: props.predicate,
367+
convention: props.convention,
368+
$defs: props.$defs,
355369
schema: value,
356370
});
357371
if (x !== null) llm.properties[key] = x;
@@ -362,8 +376,9 @@ export namespace ChatGptSchemaComposer {
362376
props.schema.additionalProperties !== null
363377
) {
364378
const [dx, dy] = separateStation({
365-
$defs: props.$defs,
366379
predicate: props.predicate,
380+
convention: props.convention,
381+
$defs: props.$defs,
367382
schema: props.schema.additionalProperties,
368383
});
369384
llm.additionalProperties = dx ?? false;
@@ -380,45 +395,49 @@ export namespace ChatGptSchemaComposer {
380395
};
381396

382397
const separateReference = (props: {
383-
$defs: Record<string, IChatGptSchema>;
384398
predicate: (schema: IChatGptSchema) => boolean;
399+
convention: (key: string, type: "llm" | "human") => string;
400+
$defs: Record<string, IChatGptSchema>;
385401
schema: IChatGptSchema.IReference;
386402
}): [IChatGptSchema.IReference | null, IChatGptSchema.IReference | null] => {
387403
const key: string = props.schema.$ref.split("#/$defs/")[1];
404+
const humanKey: string = props.convention(key, "human");
405+
const llmKey: string = props.convention(key, "llm");
388406

389407
// FIND EXISTING
390-
if (props.$defs?.[`${key}.Human`] || props.$defs?.[`${key}.Llm`])
408+
if (props.$defs?.[humanKey] || props.$defs?.[llmKey])
391409
return [
392-
props.$defs?.[`${key}.Llm`]
410+
props.$defs?.[llmKey]
393411
? {
394412
...props.schema,
395-
$ref: `#/$defs/${key}.Llm`,
413+
$ref: `#/$defs/${llmKey}`,
396414
}
397415
: null,
398-
props.$defs?.[`${key}.Human`]
416+
props.$defs?.[humanKey]
399417
? {
400418
...props.schema,
401-
$ref: `#/$defs/${key}.Human`,
419+
$ref: `#/$defs/${humanKey}`,
402420
}
403421
: null,
404422
];
405423

406424
// PRE-ASSIGNMENT
407-
props.$defs![`${key}.Llm`] = {};
408-
props.$defs![`${key}.Human`] = {};
425+
props.$defs![llmKey] = {};
426+
props.$defs![humanKey] = {};
409427

410428
// DO COMPOSE
411429
const schema: IChatGptSchema = props.$defs?.[key]!;
412430
const [llm, human] = separateStation({
413-
$defs: props.$defs,
414431
predicate: props.predicate,
432+
convention: props.convention,
433+
$defs: props.$defs,
415434
schema,
416435
});
417436

418437
// ONLY ONE
419438
if (llm === null || human === null) {
420-
delete props.$defs[`${key}.Llm`];
421-
delete props.$defs[`${key}.Human`];
439+
delete props.$defs[llmKey];
440+
delete props.$defs[humanKey];
422441
return llm === null ? [null, props.schema] : [props.schema, null];
423442
}
424443

@@ -427,13 +446,13 @@ export namespace ChatGptSchemaComposer {
427446
llm !== null
428447
? {
429448
...props.schema,
430-
$ref: `#/$defs/${key}.Llm`,
449+
$ref: `#/$defs/${llmKey}`,
431450
}
432451
: null,
433452
human !== null
434453
? {
435454
...props.schema,
436-
$ref: `#/$defs/${key}.Human`,
455+
$ref: `#/$defs/${humanKey}`,
437456
}
438457
: null,
439458
];

src/composers/llm/ClaudeSchemaComposer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ export namespace ClaudeSchemaComposer {
4343
});
4444

4545
export const separateParameters = (props: {
46-
predicate: (schema: IClaudeSchema) => boolean;
4746
parameters: IClaudeSchema.IParameters;
47+
predicate: (schema: IClaudeSchema) => boolean;
48+
convention?: (key: string, type: "llm" | "human") => string;
4849
}): ILlmFunction.ISeparated<"claude"> => {
4950
const separated: ILlmFunction.ISeparated<"3.1"> =
5051
LlmSchemaV3_1Composer.separateParameters(props);

src/composers/llm/LlamaSchemaComposer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ export namespace LlamaSchemaComposer {
3939
});
4040

4141
export const separateParameters = (props: {
42-
predicate: (schema: ILlamaSchema) => boolean;
4342
parameters: ILlamaSchema.IParameters;
43+
predicate: (schema: ILlamaSchema) => boolean;
44+
convention?: (key: string, type: "llm" | "human") => string;
4445
}): ILlmFunction.ISeparated<"llama"> =>
4546
LlmSchemaV3_1Composer.separateParameters(
4647
props,

0 commit comments

Comments
 (0)