@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,220 +1,220 @@
1
- import { Agentica, AgenticaOperation, AgenticaPrompt } 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: AgenticaPrompt<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
- ].vendor.api.chat.completions.create(
24
- {
25
- model: agent["props"].vendor.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"].vendor.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<AgenticaOperation<Model>>;
88
-
89
- /**
90
- * If it's `false`, check the array and let it go even if there's something wrong between them.
91
- *
92
- * @default `false`
93
- */
94
- strict?: boolean;
95
- }): boolean => successInner(props).result;
96
-
97
- const successInner = <Model extends ILlmSchema.Model>(
98
- props: Parameters<typeof success<Model>>[0],
99
- ):
100
- | {
101
- result: true;
102
- take: number;
103
- }
104
- | {
105
- result: false;
106
- } => {
107
- const call = (
108
- expected: IAgenticaBenchmarkExpected<Model>,
109
- overrideOperations?: Array<AgenticaOperation<Model>>,
110
- ) =>
111
- successInner({
112
- expected,
113
- operations: overrideOperations ?? props.operations,
114
- strict: props.strict,
115
- });
116
-
117
- switch (props.expected.type) {
118
- case "array": {
119
- let take = 0;
120
- const targetIterator = props.expected.items[Symbol.iterator]();
121
- let targeted = targetIterator.next();
122
-
123
- while (true) {
124
- if (targeted.done) {
125
- return {
126
- result: true,
127
- take,
128
- };
129
- }
130
- if (take >= props.operations.length) {
131
- return { result: false };
132
- }
133
-
134
- const result = call(targeted.value, props.operations.slice(take));
135
- if (!result.result) {
136
- if (!props.strict) {
137
- take += 1;
138
- continue;
139
- }
140
- return { result: false };
141
- }
142
-
143
- take += result.take;
144
- targeted = targetIterator.next();
145
- }
146
- }
147
- case "standalone": {
148
- const target = props.expected.operation;
149
- const result = props.operations.some((op) => op.name === target.name);
150
- if (result) {
151
- return { result, take: 1 };
152
- }
153
- return {
154
- result,
155
- };
156
- }
157
- case "anyOf":
158
- for (const expected of props.expected.anyOf) {
159
- const callResult = call(expected);
160
- if (callResult.result) {
161
- return callResult;
162
- }
163
- }
164
-
165
- return { result: false };
166
- case "allOf": {
167
- /**
168
- * @example
169
- * expected = [4, 2];
170
- * called = [1, 2, 3, 4, 5];
171
- *
172
- * { result: true, take: 3 };
173
- */
174
- const result = props.expected.allOf.map((expected) => call(expected));
175
- if (result.every((r) => r.result)) {
176
- return {
177
- result: true,
178
- take: result.reduce((acc, r) => Math.max(acc, r.take), 0),
179
- };
180
- }
181
-
182
- return {
183
- result: false,
184
- };
185
- }
186
- }
187
- };
188
- }
189
-
190
- interface IPredicatorApplication {
191
- /**
192
- * Ask user to consent for what the AI agent wants to do next.
193
- *
194
- * If AI agent wants to do some function calling at next,
195
- * but it needs the user's consent about the function calling to do,
196
- * then call this tool function.
197
- *
198
- * @param props Properties for asking the user's consent
199
- */
200
- consent(props: IConsentProps): void;
201
- }
202
-
203
- /**
204
- * Properties for asking the user's consent
205
- */
206
- interface IConsentProps {
207
- /**
208
- * Reason of the message implying what the AI agent wants
209
- * to do at the next step after the user's consent.
210
- */
211
- content: string;
212
-
213
- /**
214
- * Recommended reply message for the user.
215
- *
216
- * The message what AI agent wants the user to reply
217
- * accepting the AI agent's next job suggestion.
218
- */
219
- reply: string;
220
- }
1
+ import { Agentica, AgenticaOperation, AgenticaPrompt } 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: AgenticaPrompt<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
+ ].vendor.api.chat.completions.create(
24
+ {
25
+ model: agent["props"].vendor.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"].vendor.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<AgenticaOperation<Model>>;
88
+
89
+ /**
90
+ * If it's `false`, check the array and let it go even if there's something wrong between them.
91
+ *
92
+ * @default `false`
93
+ */
94
+ strict?: boolean;
95
+ }): boolean => successInner(props).result;
96
+
97
+ const successInner = <Model extends ILlmSchema.Model>(
98
+ props: Parameters<typeof success<Model>>[0],
99
+ ):
100
+ | {
101
+ result: true;
102
+ take: number;
103
+ }
104
+ | {
105
+ result: false;
106
+ } => {
107
+ const call = (
108
+ expected: IAgenticaBenchmarkExpected<Model>,
109
+ overrideOperations?: Array<AgenticaOperation<Model>>,
110
+ ) =>
111
+ successInner({
112
+ expected,
113
+ operations: overrideOperations ?? props.operations,
114
+ strict: props.strict,
115
+ });
116
+
117
+ switch (props.expected.type) {
118
+ case "array": {
119
+ let take = 0;
120
+ const targetIterator = props.expected.items[Symbol.iterator]();
121
+ let targeted = targetIterator.next();
122
+
123
+ while (true) {
124
+ if (targeted.done) {
125
+ return {
126
+ result: true,
127
+ take,
128
+ };
129
+ }
130
+ if (take >= props.operations.length) {
131
+ return { result: false };
132
+ }
133
+
134
+ const result = call(targeted.value, props.operations.slice(take));
135
+ if (!result.result) {
136
+ if (!props.strict) {
137
+ take += 1;
138
+ continue;
139
+ }
140
+ return { result: false };
141
+ }
142
+
143
+ take += result.take;
144
+ targeted = targetIterator.next();
145
+ }
146
+ }
147
+ case "standalone": {
148
+ const target = props.expected.operation;
149
+ const result = props.operations.some((op) => op.name === target.name);
150
+ if (result) {
151
+ return { result, take: 1 };
152
+ }
153
+ return {
154
+ result,
155
+ };
156
+ }
157
+ case "anyOf":
158
+ for (const expected of props.expected.anyOf) {
159
+ const callResult = call(expected);
160
+ if (callResult.result) {
161
+ return callResult;
162
+ }
163
+ }
164
+
165
+ return { result: false };
166
+ case "allOf": {
167
+ /**
168
+ * @example
169
+ * expected = [4, 2];
170
+ * called = [1, 2, 3, 4, 5];
171
+ *
172
+ * { result: true, take: 3 };
173
+ */
174
+ const result = props.expected.allOf.map((expected) => call(expected));
175
+ if (result.every((r) => r.result)) {
176
+ return {
177
+ result: true,
178
+ take: result.reduce((acc, r) => Math.max(acc, r.take), 0),
179
+ };
180
+ }
181
+
182
+ return {
183
+ result: false,
184
+ };
185
+ }
186
+ }
187
+ };
188
+ }
189
+
190
+ interface IPredicatorApplication {
191
+ /**
192
+ * Ask user to consent for what the AI agent wants to do next.
193
+ *
194
+ * If AI agent wants to do some function calling at next,
195
+ * but it needs the user's consent about the function calling to do,
196
+ * then call this tool function.
197
+ *
198
+ * @param props Properties for asking the user's consent
199
+ */
200
+ consent(props: IConsentProps): void;
201
+ }
202
+
203
+ /**
204
+ * Properties for asking the user's consent
205
+ */
206
+ interface IConsentProps {
207
+ /**
208
+ * Reason of the message implying what the AI agent wants
209
+ * to do at the next step after the user's consent.
210
+ */
211
+ content: string;
212
+
213
+ /**
214
+ * Recommended reply message for the user.
215
+ *
216
+ * The message what AI agent wants the user to reply
217
+ * accepting the AI agent's next job suggestion.
218
+ */
219
+ reply: string;
220
+ }
@@ -1,44 +1,44 @@
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
- }
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
+ }