graphai 0.0.12 → 0.1.1

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 (98) hide show
  1. package/README.md +14 -10
  2. package/lib/experimental_agents/array_agents/index.d.ts +3 -0
  3. package/lib/experimental_agents/array_agents/index.js +9 -0
  4. package/lib/experimental_agents/array_agents/pop_agent.d.ts +2 -0
  5. package/lib/experimental_agents/array_agents/pop_agent.js +15 -0
  6. package/lib/experimental_agents/array_agents/push_agent.d.ts +2 -0
  7. package/lib/experimental_agents/array_agents/push_agent.js +14 -0
  8. package/lib/experimental_agents/array_agents/shift_agent.d.ts +2 -0
  9. package/lib/experimental_agents/{array_agents.js → array_agents/shift_agent.js} +2 -17
  10. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.d.ts +2 -0
  11. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.js +13 -0
  12. package/lib/experimental_agents/data_agents/data_sum_template_agent.d.ts +17 -0
  13. package/lib/experimental_agents/data_agents/data_sum_template_agent.js +30 -0
  14. package/lib/experimental_agents/data_agents/index.d.ts +3 -0
  15. package/lib/experimental_agents/data_agents/index.js +9 -0
  16. package/lib/experimental_agents/data_agents/total_agent.d.ts +46 -0
  17. package/lib/experimental_agents/data_agents/total_agent.js +51 -0
  18. package/lib/experimental_agents/embedding_agent.d.ts +6 -0
  19. package/lib/experimental_agents/embedding_agent.js +43 -0
  20. package/lib/experimental_agents/index.d.ts +8 -3
  21. package/lib/experimental_agents/index.js +8 -3
  22. package/lib/experimental_agents/map_agent.d.ts +4 -0
  23. package/lib/experimental_agents/map_agent.js +46 -0
  24. package/lib/experimental_agents/matrix_agent.d.ts +9 -0
  25. package/lib/experimental_agents/matrix_agent.js +50 -0
  26. package/lib/experimental_agents/nested_agent.d.ts +2 -2
  27. package/lib/experimental_agents/nested_agent.js +14 -8
  28. package/lib/experimental_agents/slashgpt_agent.d.ts +1 -1
  29. package/lib/experimental_agents/slashgpt_agent.js +2 -7
  30. package/lib/experimental_agents/sleeper_agents/index.d.ts +2 -0
  31. package/lib/experimental_agents/sleeper_agents/index.js +7 -0
  32. package/lib/experimental_agents/sleeper_agents/sleeper_agent.d.ts +5 -0
  33. package/lib/experimental_agents/sleeper_agents/sleeper_agent.js +16 -0
  34. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.d.ts +6 -0
  35. package/lib/experimental_agents/{sleeper_agent.js → sleeper_agents/sleeper_agent_debug.js} +4 -13
  36. package/lib/experimental_agents/string_agents/index.d.ts +2 -0
  37. package/lib/experimental_agents/string_agents/index.js +7 -0
  38. package/lib/experimental_agents/string_agents/string_splitter_agent.d.ts +7 -0
  39. package/lib/experimental_agents/string_agents/string_splitter_agent.js +24 -0
  40. package/lib/experimental_agents/string_agents/string_template_agent.d.ts +4 -0
  41. package/lib/experimental_agents/{string_agent.js → string_agents/string_template_agent.js} +2 -5
  42. package/lib/experimental_agents/test_agents/bypass_agent.d.ts +2 -0
  43. package/lib/experimental_agents/test_agents/bypass_agent.js +10 -0
  44. package/lib/experimental_agents/test_agents/copy_message_agent.d.ts +2 -0
  45. package/lib/experimental_agents/test_agents/copy_message_agent.js +11 -0
  46. package/lib/experimental_agents/test_agents/counting_agent.d.ts +2 -0
  47. package/lib/experimental_agents/test_agents/counting_agent.js +11 -0
  48. package/lib/experimental_agents/test_agents/echo_agent.d.ts +2 -0
  49. package/lib/experimental_agents/test_agents/echo_agent.js +7 -0
  50. package/lib/experimental_agents/test_agents/echo_fork_index_agent.d.ts +2 -0
  51. package/lib/experimental_agents/test_agents/echo_fork_index_agent.js +7 -0
  52. package/lib/experimental_agents/test_agents/index.d.ts +5 -0
  53. package/lib/experimental_agents/test_agents/index.js +13 -0
  54. package/lib/experimental_agents/test_agents/merge_node_id_agent.d.ts +2 -0
  55. package/lib/experimental_agents/test_agents/merge_node_id_agent.js +10 -0
  56. package/lib/experimental_agents/token_agent.d.ts +6 -0
  57. package/lib/experimental_agents/token_agent.js +33 -0
  58. package/lib/graphai.d.ts +28 -111
  59. package/lib/graphai.js +140 -322
  60. package/lib/index.d.ts +2 -0
  61. package/lib/log.d.ts +22 -0
  62. package/lib/log.js +49 -0
  63. package/lib/node.d.ts +50 -0
  64. package/lib/node.js +218 -0
  65. package/lib/task.d.ts +18 -0
  66. package/lib/task.js +63 -0
  67. package/lib/task_manager.d.ts +20 -0
  68. package/lib/task_manager.js +69 -0
  69. package/lib/transaction_log.d.ts +27 -0
  70. package/lib/transaction_log.js +56 -0
  71. package/lib/type.d.ts +77 -0
  72. package/lib/type.js +14 -0
  73. package/lib/utils/utils.d.ts +4 -0
  74. package/lib/utils/utils.js +22 -1
  75. package/lib/validator.d.ts +2 -0
  76. package/lib/validator.js +28 -0
  77. package/lib/validators/agent_validator.d.ts +1 -0
  78. package/lib/validators/agent_validator.js +12 -0
  79. package/lib/validators/common.d.ts +3 -0
  80. package/lib/validators/common.js +6 -0
  81. package/lib/validators/computed_node_validator.d.ts +2 -0
  82. package/lib/validators/computed_node_validator.js +13 -0
  83. package/lib/validators/graph_data_validator.d.ts +3 -0
  84. package/lib/validators/graph_data_validator.js +43 -0
  85. package/lib/validators/nodeValidator.d.ts +2 -0
  86. package/lib/validators/nodeValidator.js +13 -0
  87. package/lib/validators/relation_validator.d.ts +2 -0
  88. package/lib/validators/relation_validator.js +62 -0
  89. package/lib/validators/static_node_validator.d.ts +2 -0
  90. package/lib/validators/static_node_validator.js +13 -0
  91. package/package.json +3 -5
  92. package/lib/experimental_agents/array_agents.d.ts +0 -4
  93. package/lib/experimental_agents/data_agent.d.ts +0 -3
  94. package/lib/experimental_agents/data_agent.js +0 -25
  95. package/lib/experimental_agents/sleeper_agent.d.ts +0 -10
  96. package/lib/experimental_agents/string_agent.d.ts +0 -7
  97. package/lib/graphai_cli.d.ts +0 -2
  98. package/lib/graphai_cli.js +0 -37
