@agentica/benchmark 0.8.3 → 0.9.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.
Files changed (42) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +326 -324
  3. package/lib/AgenticaCallBenchmark.d.ts +7 -6
  4. package/lib/AgenticaCallBenchmark.js.map +1 -1
  5. package/lib/AgenticaSelectBenchmark.d.ts +7 -6
  6. package/lib/AgenticaSelectBenchmark.js.map +1 -1
  7. package/lib/index.mjs +46 -1
  8. package/lib/index.mjs.map +1 -1
  9. package/lib/internal/AgenticaBenchmarkPredicator.d.ts +5 -4
  10. package/lib/internal/AgenticaBenchmarkPredicator.js +74 -2
  11. package/lib/internal/AgenticaBenchmarkPredicator.js.map +1 -1
  12. package/lib/internal/AgenticaBenchmarkUtil.d.ts +2 -1
  13. package/lib/internal/AgenticaBenchmarkUtil.js.map +1 -1
  14. package/lib/internal/AgenticaCallBenchmarkReporter.d.ts +2 -1
  15. package/lib/internal/AgenticaCallBenchmarkReporter.js.map +1 -1
  16. package/lib/internal/AgenticaPromptReporter.d.ts +2 -1
  17. package/lib/internal/AgenticaPromptReporter.js.map +1 -1
  18. package/lib/internal/AgenticaSelectBenchmarkReporter.js.map +1 -1
  19. package/lib/structures/IAgenticaBenchmarkExpected.d.ts +10 -9
  20. package/lib/structures/IAgenticaCallBenchmarkEvent.d.ts +8 -7
  21. package/lib/structures/IAgenticaCallBenchmarkResult.d.ts +6 -5
  22. package/lib/structures/IAgenticaCallBenchmarkScenario.d.ts +3 -2
  23. package/lib/structures/IAgenticaSelectBenchmarkEvent.d.ts +9 -8
  24. package/lib/structures/IAgenticaSelectBenchmarkResult.d.ts +6 -5
  25. package/lib/structures/IAgenticaSelectBenchmarkScenario.d.ts +3 -2
  26. package/package.json +5 -5
  27. package/src/AgenticaCallBenchmark.ts +268 -265
  28. package/src/AgenticaSelectBenchmark.ts +256 -254
  29. package/src/index.ts +3 -3
  30. package/src/internal/AgenticaBenchmarkPredicator.ts +224 -216
  31. package/src/internal/AgenticaBenchmarkUtil.ts +44 -40
  32. package/src/internal/AgenticaCallBenchmarkReporter.ts +183 -180
  33. package/src/internal/AgenticaPromptReporter.ts +46 -43
  34. package/src/internal/AgenticaSelectBenchmarkReporter.ts +213 -210
  35. package/src/structures/IAgenticaBenchmarkExpected.ts +68 -58
  36. package/src/structures/IAgenticaCallBenchmarkEvent.ts +113 -109
  37. package/src/structures/IAgenticaCallBenchmarkResult.ts +70 -69
  38. package/src/structures/IAgenticaCallBenchmarkScenario.ts +43 -39
  39. package/src/structures/IAgenticaSelectBenchmarkEvent.ts +114 -110
  40. package/src/structures/IAgenticaSelectBenchmarkResult.ts +72 -69
  41. package/src/structures/IAgenticaSelectBenchmarkScenario.ts +43 -39
  42. package/src/utils/MathUtil.ts +3 -3
