-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.ts
More file actions
108 lines (101 loc) · 3.01 KB
/
Copy pathgenerate.ts
File metadata and controls
108 lines (101 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
AutoViewAgent,
IAutoViewResult,
IAutoViewVendor,
} from "@autoview/agent";
import {
HttpLlm,
IHttpLlmApplication,
IHttpLlmFunction,
OpenApi,
} from "@samchon/openapi";
import fs from "fs";
import OpenAI from "openai";
import typia from "typia";
import { YourSchema } from "./YourSchema.js";
import env from "./env.js";
import { assertApiKey } from "./internal/assertApiKey.js";
const generateForTsType = async (vendor: IAutoViewVendor): Promise<void> => {
const agent = new AutoViewAgent({
vendor,
input: {
type: "json-schema",
unit: typia.json.schema<YourSchema>(),
},
transformFunctionName: "transformYourSchema",
experimentalAllInOne: true,
});
const result: IAutoViewResult = await agent.generate();
await fs.promises.writeFile(
"src/transformYourSchema.ts",
result.transformTsCode,
"utf8",
);
};
const generateForSwagger = async (vendor: IAutoViewVendor): Promise<void> => {
// GET SWAGGER SCHEMA INFORMATION
const document: OpenApi.IDocument = OpenApi.convert(
await fetch(
"https://raw.githubusercontent.com/samchon/shopping-backend/refs/heads/master/packages/api/swagger.json",
).then((r) => r.json()),
);
// CONVERT TO LLM FUNCTION CALLING SCHEMA
const app: IHttpLlmApplication<"chatgpt"> = HttpLlm.application({
model: "chatgpt",
document,
options: {
reference: true,
},
});
const page: IHttpLlmFunction<"chatgpt"> | undefined = app.functions.find(
(func) =>
func.path === "/shoppings/customers/sales" && func.method === "patch",
);
const sale: IHttpLlmFunction<"chatgpt"> | undefined = app.functions.find(
(func) =>
func.path === "/shoppings/customers/sales/{id}" && func.method === "get",
);
if (page === undefined || sale === undefined) {
console.error("Operation not found");
process.exit(-1);
}
// GENERATE
await Promise.all(
Object.entries({
page,
sale,
}).map(async ([key, func]) => {
const name: string = `transform${key[0].toUpperCase()}${key.slice(1)}`;
const agent: AutoViewAgent = new AutoViewAgent({
vendor,
input: {
type: "llm-schema",
model: "chatgpt",
schema: func.output!,
$defs: func.parameters.$defs,
},
instruction:
key === "page" ? "use carousel layout for each item" : undefined,
transformFunctionName: name,
experimentalAllInOne: true,
});
const result: IAutoViewResult = await agent.generate();
await fs.promises.writeFile(
`src/transform${key[0].toUpperCase()}${key.slice(1)}.ts`,
result.transformTsCode,
"utf8",
);
}),
);
};
const main = async (): Promise<void> => {
assertApiKey();
const vendor: IAutoViewVendor = {
api: new OpenAI({
apiKey: typia.assert<string>(env.OPENAI_API_KEY),
}),
model: typia.assert<OpenAI.ChatModel>(env.OPENAI_MODEL),
};
await Promise.all([generateForTsType(vendor), generateForSwagger(vendor)]);
};
main().catch(console.error);