@agentica/benchmark 0.12.1 → 0.12.2-dev.20250314

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