package/README.md CHANGED
@@ -11,12 +11,15 @@ Here is an example:
11
11
  ```YAML
12
12
  nodes:
13
13
  taskA:
14
+ agentId: sample
14
15
  params:
15
16
  // agent-specific parameters for taskA
16
17
  taskB:
18
+ agentId: sample
17
19
  params:
18
20
  // agent-specific parameters for taskB
19
21
  taskC:
22
+ agentId: sample
20
23
  params:
21
24
  // agent-specific parameters for taskC
22
25
  inputs: [taskA, taskB]
@@ -37,7 +40,7 @@ const sampleAgentFunction = async (context: AgentFunctionContext) => {
37
40
  ...
38
41
  const file = fs.readFileSync(pathToYamlFile, "utf8");
39
42
  const graphData = YAML.parse(file);
40
- const graph = new GraphAI(graphData, sampleAgentFunction);
43
+ const graph = new GraphAI(graphData, { sample: sampleAgentFunction });
41
44
  const results = await graph.run();
42
45
  return results["taskC"];
43
46
  ```
@@ -48,7 +51,7 @@ As Andrew Ng has described in his article, "[The batch: Issue 242](https://www.d
48
51
 
49
52
  Building applications that employ these workflows, however, is challenging due to the complexities of managing multiple asynchronous API calls, including error handling, timeouts, retries, and logging.
50
53
 
51
- GraphAI is designed to simplify this process by decoupling the complexity of multiple asynchronous calls from the application's core logic. It enables developers to model these calls and their dependencies within a single Data Flow Graph, enhancing development and debugging processes.
54
+ GraphAI is designed to simplify this process by decoupling the complexity of multiple asynchronous calls from the application's core logic. It enables developers to model these calls and their dependencies within a single acyclic Data Flow Graph, enhancing development and debugging processes.
52
55
 
53
56
  Furthermore, GraphAI's robust mechanisms for error handling, retry strategies, timeouts, and logging empower developers to concentrate on refining the application logic.
54
57
 