@@ -1,216 +1,224 @@
1
- import { Agentica, IAgenticaOperation, IAgenticaPrompt } from "@agentica/core";
2
- import { ILlmFunction } from "@samchon/openapi";
3
- import OpenAI from "openai";
4
- import typia from "typia";
5
-
6
- import { IAgenticaBenchmarkExpected } from "../structures/IAgenticaBenchmarkExpected";
7
-
8
- export namespace AgenticaBenchmarkPredicator {
9
- export const isNext = async (agent: Agentica): Promise<string | null> => {
10
- const last: IAgenticaPrompt | undefined = agent.getPromptHistories().at(-1);
11
- if (last?.type !== "text" || last.role !== "assistant") return null;
12
-
13
- const consent: ILlmFunction<"chatgpt"> = typia.llm.application<
14
- IPredicatorApplication,
15
- "chatgpt"
16
- >().functions[0]!;
17
- const result: OpenAI.ChatCompletion = await agent[
18
- "props"
19
- ].provider.api.chat.completions.create(
20
- {
21
- model: agent["props"].provider.model,
22
- messages: [
23
- {
24
- role: "system",
25
- content: [
26
- "You are an helpful assistant.",
27
- "",
28
- "If what the assistant said seems like to asking for",
29
- "user's consent about some function calling at the next step,",
30
- "use the tools appropriately to step to the next.",
31
- ].join("\n"),
32
- },
33
- {
34
- role: "assistant",
35
- content: last.text,
36
- },
37
- ],
38
- tools: [
39
- {
40
- type: "function",
41
- function: {
42
- name: consent.name,
43
- description: consent.description,
44
- parameters: consent.parameters as Record<string, any>,
45
- },
46
- },
47
- ],
48
- tool_choice: "required",
49
- parallel_tool_calls: false,
50
- },
51
- agent["props"].provider.options,
52
- );
53
- const toolCall: OpenAI.ChatCompletionMessageToolCall | undefined = (
54
- result.choices[0]?.message.tool_calls ?? []
55
- ).filter(
56
- (tc) => tc.type === "function" && tc.function.name === consent.name,
57
- )?.[0];
58
- if (toolCall === undefined) return null;
59
- const input: IConsentProps = JSON.parse(toolCall.function.arguments);
60
- return typia.is(input) ? input.reply : null;
61
- };
62
-
63
- /**
64
- * Check if the called operations match the expected operations.
65
- *
66
- * @param props Properties for checking the match of the called operations
67
- * and the expected operations
68
- *
69
- * @returns `true` if the called operations match the expected operations,
70
- * otherwise `false`.
71
- */
72
- export const success = (props: {
73
- /**
74
- * Expected operations to be called.
75
- *
76
- * For 'allOf' within an 'array', the next expected element starts checking from the element that follows the last called element in 'allOf'.
77
- */
78
- expected: IAgenticaBenchmarkExpected;
79
-
80
- /**
81
- * Specified operations.
82
- */
83
- operations: Array<IAgenticaOperation | IAgenticaPrompt.IExecute>;
84
-
85
- /**
86
- * If it's `false`, check the array and let it go even if there's something wrong between them.
87
- *
88
- * @default `false`
89
- */
90
- strict?: boolean;
91
- }): boolean => successInner(props).result;
92
-
93
- const successInner = (
94
- props: Parameters<typeof success>[0],
95
- ):
96
- | {
97
- result: true;
98
- take: number;
99
- }
100
- | {
101
- result: false;
102
- } => {
103
- const call = (
104
- expected: IAgenticaBenchmarkExpected,
105
- overrideOperations?: Array<IAgenticaOperation | IAgenticaPrompt.IExecute>,
106
- ) =>
107
- successInner({
108
- expected,
109
- operations: overrideOperations ?? props.operations,
110
- strict: props.strict,
111
- });
112
-
113
- switch (props.expected.type) {
114
- case "array": {
115
- let take = 0;
116
- const targetIterator = props.expected.items[Symbol.iterator]();
117
- let targeted = targetIterator.next();
118
-
119
- while (true) {
120
- if (targeted.done) {
121
- return {
122
- result: true,
123
- take,
124
- };
125
- }
126
- if (take >= props.operations.length) {
127
- return { result: false };
128
- }
129
-
130
- const result = call(targeted.value, props.operations.slice(take));
131
- if (!result.result) {
132
- if (!props.strict) {
133
- take += 1;
134
- continue;
135
- }
136
- return { result: false };
137
- }
138
-
139
- take += result.take;
140
- targeted = targetIterator.next();
141
- }
142
- }
143
- case "standalone": {
144
- const target = props.expected.operation;
145
- const result = props.operations.some((op) => op.name === target.name);
146
- if (result) {
147
- return { result, take: 1 };
148
- }
149
- return {
150
- result,
151
- };
152
- }
153
- case "anyOf":
154
- for (const expected of props.expected.anyOf) {
155
- const callResult = call(expected);
156
- if (callResult.result) {
157
- return callResult;
158
- }
159
- }
160
-
161
- return { result: false };
162
- case "allOf": {
163
- /**
164
- * @example
165
- * expected = [4, 2];
166
- * called = [1, 2, 3, 4, 5];
167
- *
168
- * { result: true, take: 3 };
169
- */
170
- const result = props.expected.allOf.map((expected) => call(expected));
171
- if (result.every((r) => r.result)) {
172
- return {
173
- result: true,
174
- take: result.reduce((acc, r) => Math.max(acc, r.take), 0),
175
- };
176
- }
177
-
178
- return {
179
- result: false,
180
- };
181
- }
182
- }
183
- };
184
- }
185
-
186
- interface IPredicatorApplication {
187
- /**
188
- * Ask user to consent for what the AI agent wants to do next.
189
- *
190
- * If AI agent wants to do some function calling at next,
191
- * but it needs the user's consent about the function calling to do,
192
- * then call this tool function.
193
- *
194
- * @param props Properties for asking the user's consent
195
- */
196
- consent(props: IConsentProps): void;
197
- }
198
-
199
- /**
200
- * Properties for asking the user's consent
201
- */
202
- interface IConsentProps {
203
- /**
204
- * Reason of the message implying what the AI agent wants
205
- * to do at the next step after the user's consent.
206
- */
207
- content: string;
208
-
209
- /**
210
- * Recommended reply message for the user.
211
- *
212
- * The message what AI agent wants the user to reply
213
- * accepting the AI agent's next job suggestion.
214
- */
215
- reply: string;
216
- }
1
+ import { Agentica, IAgenticaOperation, IAgenticaPrompt } from "@agentica/core";
2
+ import { ILlmFunction, ILlmSchema } from "@samchon/openapi";
3
+ import OpenAI from "openai";
4
+ import typia from "typia";
5
+
6
+ import { IAgenticaBenchmarkExpected } from "../structures/IAgenticaBenchmarkExpected";
7
+
8
+ export namespace AgenticaBenchmarkPredicator {
9
+ export const isNext = async <Model extends ILlmSchema.Model>(
10
+ agent: Agentica<Model>,
11
+ ): Promise<string | null> => {
12
+ const last: IAgenticaPrompt<Model> | undefined = agent
13
+ .getPromptHistories()
14
+ .at(-1);
15
+ if (last?.type !== "text" || last.role !== "assistant") return null;
16
+
17
+ const consent: ILlmFunction<"chatgpt"> = typia.llm.application<
18
+ IPredicatorApplication,
19
+ "chatgpt"
20
+ >().functions[0]!;
21
+ const result: OpenAI.ChatCompletion = await agent[
22
+ "props"
23
+ ].provider.api.chat.completions.create(
24
+ {
25
+ model: agent["props"].provider.model,
26
+ messages: [
27
+ {
28
+ role: "system",
29
+ content: [
30
+ "You are an helpful assistant.",
31
+ "",
32
+ "If what the assistant said seems like to asking for",
33
+ "user's consent about some function calling at the next step,",
34
+ "use the tools appropriately to step to the next.",
35
+ ].join("\n"),
36
+ },
37
+ {
38
+ role: "assistant",
39
+ content: last.text,
40
+ },
41
+ ],
42
+ tools: [
43
+ {
44
+ type: "function",
45
+ function: {
46
+ name: consent.name,
47
+ description: consent.description,
48
+ parameters: consent.parameters as Record<string, any>,
49
+ },
50
+ },
51
+ ],
52
+ tool_choice: "required",
53
+ parallel_tool_calls: false,
54
+ },
55
+ agent["props"].provider.options,
56
+ );
57
+ const toolCall: OpenAI.ChatCompletionMessageToolCall | undefined = (
58
+ result.choices[0]?.message.tool_calls ?? []
59
+ ).filter(
60
+ (tc) => tc.type === "function" && tc.function.name === consent.name,
61
+ )?.[0];
62
+ if (toolCall === undefined) return null;
63
+ const input: IConsentProps = JSON.parse(toolCall.function.arguments);
64
+ return typia.is(input) ? input.reply : null;
65
+ };
66
+
67
+ /**
68
+ * Check if the called operations match the expected operations.
69
+ *
70
+ * @param props Properties for checking the match of the called operations
71
+ * and the expected operations
72
+ *
73
+ * @returns `true` if the called operations match the expected operations,
74
+ * otherwise `false`.
75
+ */
76
+ export const success = <Model extends ILlmSchema.Model>(props: {
77
+ /**
78
+ * Expected operations to be called.
79
+ *
80
+ * For 'allOf' within an 'array', the next expected element starts checking from the element that follows the last called element in 'allOf'.
81
+ */
82
+ expected: IAgenticaBenchmarkExpected<Model>;
83
+
84
+ /**
85
+ * Specified operations.
86
+ */
87
+ operations: Array<
88
+ IAgenticaOperation<Model> | IAgenticaPrompt.IExecute<Model>
89
+ >;
90
+
91
+ /**
92
+ * If it's `false`, check the array and let it go even if there's something wrong between them.
93
+ *
94
+ * @default `false`
95
+ */
96
+ strict?: boolean;
97
+ }): boolean => successInner(props).result;
98
+
99
+ const successInner = <Model extends ILlmSchema.Model>(
100
+ props: Parameters<typeof success<Model>>[0],
101
+ ):
102
+ | {
103
+ result: true;
104
+ take: number;
105
+ }
106
+ | {
107
+ result: false;
108
+ } => {
109
+ const call = (
110
+ expected: IAgenticaBenchmarkExpected<Model>,
111
+ overrideOperations?: Array<
112
+ IAgenticaOperation<Model> | IAgenticaPrompt.IExecute<Model>
113
+ >,
114
+ ) =>
115
+ successInner({
116
+ expected,
117
+ operations: overrideOperations ?? props.operations,
118
+ strict: props.strict,
119
+ });
120
+
121
+ switch (props.expected.type) {
122
+ case "array": {
123
+ let take = 0;
124
+ const targetIterator = props.expected.items[Symbol.iterator]();
125
+ let targeted = targetIterator.next();
126
+
127
+ while (true) {
128
+ if (targeted.done) {
129
+ return {
130
+ result: true,
131
+ take,
132
+ };
133
+ }
134
+ if (take >= props.operations.length) {
135
+ return { result: false };
136
+ }
137
+
138
+ const result = call(targeted.value, props.operations.slice(take));
139
+ if (!result.result) {
140
+ if (!props.strict) {
141
+ take += 1;
142
+ continue;
143
+ }
144
+ return { result: false };
145
+ }
146
+
147
+ take += result.take;
148
+ targeted = targetIterator.next();
149
+ }
150
+ }
151
+ case "standalone": {
152
+ const target = props.expected.operation;
153
+ const result = props.operations.some((op) => op.name === target.name);
154
+ if (result) {
155
+ return { result, take: 1 };
156
+ }
157
+ return {
158
+ result,
159
+ };
160
+ }
161
+ case "anyOf":
162
+ for (const expected of props.expected.anyOf) {
163
+ const callResult = call(expected);
164
+ if (callResult.result) {
165
+ return callResult;
166
+ }
167
+ }
168
+
169
+ return { result: false };
170
+ case "allOf": {
171
+ /**
172
+ * @example
173
+ * expected = [4, 2];
174
+ * called = [1, 2, 3, 4, 5];
175
+ *
176
+ * { result: true, take: 3 };
177
+ */
178
+ const result = props.expected.allOf.map((expected) => call(expected));
179
+ if (result.every((r) => r.result)) {
180
+ return {
181
+ result: true,
182
+ take: result.reduce((acc, r) => Math.max(acc, r.take), 0),
183
+ };
184
+ }
185
+
186
+ return {
187
+ result: false,
188
+ };
189
+ }
190
+ }
191
+ };
192
+ }
193
+
194
+ interface IPredicatorApplication {
195
+ /**
196
+ * Ask user to consent for what the AI agent wants to do next.
197
+ *
198
+ * If AI agent wants to do some function calling at next,
199
+ * but it needs the user's consent about the function calling to do,
200
+ * then call this tool function.
201
+ *
202
+ * @param props Properties for asking the user's consent
203
+ */
204
+ consent(props: IConsentProps): void;
205
+ }
206
+
207
+ /**
208
+ * Properties for asking the user's consent
209
+ */
210
+ interface IConsentProps {
211
+ /**
212
+ * Reason of the message implying what the AI agent wants
213
+ * to do at the next step after the user's consent.
214
+ */
215
+ content: string;
216
+
217
+ /**
218
+ * Recommended reply message for the user.
219
+ *
220
+ * The message what AI agent wants the user to reply
221
+ * accepting the AI agent's next job suggestion.
222
+ */
223
+ reply: string;
224
+ }
@@ -1,40 +1,44 @@
1
- import { IAgenticaBenchmarkExpected } from "../structures/IAgenticaBenchmarkExpected";
2
-
3
- export namespace AgenticaBenchmarkUtil {
4
- export const errorToJson = (error: any): any => {
5
- if (error instanceof Error)
6
- return {
7
- ...error,
8
- name: error.name,
9
- message: error.message,
10
- stack: error.stack,
11
- };
12
- return error;
13
- };
14
-
15
- export const expectedToJson = (expected: IAgenticaBenchmarkExpected): any => {
16
- if (expected.type === "standalone")
17
- return {
18
- type: expected.type,
19
- operation: {
20
- name: expected.operation.name,
21
- description: expected.operation.function.description,
22
- },
23
- };
24
- else if (expected.type === "array")
25
- return {
26
- type: expected.type,
27
- items: expected.items.map(expectedToJson),
28
- };
29
- else if (expected.type === "allOf")
30
- return {
31
- type: expected.type,
32
- allOf: expected.allOf.map(expectedToJson),
33
- };
34
- else
35
- return {
36
- type: expected.type,
37
- anyOf: expected.anyOf.map(expectedToJson),
38
- };
39
- };
40
- }
1
+ import { ILlmSchema } from "@samchon/openapi";
2
+
3
+ import { IAgenticaBenchmarkExpected } from "../structures/IAgenticaBenchmarkExpected";
4
+
5
+ export namespace AgenticaBenchmarkUtil {
6
+ export const errorToJson = (error: any): any => {
7
+ if (error instanceof Error)
8
+ return {
9
+ ...error,
10
+ name: error.name,
11
+ message: error.message,
12
+ stack: error.stack,
13
+ };
14
+ return error;
15
+ };
16
+
17
+ export const expectedToJson = <Model extends ILlmSchema.Model>(
18
+ expected: IAgenticaBenchmarkExpected<Model>,
19
+ ): any => {
20
+ if (expected.type === "standalone")
21
+ return {
22
+ type: expected.type,
23
+ operation: {
24
+ name: expected.operation.name,
25
+ description: expected.operation.function.description,
26
+ },
27
+ };
28
+ else if (expected.type === "array")
29
+ return {
30
+ type: expected.type,
31
+ items: expected.items.map(expectedToJson),
32
+ };
33
+ else if (expected.type === "allOf")
34
+ return {
35
+ type: expected.type,
36
+ allOf: expected.allOf.map(expectedToJson),
37
+ };
38
+ else
39
+ return {
40
+ type: expected.type,
41
+ anyOf: expected.anyOf.map(expectedToJson),
42
+ };
43
+ };
44
+ }