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