@@ -114,13 +117,14 @@ Key principles:
114
117
 
115
118
  A Data Flow Graph (DFG) is a JavaScript object, which defines the flow of data. It is typically described in YAML file and loaded at runtime.
116
119
 
117
- A DFG consists of a collection of 'nodes', which contains a series of nested keys representing individual nodes in the data flow. Each node is identified by a unique key (e.g., node1, node2) and can contain several predefined keys (params, inputs, outputs, retry, timeout, source, agentId, fork, value) that dictate the node's behavior and its relationship with other nodes.
120
+ A DFG consists of a collection of 'nodes', which contains a series of nested properties representing individual nodes in the data flow. Each node is identified by a unique key, *nodeId* (e.g., node1, node2) and can contain several predefined properties (params, inputs, anyInput, retry, timeout, agentId, fork, value, update) that dictate the node's behavior and its relationship with other nodes.
121
+
122
+ Connections between nodes will be established by references from one not to another, using either its "inputs" or "update" property. The values of those properties are *data sources*. A *data souce* is specified by either the nodeId (e.g., "node1"), or nodeId + propertyId ("node1.item").
118
123
 
119
124
  ### DFG Structure
120
125
 
121
126
  - 'nodes': A list of node. Required.
122
127
  - 'concurrency': An optional property, which specifies the maximum number of concurrent operations (agent functions to be executed at the same time). The default is 8.
123
- - 'agentId': An optional property, which specifies the default agent for all the nodes.
124
128
  - 'loop': An optional property, which specifies if the graph needs to be executed multiple times. The loop is an JavaScript object, which has two optinoal properties. The *count* property specifies the number of times the graph needs to be executed and the *while* property specifies the condition required to contineu the loop in the form of node name (nodeId) or its property (nodeId.propId). Unlike JavaScript, an empty array will be treated as false.
125
129
 
