typia 7.0.0-dev.20241123 → 7.0.0-dev.20241125
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/executable/typia.js +17 -7
- package/lib/executable/typia.js.map +1 -1
- package/lib/index.js +17 -7
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +7 -0
- package/lib/index.mjs.map +1 -1
- package/lib/internal/_llmApplicationFinalize.d.ts +1 -1
- package/lib/internal/_llmApplicationFinalize.js +4 -13
- package/lib/internal/_llmApplicationFinalize.js.map +1 -1
- package/lib/llm.d.ts +59 -4
- package/lib/llm.js +7 -0
- package/lib/llm.js.map +1 -1
- package/lib/module.js +17 -7
- package/lib/module.js.map +1 -1
- package/lib/programmers/llm/LlmApplicationProgrammer.js +13 -89
- package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
- package/lib/programmers/llm/LlmParametersProgrammer.d.ts +10 -0
- package/lib/programmers/llm/LlmParametersProgrammer.js +54 -0
- package/lib/programmers/llm/LlmParametersProgrammer.js.map +1 -0
- package/lib/programmers/llm/LlmSchemaProgrammer.d.ts +2 -2
- package/lib/programmers/llm/LlmSchemaProgrammer.js +4 -34
- package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
- package/lib/transformers/CallExpressionTransformer.js +2 -0
- package/lib/transformers/CallExpressionTransformer.js.map +1 -1
- package/lib/transformers/features/llm/LlmApplicationTransformer.js +7 -6
- package/lib/transformers/features/llm/LlmApplicationTransformer.js.map +1 -1
- package/lib/transformers/features/llm/LlmParametersTransformer.d.ts +5 -0
- package/lib/transformers/features/llm/LlmParametersTransformer.js +91 -0
- package/lib/transformers/features/llm/LlmParametersTransformer.js.map +1 -0
- package/lib/transformers/features/llm/LlmSchemaTransformer.js +7 -6
- package/lib/transformers/features/llm/LlmSchemaTransformer.js.map +1 -1
- package/package.json +2 -2
- package/src/internal/_llmApplicationFinalize.ts +7 -24
- package/src/llm.ts +74 -4
- package/src/programmers/llm/LlmApplicationProgrammer.ts +19 -92
- package/src/programmers/llm/LlmParametersProgrammer.ts +63 -0
- package/src/programmers/llm/LlmSchemaProgrammer.ts +12 -70
- package/src/transformers/CallExpressionTransformer.ts +2 -0
- package/src/transformers/features/llm/LlmApplicationTransformer.ts +14 -12
- package/src/transformers/features/llm/LlmParametersTransformer.ts +113 -0
- package/src/transformers/features/llm/LlmSchemaTransformer.ts +14 -12
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { GeminiConverter } from "@samchon/openapi/lib/converters/GeminiConverter";
|
|
4
|
-
import { LlmConverterV3 } from "@samchon/openapi/lib/converters/LlmConverterV3";
|
|
5
|
-
import { LlmConverterV3_1 } from "@samchon/openapi/lib/converters/LlmConverterV3_1";
|
|
1
|
+
import { ILlmApplication, OpenApi } from "@samchon/openapi";
|
|
2
|
+
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";
|
|
6
3
|
import { ILlmFunction } from "@samchon/openapi/lib/structures/ILlmFunction";
|
|
7
4
|
|
|
8
5
|
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
@@ -120,18 +117,10 @@ export namespace LlmApplicationProgrammer {
|
|
|
120
117
|
function: func,
|
|
121
118
|
}),
|
|
122
119
|
),
|
|
123
|
-
options:
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
constraint: false,
|
|
128
|
-
} satisfies ILlmApplication.IOptions<"chatgpt">)
|
|
129
|
-
: ({
|
|
130
|
-
separate: null,
|
|
131
|
-
recursive: props.model === "chatgpt" ? undefined : (3 as any),
|
|
132
|
-
} satisfies ILlmApplication.ICommonOptions<
|
|
133
|
-
Exclude<Model, "chatgpt">
|
|
134
|
-
>)) as ILlmApplication.IOptions<Model>,
|
|
120
|
+
options: {
|
|
121
|
+
...LlmSchemaConverter.defaultConfig(props.model),
|
|
122
|
+
separate: null,
|
|
123
|
+
},
|
|
135
124
|
};
|
|
136
125
|
};
|
|
137
126
|
|
|
@@ -201,33 +190,12 @@ export namespace LlmApplicationProgrammer {
|
|
|
201
190
|
.map((p) => p.name),
|
|
202
191
|
additionalProperties: false,
|
|
203
192
|
};
|
|
204
|
-
const parameters: ILlmApplication.ModelParameters[Model] | null =
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
});
|
|
211
|
-
else if (props.model === "gemini")
|
|
212
|
-
return GeminiConverter.parameters({
|
|
213
|
-
recursive: DEFAULT_V3_OPTIONS.recursive,
|
|
214
|
-
components: props.components,
|
|
215
|
-
schema,
|
|
216
|
-
}) as ILlmApplication.ModelParameters[Model] | null;
|
|
217
|
-
else if (props.model === "3.0")
|
|
218
|
-
return LlmConverterV3.parameters({
|
|
219
|
-
recursive: DEFAULT_V3_OPTIONS.recursive,
|
|
220
|
-
components: props.components,
|
|
221
|
-
schema,
|
|
222
|
-
}) as ILlmApplication.ModelParameters[Model] | null;
|
|
223
|
-
else if (props.model === "3.1")
|
|
224
|
-
return LlmConverterV3_1.parameters({
|
|
225
|
-
recursive: DEFAULT_V3_OPTIONS.recursive,
|
|
226
|
-
components: props.components,
|
|
227
|
-
schema,
|
|
228
|
-
}) as ILlmApplication.ModelParameters[Model] | null;
|
|
229
|
-
else return null;
|
|
230
|
-
})();
|
|
193
|
+
const parameters: ILlmApplication.ModelParameters[Model] | null =
|
|
194
|
+
LlmSchemaConverter.parameters(props.model)({
|
|
195
|
+
config: LlmSchemaConverter.defaultConfig(props.model) as any,
|
|
196
|
+
components: props.components,
|
|
197
|
+
schema,
|
|
198
|
+
}) as ILlmApplication.ModelParameters[Model] | null;
|
|
231
199
|
if (parameters === null)
|
|
232
200
|
throw new Error("Failed to write LLM application parameters.");
|
|
233
201
|
return parameters;
|
|
@@ -240,56 +208,15 @@ export namespace LlmApplicationProgrammer {
|
|
|
240
208
|
schema: OpenApi.IJsonSchema | null;
|
|
241
209
|
}): ILlmApplication.ModelSchema[Model] | null => {
|
|
242
210
|
if (props.schema === null) return null;
|
|
243
|
-
const output: ILlmApplication.ModelSchema[Model] | null =
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
$defs,
|
|
251
|
-
schema: props.schema,
|
|
252
|
-
});
|
|
253
|
-
if (
|
|
254
|
-
output !== null &&
|
|
255
|
-
(props.parameters as IChatGptSchema.IParameters).$defs ===
|
|
256
|
-
undefined &&
|
|
257
|
-
Object.keys($defs).length !== 0
|
|
258
|
-
)
|
|
259
|
-
(props.parameters as IChatGptSchema.IParameters).$defs = $defs;
|
|
260
|
-
return output;
|
|
261
|
-
} else if (props.model === "gemini")
|
|
262
|
-
return GeminiConverter.schema({
|
|
263
|
-
recursive: DEFAULT_V3_OPTIONS.recursive,
|
|
264
|
-
components: props.components,
|
|
265
|
-
schema: props.schema,
|
|
266
|
-
}) as ILlmApplication.ModelSchema[Model] | null;
|
|
267
|
-
else if (props.model === "3.0")
|
|
268
|
-
return LlmConverterV3.schema({
|
|
269
|
-
recursive: DEFAULT_V3_OPTIONS.recursive,
|
|
270
|
-
components: props.components,
|
|
271
|
-
schema: props.schema,
|
|
272
|
-
}) as ILlmApplication.ModelSchema[Model] | null;
|
|
273
|
-
else if (props.model === "3.1")
|
|
274
|
-
return LlmConverterV3_1.schema({
|
|
275
|
-
recursive: DEFAULT_V3_OPTIONS.recursive,
|
|
276
|
-
components: props.components,
|
|
277
|
-
schema: props.schema,
|
|
278
|
-
}) as ILlmApplication.ModelSchema[Model] | null;
|
|
279
|
-
else return null;
|
|
280
|
-
})();
|
|
211
|
+
const output: ILlmApplication.ModelSchema[Model] | null =
|
|
212
|
+
LlmSchemaConverter.schema(props.model)({
|
|
213
|
+
config: LlmSchemaConverter.defaultConfig(props.model) as any,
|
|
214
|
+
components: props.components,
|
|
215
|
+
schema: props.schema,
|
|
216
|
+
$defs: (props.parameters as any).$defs,
|
|
217
|
+
}) as ILlmApplication.ModelSchema[Model] | null;
|
|
281
218
|
if (output === null)
|
|
282
219
|
throw new Error("Failed to write LLM application output.");
|
|
283
220
|
return output;
|
|
284
221
|
};
|
|
285
222
|
}
|
|
286
|
-
|
|
287
|
-
const DEFAULT_CHATGPT_OPTION: ILlmApplication.IChatGptOptions = {
|
|
288
|
-
separate: null,
|
|
289
|
-
reference: false,
|
|
290
|
-
constraint: false,
|
|
291
|
-
};
|
|
292
|
-
const DEFAULT_V3_OPTIONS = {
|
|
293
|
-
separate: null,
|
|
294
|
-
recursive: 3,
|
|
295
|
-
} satisfies ILlmApplication.ICommonOptions<"3.0">;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ILlmApplication, OpenApi, OpenApiTypeChecker } from "@samchon/openapi";
|
|
2
|
+
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";
|
|
3
|
+
|
|
4
|
+
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
5
|
+
|
|
6
|
+
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
7
|
+
|
|
8
|
+
import { IJsonSchemaCollection } from "../../module";
|
|
9
|
+
import { JsonSchemasProgrammer } from "../json/JsonSchemasProgrammer";
|
|
10
|
+
import { LlmSchemaProgrammer } from "./LlmSchemaProgrammer";
|
|
11
|
+
|
|
12
|
+
export namespace LlmParametersProgrammer {
|
|
13
|
+
export const write = <Model extends ILlmApplication.Model>(props: {
|
|
14
|
+
model: Model;
|
|
15
|
+
metadata: Metadata;
|
|
16
|
+
}): ILlmApplication.ModelParameters[Model] => {
|
|
17
|
+
const collection: IJsonSchemaCollection<"3.1"> =
|
|
18
|
+
JsonSchemasProgrammer.write({
|
|
19
|
+
version: "3.1",
|
|
20
|
+
metadatas: [props.metadata],
|
|
21
|
+
});
|
|
22
|
+
const schema: OpenApi.IJsonSchema.IObject = (() => {
|
|
23
|
+
const schema: OpenApi.IJsonSchema = collection.schemas[0]!;
|
|
24
|
+
if (OpenApiTypeChecker.isObject(schema)) return schema;
|
|
25
|
+
else if (OpenApiTypeChecker.isReference(schema)) {
|
|
26
|
+
const last =
|
|
27
|
+
collection.components.schemas?.[schema.$ref.split("/").pop()!];
|
|
28
|
+
if (last && OpenApiTypeChecker.isObject(last)) return last;
|
|
29
|
+
}
|
|
30
|
+
throw new Error("Unreachable code. Failed to find the object schema.");
|
|
31
|
+
})();
|
|
32
|
+
|
|
33
|
+
const parameters: ILlmApplication.ModelParameters[Model] | null =
|
|
34
|
+
LlmSchemaConverter.parameters(props.model)({
|
|
35
|
+
config: LlmSchemaConverter.defaultConfig(props.model) as any,
|
|
36
|
+
components: collection.components,
|
|
37
|
+
schema,
|
|
38
|
+
}) as ILlmApplication.ModelParameters[Model] | null;
|
|
39
|
+
if (parameters === null)
|
|
40
|
+
throw new Error("Failed to convert JSON schema to LLM schema.");
|
|
41
|
+
return parameters;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const validate =
|
|
45
|
+
(model: ILlmApplication.Model) =>
|
|
46
|
+
(metadata: Metadata, explore: MetadataFactory.IExplore): string[] => {
|
|
47
|
+
const output: string[] = [];
|
|
48
|
+
if (explore.top === true) {
|
|
49
|
+
if (metadata.objects.length === 0)
|
|
50
|
+
output.push("LLM parameters must be an objec type.");
|
|
51
|
+
else if (metadata.objects.length !== 1 || metadata.size() > 1)
|
|
52
|
+
output.push("LLM parameters must be a single object type.");
|
|
53
|
+
else {
|
|
54
|
+
if (metadata.nullable)
|
|
55
|
+
output.push("LLM parameters must be a non-nullable object type.");
|
|
56
|
+
else if (metadata.isRequired() === false)
|
|
57
|
+
output.push("LLM parameters must be a non-undefined object type.");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
output.push(...LlmSchemaProgrammer.validate(model)(metadata));
|
|
61
|
+
return output;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
IHttpLlmApplication,
|
|
4
|
-
ILlmApplication,
|
|
5
|
-
OpenApi,
|
|
6
|
-
} from "@samchon/openapi";
|
|
7
|
-
import { ChatGptConverter } from "@samchon/openapi/lib/converters/ChatGptConverter";
|
|
8
|
-
import { GeminiConverter } from "@samchon/openapi/lib/converters/GeminiConverter";
|
|
9
|
-
import { LlmConverterV3 } from "@samchon/openapi/lib/converters/LlmConverterV3";
|
|
10
|
-
import { LlmConverterV3_1 } from "@samchon/openapi/lib/converters/LlmConverterV3_1";
|
|
1
|
+
import { ILlmApplication } from "@samchon/openapi";
|
|
2
|
+
import { LlmSchemaConverter } from "@samchon/openapi/lib/converters/LlmSchemaConverter";
|
|
11
3
|
|
|
12
4
|
import { IJsonSchemaCollection } from "../../schemas/json/IJsonSchemaCollection";
|
|
13
5
|
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
@@ -24,8 +16,9 @@ export namespace LlmSchemaProgrammer {
|
|
|
24
16
|
export interface IOutput<Model extends ILlmApplication.Model> {
|
|
25
17
|
model: Model;
|
|
26
18
|
schema: ILlmApplication.ModelSchema[Model];
|
|
27
|
-
$defs: Record<string,
|
|
19
|
+
$defs: Record<string, ILlmApplication.ModelSchema[Model]>;
|
|
28
20
|
}
|
|
21
|
+
|
|
29
22
|
export const write = <Model extends ILlmApplication.Model>(props: {
|
|
30
23
|
model: Model;
|
|
31
24
|
metadata: Metadata;
|
|
@@ -36,20 +29,14 @@ export namespace LlmSchemaProgrammer {
|
|
|
36
29
|
metadatas: [props.metadata],
|
|
37
30
|
});
|
|
38
31
|
|
|
39
|
-
const $defs: Record<string,
|
|
40
|
-
const schema: ILlmApplication.ModelSchema[Model] | null =
|
|
41
|
-
props.model
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
} satisfies Omit<ILlmApplication.IChatGptOptions, "separate"> &
|
|
48
|
-
Omit<ILlmApplication.ICommonOptions<any>, "separate"> as any,
|
|
49
|
-
components: collection.components,
|
|
50
|
-
schema: collection.schemas[0]!,
|
|
51
|
-
$defs,
|
|
52
|
-
}) as ILlmApplication.ModelSchema[Model] | null;
|
|
32
|
+
const $defs: Record<string, ILlmApplication.ModelSchema[Model]> = {};
|
|
33
|
+
const schema: ILlmApplication.ModelSchema[Model] | null =
|
|
34
|
+
LlmSchemaConverter.schema(props.model)({
|
|
35
|
+
config: LlmSchemaConverter.defaultConfig(props.model) as any,
|
|
36
|
+
components: collection.components,
|
|
37
|
+
schema: collection.schemas[0]!,
|
|
38
|
+
$defs: $defs as any,
|
|
39
|
+
}) as ILlmApplication.ModelSchema[Model] | null;
|
|
53
40
|
if (schema === null)
|
|
54
41
|
throw new Error("Failed to convert JSON schema to LLM schema.");
|
|
55
42
|
return {
|
|
@@ -138,48 +125,3 @@ const size = (metadata: Metadata): number =>
|
|
|
138
125
|
}).length,
|
|
139
126
|
)
|
|
140
127
|
.reduce((a, b) => a + b, 0);
|
|
141
|
-
|
|
142
|
-
const CASTERS = {
|
|
143
|
-
"3.0": (props: {
|
|
144
|
-
components: OpenApi.IComponents;
|
|
145
|
-
schema: OpenApi.IJsonSchema;
|
|
146
|
-
options: IHttpLlmApplication.IOptions<"3.0">;
|
|
147
|
-
}) =>
|
|
148
|
-
LlmConverterV3.schema({
|
|
149
|
-
components: props.components,
|
|
150
|
-
schema: props.schema,
|
|
151
|
-
recursive: props.options.recursive,
|
|
152
|
-
}),
|
|
153
|
-
"3.1": (props: {
|
|
154
|
-
components: OpenApi.IComponents;
|
|
155
|
-
schema: OpenApi.IJsonSchema;
|
|
156
|
-
options: IHttpLlmApplication.IOptions<"3.1">;
|
|
157
|
-
}) =>
|
|
158
|
-
LlmConverterV3_1.schema({
|
|
159
|
-
components: props.components,
|
|
160
|
-
schema: props.schema,
|
|
161
|
-
recursive: props.options.recursive,
|
|
162
|
-
}),
|
|
163
|
-
chatgpt: (props: {
|
|
164
|
-
components: OpenApi.IComponents;
|
|
165
|
-
schema: OpenApi.IJsonSchema;
|
|
166
|
-
$defs: Record<string, IChatGptSchema>;
|
|
167
|
-
options: Omit<IHttpLlmApplication.IChatGptOptions, "separate">;
|
|
168
|
-
}) =>
|
|
169
|
-
ChatGptConverter.schema({
|
|
170
|
-
components: props.components,
|
|
171
|
-
schema: props.schema,
|
|
172
|
-
$defs: props.$defs,
|
|
173
|
-
options: props.options,
|
|
174
|
-
}),
|
|
175
|
-
gemini: (props: {
|
|
176
|
-
components: OpenApi.IComponents;
|
|
177
|
-
schema: OpenApi.IJsonSchema;
|
|
178
|
-
options: IHttpLlmApplication.IOptions<"gemini">;
|
|
179
|
-
}) =>
|
|
180
|
-
GeminiConverter.schema({
|
|
181
|
-
components: props.components,
|
|
182
|
-
schema: props.schema,
|
|
183
|
-
recursive: props.options.recursive,
|
|
184
|
-
}),
|
|
185
|
-
};
|
|
@@ -67,6 +67,7 @@ import { JsonStringifyTransformer } from "./features/json/JsonStringifyTransform
|
|
|
67
67
|
import { JsonValidateParseTransformer } from "./features/json/JsonValidateParseTransformer";
|
|
68
68
|
import { JsonValidateStringifyTransformer } from "./features/json/JsonValidateStringifyTransformer";
|
|
69
69
|
import { LlmApplicationTransformer } from "./features/llm/LlmApplicationTransformer";
|
|
70
|
+
import { LlmParametersTransformer } from "./features/llm/LlmParametersTransformer";
|
|
70
71
|
import { LlmSchemaTransformer } from "./features/llm/LlmSchemaTransformer";
|
|
71
72
|
import { MiscAssertCloneTransformer } from "./features/misc/MiscAssertCloneTransformer";
|
|
72
73
|
import { MiscAssertPruneTransformer } from "./features/misc/MiscAssertPruneTransformer";
|
|
@@ -394,6 +395,7 @@ const FUNCTORS: Record<string, Record<string, () => Task>> = {
|
|
|
394
395
|
},
|
|
395
396
|
llm: {
|
|
396
397
|
application: () => LlmApplicationTransformer.transform,
|
|
398
|
+
parameters: () => LlmParametersTransformer.transform,
|
|
397
399
|
schema: () => LlmSchemaTransformer.transform,
|
|
398
400
|
},
|
|
399
401
|
json: {
|
|
@@ -29,17 +29,19 @@ export namespace LlmApplicationTransformer {
|
|
|
29
29
|
if (ts.isTypeNode(top) === false) return props.expression;
|
|
30
30
|
|
|
31
31
|
// GET MODEL
|
|
32
|
-
const model: ILlmApplication.Model =
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
value
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
32
|
+
const model: ILlmApplication.Model =
|
|
33
|
+
getTemplateArgument<ILlmApplication.Model>({
|
|
34
|
+
checker: props.context.checker,
|
|
35
|
+
name: "Model",
|
|
36
|
+
is: (value) =>
|
|
37
|
+
value === "chatgpt" ||
|
|
38
|
+
value === "gemini" ||
|
|
39
|
+
value === "claude" ||
|
|
40
|
+
value === "3.1" ||
|
|
41
|
+
value === "3.0",
|
|
42
|
+
cast: (value) => value as ILlmApplication.Model,
|
|
43
|
+
default: () => "3.1",
|
|
44
|
+
})(props.expression.typeArguments[1]);
|
|
43
45
|
|
|
44
46
|
// GET TYPE
|
|
45
47
|
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
@@ -101,7 +103,7 @@ export namespace LlmApplicationTransformer {
|
|
|
101
103
|
);
|
|
102
104
|
};
|
|
103
105
|
|
|
104
|
-
const
|
|
106
|
+
const getTemplateArgument =
|
|
105
107
|
<Value>(props: {
|
|
106
108
|
checker: ts.TypeChecker;
|
|
107
109
|
name: string;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { ILlmApplication } from "@samchon/openapi";
|
|
2
|
+
import { ILlmFunction } from "@samchon/openapi/lib/structures/ILlmFunction";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
import { LiteralFactory } from "../../../factories/LiteralFactory";
|
|
6
|
+
import { MetadataCollection } from "../../../factories/MetadataCollection";
|
|
7
|
+
import { MetadataFactory } from "../../../factories/MetadataFactory";
|
|
8
|
+
|
|
9
|
+
import { Metadata } from "../../../schemas/metadata/Metadata";
|
|
10
|
+
|
|
11
|
+
import { LlmParametersProgrammer } from "../../../programmers/llm/LlmParametersProgrammer";
|
|
12
|
+
import { LlmSchemaProgrammer } from "../../../programmers/llm/LlmSchemaProgrammer";
|
|
13
|
+
|
|
14
|
+
import { ValidationPipe } from "../../../typings/ValidationPipe";
|
|
15
|
+
|
|
16
|
+
import { ITransformProps } from "../../ITransformProps";
|
|
17
|
+
import { TransformerError } from "../../TransformerError";
|
|
18
|
+
|
|
19
|
+
export namespace LlmParametersTransformer {
|
|
20
|
+
export const transform = (
|
|
21
|
+
props: Omit<ITransformProps, "modulo">,
|
|
22
|
+
): ts.Expression => {
|
|
23
|
+
// GET GENERIC ARGUMENT
|
|
24
|
+
if (!props.expression.typeArguments?.length)
|
|
25
|
+
throw new TransformerError({
|
|
26
|
+
code: "typia.llm.parameters",
|
|
27
|
+
message: "no generic argument.",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const top: ts.Node = props.expression.typeArguments[0]!;
|
|
31
|
+
if (ts.isTypeNode(top) === false) return props.expression;
|
|
32
|
+
|
|
33
|
+
// GET MODEL
|
|
34
|
+
const model: ILlmApplication.Model =
|
|
35
|
+
getTemplateArgument<ILlmApplication.Model>({
|
|
36
|
+
checker: props.context.checker,
|
|
37
|
+
name: "Model",
|
|
38
|
+
is: (value) =>
|
|
39
|
+
value === "chatgpt" ||
|
|
40
|
+
value === "gemini" ||
|
|
41
|
+
value === "claude" ||
|
|
42
|
+
value === "3.1" ||
|
|
43
|
+
value === "3.0",
|
|
44
|
+
cast: (value) => value as ILlmApplication.Model,
|
|
45
|
+
default: () => "3.1",
|
|
46
|
+
})(props.expression.typeArguments[1]);
|
|
47
|
+
|
|
48
|
+
// GET TYPE
|
|
49
|
+
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
50
|
+
const collection: MetadataCollection = new MetadataCollection({
|
|
51
|
+
replace: MetadataCollection.replace,
|
|
52
|
+
});
|
|
53
|
+
const result: ValidationPipe<Metadata, MetadataFactory.IError> =
|
|
54
|
+
MetadataFactory.analyze({
|
|
55
|
+
checker: props.context.checker,
|
|
56
|
+
transformer: props.context.transformer,
|
|
57
|
+
options: {
|
|
58
|
+
escape: true,
|
|
59
|
+
constant: true,
|
|
60
|
+
absorb: false,
|
|
61
|
+
validate: LlmSchemaProgrammer.validate(model),
|
|
62
|
+
},
|
|
63
|
+
collection,
|
|
64
|
+
type,
|
|
65
|
+
});
|
|
66
|
+
if (result.success === false)
|
|
67
|
+
throw TransformerError.from({
|
|
68
|
+
code: "typia.llm.parameters",
|
|
69
|
+
errors: result.errors,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// GENERATE LLM SCHEMA
|
|
73
|
+
const out: ILlmFunction<any>["parameters"] = LlmParametersProgrammer.write({
|
|
74
|
+
model,
|
|
75
|
+
metadata: result.data,
|
|
76
|
+
});
|
|
77
|
+
return LiteralFactory.write(out);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const getTemplateArgument =
|
|
81
|
+
<Value>(props: {
|
|
82
|
+
checker: ts.TypeChecker;
|
|
83
|
+
name: string;
|
|
84
|
+
is: (value: string) => boolean;
|
|
85
|
+
cast: (value: string) => Value;
|
|
86
|
+
default: () => Value;
|
|
87
|
+
}) =>
|
|
88
|
+
(node: ts.TypeNode | undefined): Value => {
|
|
89
|
+
if (!node) return props.default();
|
|
90
|
+
|
|
91
|
+
// CHECK LITERAL TYPE
|
|
92
|
+
const type: ts.Type = props.checker.getTypeFromTypeNode(node);
|
|
93
|
+
if (
|
|
94
|
+
!type.isLiteral() &&
|
|
95
|
+
(type.getFlags() & ts.TypeFlags.BooleanLiteral) === 0
|
|
96
|
+
)
|
|
97
|
+
throw new TransformerError({
|
|
98
|
+
code: "typia.llm.parameters",
|
|
99
|
+
message: `generic argument "${props.name}" must be constant.`,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// GET VALUE AND VALIDATE IT
|
|
103
|
+
const value = type.isLiteral()
|
|
104
|
+
? type.value
|
|
105
|
+
: props.checker.typeToString(type);
|
|
106
|
+
if (typeof value !== "string" || props.is(value) === false)
|
|
107
|
+
throw new TransformerError({
|
|
108
|
+
code: "typia.llm.parameters",
|
|
109
|
+
message: `invalid value on generic argument "${props.name}".`,
|
|
110
|
+
});
|
|
111
|
+
return props.cast(value);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -30,17 +30,19 @@ export namespace LlmSchemaTransformer {
|
|
|
30
30
|
if (ts.isTypeNode(top) === false) return props.expression;
|
|
31
31
|
|
|
32
32
|
// GET MODEL
|
|
33
|
-
const model: ILlmApplication.Model =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
value
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
const model: ILlmApplication.Model =
|
|
34
|
+
getTemplateArgument<ILlmApplication.Model>({
|
|
35
|
+
checker: props.context.checker,
|
|
36
|
+
name: "Model",
|
|
37
|
+
is: (value) =>
|
|
38
|
+
value === "chatgpt" ||
|
|
39
|
+
value === "gemini" ||
|
|
40
|
+
value === "claude" ||
|
|
41
|
+
value === "3.1" ||
|
|
42
|
+
value === "3.0",
|
|
43
|
+
cast: (value) => value as ILlmApplication.Model,
|
|
44
|
+
default: () => "3.1",
|
|
45
|
+
})(props.expression.typeArguments[1]);
|
|
44
46
|
|
|
45
47
|
// GET TYPE
|
|
46
48
|
const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
|
|
@@ -108,7 +110,7 @@ export namespace LlmSchemaTransformer {
|
|
|
108
110
|
);
|
|
109
111
|
};
|
|
110
112
|
|
|
111
|
-
const
|
|
113
|
+
const getTemplateArgument =
|
|
112
114
|
<Value>(props: {
|
|
113
115
|
checker: ts.TypeChecker;
|
|
114
116
|
name: string;
|