@agentica/core 0.9.0 → 0.10.0-dev.20250302
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/LICENSE +21 -21
- package/README.md +419 -419
- package/lib/Agentica.d.ts +4 -4
- package/lib/Agentica.js +7 -7
- package/lib/Agentica.js.map +1 -1
- package/lib/chatgpt/ChatGptCallFunctionAgent.js +14 -6
- package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +10 -5
- package/lib/index.mjs.map +1 -1
- package/lib/structures/IAgenticaContext.d.ts +1 -1
- package/lib/structures/IAgenticaController.d.ts +4 -5
- package/lib/structures/IAgenticaEvent.d.ts +3 -3
- package/lib/structures/IAgenticaOperation.d.ts +2 -3
- package/lib/structures/IAgenticaOperationSelection.d.ts +2 -3
- package/lib/structures/IAgenticaPrompt.d.ts +2 -3
- package/lib/structures/IAgenticaProps.d.ts +4 -4
- package/lib/structures/IAgenticaTokenUsage.d.ts +3 -3
- package/lib/structures/{IAgenticaProvider.d.ts → IAgenticaVendor.d.ts} +6 -6
- package/lib/structures/{IAgenticaProvider.js → IAgenticaVendor.js} +1 -1
- package/lib/structures/IAgenticaVendor.js.map +1 -0
- package/package.json +1 -1
- package/prompts/cancel.md +4 -4
- package/prompts/common.md +2 -2
- package/prompts/describe.md +6 -6
- package/prompts/execute.md +6 -6
- package/prompts/initialize.md +2 -2
- package/prompts/select.md +6 -6
- package/src/Agentica.ts +323 -323
- package/src/chatgpt/ChatGptAgent.ts +75 -75
- package/src/chatgpt/ChatGptCallFunctionAgent.ts +464 -448
- package/src/chatgpt/ChatGptCancelFunctionAgent.ts +287 -287
- package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +52 -52
- package/src/chatgpt/ChatGptHistoryDecoder.ts +88 -88
- package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -88
- package/src/chatgpt/ChatGptSelectFunctionAgent.ts +319 -319
- package/src/functional/createHttpLlmApplication.ts +63 -63
- package/src/index.ts +19 -19
- package/src/internal/AgenticaConstant.ts +4 -4
- package/src/internal/AgenticaDefaultPrompt.ts +43 -43
- package/src/internal/AgenticaOperationComposer.ts +87 -87
- package/src/internal/AgenticaPromptFactory.ts +32 -32
- package/src/internal/AgenticaPromptTransformer.ts +86 -86
- package/src/internal/AgenticaTokenUsageAggregator.ts +115 -115
- package/src/internal/MathUtil.ts +3 -3
- package/src/internal/Singleton.ts +22 -22
- package/src/internal/__map_take.ts +15 -15
- package/src/structures/IAgenticaConfig.ts +123 -123
- package/src/structures/IAgenticaContext.ts +129 -129
- package/src/structures/IAgenticaController.ts +133 -132
- package/src/structures/IAgenticaEvent.ts +229 -229
- package/src/structures/IAgenticaExecutor.ts +156 -156
- package/src/structures/IAgenticaOperation.ts +63 -64
- package/src/structures/IAgenticaOperationCollection.ts +52 -52
- package/src/structures/IAgenticaOperationSelection.ts +68 -69
- package/src/structures/IAgenticaPrompt.ts +182 -178
- package/src/structures/IAgenticaProps.ts +70 -70
- package/src/structures/IAgenticaSystemPrompt.ts +124 -124
- package/src/structures/IAgenticaTokenUsage.ts +107 -107
- package/src/structures/{IAgenticaProvider.ts → IAgenticaVendor.ts} +39 -39
- package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -23
- package/src/structures/internal/__IChatFunctionReference.ts +21 -21
- package/src/structures/internal/__IChatInitialApplication.ts +15 -15
- package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -24
- package/src/typings/AgenticaSource.ts +6 -6
- package/lib/structures/IAgenticaProvider.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,419 +1,419 @@
|
|
|
1
|
-
# `@agentica/core`
|
|
2
|
-

