@agentica/benchmark 0.8.2 → 0.8.3-dev.20250227

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.
@@ -1,265 +1,265 @@
1
- import { Agentica } from "@agentica/core";
2
- import { AgenticaTokenUsageAggregator } from "@agentica/core/src/internal/AgenticaTokenUsageAggregator";
3
- import { Semaphore } from "tstl";
4
- import { tags } from "typia";
5
-
6
- import { AgenticaBenchmarkPredicator } from "./internal/AgenticaBenchmarkPredicator";
7
- import { AgenticaCallBenchmarkReporter } from "./internal/AgenticaCallBenchmarkReporter";
8
- import { IAgenticaCallBenchmarkEvent } from "./structures/IAgenticaCallBenchmarkEvent";
9
- import { IAgenticaCallBenchmarkResult } from "./structures/IAgenticaCallBenchmarkResult";
10
- import { IAgenticaCallBenchmarkScenario } from "./structures/IAgenticaCallBenchmarkScenario";
11
-
12
- /**
13
- * LLM function calling selection benchmark.
14
- *
15
- * `AgenticaCallBenchmark` is a class for the benchmark of the
16
- * LLM (Large Model Language) function calling part. It utilizes both
17
- * `selector` and `caller` agents and tests whether the expected
18
- * {@link IAgenticaOperation operations} are properly selected and
19
- * called from the given
20
- * {@link IAgenticaCallBenchmarkScenario scenarios}.
21
- *
22
- * Note that, this `AgenticaCallBenchmark` consumes a lot of time and
23
- * LLM token costs because it needs the whole process of the
24
- * {@link Agentica} class with a lot of repetitions. If you don't want
25
- * such a heavy benchmark, consider to using
26
- * {@link AgenticaSelectBenchmark} instead. In my experience,
27
- * {@link Agentica} does not fail to function calling, so the function
28
- * selection benchmark is much economical.
29
- *
30
- * @author Samchon
31
- */
32
- export class AgenticaCallBenchmark {
33
- private agent_: Agentica;
34
- private scenarios_: IAgenticaCallBenchmarkScenario[];
35
- private config_: AgenticaCallBenchmark.IConfig;
36
- private result_: IAgenticaCallBenchmarkResult | null;
37
-
38
- /**
39
- * Initializer Constructor.
40
- *
41
- * @param props Properties of the selection benchmark
42
- */
43
- public constructor(props: AgenticaCallBenchmark.IProps) {
44
- this.agent_ = props.agent;
45
- this.scenarios_ = props.scenarios.slice();
46
- this.config_ = {
47
- repeat: props.config?.repeat ?? 10,
48
- simultaneous: props.config?.simultaneous ?? 10,
49
- consent: props.config?.consent ?? 3,
50
- };
51
- this.result_ = null;
52
- }
53
-
54
- /**
55
- * Execute the benchmark.
56
- *
57
- * Execute the benchmark of the LLM function calling, and returns
58
- * the result of the benchmark.
59
- *
60
- * If you wanna see progress of the benchmark, you can pass a callback
61
- * function as the argument of the `listener`. The callback function
62
- * would be called whenever a benchmark event is occurred.
63
- *
64
- * Also, you can publish a markdown format report by calling
65
- * the {@link report} function after the benchmark execution.
66
- *
67
- * @param listener Callback function listening the benchmark events
68
- * @returns Results of the function calling benchmark
69
- */
70
- public async execute(
71
- listener?: (event: IAgenticaCallBenchmarkEvent) => void,
72
- ): Promise<IAgenticaCallBenchmarkResult> {
73
- const started_at: Date = new Date();
74
- const semaphore: Semaphore = new Semaphore(this.config_.simultaneous);
75
- const experiments: IAgenticaCallBenchmarkResult.IExperiment[] =
76
- await Promise.all(
77
- this.scenarios_.map(async (scenario) => {
78
- const events: IAgenticaCallBenchmarkEvent[] = await Promise.all(
79
- new Array(this.config_.repeat).fill(0).map(async () => {
80
- await semaphore.acquire();
81
- const e: IAgenticaCallBenchmarkEvent = await this.step(scenario);
82
- await semaphore.release();
83
- if (listener !== undefined) listener(e);
84
- return e;
85
- }),
86
- );
87
- return {
88
- scenario,
89
- events,
90
- usage: events
91
- .filter((e) => e.type !== "error")
92
- .map((e) => e.usage)
93
- .reduce(
94
- AgenticaTokenUsageAggregator.plus,
95
- AgenticaTokenUsageAggregator.zero(),
96
- ),
97
- };
98
- }),
99
- );
100
- return (this.result_ = {
101
- experiments,
102
- started_at,
103
- completed_at: new Date(),
104
- usage: experiments
105
- .map((p) => p.usage)
106
- .reduce(
107
- AgenticaTokenUsageAggregator.plus,
108
- AgenticaTokenUsageAggregator.zero(),
109
- ),
110
- });
111
- }
112
-
113
- /**
114
- * Report the benchmark result as markdown files.
115
- *
116
- * Report the benchmark result {@link execute}d by
117
- * `AgenticaCallBenchmark` as markdown files, and returns a dictionary
118
- * object of the markdown reporting files. The key of the dictionary
119
- * would be file name, and the value would be the markdown content.
120
- *
121
- * For reference, the markdown files are composed like below:
122
- *
123
- * - `./README.md`
124
- * - `./scenario-1/README.md`
125
- * - `./scenario-1/1.success.md`
126
- * - `./scenario-1/2.failure.md`
127
- * - `./scenario-1/3.error.md`
128
- *
129
- * @returns Dictionary of markdown files.
130
- */
131
- public report(): Record<string, string> {
132
- if (this.result_ === null)
133
- throw new Error("Benchmark is not executed yet.");
134
- return AgenticaCallBenchmarkReporter.markdown(this.result_);
135
- }
136
-
137
- private async step(
138
- scenario: IAgenticaCallBenchmarkScenario,
139
- ): Promise<IAgenticaCallBenchmarkEvent> {
140
- const agent: Agentica = this.agent_.clone();
141
- const started_at: Date = new Date();
142
- const success = () =>
143
- AgenticaBenchmarkPredicator.success({
144
- expected: scenario.expected,
145
- operations: agent
146
- .getPromptHistories()
147
- .filter((p) => p.type === "execute"),
148
- strict: false,
149
- });
150
- const out = (): IAgenticaCallBenchmarkEvent => {
151
- const select = AgenticaBenchmarkPredicator.success({
152
- expected: scenario.expected,
153
- operations: agent
154
- .getPromptHistories()
155
- .filter((p) => p.type === "select")
156
- .map((p) => p.operations)
157
- .flat(),
158
- strict: false,
159
- });
160
- const call = success();
161
- return {
162
- type: (call ? "success" : "failure") as "failure",
163
- scenario,
164
- select,
165
- call,
166
- prompts: agent.getPromptHistories(),
167
- usage: agent.getTokenUsage(),
168
- started_at,
169
- completed_at: new Date(),
170
- } satisfies IAgenticaCallBenchmarkEvent.IFailure;
171
- };
172
-
173
- try {
174
- await agent.conversate(scenario.text);
175
- if (success()) return out();
176
- for (let i: number = 0; i < this.config_.consent; ++i) {
177
- const next: string | null =
178
- await AgenticaBenchmarkPredicator.isNext(agent);
179
- if (next === null) break;
180
-
181
- await agent.conversate(next);
182
- if (success()) return out();
183
- }
184
- return out();
185
- } catch (error) {
186
- return {
187
- type: "error",
188
- scenario,
189
- prompts: agent.getPromptHistories(),
190
- usage: agent.getTokenUsage(),
191
- error,
192
- started_at,
193
- completed_at: new Date(),
194
- };
195
- }
196
- }
197
- }
198
- export namespace AgenticaCallBenchmark {
199
- /**
200
- * Properties of the {@link AgenticaCallBenchmark} constructor.
201
- */
202
- export interface IProps {
203
- /**
204
- * AI agent instance.
205
- */
206
- agent: Agentica;
207
-
208
- /**
209
- * List of scenarios what you expect.
210
- */
211
- scenarios: IAgenticaCallBenchmarkScenario[];
212
-
213
- /**
214
- * Configuration for the benchmark.
215
- */
216
- config?: Partial<IConfig>;
217
- }
218
-
219
- /**
220
- * Configuration for the benchmark.
221
- *
222
- * `AgenticaSelectBenchmark.IConfig` is a data structure which
223
- * represents a configuration for the benchmark, especially the
224
- * capacity information of the benchmark execution.
225
- */
226
- export interface IConfig {
227
- /**
228
- * Repeat count.
229
- *
230
- * The number of repeating count for the benchmark execution
231
- * for each scenario.
232
- *
233
- * @default 10
234
- */
235
- repeat: number & tags.Type<"uint32"> & tags.Minimum<1>;
236
-
237
- /**
238
- * Simultaneous count.
239
- *
240
- * The number of simultaneous count for the parallel benchmark
241
- * execution.
242
- *
243
- * If you configure this property greater than `1`, the benchmark
244
- * for each scenario would be executed in parallel in the given
245
- * count.
246
- *
247
- * @default 10
248
- */
249
- simultaneous: number & tags.Type<"uint32"> & tags.Minimum<1>;
250
-
251
- /**
252
- * Number of consents.
253
- *
254
- * AI agent sometimes asks user to consent to the function
255
- * calling, and perform it at the next step.
256
- *
257
- * This property represents the number of consents to allow.
258
- * If the number of consents from the AI agent exceeds the
259
- * configured value, the benchmark will be failed.
260
- *
261
- * @default 3
262
- */
263
- consent: number;
264
- }
265
- }
1
+ import { Agentica } from "@agentica/core";
2
+ import { AgenticaTokenUsageAggregator } from "@agentica/core/src/internal/AgenticaTokenUsageAggregator";
3
+ import { Semaphore } from "tstl";
4
+ import { tags } from "typia";
5
+
6
+ import { AgenticaBenchmarkPredicator } from "./internal/AgenticaBenchmarkPredicator";
7
+ import { AgenticaCallBenchmarkReporter } from "./internal/AgenticaCallBenchmarkReporter";
8
+ import { IAgenticaCallBenchmarkEvent } from "./structures/IAgenticaCallBenchmarkEvent";
9
+ import { IAgenticaCallBenchmarkResult } from "./structures/IAgenticaCallBenchmarkResult";
10
+ import { IAgenticaCallBenchmarkScenario } from "./structures/IAgenticaCallBenchmarkScenario";
11
+
12
+ /**
13
+ * LLM function calling selection benchmark.
14
+ *
15
+ * `AgenticaCallBenchmark` is a class for the benchmark of the
16
+ * LLM (Large Model Language) function calling part. It utilizes both
17
+ * `selector` and `caller` agents and tests whether the expected
18
+ * {@link IAgenticaOperation operations} are properly selected and
19
+ * called from the given
20
+ * {@link IAgenticaCallBenchmarkScenario scenarios}.
21
+ *
22
+ * Note that, this `AgenticaCallBenchmark` consumes a lot of time and
23
+ * LLM token costs because it needs the whole process of the
24
+ * {@link Agentica} class with a lot of repetitions. If you don't want
25
+ * such a heavy benchmark, consider to using
26
+ * {@link AgenticaSelectBenchmark} instead. In my experience,
27
+ * {@link Agentica} does not fail to function calling, so the function
28
+ * selection benchmark is much economical.
29
+ *
30
+ * @author Samchon
31
+ */
32
+ export class AgenticaCallBenchmark {
33
+ private agent_: Agentica;
34
+ private scenarios_: IAgenticaCallBenchmarkScenario[];
35
+ private config_: AgenticaCallBenchmark.IConfig;
36
+ private result_: IAgenticaCallBenchmarkResult | null;
37
+
38
+ /**
39
+ * Initializer Constructor.
40
+ *
41
+ * @param props Properties of the selection benchmark
42
+ */
43
+ public constructor(props: AgenticaCallBenchmark.IProps) {
44
+ this.agent_ = props.agent;
45
+ this.scenarios_ = props.scenarios.slice();
46
+ this.config_ = {
47
+ repeat: props.config?.repeat ?? 10,
48
+ simultaneous: props.config?.simultaneous ?? 10,
49
+ consent: props.config?.consent ?? 3,
50
+ };
51
+ this.result_ = null;
52
+ }
53
+
54
+ /**
55
+ * Execute the benchmark.
56
+ *
57
+ * Execute the benchmark of the LLM function calling, and returns
58
+ * the result of the benchmark.
59
+ *
60
+ * If you wanna see progress of the benchmark, you can pass a callback
61
+ * function as the argument of the `listener`. The callback function
62
+ * would be called whenever a benchmark event is occurred.
63
+ *
64
+ * Also, you can publish a markdown format report by calling
65
+ * the {@link report} function after the benchmark execution.
66
+ *
67
+ * @param listener Callback function listening the benchmark events
68
+ * @returns Results of the function calling benchmark
69
+ */
70
+ public async execute(
71
+ listener?: (event: IAgenticaCallBenchmarkEvent) => void,
72
+ ): Promise<IAgenticaCallBenchmarkResult> {
73
+ const started_at: Date = new Date();
74
+ const semaphore: Semaphore = new Semaphore(this.config_.simultaneous);
75
+ const experiments: IAgenticaCallBenchmarkResult.IExperiment[] =
76
+ await Promise.all(
77
+ this.scenarios_.map(async (scenario) => {
78
+ const events: IAgenticaCallBenchmarkEvent[] = await Promise.all(
79
+ new Array(this.config_.repeat).fill(0).map(async () => {
80
+ await semaphore.acquire();
81
+ const e: IAgenticaCallBenchmarkEvent = await this.step(scenario);
82
+ await semaphore.release();
83
+ if (listener !== undefined) listener(e);
84
+ return e;
85
+ }),
86
+ );
87
+ return {
88
+ scenario,
89
+ events,
90
+ usage: events
91
+ .filter((e) => e.type !== "error")
92
+ .map((e) => e.usage)
93
+ .reduce(
94
+ AgenticaTokenUsageAggregator.plus,
95
+ AgenticaTokenUsageAggregator.zero(),
96
+ ),
97
+ };
98
+ }),
99
+ );
100
+ return (this.result_ = {
101
+ experiments,
102
+ started_at,
103
+ completed_at: new Date(),
104
+ usage: experiments
105
+ .map((p) => p.usage)
106
+ .reduce(
107
+ AgenticaTokenUsageAggregator.plus,
108
+ AgenticaTokenUsageAggregator.zero(),
109
+ ),
110
+ });
111
+ }
112
+
113
+ /**
114
+ * Report the benchmark result as markdown files.
115
+ *
116
+ * Report the benchmark result {@link execute}d by
117
+ * `AgenticaCallBenchmark` as markdown files, and returns a dictionary
118
+ * object of the markdown reporting files. The key of the dictionary
119
+ * would be file name, and the value would be the markdown content.
120
+ *
121
+ * For reference, the markdown files are composed like below:
122
+ *
123
+ * - `./README.md`
124
+ * - `./scenario-1/README.md`
125
+ * - `./scenario-1/1.success.md`
126
+ * - `./scenario-1/2.failure.md`
127
+ * - `./scenario-1/3.error.md`
128
+ *
129
+ * @returns Dictionary of markdown files.
130
+ */
131
+ public report(): Record<string, string> {
132
+ if (this.result_ === null)
133
+ throw new Error("Benchmark is not executed yet.");
134
+ return AgenticaCallBenchmarkReporter.markdown(this.result_);
135
+ }
136
+
137
+ private async step(
138
+ scenario: IAgenticaCallBenchmarkScenario,
139
+ ): Promise<IAgenticaCallBenchmarkEvent> {
140
+ const agent: Agentica = this.agent_.clone();
141
+ const started_at: Date = new Date();
142
+ const success = () =>
143
+ AgenticaBenchmarkPredicator.success({
144
+ expected: scenario.expected,
145
+ operations: agent
146
+ .getPromptHistories()
147
+ .filter((p) => p.type === "execute"),
148
+ strict: false,
149
+ });
150
+ const out = (): IAgenticaCallBenchmarkEvent => {
151
+ const select = AgenticaBenchmarkPredicator.success({
152
+ expected: scenario.expected,
153
+ operations: agent
154
+ .getPromptHistories()
155
+ .filter((p) => p.type === "select")
156
+ .map((p) => p.operations)
157
+ .flat(),
158
+ strict: false,
159
+ });
160
+ const call = success();
161
+ return {
162
+ type: (call ? "success" : "failure") as "failure",
163
+ scenario,
164
+ select,
165
+ call,
166
+ prompts: agent.getPromptHistories(),
167
+ usage: agent.getTokenUsage(),
168
+ started_at,
169
+ completed_at: new Date(),
170
+ } satisfies IAgenticaCallBenchmarkEvent.IFailure;
171
+ };
172
+
173
+ try {
174
+ await agent.conversate(scenario.text);
175
+ if (success()) return out();
176
+ for (let i: number = 0; i < this.config_.consent; ++i) {
177
+ const next: string | null =
178
+ await AgenticaBenchmarkPredicator.isNext(agent);
179
+ if (next === null) break;
180
+
181
+ await agent.conversate(next);
182
+ if (success()) return out();
183
+ }
184
+ return out();
185
+ } catch (error) {
186
+ return {
187
+ type: "error",
188
+ scenario,
189
+ prompts: agent.getPromptHistories(),
190
+ usage: agent.getTokenUsage(),
191
+ error,
192
+ started_at,
193
+ completed_at: new Date(),
194
+ };
195
+ }
196
+ }
197
+ }
198
+ export namespace AgenticaCallBenchmark {
199
+ /**
200
+ * Properties of the {@link AgenticaCallBenchmark} constructor.
201
+ */
202
+ export interface IProps {
203
+ /**
204
+ * AI agent instance.
205
+ */
206
+ agent: Agentica;
207
+
208
+ /**
209
+ * List of scenarios what you expect.
210
+ */
211
+ scenarios: IAgenticaCallBenchmarkScenario[];
212
+
213
+ /**
214
+ * Configuration for the benchmark.
215
+ */
216
+ config?: Partial<IConfig>;
217
+ }
218
+
219
+ /**
220
+ * Configuration for the benchmark.
221
+ *
222
+ * `AgenticaSelectBenchmark.IConfig` is a data structure which
223
+ * represents a configuration for the benchmark, especially the
224
+ * capacity information of the benchmark execution.
225
+ */
226
+ export interface IConfig {
227
+ /**
228
+ * Repeat count.
229
+ *
230
+ * The number of repeating count for the benchmark execution
231
+ * for each scenario.
232
+ *
233
+ * @default 10
234
+ */
235
+ repeat: number & tags.Type<"uint32"> & tags.Minimum<1>;
236
+
237
+ /**
238
+ * Simultaneous count.
239
+ *
240
+ * The number of simultaneous count for the parallel benchmark
241
+ * execution.
242
+ *
243
+ * If you configure this property greater than `1`, the benchmark
244
+ * for each scenario would be executed in parallel in the given
245
+ * count.
246
+ *
247
+ * @default 10
248
+ */
249
+ simultaneous: number & tags.Type<"uint32"> & tags.Minimum<1>;
250
+
251
+ /**
252
+ * Number of consents.
253
+ *
254
+ * AI agent sometimes asks user to consent to the function
255
+ * calling, and perform it at the next step.
256
+ *
257
+ * This property represents the number of consents to allow.
258
+ * If the number of consents from the AI agent exceeds the
259
+ * configured value, the benchmark will be failed.
260
+ *
261
+ * @default 3
262
+ */
263
+ consent: number;
264
+ }
265
+ }