126
130
  ```
@@ -132,10 +136,10 @@ nodes:
132
136
  - Steve Jobs
133
137
  - Elon Musk
134
138
  - Nikola Tesla
135
- next: retriever.array
139
+ update: retriever.array
136
140
  result:
137
141
  value: []
138
- next: reducer
142
+ update: reducer
139
143
  retriever:
140
144
  agentId: shift
141
145
  inputs: [people]
@@ -168,16 +172,16 @@ A *computed node* have following properties.
168
172
 
169
173
  - 'agentId': An **required** property, which specifies the id of the *agent function*.
170
174
  - 'inputs': An optional list of *data sources* that the current node depends on. This establishes a flow where the current node can only be executed after the completion of the nodes listed under 'inputs'. If this list is empty, the associated *agent function* will be immediatley executed.
175
+ - 'anyInput': An optiona boolean flag, which indicates that the associated *agent function* will be called when at least one of input data became available. Otherwise, it will wait until all the data became available.
171
176
  - 'retry': An optional number, which specifies the maximum number of retries to be made. If the last attempt fails, that return value will be recorded.
172
177
  - 'timeout': An optional number, which specifies the maximum waittime in msec. If the associated agent function does not return the value in time, the "Timeout" error will be recorded and the returned value will be discarded.
173
178
  - 'params': An optional property to the associated agent function, which are agent specific.
174
179
  - 'fork': An optional paramter, which specifies the number of concurrent transactions to be created for the current node.
175
- - 'outputs' (MAY BECOME OBSOLETE): An optinal property, which specifies the mapping from outputId to nodeId. If this property is set, the node become a special node called *dispatcher*. A *dispatcher* node injects result(s) into specified static nodes, enabling the dynamic flow of data.
176
180
 
177
181
  A *static* node have following properties.
178
182
 
179
- - 'value': An optional property, which specifies the value of this static node (equivalent to calling the injectValue method from outside).
180
- - 'next': An optional property, which specifies the *data source* after each iteration.
183
+ - 'value': An **required** property, which specifies the initial value of this static node (equivalent to calling the injectValue method from outside).
184
+ - 'update': An optional property, which specifies the *data source* after each iteration.
181
185
 
182
186
  ## GraphAI class
183
187
 
@@ -207,7 +211,7 @@ Retrieves all transaction logs recorded during the execution of the graph.
207
211
 
208
212
  Returns: An array of transaction logs detailing the execution states and outcomes of the nodes within the graph.
209
213
 
210
- ### ```injectResult(nodeId: string, result: ResultData): void```
214
+ ### ```injectValue(nodeId: string, result: ResultData): void```
211
215
  Injects a result into a specified node. This is used to manually set the result of a static node, allowing dependent nodes to proceed with execution.
212
216
 
213
217
  - ```nodeId: string```: The ID of the static node into which the result is to be injected.
@@ -0,0 +1,3 @@
1
+ export { pushAgent } from "../../experimental_agents/array_agents/push_agent";
2
+ export { popAgent } from "../../experimental_agents/array_agents/pop_agent";
3
+ export { shiftAgent } from "../../experimental_agents/array_agents/shift_agent";
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shiftAgent = exports.popAgent = exports.pushAgent = void 0;
4
+ var push_agent_1 = require("../../experimental_agents/array_agents/push_agent");
5
+ Object.defineProperty(exports, "pushAgent", { enumerable: true, get: function () { return push_agent_1.pushAgent; } });
6
+ var pop_agent_1 = require("../../experimental_agents/array_agents/pop_agent");
7
+ Object.defineProperty(exports, "popAgent", { enumerable: true, get: function () { return pop_agent_1.popAgent; } });
8
+ var shift_agent_1 = require("../../experimental_agents/array_agents/shift_agent");
9
+ Object.defineProperty(exports, "shiftAgent", { enumerable: true, get: function () { return shift_agent_1.shiftAgent; } });
@@ -0,0 +1,2 @@
1
+ import { AgentFunction } from "../../graphai";
2
+ export declare const popAgent: AgentFunction<Record<string, any>, Record<string, any>, Record<string, any>>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.popAgent = void 0;
7
+ const deepmerge_1 = __importDefault(require("deepmerge"));
8
+ const popAgent = async (context) => {
9
+ const { inputs } = context;
10
+ const [array] = (0, deepmerge_1.default)({ inputs }, {}).inputs;
11
+ // TODO: Validation
12
+ const item = array.pop();
13
+ return { array, item };
14
+ };
15
+ exports.popAgent = popAgent;
@@ -0,0 +1,2 @@
1
+ import { AgentFunction } from "../../graphai";
2
+ export declare const pushAgent: AgentFunction<Record<string, any>, Record<string, any>, Record<string, any>>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.pushAgent = void 0;
7
+ const deepmerge_1 = __importDefault(require("deepmerge"));
8
+ const pushAgent = async ({ inputs }) => {
9
+ const [array, item] = (0, deepmerge_1.default)({ inputs }, {}).inputs;
10
+ // TODO: Validation
11
+ array.push(item);
12
+ return array;
13
+ };
14
+ exports.pushAgent = pushAgent;
@@ -0,0 +1,2 @@
1
+ import { AgentFunction } from "../../graphai";
2
+ export declare const shiftAgent: AgentFunction<Record<string, any>, Record<string, any>, Record<string, any>>;
@@ -3,27 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.shiftAgent = exports.popAgent = exports.pushAgent = void 0;
6
+ exports.shiftAgent = void 0;
7
7
  const deepmerge_1 = __importDefault(require("deepmerge"));
8
- const pushAgent = async ({ inputs }) => {
9
- const [array, item] = (0, deepmerge_1.default)({ inputs }, {}).inputs;
10
- // TODO: Validation
11
- array.push(item);
12
- return array;
13
- };
14
- exports.pushAgent = pushAgent;
15
- const popAgent = async (context) => {
16
- const { inputs } = context;
17
- const [array] = (0, deepmerge_1.default)({ inputs }, {}).inputs;
18
- // TODO: Varidation
19
- const item = array.pop();
20
- return { array, item };
21
- };
22
- exports.popAgent = popAgent;
23
8
  const shiftAgent = async (context) => {
24
9
  const { inputs } = context;
25
10
  const [array] = (0, deepmerge_1.default)({ inputs }, {}).inputs;
26
- // TODO: Varidation
11
+ // TODO: Validation
27
12
  const item = array.shift();
28
13
  return { array, item };
29
14
  };
@@ -0,0 +1,2 @@
1
+ import { AgentFunction } from "../../graphai";
2
+ export declare const dataObjectMergeTemplateAgent: AgentFunction;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.dataObjectMergeTemplateAgent = void 0;
7
+ const deepmerge_1 = __importDefault(require("deepmerge"));
8
+ const dataObjectMergeTemplateAgent = async ({ inputs }) => {
9
+ return inputs.reduce((tmp, input) => {
10
+ return (0, deepmerge_1.default)(tmp, input);
11
+ }, {});
12
+ };
13
+ exports.dataObjectMergeTemplateAgent = dataObjectMergeTemplateAgent;
@@ -0,0 +1,17 @@
1
+ import { AgentFunction } from "../../graphai";
2
+ export declare const dataSumTemplateAgent: AgentFunction<Record<string, any>, number, number>;
3
+ declare const dataSumTemplateAgentInfo: {
4
+ name: string;
5
+ agent: AgentFunction<Record<string, any>, number, number>;
6
+ mock: AgentFunction<Record<string, any>, number, number>;
7
+ samples: {
8
+ inputs: number[];
9
+ params: {};
10
+ result: number;
11
+ }[];
12
+ description: string;
13
+ author: string;
14
+ repository: string;
15
+ license: string;
16
+ };
17
+ export default dataSumTemplateAgentInfo;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dataSumTemplateAgent = void 0;
4
+ const dataSumTemplateAgent = async ({ inputs }) => {
5
+ return inputs.reduce((tmp, input) => {
6
+ return tmp + input;
7
+ }, 0);
8
+ };
9
+ exports.dataSumTemplateAgent = dataSumTemplateAgent;
10
+ // for test and document
11
+ const sampleInputs = [1, 2, 3];
12
+ const sampleParams = {};
13
+ const sampleResult = 6;
14
+ const dataSumTemplateAgentInfo = {
15
+ name: "dataSumTemplateAgent",
16
+ agent: exports.dataSumTemplateAgent,
17
+ mock: exports.dataSumTemplateAgent,
18
+ samples: [
19
+ {
20
+ inputs: sampleInputs,
21
+ params: sampleParams,
22
+ result: sampleResult,
23
+ },
24
+ ],
25
+ description: "Returns the sum of input values",
26
+ author: "Satoshi Nakajima",
27
+ repository: "https://github.com/snakajima/graphai",
28
+ license: "MIT",
29
+ };
30
+ exports.default = dataSumTemplateAgentInfo;
@@ -0,0 +1,3 @@
1
+ export { totalAgent } from "../../experimental_agents/data_agents/total_agent";
2
+ export { dataObjectMergeTemplateAgent } from "../../experimental_agents/data_agents/data_object_merge_template_agent";
3
+ export { dataSumTemplateAgent } from "../../experimental_agents/data_agents/data_sum_template_agent";
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dataSumTemplateAgent = exports.dataObjectMergeTemplateAgent = exports.totalAgent = void 0;
4
+ var total_agent_1 = require("../../experimental_agents/data_agents/total_agent");
5
+ Object.defineProperty(exports, "totalAgent", { enumerable: true, get: function () { return total_agent_1.totalAgent; } });
6
+ var data_object_merge_template_agent_1 = require("../../experimental_agents/data_agents/data_object_merge_template_agent");
7
+ Object.defineProperty(exports, "dataObjectMergeTemplateAgent", { enumerable: true, get: function () { return data_object_merge_template_agent_1.dataObjectMergeTemplateAgent; } });
8
+ var data_sum_template_agent_1 = require("../../experimental_agents/data_agents/data_sum_template_agent");
9
+ Object.defineProperty(exports, "dataSumTemplateAgent", { enumerable: true, get: function () { return data_sum_template_agent_1.dataSumTemplateAgent; } });
@@ -0,0 +1,46 @@
1
+ import { AgentFunction } from "../../graphai";
2
+ export declare const totalAgent: AgentFunction<Record<never, never>, Record<string, number>>;
3
+ declare const totalAgentInfo: {
4
+ name: string;
5
+ agent: AgentFunction<Record<never, never>, Record<string, number>>;
6
+ mock: AgentFunction<Record<never, never>, Record<string, number>>;
7
+ samples: ({
8
+ inputs: {
9
+ a: number;
10
+ }[];
11
+ params: {};
12
+ result: {
13
+ a: number;
14
+ };
15
+ } | {
16
+ inputs: (({
17
+ a: number;
18
+ b: number;
19
+ c?: undefined;
20
+ } | {
21
+ c: number;
22
+ a?: undefined;
23
+ b?: undefined;
24
+ })[] | ({
25
+ a: number;
26
+ b: number;
27
+ d?: undefined;
28
+ } | {
29
+ d: number;
30
+ a?: undefined;
31
+ b?: undefined;
32
+ })[])[];
33
+ params: {};
34
+ result: {
35
+ a: number;
36
+ b: number;
37
+ c: number;
38
+ d: number;
39
+ };
40
+ })[];
41
+ description: string;
42
+ author: string;
43
+ repository: string;
44
+ license: string;
45
+ };
46
+ export default totalAgentInfo;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.totalAgent = void 0;
4
+ const totalAgent = async ({ inputs }) => {
5
+ return inputs.reduce((result, input) => {
6
+ const inputArray = Array.isArray(input) ? input : [input];
7
+ inputArray.forEach((innerInput) => {
8
+ Object.keys(innerInput).forEach((key) => {
9
+ const value = innerInput[key];
10
+ if (result[key]) {
11
+ result[key] += value;
12
+ }
13
+ else {
14
+ result[key] = value;
15
+ }
16
+ });
17
+ });
18
+ return result;
19
+ }, {});
20
+ };
21
+ exports.totalAgent = totalAgent;
22
+ // for test and document
23
+ const sampleInputs = [{ a: 1 }, { a: 2 }, { a: 3 }];
24
+ const sampleParams = {};
25
+ const sampleResult = { a: 6 };
26
+ const sample2Inputs = [[{ a: 1, b: -1 }, { c: 10 }], [{ a: 2, b: -1 }], [{ a: 3, b: -2 }, { d: -10 }]];
27
+ const sample2Params = {};
28
+ const sample2Result = { a: 6, b: -4, c: 10, d: -10 };
29
+ //
30
+ const totalAgentInfo = {
31
+ name: "totalAgent",
32
+ agent: exports.totalAgent,
33
+ mock: exports.totalAgent,
34
+ samples: [
35
+ {
36
+ inputs: sampleInputs,
37
+ params: sampleParams,
38
+ result: sampleResult,
39
+ },
40
+ {
41
+ inputs: sample2Inputs,
42
+ params: sample2Params,
43
+ result: sample2Result,
44
+ },
45
+ ],
46
+ description: "Returns the sum of input values",
47
+ author: "Satoshi Nakajima",
48
+ repository: "https://github.com/snakajima/graphai",
49
+ license: "MIT",
50
+ };
51
+ exports.default = totalAgentInfo;
@@ -0,0 +1,6 @@
1
+ import { AgentFunction } from "../graphai";
2
+ export declare const stringEmbeddingsAgent: AgentFunction<{
3
+ model?: string;
4
+ }, {
5
+ contents: any;
6
+ }, Array<string> | string>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stringEmbeddingsAgent = void 0;
4
+ const defaultEmbeddingModel = "text-embedding-3-small";
5
+ const OpenAI_embedding_API = "https://api.openai.com/v1/embeddings";
6
+ // This agent retrieves embedding vectors for an array of strings using OpenAI's API
7
+ //
8
+ // Parameters:
9
+ // model: Specifies the model (default is "text-embedding-3-small")
10
+ // Inputs:
11
+ // inputs[0]: Array<string>
12
+ // Result:
13
+ // contents: Array<Array<number>>
14
+ //
15
+ const stringEmbeddingsAgent = async ({ params, inputs }) => {
16
+ const input = inputs[0];
17
+ const sources = Array.isArray(input) ? input : [input];
18
+ const apiKey = process.env.OPENAI_API_KEY;
19
+ if (!apiKey) {
20
+ throw new Error("OPENAI_API_KEY key is not set in environment variables.");
21
+ }
22
+ const headers = {
23
+ "Content-Type": "application/json",
24
+ Authorization: `Bearer ${apiKey}`,
25
+ };
26
+ const response = await fetch(OpenAI_embedding_API, {
27
+ method: "POST",
28
+ headers: headers,
29
+ body: JSON.stringify({
30
+ input: sources,
31
+ model: params?.model ?? defaultEmbeddingModel,
32
+ }),
33
+ });
34
+ const jsonResponse = await response.json();
35
+ if (!response.ok) {
36
+ throw new Error(`HTTP error! status: ${response.status}`);
37
+ }
38
+ const embeddings = jsonResponse.data.map((object) => {
39
+ return object.embedding;
40
+ });
41
+ return { contents: embeddings };
42
+ };
43
+ exports.stringEmbeddingsAgent = stringEmbeddingsAgent;
@@ -1,6 +1,11 @@
1
- export * from "./string_agent";
1
+ export * from "./string_agents";
2
2
  export * from "./slashgpt_agent";
3
- export * from "./sleeper_agent";
4
- export * from "./data_agent";
3
+ export * from "./sleeper_agents";
4
+ export * from "./data_agents";
5
5
  export * from "./nested_agent";
6
6
  export * from "./array_agents";
7
+ export * from "./embedding_agent";
8
+ export * from "./matrix_agent";
9
+ export * from "./token_agent";
10
+ export * from "./test_agents";
11
+ export * from "./map_agent";
@@ -14,9 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./string_agent"), exports);
17
+ __exportStar(require("./string_agents"), exports);
18
18
  __exportStar(require("./slashgpt_agent"), exports);
19
- __exportStar(require("./sleeper_agent"), exports);
20
- __exportStar(require("./data_agent"), exports);
19
+ __exportStar(require("./sleeper_agents"), exports);
20
+ __exportStar(require("./data_agents"), exports);
21
21
  __exportStar(require("./nested_agent"), exports);
22
22
  __exportStar(require("./array_agents"), exports);
23
+ __exportStar(require("./embedding_agent"), exports);
24
+ __exportStar(require("./matrix_agent"), exports);
25
+ __exportStar(require("./token_agent"), exports);
26
+ __exportStar(require("./test_agents"), exports);
27
+ __exportStar(require("./map_agent"), exports);
@@ -0,0 +1,4 @@
1
+ import { AgentFunction } from "../graphai";
2
+ export declare const mapAgent: AgentFunction<{
3
+ injectionTo?: string;
4
+ }, Record<string, Array<any>>, any>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapAgent = void 0;
4
+ const graphai_1 = require("../graphai");
5
+ const utils_1 = require("../utils/utils");
6
+ const mapAgent = async ({ params, inputs, agents, log, taskManager, graphData }) => {
7
+ if (taskManager) {
8
+ const status = taskManager.getStatus();
9
+ (0, utils_1.assert)(status.concurrency > status.running, `mapAgent: Concurrency is too low: ${status.concurrency}`);
10
+ }
11
+ (0, utils_1.assert)(graphData !== undefined, "mapAgent: graphData is required");
12
+ const input = Array.isArray(inputs[0]) ? inputs[0] : inputs;
13
+ const injectionTo = params.injectionTo ?? "$0";
14
+ if (graphData.nodes[injectionTo] === undefined) {
15
+ // If the input node does not exist, automatically create a static node
16
+ graphData.nodes[injectionTo] = { value: {} };
17
+ }
18
+ const graphs = input.map((data) => {
19
+ const graphAI = new graphai_1.GraphAI(graphData, agents || {}, taskManager);
20
+ graphAI.injectValue(injectionTo, data, "__mapAgent_inputs__");
21
+ return graphAI;
22
+ });
23
+ const runs = graphs.map((graph) => {
24
+ return graph.run(false);
25
+ });
26
+ const results = await Promise.all(runs);
27
+ const nodeIds = Object.keys(results[0]);
28
+ (0, utils_1.assert)(nodeIds.length > 0, "mapAgent: no return values (missing isResult)");
29
+ const compositeResult = nodeIds.reduce((tmp, nodeId) => {
30
+ tmp[nodeId] = results.map((result) => {
31
+ return result[nodeId];
32
+ });
33
+ return tmp;
34
+ }, {});
35
+ if (log) {
36
+ const logs = graphs.map((graph, index) => {
37
+ return graph.transactionLogs().map((log) => {
38
+ log.mapIndex = index;
39
+ return log;
40
+ });
41
+ });
42
+ log.push(...logs.flat());
43
+ }
44
+ return compositeResult;
45
+ };
46
+ exports.mapAgent = mapAgent;
@@ -0,0 +1,9 @@
1
+ import { AgentFunction } from "../graphai";
2
+ export declare const dotProductAgent: AgentFunction<Record<string, any>, {
3
+ contents: Array<number>;
4
+ }, Array<Array<number>>>;
5
+ export declare const sortByValuesAgent: AgentFunction<{
6
+ assendant?: boolean;
7
+ }, {
8
+ contents: Array<any>;
9
+ }, Array<any>>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortByValuesAgent = exports.dotProductAgent = void 0;
4
+ // This agent calculates the dot product of an array of vectors (A[]) and a vector (B),
5
+ // typically used to calculate cosine similarity of embedding vectors.
6
+ // Inputs:
7
+ // inputs[0]: Two dimentional array of numbers.
8
+ // inputs[1]: Two dimentional array of numbers (but the array size is 1 for the first dimention)
9
+ // Outputs:
10
+ // { contents: Array<number> } // array of docProduct of each vector (A[]) and vector B
11
+ const dotProductAgent = async ({ inputs }) => {
12
+ const embeddings = inputs[0];
13
+ const reference = inputs[1][0];
14
+ if (embeddings[0].length != reference.length) {
15
+ throw new Error("dotProduct: Length of vectors do not match.");
16
+ }
17
+ const contents = embeddings.map((embedding) => {
18
+ return embedding.reduce((dotProduct, value, index) => {
19
+ return dotProduct + value * reference[index];
20
+ }, 0);
21
+ });
22
+ return { contents };
23
+ };
24
+ exports.dotProductAgent = dotProductAgent;
25
+ // This agent returned a sorted array of one array (A) based on another array (B).
26
+ // The default sorting order is "decendant".
27
+ //
28
+ // Parameters:
29
+ // acendant: Specifies if the sorting order should be acendant. The default is "false" (decendant).
30
+ // Inputs:
31
+ // inputs[0]: Array<any>; // array to be sorted
32
+ // inputs[1]: Array<number>; // array of numbers for sorting
33
+ //
34
+ const sortByValuesAgent = async ({ params, inputs }) => {
35
+ const direction = params?.assendant ?? false ? -1 : 1;
36
+ const sources = inputs[0];
37
+ const values = inputs[1];
38
+ const joined = sources.map((item, index) => {
39
+ return { item, value: values[index] };
40
+ });
41
+ const contents = joined
42
+ .sort((a, b) => {
43
+ return (b.value - a.value) * direction;
44
+ })
45
+ .map((a) => {
46
+ return a.item;
47
+ });
48
+ return { contents };
49
+ };
50
+ exports.sortByValuesAgent = sortByValuesAgent;
@@ -1,6 +1,6 @@
1
1
  import { GraphData, AgentFunction } from "../graphai";
2
2
  export declare const nestedAgent: AgentFunction<{
3
3
  graph: GraphData;
4
- nodeId: string;
5
- inputNodes?: Array<string>;
4
+ resultFrom: string;
5
+ injectionTo?: Array<string>;
6
6
  }>;
@@ -2,19 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nestedAgent = void 0;
4
4
  const graphai_1 = require("../graphai");
5
- const nestedAgent = async ({ params, inputs, agents, log }) => {
6
- const graph = new graphai_1.GraphAI(params.graph, agents);
5
+ const utils_1 = require("../utils/utils");
6
+ const nestedAgent = async ({ params, inputs, agents, log, taskManager, graphData }) => {
7
+ if (taskManager) {
8
+ const status = taskManager.getStatus(false);
9
+ (0, utils_1.assert)(status.concurrency > status.running, `nestedAgent: Concurrency is too low: ${status.concurrency}`);
10
+ }
11
+ (0, utils_1.assert)(graphData !== undefined, "nestedAgent: graphData is required");
12
+ const graphAI = new graphai_1.GraphAI(graphData, agents || {}, taskManager);
7
13
  try {
8
14
  // Inject inputs to specified source nodes
9
- (params.inputNodes ?? []).forEach((nodeId, index) => {
10
- graph.injectValue(nodeId, inputs[index]);
15
+ (params.injectionTo ?? []).forEach((injectToNodeId, index) => {
16
+ graphAI.injectValue(injectToNodeId, inputs[index]);
11
17
  });
12
- const results = await graph.run();
13
- log.push(...graph.transactionLogs());
14
- return results[params.nodeId];
18
+ const results = await graphAI.run(true);
19
+ log?.push(...graphAI.transactionLogs());
20
+ return results[params.resultFrom];
15
21
  }
16
22
  catch (error) {
17
- log.push(...graph.transactionLogs());
23
+ log?.push(...graphAI.transactionLogs());
18
24
  if (error instanceof Error) {
19
25
  console.log("Error:", error.message);
20
26
  }
@@ -6,4 +6,4 @@ export declare const slashGPTAgent: AgentFunction<{
6
6
  function_result?: boolean;
7
7
  }, {
8
8
  content: string;
9
- }>;
9
+ }, string>;
@@ -7,18 +7,13 @@ exports.slashGPTAgent = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const slashgpt_1 = require("slashgpt");
9
9
  const config = new slashgpt_1.ChatConfig(path_1.default.resolve(__dirname));
10
- const slashGPTAgent = async ({ nodeId, params, inputs, verbose }) => {
10
+ const slashGPTAgent = async ({ params, inputs, debugInfo: { verbose, nodeId } }) => {
11
11
  if (verbose) {
12
12
  console.log("executing", nodeId, params);
13
13
  }
14
14
  const session = new slashgpt_1.ChatSession(config, params.manifest ?? {});
15
15
  const query = params?.query ? [params.query] : [];
16
- const contents = query.concat(inputs.map((input) => {
17
- if (typeof input === "string") {
18
- return input;
19
- }
20
- return input.content;
21
- }));
16
+ const contents = query.concat(inputs);
22
17
  session.append_user_question(contents.join("\n"));
23
18
  await session.call_loop(() => { });
24
19
  const message = (() => {
@@ -0,0 +1,2 @@
1
+ export { sleeperAgent } from "../../experimental_agents/sleeper_agents/sleeper_agent";
2
+ export { sleeperAgentDebug } from "../../experimental_agents/sleeper_agents/sleeper_agent_debug";