|
|
3
|
-
|
|
4
|
-
[](https://github.com/wrtnlabs/agentica/blob/master/LICENSE)
|
|
5
|
-
[](https://www.npmjs.com/package/@agentica/core)
|
|
6
|
-
[](https://www.npmjs.com/package/@agentica/core)
|
|
7
|
-
[](https://github.com/wrtnlabs/agentica/actions?query=workflow%3Abuild)
|
|
8
|
-
|
|
9
|
-
The simplest **Agentic AI** library, specialized in **LLM Function Calling**.
|
|
10
|
-
|
|
11
|
-
Don't compose complicate agent graph or workflow, but just deliver **Swagger/OpenAPI** documents or **TypeScript class** types linearly to the `@agentica/core`. Then `@agentica/core` will do everything with the function calling.
|
|
12
|
-
|
|
13
|
-
Look at the below demonstration, and feel how `@agentica/core` is easy and powerful.
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { Agentica } from "@agentica/core";
|
|
17
|
-
import typia from "typia";
|
|
18
|
-
|
|
19
|
-
const agent = new Agentica({
|
|
20
|
-
controllers: [
|
|
21
|
-
await fetch(
|
|
22
|
-
"https://shopping-be.wrtn.ai/editor/swagger.json",
|
|
23
|
-
).then(r => r.json()),
|
|
24
|
-
typia.llm.application<ShoppingCounselor>(),
|
|
25
|
-
typia.llm.application<ShoppingPolicy>(),
|
|
26
|
-
typia.llm.application<ShoppingSearchRag>(),
|
|
27
|
-
],
|
|
28
|
-
});
|
|
29
|
-
await agent.conversate("I wanna buy MacBook Pro");
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
> https://github.com/user-attachments/assets/01604b53-aca4-41cb-91aa-3faf63549ea6
|
|
33
|
-
>
|
|
34
|
-
> Demonstration video of Shopping AI Chatbot
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
## How to Use
|
|
40
|
-
### Setup
|
|
41
|
-
```bash
|
|
42
|
-
npm install @agentica/core @samchon/openapi typia
|
|
43
|
-
npx typia setup
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
Install not only `@agentica/core`, but also [`@samchon/openapi`](https://github.com/samchon/openapi) and [`typia`](https://github.com/samchon/typia).
|
|
47
|
-
|
|
48
|
-
`@samchon/openapi` is an OpenAPI specification library which can convert Swagger/OpenAPI document to LLM function calling schema. And `typia` is a transformer (compiler) library which can compose LLM function calling schema from a TypeScript class type.
|
|
49
|
-
|
|
50
|
-
By the way, as `typia` is a transformer library analyzing TypeScript source code in the compilation level, it needs additional setup command `npx typia setup`. Also, if you're not using non-standard TypeScript compiler (not `tsc`) or developing the agent in the frontend environment, you have to setup [`@ryoppippi/unplugin-typia`](https://typia.io/docs/setup/#unplugin-typia) too.
|
|
51
|
-
|
|
52
|
-
### Chat with Backend Server
|
|
53
|
-
```typescript
|
|
54
|
-
import { IHttpLlmApplication } from "@samchon/openapi";
|
|
55
|
-
import { Agentica, createHttpApplication } from "@agentica/core";
|
|
56
|
-
import OpenAI from "openai";
|
|
57
|
-
import { IValidation } from "typia";
|
|
58
|
-
|
|
59
|
-
const main = async (): Promise<void> => {
|
|
60
|
-
// LOAD SWAGGER DOCUMENT, AND CONVERT TO LLM APPLICATION SCHEMA
|
|
61
|
-
const application: IValidation<IHttpLlmApplication<"chatgpt">> =
|
|
62
|
-
createHttpApplication({
|
|
63
|
-
model: "chatgpt",
|
|
64
|
-
document: OpenApi.convert(
|
|
65
|
-
await fetch("https://shopping-be.wrtn.ai/editor/swagger.json").then(
|
|
66
|
-
(r) => r.json()
|
|
67
|
-
)
|
|
68
|
-
),
|
|
69
|
-
});
|
|
70
|
-
if (application.success === false) {
|
|
71
|
-
console.error(application.errors);
|
|
72
|
-
throw new Error("Type error on the target swagger document");
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// CREATE AN AGENT WITH THE APPLICATION
|
|
76
|
-
const agent: Agentica<"chatgpt"> = new Agentica({
|
|
77
|
-
model: "chatgpt",
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
},
|
|
84
|
-
controllers: [
|
|
85
|
-
{
|
|
86
|
-
protocol: "http",
|
|
87
|
-
name: "shopping",
|
|
88
|
-
application: application.data,
|
|
89
|
-
connection: {
|
|
90
|
-
host: "https://shopping-be.wrtn.ai",
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
config: {
|
|
95
|
-
locale: "en-US",
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// ADD EVENT LISTENERS
|
|
100
|
-
agent.on("select", async (select) => {
|
|
101
|
-
console.log("selected function", select.operation.function.name);
|
|
102
|
-
});
|
|
103
|
-
agent.on("execute", async (execute) => {
|
|
104
|
-
consoe.log("execute function", {
|
|
105
|
-
function: execute.operation.function.name,
|
|
106
|
-
arguments: execute.arguments,
|
|
107
|
-
value: execute.value,
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// CONVERSATE TO AI CHATBOT
|
|
112
|
-
await agent.conversate("What you can do?");
|
|
113
|
-
};
|
|
114
|
-
main().catch(console.error);
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
Just load your swagger document, and put it into the `@agentica/core`.
|
|
118
|
-
|
|
119
|
-
Then you can start conversation with your backend server, and the API functions of the backend server would be automatically called. AI chatbot will analyze your conversation texts, and executes proper API functions by the LLM (Large Language Model) function calling feature.
|
|
120
|
-
|
|
121
|
-
From now on, every backend developer is also an AI developer.
|
|
122
|
-
|
|
123
|
-
### Chat with TypeScript Class
|
|
124
|
-
```typescript
|
|
125
|
-
import { Agentica } from "@agentica/core";
|
|
126
|
-
import typia, { tags } from "typia";
|
|
127
|
-
import OpenAI from "openai";
|
|
128
|
-
|
|
129
|
-
class BbsArticleService {
|
|
130
|
-
/**
|
|
131
|
-
* Create a new article.
|
|
132
|
-
*
|
|
133
|
-
* Writes a new article and archives it into the DB.
|
|
134
|
-
*
|
|
135
|
-
* @param props Properties of create function
|
|
136
|
-
* @returns Newly created article
|
|
137
|
-
*/
|
|
138
|
-
public async create(props: {
|
|
139
|
-
/**
|
|
140
|
-
* Information of the article to create
|
|
141
|
-
*/
|
|
142
|
-
input: IBbsArticle.ICreate;
|
|
143
|
-
}): Promise<IBbsArticle>;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Update an article.
|
|
147
|
-
*
|
|
148
|
-
* Updates an article with new content.
|
|
149
|
-
*
|
|
150
|
-
* @param props Properties of update function
|
|
151
|
-
* @param input New content to update
|
|
152
|
-
*/
|
|
153
|
-
public async update(props: {
|
|
154
|
-
/**
|
|
155
|
-
* Target article's {@link IBbsArticle.id}.
|
|
156
|
-
*/
|
|
157
|
-
id: string & tags.Format<"uuid">;
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* New content to update.
|
|
161
|
-
*/
|
|
162
|
-
input: IBbsArticle.IUpdate;
|
|
163
|
-
}): Promise<void>;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const main = async (): Promise<void> => {
|
|
167
|
-
const api: OpenAI = new OpenAI({
|
|
168
|
-
apiKey: "YOUR_OPENAI_API_KEY",
|
|
169
|
-
});
|
|
170
|
-
const agent: Agentica<"chatgpt"> = new Agentica({
|
|
171
|
-
model: "chatgpt",
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
},
|
|
178
|
-
controllers: [
|
|
179
|
-
{
|
|
180
|
-
protocol: "class",
|
|
181
|
-
name: "vectorStore",
|
|
182
|
-
application: typia.llm.
|
|
183
|
-
BbsArticleService,
|
|
184
|
-
"chatgpt"
|
|
185
|
-
>(),
|
|
186
|
-
execute: new BbsArticleService(),
|
|
187
|
-
},
|
|
188
|
-
],
|
|
189
|
-
});
|
|
190
|
-
await agent.conversate("I wanna write an article.");
|
|
191
|
-
};
|
|
192
|
-
main().catch(console.error);
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
You also can chat with a TypeScript class.
|
|
196
|
-
|
|
197
|
-
Just deliver the TypeScript type to the `@agentica/core`, and start conversation. Then `@agentica/core` will call the proper class functions by analyzing your conversation texts with LLM function calling feature.
|
|
198
|
-
|
|
199
|
-
From now on, every TypeScript classes you've developed can be the AI chatbot.
|
|
200
|
-
|
|
201
|
-
### Multi Agent Orchestration
|
|
202
|
-
```typescript
|
|
203
|
-
import { Agentica } from "@agentica/core";
|
|
204
|
-
import typia from "typia";
|
|
205
|
-
import OpenAI from "openai";
|
|
206
|
-
|
|
207
|
-
class OpenAIVectorStoreAgent {
|
|
208
|
-
/**
|
|
209
|
-
* Retrieve Vector DB with RAG.
|
|
210
|
-
*
|
|
211
|
-
* @param props Properties of Vector DB retrievelance
|
|
212
|
-
*/
|
|
213
|
-
public query(props: {
|
|
214
|
-
/**
|
|
215
|
-
* Keywords to look up.
|
|
216
|
-
*
|
|
217
|
-
* Put all the keywords you want to look up. However, keywords
|
|
218
|
-
* should only be included in the core, and all ambiguous things
|
|
219
|
-
* should be excluded to achieve accurate results.
|
|
220
|
-
*/
|
|
221
|
-
keywords: string;
|
|
222
|
-
}): Promise<IVectorStoreQueryResult>;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const main = async (): Promise<void> => {
|
|
226
|
-
const api: OpenAI = new OpenAI({
|
|
227
|
-
apiKey: "YOUR_OPENAI_API_KEY",
|
|
228
|
-
});
|
|
229
|
-
const agent: Agentica<"chatgpt"> = new Agentica({
|
|
230
|
-
model: "chatgpt",
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
},
|
|
237
|
-
controllers: [
|
|
238
|
-
{
|
|
239
|
-
protocol: "class",
|
|
240
|
-
name: "vectorStore",
|
|
241
|
-
application: typia.llm.
|
|
242
|
-
OpenAIVectorStoreAgent,
|
|
243
|
-
"chatgpt"
|
|
244
|
-
>(),
|
|
245
|
-
execute: new OpenAIVectorStoreAgent({
|
|
246
|
-
api,
|
|
247
|
-
id: "YOUR_OPENAI_VECTOR_STORE_ID",
|
|
248
|
-
}),
|
|
249
|
-
},
|
|
250
|
-
],
|
|
251
|
-
});
|
|
252
|
-
await agent.conversate("I wanna research economic articles");
|
|
253
|
-
};
|
|
254
|
-
main().catch(console.error);
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
In the `@agentica/core`, you can implement multi-agent orchestration super easily.
|
|
258
|
-
|
|
259
|
-
Just develop a TypeScript class which contains agent feature like Vector Store, and just deliver the TypeScript class type to the `@agentica/core` like above. The `@agentica/core` will centralize and realize the multi-agent orchestration by LLM function calling strategy to the TypeScript class.
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
## Principles
|
|
265
|
-
### Agent Strategy
|
|
266
|
-
```mermaid
|
|
267
|
-
sequenceDiagram
|
|
268
|
-
actor User
|
|
269
|
-
actor Agent
|
|
270
|
-
participant Selector
|
|
271
|
-
participant Caller
|
|
272
|
-
participant Describer
|
|
273
|
-
activate User
|
|
274
|
-
User-->>Agent: Conversate:<br/>user says
|
|
275
|
-
activate Agent
|
|
276
|
-
Agent->>Selector: Deliver conversation text
|
|
277
|
-
activate Selector
|
|
278
|
-
deactivate User
|
|
279
|
-
Note over Selector: Select or remove candidate functions
|
|
280
|
-
alt No candidate
|
|
281
|
-
Selector->>Agent: Talk like plain ChatGPT
|
|
282
|
-
deactivate Selector
|
|
283
|
-
Agent->>User: Conversate:<br/>agent says
|
|
284
|
-
activate User
|
|
285
|
-
deactivate User
|
|
286
|
-
end
|
|
287
|
-
deactivate Agent
|
|
288
|
-
loop Candidate functions exist
|
|
289
|
-
activate Agent
|
|
290
|
-
Agent->>Caller: Deliver conversation text
|
|
291
|
-
activate Caller
|
|
292
|
-
alt Contexts are enough
|
|
293
|
-
Note over Caller: Call fulfilled functions
|
|
294
|
-
Caller->>Describer: Function call histories
|
|
295
|
-
deactivate Caller
|
|
296
|
-
activate Describer
|
|
297
|
-
Describer->>Agent: Describe function calls
|
|
298
|
-
deactivate Describer
|
|
299
|
-
Agent->>User: Conversate:<br/>agent describes
|
|
300
|
-
activate User
|
|
301
|
-
deactivate User
|
|
302
|
-
else Contexts are not enough
|
|
303
|
-
break
|
|
304
|
-
Caller->>Agent: Request more information
|
|
305
|
-
end
|
|
306
|
-
Agent->>User: Conversate:<br/>agent requests
|
|
307
|
-
activate User
|
|
308
|
-
deactivate User
|
|
309
|
-
end
|
|
310
|
-
deactivate Agent
|
|
311
|
-
end
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
When user says, `@agentica/core` delivers the conversation text to the `selector` agent, and let the `selector` agent to find (or cancel) candidate functions from the context. If the `selector` agent could not find any candidate function to call and there is not any candidate function previously selected either, the `selector` agent will work just like a plain ChatGPT.
|
|
315
|
-
|
|
316
|
-
And `@agentica/core` enters to a loop statement until the candidate functions to be empty. In the loop statement, `caller` agent tries to LLM function calling by analyzing the user's conversation text. If context is enough to compose arguments of candidate functions, the `caller` agent actually calls the target functions, and let `decriber` agent to explain the function calling results. Otherwise the context is not enough to compose arguments, `caller` agent requests more information to user.
|
|
317
|
-
|
|
318
|
-
Such LLM (Large Language Model) function calling strategy separating `selector`, `caller`, and `describer` is the key logic of `@agentica/core`.
|
|
319
|
-
|
|
320
|
-
### Validation Feedback
|
|
321
|
-
```typescript
|
|
322
|
-
import { FunctionCall } from "pseudo";
|
|
323
|
-
import {
|
|
324
|
-
|
|
325
|
-
export const correctFunctionCall = (p: {
|
|
326
|
-
call: FunctionCall;
|
|
327
|
-
functions: Array<
|
|
328
|
-
retry: (reason: string, errors?: IValidation.IError[]) => Promise<unknown>;
|
|
329
|
-
}): Promise<unknown> => {
|
|
330
|
-
// FIND FUNCTION
|
|
331
|
-
const func:
|
|
332
|
-
p.functions.find((f) => f.name === p.call.name);
|
|
333
|
-
if (func === undefined) {
|
|
334
|
-
// never happened in my experience
|
|
335
|
-
return p.retry(
|
|
336
|
-
"Unable to find the matched function name. Try it again.",
|
|
337
|
-
);
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// VALIDATE
|
|
341
|
-
const result: IValidation<unknown> = func.validate(p.call.arguments);
|
|
342
|
-
if (result.success === false) {
|
|
343
|
-
// 1st trial: 50% (gpt-4o-mini in shopping mall chatbot)
|
|
344
|
-
// 2nd trial with validation feedback: 99%
|
|
345
|
-
// 3nd trial with validation feedback again: never have failed
|
|
346
|
-
return p.retry(
|
|
347
|
-
"Type errors are detected. Correct it through validation errors",
|
|
348
|
-
{
|
|
349
|
-
errors: result.errors,
|
|
350
|
-
},
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
return result.data;
|
|
354
|
-
}
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
Is LLM function calling perfect?
|
|
358
|
-
|
|
359
|
-
The answer is not, and LLM (Large Language Model)
|
|
360
|
-
|
|
361
|
-
Therefore, when developing an LLM function calling agent, the validation feedback process is essentially required. If LLM takes a type level mistake on arguments composition, the agent must feedback the most detailed validation errors, and let the LLM to retry the function calling referencing the validation errors.
|
|
362
|
-
|
|
363
|
-
About the validation feedback, `@agentica/core` is utilizing [`typia.validate<T>()`](https://typia.io/docs/validators/validate) and [`typia.llm.
|
|
364
|
-
|
|
365
|
-
Such validation feedback strategy and combination with `typia` runtime validator, `@agentica/core` has achieved the most ideal LLM function calling. In my experience, when using OpenAI's `gpt-4o-mini` model, it tends to construct invalid function calling arguments at the first trial about 50% of the time. By the way, if correct it through validation feedback with `typia`, success rate soars to 99%. And I've never had a failure when trying validation feedback twice.
|
|
366
|
-
|
|
367
|
-
Components | `typia` | `TypeBox` | `ajv` | `io-ts` | `zod` | `C.V.`
|
|
368
|
-
-------------------------|--------|-----------|-------|---------|-------|------------------
|
|
369
|
-
**Easy to use** | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
370
|
-
[Object (simple)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectSimple.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
371
|
-
[Object (hierarchical)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectHierarchical.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
372
|
-
[Object (recursive)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectRecursive.ts) | ✔ | ❌ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
373
|
-
[Object (union, implicit)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectUnionImplicit.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
374
|
-
[Object (union, explicit)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectUnionExplicit.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ❌
|
|
375
|
-
[Object (additional tags)](https://github.com/samchon/typia/#comment-tags) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
376
|
-
[Object (template literal types)](https://github.com/samchon/typia/blob/master/test/src/structures/TemplateUnion.ts) | ✔ | ✔ | ✔ | ❌ | ❌ | ❌
|
|
377
|
-
[Object (dynamic properties)](https://github.com/samchon/typia/blob/master/test/src/structures/DynamicTemplate.ts) | ✔ | ✔ | ✔ | ❌ | ❌ | ❌
|
|
378
|
-
[Array (rest tuple)](https://github.com/samchon/typia/blob/master/test/src/structures/TupleRestAtomic.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
379
|
-
[Array (hierarchical)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayHierarchical.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
380
|
-
[Array (recursive)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRecursive.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ❌
|
|
381
|
-
[Array (recursive, union)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRecursiveUnionExplicit.ts) | ✔ | ✔ | ❌ | ✔ | ✔ | ❌
|
|
382
|
-
[Array (R+U, implicit)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRecursiveUnionImplicit.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
383
|
-
[Array (repeated)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRepeatedNullable.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
384
|
-
[Array (repeated, union)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRepeatedUnionWithTuple.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
385
|
-
[**Ultimate Union Type**](https://github.com/samchon/typia/blob/master/test/src/structures/UltimateUnion.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
386
|
-
|
|
387
|
-
> `C.V.` means `class-validator`
|
|
388
|
-
|
|
389
|
-
### OpenAPI Specification
|
|
390
|
-
```mermaid
|
|
391
|
-
flowchart
|
|
392
|
-
subgraph "OpenAPI Specification"
|
|
393
|
-
v20("Swagger v2.0") --upgrades--> emended[["OpenAPI v3.1 (emended)"]]
|
|
394
|
-
v30("OpenAPI v3.0") --upgrades--> emended
|
|
395
|
-
v31("OpenAPI v3.1") --emends--> emended
|
|
396
|
-
end
|
|
397
|
-
subgraph "OpenAPI Generator"
|
|
398
|
-
emended --normalizes--> migration[["Migration Schema"]]
|
|
399
|
-
migration --"Artificial Intelligence"--> lfc{{"LLM Function Calling"}}
|
|
400
|
-
lfc --"OpenAI"--> chatgpt("ChatGPT")
|
|
401
|
-
lfc --"Anthropic"--> claude("Claude")
|
|
402
|
-
lfc --"Google"--> gemini("Gemini")
|
|
403
|
-
lfc --"Meta"--> llama("Llama")
|
|
404
|
-
end
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
`@agentica/core` obtains LLM function calling schemas from both Swagger/OpenAPI documents and TypeScript class types. The TypeScript class type can be converted to LLM function calling schema by [`typia.llm.
|
|
408
|
-
|
|
409
|
-
The secret is on the above diagram.
|
|
410
|
-
|
|
411
|
-
In the OpenAPI specification, there are three versions with different definitions. And even in the same version, there are too much ambiguous and duplicated expressions. To resolve these problems, [`@samchon/openapi`](https://github.com/samchon/openapi) is transforming every OpenAPI documents to v3.1 emended specification. The `@samchon/openapi`'s emended v3.1 specification has removed every ambiguous and duplicated expressions for clarity.
|
|
412
|
-
|
|
413
|
-
With the v3.1 emended OpenAPI document, `@samchon/openapi` converts it to a migration schema that is near to the function structure. And as the last step, the migration schema will be transformed to a specific LLM
|
|
414
|
-
|
|
415
|
-
> **Why do not directly convert, but intermediate?**
|
|
416
|
-
>
|
|
417
|
-
> If directly convert from each version of OpenAPI specification to specific LLM's function calling schema, I have to make much more converters increased by cartesian product. In current models, number of converters would be 12 = 3 x 4.
|
|
418
|
-
>
|
|
419
|
-
> However, if define intermediate schema, number of converters are shrunk to plus operation. In current models, I just need to develop only (7 = 3 + 4) converters, and this is the reason why I've defined intermediate specification. This way is economic.
|
|
1
|
+
# `@agentica/core`
|
|
2
|
+

|
|
3
|
+
|
|
4
|
+
[](https://github.com/wrtnlabs/agentica/blob/master/LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/@agentica/core)
|
|
6
|
+
[](https://www.npmjs.com/package/@agentica/core)
|
|
7
|
+
[](https://github.com/wrtnlabs/agentica/actions?query=workflow%3Abuild)
|
|
8
|
+
|
|
9
|
+
The simplest **Agentic AI** library, specialized in **LLM Function Calling**.
|
|
10
|
+
|
|
11
|
+
Don't compose complicate agent graph or workflow, but just deliver **Swagger/OpenAPI** documents or **TypeScript class** types linearly to the `@agentica/core`. Then `@agentica/core` will do everything with the function calling.
|
|
12
|
+
|
|
13
|
+
Look at the below demonstration, and feel how `@agentica/core` is easy and powerful.
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Agentica } from "@agentica/core";
|
|
17
|
+
import typia from "typia";
|
|
18
|
+
|
|
19
|
+
const agent = new Agentica({
|
|
20
|
+
controllers: [
|
|
21
|
+
await fetch(
|
|
22
|
+
"https://shopping-be.wrtn.ai/editor/swagger.json",
|
|
23
|
+
).then(r => r.json()),
|
|
24
|
+
typia.llm.application<ShoppingCounselor>(),
|
|
25
|
+
typia.llm.application<ShoppingPolicy>(),
|
|
26
|
+
typia.llm.application<ShoppingSearchRag>(),
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
await agent.conversate("I wanna buy MacBook Pro");
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
> https://github.com/user-attachments/assets/01604b53-aca4-41cb-91aa-3faf63549ea6
|
|
33
|
+
>
|
|
34
|
+
> Demonstration video of Shopping AI Chatbot
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## How to Use
|
|
40
|
+
### Setup
|
|
41
|
+
```bash
|
|
42
|
+
npm install @agentica/core @samchon/openapi typia
|
|
43
|
+
npx typia setup
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Install not only `@agentica/core`, but also [`@samchon/openapi`](https://github.com/samchon/openapi) and [`typia`](https://github.com/samchon/typia).
|
|
47
|
+
|
|
48
|
+
`@samchon/openapi` is an OpenAPI specification library which can convert Swagger/OpenAPI document to LLM function calling schema. And `typia` is a transformer (compiler) library which can compose LLM function calling schema from a TypeScript class type.
|
|
49
|
+
|
|
50
|
+
By the way, as `typia` is a transformer library analyzing TypeScript source code in the compilation level, it needs additional setup command `npx typia setup`. Also, if you're not using non-standard TypeScript compiler (not `tsc`) or developing the agent in the frontend environment, you have to setup [`@ryoppippi/unplugin-typia`](https://typia.io/docs/setup/#unplugin-typia) too.
|
|
51
|
+
|
|
52
|
+
### Chat with Backend Server
|
|
53
|
+
```typescript
|
|
54
|
+
import { IHttpLlmApplication } from "@samchon/openapi";
|
|
55
|
+
import { Agentica, createHttpApplication } from "@agentica/core";
|
|
56
|
+
import OpenAI from "openai";
|
|
57
|
+
import { IValidation } from "typia";
|
|
58
|
+
|
|
59
|
+
const main = async (): Promise<void> => {
|
|
60
|
+
// LOAD SWAGGER DOCUMENT, AND CONVERT TO LLM APPLICATION SCHEMA
|
|
61
|
+
const application: IValidation<IHttpLlmApplication<"chatgpt">> =
|
|
62
|
+
createHttpApplication({
|
|
63
|
+
model: "chatgpt",
|
|
64
|
+
document: OpenApi.convert(
|
|
65
|
+
await fetch("https://shopping-be.wrtn.ai/editor/swagger.json").then(
|
|
66
|
+
(r) => r.json()
|
|
67
|
+
)
|
|
68
|
+
),
|
|
69
|
+
});
|
|
70
|
+
if (application.success === false) {
|
|
71
|
+
console.error(application.errors);
|
|
72
|
+
throw new Error("Type error on the target swagger document");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// CREATE AN AGENT WITH THE APPLICATION
|
|
76
|
+
const agent: Agentica<"chatgpt"> = new Agentica({
|
|
77
|
+
model: "chatgpt",
|
|
78
|
+
vendor: {
|
|
79
|
+
api: new OpenAI({
|
|
80
|
+
apiKey: "YOUR_OPENAI_API_KEY",
|
|
81
|
+
}),
|
|
82
|
+
model: "gpt-4o-mini",
|
|
83
|
+
},
|
|
84
|
+
controllers: [
|
|
85
|
+
{
|
|
86
|
+
protocol: "http",
|
|
87
|
+
name: "shopping",
|
|
88
|
+
application: application.data,
|
|
89
|
+
connection: {
|
|
90
|
+
host: "https://shopping-be.wrtn.ai",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
config: {
|
|
95
|
+
locale: "en-US",
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// ADD EVENT LISTENERS
|
|
100
|
+
agent.on("select", async (select) => {
|
|
101
|
+
console.log("selected function", select.operation.function.name);
|
|
102
|
+
});
|
|
103
|
+
agent.on("execute", async (execute) => {
|
|
104
|
+
consoe.log("execute function", {
|
|
105
|
+
function: execute.operation.function.name,
|
|
106
|
+
arguments: execute.arguments,
|
|
107
|
+
value: execute.value,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// CONVERSATE TO AI CHATBOT
|
|
112
|
+
await agent.conversate("What you can do?");
|
|
113
|
+
};
|
|
114
|
+
main().catch(console.error);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Just load your swagger document, and put it into the `@agentica/core`.
|
|
118
|
+
|
|
119
|
+
Then you can start conversation with your backend server, and the API functions of the backend server would be automatically called. AI chatbot will analyze your conversation texts, and executes proper API functions by the LLM (Large Language Model) function calling feature.
|
|
120
|
+
|
|
121
|
+
From now on, every backend developer is also an AI developer.
|
|
122
|
+
|
|
123
|
+
### Chat with TypeScript Class
|
|
124
|
+
```typescript
|
|
125
|
+
import { Agentica } from "@agentica/core";
|
|
126
|
+
import typia, { tags } from "typia";
|
|
127
|
+
import OpenAI from "openai";
|
|
128
|
+
|
|
129
|
+
class BbsArticleService {
|
|
130
|
+
/**
|
|
131
|
+
* Create a new article.
|
|
132
|
+
*
|
|
133
|
+
* Writes a new article and archives it into the DB.
|
|
134
|
+
*
|
|
135
|
+
* @param props Properties of create function
|
|
136
|
+
* @returns Newly created article
|
|
137
|
+
*/
|
|
138
|
+
public async create(props: {
|
|
139
|
+
/**
|
|
140
|
+
* Information of the article to create
|
|
141
|
+
*/
|
|
142
|
+
input: IBbsArticle.ICreate;
|
|
143
|
+
}): Promise<IBbsArticle>;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Update an article.
|
|
147
|
+
*
|
|
148
|
+
* Updates an article with new content.
|
|
149
|
+
*
|
|
150
|
+
* @param props Properties of update function
|
|
151
|
+
* @param input New content to update
|
|
152
|
+
*/
|
|
153
|
+
public async update(props: {
|
|
154
|
+
/**
|
|
155
|
+
* Target article's {@link IBbsArticle.id}.
|
|
156
|
+
*/
|
|
157
|
+
id: string & tags.Format<"uuid">;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* New content to update.
|
|
161
|
+
*/
|
|
162
|
+
input: IBbsArticle.IUpdate;
|
|
163
|
+
}): Promise<void>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const main = async (): Promise<void> => {
|
|
167
|
+
const api: OpenAI = new OpenAI({
|
|
168
|
+
apiKey: "YOUR_OPENAI_API_KEY",
|
|
169
|
+
});
|
|
170
|
+
const agent: Agentica<"chatgpt"> = new Agentica({
|
|
171
|
+
model: "chatgpt",
|
|
172
|
+
vendor: {
|
|
173
|
+
api: new OpenAI({
|
|
174
|
+
apiKey: "YOUR_OPENAI_API_KEY",
|
|
175
|
+
}),
|
|
176
|
+
model: "gpt-4o-mini",
|
|
177
|
+
},
|
|
178
|
+
controllers: [
|
|
179
|
+
{
|
|
180
|
+
protocol: "class",
|
|
181
|
+
name: "vectorStore",
|
|
182
|
+
application: typia.llm.application<
|
|
183
|
+
BbsArticleService,
|
|
184
|
+
"chatgpt"
|
|
185
|
+
>(),
|
|
186
|
+
execute: new BbsArticleService(),
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
});
|
|
190
|
+
await agent.conversate("I wanna write an article.");
|
|
191
|
+
};
|
|
192
|
+
main().catch(console.error);
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
You also can chat with a TypeScript class.
|
|
196
|
+
|
|
197
|
+
Just deliver the TypeScript type to the `@agentica/core`, and start conversation. Then `@agentica/core` will call the proper class functions by analyzing your conversation texts with LLM function calling feature.
|
|
198
|
+
|
|
199
|
+
From now on, every TypeScript classes you've developed can be the AI chatbot.
|
|
200
|
+
|
|
201
|
+
### Multi Agent Orchestration
|
|
202
|
+
```typescript
|
|
203
|
+
import { Agentica } from "@agentica/core";
|
|
204
|
+
import typia from "typia";
|
|
205
|
+
import OpenAI from "openai";
|
|
206
|
+
|
|
207
|
+
class OpenAIVectorStoreAgent {
|
|
208
|
+
/**
|
|
209
|
+
* Retrieve Vector DB with RAG.
|
|
210
|
+
*
|
|
211
|
+
* @param props Properties of Vector DB retrievelance
|
|
212
|
+
*/
|
|
213
|
+
public query(props: {
|
|
214
|
+
/**
|
|
215
|
+
* Keywords to look up.
|
|
216
|
+
*
|
|
217
|
+
* Put all the keywords you want to look up. However, keywords
|
|
218
|
+
* should only be included in the core, and all ambiguous things
|
|
219
|
+
* should be excluded to achieve accurate results.
|
|
220
|
+
*/
|
|
221
|
+
keywords: string;
|
|
222
|
+
}): Promise<IVectorStoreQueryResult>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const main = async (): Promise<void> => {
|
|
226
|
+
const api: OpenAI = new OpenAI({
|
|
227
|
+
apiKey: "YOUR_OPENAI_API_KEY",
|
|
228
|
+
});
|
|
229
|
+
const agent: Agentica<"chatgpt"> = new Agentica({
|
|
230
|
+
model: "chatgpt",
|
|
231
|
+
context: {
|
|
232
|
+
api: new OpenAI({
|
|
233
|
+
apiKey: "YOUR_OPENAI_API_KEY",
|
|
234
|
+
}),
|
|
235
|
+
model: "gpt-4o-mini",
|
|
236
|
+
},
|
|
237
|
+
controllers: [
|
|
238
|
+
{
|
|
239
|
+
protocol: "class",
|
|
240
|
+
name: "vectorStore",
|
|
241
|
+
application: typia.llm.application<
|
|
242
|
+
OpenAIVectorStoreAgent,
|
|
243
|
+
"chatgpt"
|
|
244
|
+
>(),
|
|
245
|
+
execute: new OpenAIVectorStoreAgent({
|
|
246
|
+
api,
|
|
247
|
+
id: "YOUR_OPENAI_VECTOR_STORE_ID",
|
|
248
|
+
}),
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
});
|
|
252
|
+
await agent.conversate("I wanna research economic articles");
|
|
253
|
+
};
|
|
254
|
+
main().catch(console.error);
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
In the `@agentica/core`, you can implement multi-agent orchestration super easily.
|
|
258
|
+
|
|
259
|
+
Just develop a TypeScript class which contains agent feature like Vector Store, and just deliver the TypeScript class type to the `@agentica/core` like above. The `@agentica/core` will centralize and realize the multi-agent orchestration by LLM function calling strategy to the TypeScript class.
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
## Principles
|
|
265
|
+
### Agent Strategy
|
|
266
|
+
```mermaid
|
|
267
|
+
sequenceDiagram
|
|
268
|
+
actor User
|
|
269
|
+
actor Agent
|
|
270
|
+
participant Selector
|
|
271
|
+
participant Caller
|
|
272
|
+
participant Describer
|
|
273
|
+
activate User
|
|
274
|
+
User-->>Agent: Conversate:<br/>user says
|
|
275
|
+
activate Agent
|
|
276
|
+
Agent->>Selector: Deliver conversation text
|
|
277
|
+
activate Selector
|
|
278
|
+
deactivate User
|
|
279
|
+
Note over Selector: Select or remove candidate functions
|
|
280
|
+
alt No candidate
|
|
281
|
+
Selector->>Agent: Talk like plain ChatGPT
|
|
282
|
+
deactivate Selector
|
|
283
|
+
Agent->>User: Conversate:<br/>agent says
|
|
284
|
+
activate User
|
|
285
|
+
deactivate User
|
|
286
|
+
end
|
|
287
|
+
deactivate Agent
|
|
288
|
+
loop Candidate functions exist
|
|
289
|
+
activate Agent
|
|
290
|
+
Agent->>Caller: Deliver conversation text
|
|
291
|
+
activate Caller
|
|
292
|
+
alt Contexts are enough
|
|
293
|
+
Note over Caller: Call fulfilled functions
|
|
294
|
+
Caller->>Describer: Function call histories
|
|
295
|
+
deactivate Caller
|
|
296
|
+
activate Describer
|
|
297
|
+
Describer->>Agent: Describe function calls
|
|
298
|
+
deactivate Describer
|
|
299
|
+
Agent->>User: Conversate:<br/>agent describes
|
|
300
|
+
activate User
|
|
301
|
+
deactivate User
|
|
302
|
+
else Contexts are not enough
|
|
303
|
+
break
|
|
304
|
+
Caller->>Agent: Request more information
|
|
305
|
+
end
|
|
306
|
+
Agent->>User: Conversate:<br/>agent requests
|
|
307
|
+
activate User
|
|
308
|
+
deactivate User
|
|
309
|
+
end
|
|
310
|
+
deactivate Agent
|
|
311
|
+
end
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
When user says, `@agentica/core` delivers the conversation text to the `selector` agent, and let the `selector` agent to find (or cancel) candidate functions from the context. If the `selector` agent could not find any candidate function to call and there is not any candidate function previously selected either, the `selector` agent will work just like a plain ChatGPT.
|
|
315
|
+
|
|
316
|
+
And `@agentica/core` enters to a loop statement until the candidate functions to be empty. In the loop statement, `caller` agent tries to LLM function calling by analyzing the user's conversation text. If context is enough to compose arguments of candidate functions, the `caller` agent actually calls the target functions, and let `decriber` agent to explain the function calling results. Otherwise the context is not enough to compose arguments, `caller` agent requests more information to user.
|
|
317
|
+
|
|
318
|
+
Such LLM (Large Language Model) function calling strategy separating `selector`, `caller`, and `describer` is the key logic of `@agentica/core`.
|
|
319
|
+
|
|
320
|
+
### Validation Feedback
|
|
321
|
+
```typescript
|
|
322
|
+
import { FunctionCall } from "pseudo";
|
|
323
|
+
import { ILlmFunction, IValidation } from "typia";
|
|
324
|
+
|
|
325
|
+
export const correctFunctionCall = (p: {
|
|
326
|
+
call: FunctionCall;
|
|
327
|
+
functions: Array<ILlmFunction<"chatgpt">>;
|
|
328
|
+
retry: (reason: string, errors?: IValidation.IError[]) => Promise<unknown>;
|
|
329
|
+
}): Promise<unknown> => {
|
|
330
|
+
// FIND FUNCTION
|
|
331
|
+
const func: ILlmFunction<"chatgpt"> | undefined =
|
|
332
|
+
p.functions.find((f) => f.name === p.call.name);
|
|
333
|
+
if (func === undefined) {
|
|
334
|
+
// never happened in my experience
|
|
335
|
+
return p.retry(
|
|
336
|
+
"Unable to find the matched function name. Try it again.",
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// VALIDATE
|
|
341
|
+
const result: IValidation<unknown> = func.validate(p.call.arguments);
|
|
342
|
+
if (result.success === false) {
|
|
343
|
+
// 1st trial: 50% (gpt-4o-mini in shopping mall chatbot)
|
|
344
|
+
// 2nd trial with validation feedback: 99%
|
|
345
|
+
// 3nd trial with validation feedback again: never have failed
|
|
346
|
+
return p.retry(
|
|
347
|
+
"Type errors are detected. Correct it through validation errors",
|
|
348
|
+
{
|
|
349
|
+
errors: result.errors,
|
|
350
|
+
},
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
return result.data;
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Is LLM function calling perfect?
|
|
358
|
+
|
|
359
|
+
The answer is not, and LLM (Large Language Model) vendors like OpenAI take a lot of type level mistakes when composing the arguments of the target function to call. Even though an LLM function calling schema has defined an `Array<string>` type, LLM often fills it just by a `string` typed value.
|
|
360
|
+
|
|
361
|
+
Therefore, when developing an LLM function calling agent, the validation feedback process is essentially required. If LLM takes a type level mistake on arguments composition, the agent must feedback the most detailed validation errors, and let the LLM to retry the function calling referencing the validation errors.
|
|
362
|
+
|
|
363
|
+
About the validation feedback, `@agentica/core` is utilizing [`typia.validate<T>()`](https://typia.io/docs/validators/validate) and [`typia.llm.application<Class, Model>()`](https://typia.io/docs/llm/application/#application) functions. They construct validation logic by analyzing TypeScript source codes and types in the compilation level, so that detailed and accurate than any other validators like below.
|
|
364
|
+
|
|
365
|
+
Such validation feedback strategy and combination with `typia` runtime validator, `@agentica/core` has achieved the most ideal LLM function calling. In my experience, when using OpenAI's `gpt-4o-mini` model, it tends to construct invalid function calling arguments at the first trial about 50% of the time. By the way, if correct it through validation feedback with `typia`, success rate soars to 99%. And I've never had a failure when trying validation feedback twice.
|
|
366
|
+
|
|
367
|
+
Components | `typia` | `TypeBox` | `ajv` | `io-ts` | `zod` | `C.V.`
|
|
368
|
+
-------------------------|--------|-----------|-------|---------|-------|------------------
|
|
369
|
+
**Easy to use** | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
370
|
+
[Object (simple)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectSimple.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
371
|
+
[Object (hierarchical)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectHierarchical.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
372
|
+
[Object (recursive)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectRecursive.ts) | ✔ | ❌ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
373
|
+
[Object (union, implicit)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectUnionImplicit.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
374
|
+
[Object (union, explicit)](https://github.com/samchon/typia/blob/master/test/src/structures/ObjectUnionExplicit.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ❌
|
|
375
|
+
[Object (additional tags)](https://github.com/samchon/typia/#comment-tags) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
376
|
+
[Object (template literal types)](https://github.com/samchon/typia/blob/master/test/src/structures/TemplateUnion.ts) | ✔ | ✔ | ✔ | ❌ | ❌ | ❌
|
|
377
|
+
[Object (dynamic properties)](https://github.com/samchon/typia/blob/master/test/src/structures/DynamicTemplate.ts) | ✔ | ✔ | ✔ | ❌ | ❌ | ❌
|
|
378
|
+
[Array (rest tuple)](https://github.com/samchon/typia/blob/master/test/src/structures/TupleRestAtomic.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
379
|
+
[Array (hierarchical)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayHierarchical.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔
|
|
380
|
+
[Array (recursive)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRecursive.ts) | ✔ | ✔ | ✔ | ✔ | ✔ | ❌
|
|
381
|
+
[Array (recursive, union)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRecursiveUnionExplicit.ts) | ✔ | ✔ | ❌ | ✔ | ✔ | ❌
|
|
382
|
+
[Array (R+U, implicit)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRecursiveUnionImplicit.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
383
|
+
[Array (repeated)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRepeatedNullable.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
384
|
+
[Array (repeated, union)](https://github.com/samchon/typia/blob/master/test/src/structures/ArrayRepeatedUnionWithTuple.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
385
|
+
[**Ultimate Union Type**](https://github.com/samchon/typia/blob/master/test/src/structures/UltimateUnion.ts) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌
|
|
386
|
+
|
|
387
|
+
> `C.V.` means `class-validator`
|
|
388
|
+
|
|
389
|
+
### OpenAPI Specification
|
|
390
|
+
```mermaid
|
|
391
|
+
flowchart
|
|
392
|
+
subgraph "OpenAPI Specification"
|
|
393
|
+
v20("Swagger v2.0") --upgrades--> emended[["OpenAPI v3.1 (emended)"]]
|
|
394
|
+
v30("OpenAPI v3.0") --upgrades--> emended
|
|
395
|
+
v31("OpenAPI v3.1") --emends--> emended
|
|
396
|
+
end
|
|
397
|
+
subgraph "OpenAPI Generator"
|
|
398
|
+
emended --normalizes--> migration[["Migration Schema"]]
|
|
399
|
+
migration --"Artificial Intelligence"--> lfc{{"LLM Function Calling"}}
|
|
400
|
+
lfc --"OpenAI"--> chatgpt("ChatGPT")
|
|
401
|
+
lfc --"Anthropic"--> claude("Claude")
|
|
402
|
+
lfc --"Google"--> gemini("Gemini")
|
|
403
|
+
lfc --"Meta"--> llama("Llama")
|
|
404
|
+
end
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
`@agentica/core` obtains LLM function calling schemas from both Swagger/OpenAPI documents and TypeScript class types. The TypeScript class type can be converted to LLM function calling schema by [`typia.llm.application<Class, Model>()`](https://typia.io/docs/llm/application#application) function. Then how about OpenAPI document? How Swagger document can be LLM function calling schema.
|
|
408
|
+
|
|
409
|
+
The secret is on the above diagram.
|
|
410
|
+
|
|
411
|
+
In the OpenAPI specification, there are three versions with different definitions. And even in the same version, there are too much ambiguous and duplicated expressions. To resolve these problems, [`@samchon/openapi`](https://github.com/samchon/openapi) is transforming every OpenAPI documents to v3.1 emended specification. The `@samchon/openapi`'s emended v3.1 specification has removed every ambiguous and duplicated expressions for clarity.
|
|
412
|
+
|
|
413
|
+
With the v3.1 emended OpenAPI document, `@samchon/openapi` converts it to a migration schema that is near to the function structure. And as the last step, the migration schema will be transformed to a specific LLM vendor's function calling schema. LLM function calling schemas are composed like this way.
|
|
414
|
+
|
|
415
|
+
> **Why do not directly convert, but intermediate?**
|
|
416
|
+
>
|
|
417
|
+
> If directly convert from each version of OpenAPI specification to specific LLM's function calling schema, I have to make much more converters increased by cartesian product. In current models, number of converters would be 12 = 3 x 4.
|
|
418
|
+
>
|
|
419
|
+
> However, if define intermediate schema, number of converters are shrunk to plus operation. In current models, I just need to develop only (7 = 3 + 4) converters, and this is the reason why I've defined intermediate specification. This way is economic.
|