@caretakerai/agent 0.0.3 → 0.0.4
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/README.md +166 -0
- package/dist/action.d.ts +11 -10
- package/dist/action.js +66 -23
- package/dist/action.js.map +1 -1
- package/dist/activity.d.ts +5 -8
- package/dist/activity.js +22 -20
- package/dist/activity.js.map +1 -1
- package/dist/agent.d.ts +26 -17
- package/dist/agent.js +149 -82
- package/dist/agent.js.map +1 -1
- package/dist/constants.d.ts +1 -2
- package/dist/constants.js +2 -20
- package/dist/constants.js.map +1 -1
- package/package.json +13 -5
package/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Agent Framework Documentation
|
|
2
|
+
|
|
3
|
+
The Agent Framework provides a flexible and extensible environment for building intelligent agents that can perform a variety of tasks. It is built on top of the `langchain` library and utilizes language models for natural language understanding and generation.
|
|
4
|
+
|
|
5
|
+
## Key Components
|
|
6
|
+
|
|
7
|
+
- `Agent`: The core class that represents an intelligent agent. It is initialized with a set of parameters that define its behavior, capabilities, and objectives.
|
|
8
|
+
|
|
9
|
+
- `Activity`: Represents a single unit of work or thought that the agent performs. Activities can be of different kinds, such as `Observation`, `Thought`, or `Action`.
|
|
10
|
+
|
|
11
|
+
- `Action`: A subclass of `Activity` that represents a concrete action the agent can take.
|
|
12
|
+
|
|
13
|
+
- `Optimizer`: A component used to improve the agent's performance by optimizing its activities and decisions.
|
|
14
|
+
|
|
15
|
+
## Initialization Parameters
|
|
16
|
+
|
|
17
|
+
When creating an `Agent`, you must provide an `AgentPrams` object with the following properties:
|
|
18
|
+
|
|
19
|
+
- `name`: The name of the agent.
|
|
20
|
+
- `llm`: The language model the agent will use for understanding and generating text.
|
|
21
|
+
- `actions`: A list of `Action` objects that the agent can perform.
|
|
22
|
+
- `description`: (Optional) A description of the agent's purpose and capabilities.
|
|
23
|
+
- `history`: (Optional) A list of `Activity` objects representing the agent's past experiences.
|
|
24
|
+
- `examples`: (Optional) Examples of activities to guide the agent's behavior.
|
|
25
|
+
- `constraints`: (Optional) A list of constraints that the agent must adhere to.
|
|
26
|
+
- `objective`: (Optional) The goal the agent is trying to achieve.
|
|
27
|
+
- `thoughtSuffix`: (Optional) A suffix to append to thoughts in the agent's output.
|
|
28
|
+
- `actionSuffix`: (Optional) A suffix to append to actions in the agent's output.
|
|
29
|
+
- `maxIterations`: (Optional) The maximum number of iterations the agent can perform in a single run.
|
|
30
|
+
- `maxRetries`: (Optional) The maximum number of retries for actions.
|
|
31
|
+
- `optimizer`: The optimizer used to improve the agent's performance.
|
|
32
|
+
- `template`: (Optional) The template for generating prompts for the agent.
|
|
33
|
+
- `logger`: (Optional) The logger the agent will use for outputting information.
|
|
34
|
+
|
|
35
|
+
## Objective
|
|
36
|
+
The `objective` parameter defines the goal or purpose that the Agent is trying to achieve. It is a guiding force for the Agent's behavior and decision-making process. When initializing an Agent, you can optionally provide an objective to inform the Agent's actions and ensure they align with the desired outcomes. Here is an example of how an objective might be set:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
const agent = new Agent({
|
|
40
|
+
// ... other parameters
|
|
41
|
+
objective: `Help the user with math calculations using specific actions.`,
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The objective will be included in the Agent's prompt template to provide context to the language model, ensuring that the Agent's interactions are focused and relevant to the goal.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Action
|
|
49
|
+
Actions are the mechanisms through which an AI agent interacts with the application and its environment. They are the operational components that translate the agent's decisions into executable tasks. Each action extends from an abstract `Action` class, which provides a common structure and ensures compatibility with the agent framework.
|
|
50
|
+
|
|
51
|
+
Here's an example of an `Action` subclass:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Action, ActionInput } from '@caretaker/agent';
|
|
55
|
+
|
|
56
|
+
export class Say extends Action {
|
|
57
|
+
async call(input: ActionInput): Promise<void> {
|
|
58
|
+
// Logic to perform the action, e.g., output a message
|
|
59
|
+
console.log(input.params.message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
In this `Say` action example, the `call` method is where the action's specific behavior is implemented. When an agent decides to use the `Say` action, it will call this method, passing in the necessary parameters.
|
|
65
|
+
|
|
66
|
+
Actions are defined and added to the agent's configuration, allowing the agent to use them during its processing loop. They serve as the bridge between the AI's decision-making capabilities and the application's functional operations, enabling the agent to perform tasks such as communicating with the user or calculating results.
|
|
67
|
+
|
|
68
|
+
The parameters for actions are defined using JSON Schema, which provides a clear and enforceable structure for the data that an action can accept. This ensures that the agent and the actions it performs can interact with consistent and validated data formats.
|
|
69
|
+
|
|
70
|
+
For example, in the `Say` action from `@say.ts`, the parameters are defined as follows:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const SayParamsSchema = z.object({
|
|
74
|
+
message: z.string().describe('message to say to the user'),
|
|
75
|
+
}).describe('Parameters for Say action');
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
This schema is then converted to a JSON Schema, which is used by the `Say` action to validate the parameters before the action is executed:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const SayParamsJsonSchema = zodToJsonSchema(SayParamsSchema, 'SayParamsSchema')
|
|
82
|
+
.definitions!.SayParamsSchema as JSONSchema;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The use of JSON Schema in action parameters allows for the automatic validation of inputs, ensuring that only the correct data types and structures are passed to the action's `call` method. This is crucial for maintaining the integrity of the agent's operations and preventing runtime errors due to invalid data.
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
The `Multiply` action example above demonstrates how to define an action with input parameters and a return type using Zod schemas and TypeScript. The `params` property is assigned a JSON Schema that is generated from the Zod schema, which is used for runtime validation of the action's input parameters. The `call` method implements the logic to multiply an array of numbers, which are provided as input parameters, and returns the multiplication result. This action can be used by the agent to perform multiplication operations as part of its task execution.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { z } from 'zod';
|
|
93
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
94
|
+
import { Action, ActionInput } from '@caretaker/agent';
|
|
95
|
+
import { JSONSchema } from 'json-schema-to-typescript';
|
|
96
|
+
|
|
97
|
+
// Define the parameters schema using Zod for the Multiply action
|
|
98
|
+
const MultiplyParamsSchema = z.array(z.number()).describe('Array of numbers to multiply');
|
|
99
|
+
// Infer the TypeScript type from the Zod schema
|
|
100
|
+
type MultiplyParams = z.infer<typeof MultiplyParamsSchema>;
|
|
101
|
+
// Convert the Zod schema to a JSON Schema for runtime validation
|
|
102
|
+
const MultiplyParamsJsonSchema = zodToJsonSchema(MultiplyParamsSchema, 'MultiplyParamsSchema')
|
|
103
|
+
.definitions!.MultiplyParamsSchema as JSONSchema;
|
|
104
|
+
|
|
105
|
+
// Define the result schema using Zod for the Multiply action
|
|
106
|
+
const MultiplyResultSchema = z.number().describe('The result of the multiplication');
|
|
107
|
+
// Infer the TypeScript type from the Zod schema
|
|
108
|
+
type MultiplyResult = z.infer<typeof MultiplyResultSchema>;
|
|
109
|
+
// Convert the Zod schema to a JSON Schema for runtime validation
|
|
110
|
+
const MultiplyResultJsonSchema = zodToJsonSchema(MultiplyResultSchema, 'MultiplyResultSchema')
|
|
111
|
+
.definitions!.MultiplyResultSchema as JSONSchema;
|
|
112
|
+
|
|
113
|
+
// Multiply action class definition
|
|
114
|
+
export class Multiply extends Action<MultiplyParams, MultiplyResult> {
|
|
115
|
+
// Assign the JSON Schema to the params and result properties
|
|
116
|
+
readonly params = MultiplyParamsJsonSchema;
|
|
117
|
+
readonly result = MultiplyResultJsonSchema;
|
|
118
|
+
// Define whether this action should signal the agent to exit
|
|
119
|
+
readonly exit = false;
|
|
120
|
+
// Define the kind of action, which is the class name
|
|
121
|
+
readonly kind = Multiply.name;
|
|
122
|
+
// Provide a description for the action
|
|
123
|
+
readonly description = 'Multiply the numbers and provide you with the result.';
|
|
124
|
+
// Provide examples of usage (optional)
|
|
125
|
+
readonly examples = [];
|
|
126
|
+
|
|
127
|
+
// The call method is invoked by the agent when this action is performed
|
|
128
|
+
async call({ params }: ActionInput<MultiplyParams>): Promise<MultiplyResult> {
|
|
129
|
+
// Implement the action logic: multiply the numbers provided in params
|
|
130
|
+
return params.reduce((acc, n) => acc * n, 1);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Usage
|
|
136
|
+
|
|
137
|
+
To use the Agent Framework, create an instance of the `Agent` class with the necessary parameters, including the language model, actions, and any optional configurations such as history, objectives, and optimizers. Then, invoke the `agent.invoke()` method to start the agent's processing loop. The agent will utilize the provided actions, such as `Say` to communicate with the user or `Multiply` to perform multiplication tasks, to fulfill its objectives within the given constraints.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { Agent, Activity, ActivityKind } from '@caretaker/agent';
|
|
141
|
+
import { OpenAI } from 'langchain/llms/openai';
|
|
142
|
+
import { Say } from './actions/say';
|
|
143
|
+
|
|
144
|
+
const llm = new OpenAI({
|
|
145
|
+
modelName: 'gpt-3.5-turbo',
|
|
146
|
+
temperature: 0.7,
|
|
147
|
+
maxTokens: 256,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const agent = new Agent({
|
|
151
|
+
name: 'CalculatorAI',
|
|
152
|
+
description: 'An agent that performs arithmetic operations',
|
|
153
|
+
llm,
|
|
154
|
+
actions: [
|
|
155
|
+
new Say(),
|
|
156
|
+
// ... other actions
|
|
157
|
+
],
|
|
158
|
+
history: [
|
|
159
|
+
new Activity({ kind: ActivityKind.Observation, input: 'The user says: How can you help me?' })
|
|
160
|
+
],
|
|
161
|
+
// ... other parameters
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
await agent.invoke();
|
|
165
|
+
```
|
|
166
|
+
|
package/dist/action.d.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import type { Agent } from './agent';
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { type JSONSchema } from 'json-schema-to-typescript';
|
|
3
|
+
import { Activity } from './activity';
|
|
4
|
+
export interface ActionInput<T> {
|
|
5
|
+
params: T;
|
|
5
6
|
agent: Agent;
|
|
6
7
|
}
|
|
7
8
|
export interface ActionExample {
|
|
8
9
|
description?: string;
|
|
9
10
|
activities: Activity[];
|
|
10
11
|
}
|
|
11
|
-
export declare abstract class Action {
|
|
12
|
+
export declare abstract class Action<P = any, R = any> {
|
|
12
13
|
abstract get exit(): boolean;
|
|
13
14
|
abstract get kind(): string;
|
|
14
15
|
abstract get description(): string;
|
|
16
|
+
abstract get params(): JSONSchema;
|
|
17
|
+
abstract get result(): JSONSchema;
|
|
15
18
|
abstract get examples(): ActionExample[];
|
|
16
|
-
abstract
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
input: string;
|
|
21
|
-
};
|
|
19
|
+
abstract call(input: ActionInput<P>): Promise<R>;
|
|
20
|
+
private _examplesPrompt;
|
|
21
|
+
_prompt(template?: string): Promise<string>;
|
|
22
|
+
_call(input: string, agent: Agent): Promise<string>;
|
|
22
23
|
}
|
package/dist/action.js
CHANGED
|
@@ -5,32 +5,75 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Action = void 0;
|
|
7
7
|
const dedent_1 = __importDefault(require("dedent"));
|
|
8
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
9
|
+
const ajv_errors_1 = __importDefault(require("ajv-errors"));
|
|
10
|
+
const prompts_1 = require("langchain/prompts");
|
|
11
|
+
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
|
8
12
|
const constants_1 = require("./constants");
|
|
13
|
+
const ajv = (0, ajv_errors_1.default)(new ajv_1.default({
|
|
14
|
+
useDefaults: true,
|
|
15
|
+
removeAdditional: true,
|
|
16
|
+
allErrors: true,
|
|
17
|
+
}));
|
|
18
|
+
const ACTION_TEMPLATE = (`
|
|
19
|
+
\`\`\`ts
|
|
20
|
+
{params}
|
|
21
|
+
{result}
|
|
22
|
+
/**
|
|
23
|
+
{description}
|
|
24
|
+
* @kind {kind}
|
|
25
|
+
* @param {{{paramsType}}} params - {kind} action params
|
|
26
|
+
* @returns {{resultType}} {kind} action result
|
|
27
|
+
* /
|
|
28
|
+
function {kind}(params: {paramsType}): {resultType}
|
|
29
|
+
{examples}\`\`\`
|
|
30
|
+
`).trim();
|
|
9
31
|
class Action {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
#### Examples
|
|
25
|
-
${examples}
|
|
26
|
-
`.trim();
|
|
32
|
+
_examplesPrompt() {
|
|
33
|
+
return this.examples
|
|
34
|
+
.map(({ activities, description }) => [
|
|
35
|
+
(0, dedent_1.default) `
|
|
36
|
+
/**
|
|
37
|
+
* @example ${description}`,
|
|
38
|
+
activities.map(a => a.prompt())
|
|
39
|
+
.join(constants_1.ACTIVITY_SEP)
|
|
40
|
+
.split('\n')
|
|
41
|
+
.map(s => ` * ${s}`)
|
|
42
|
+
.join('\n'),
|
|
43
|
+
' */'
|
|
44
|
+
].join('\n'))
|
|
45
|
+
.join('\n\n') + '\n';
|
|
27
46
|
}
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
47
|
+
async _prompt(template = ACTION_TEMPLATE) {
|
|
48
|
+
const paramsType = `${this.kind}Params`;
|
|
49
|
+
const resultType = `${this.kind}Result`;
|
|
50
|
+
const partial = await prompts_1.PromptTemplate.fromTemplate(template).partial({
|
|
51
|
+
params: () => (0, json_schema_to_typescript_1.compile)(this.params, paramsType, { bannerComment: '' }),
|
|
52
|
+
result: () => (0, json_schema_to_typescript_1.compile)(this.result, resultType, { bannerComment: '' }),
|
|
53
|
+
examples: () => this._examplesPrompt()
|
|
54
|
+
});
|
|
55
|
+
return partial.format({
|
|
56
|
+
kind: this.kind,
|
|
57
|
+
description: this.description
|
|
58
|
+
.split('\n')
|
|
59
|
+
.map(s => ` * ${s}`)
|
|
60
|
+
.join('\n'),
|
|
61
|
+
paramsType,
|
|
62
|
+
resultType,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async _call(input, agent) {
|
|
66
|
+
const params = JSON.parse(input);
|
|
67
|
+
const validator = ajv.compile(this.params);
|
|
68
|
+
const isValid = validator(params);
|
|
69
|
+
if (!isValid) {
|
|
70
|
+
throw new Error(`Action "${this.kind}" params are not valid: ${ajv.errorsText(validator.errors)} `);
|
|
71
|
+
}
|
|
72
|
+
const result = await this.call({ params, agent });
|
|
73
|
+
if (typeof result === 'string') {
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
return JSON.stringify(result);
|
|
34
77
|
}
|
|
35
78
|
}
|
|
36
79
|
exports.Action = Action;
|
package/dist/action.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"action.js","sourceRoot":"","sources":["../src/action.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;
|
|
1
|
+
{"version":3,"file":"action.js","sourceRoot":"","sources":["../src/action.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,8CAAsB;AACtB,4DAAyC;AACzC,+CAAmD;AAEnD,yEAAqE;AAErE,2CAA2C;AAE3C,MAAM,GAAG,GAAG,IAAA,oBAAe,EACzB,IAAI,aAAG,CAAC;IACN,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;IACtB,SAAS,EAAE,IAAI;CAChB,CAAC,CACH,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC;;;;;;;;;;;;CAYxB,CAAC,CAAC,IAAI,EAAE,CAAC;AAaV,MAAsB,MAAM;IA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CAAC,IAAI,CAAC;iBACX,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC;iBACnB,IAAI,CAAC,IAAI,CAAC;YACb,UAAU;YACV,UAAU;SACX,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,KAAa,EAAE,KAAY;QACrC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAM,CAAC;QACtC,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAC1C,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;QAElC,IAAI,CAAC,OAAO,EAAE;YACZ,MAAM,IAAI,KAAK,CAAC,WAAW,IAAI,CAAC,IAAI,2BAA2B,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;SACrG;QAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QAElD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;YAC9B,OAAO,MAAM,CAAC;SACf;QAED,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;CACF;AAjED,wBAiEC"}
|
package/dist/activity.d.ts
CHANGED
|
@@ -5,19 +5,16 @@ export declare enum ActivityKind {
|
|
|
5
5
|
}
|
|
6
6
|
export type ActivityParams = {
|
|
7
7
|
kind: ActivityKind;
|
|
8
|
-
order: number;
|
|
9
8
|
input: string;
|
|
10
|
-
|
|
9
|
+
attributes?: Record<string, string>;
|
|
11
10
|
};
|
|
12
11
|
export declare class Activity implements ActivityParams {
|
|
13
12
|
kind: ActivityKind;
|
|
14
|
-
|
|
13
|
+
attributes?: Record<string, string>;
|
|
15
14
|
input: string;
|
|
16
|
-
tokens?: number;
|
|
17
|
-
static readonly defaults: Partial<ActivityParams>;
|
|
18
15
|
constructor(params: ActivityParams);
|
|
19
|
-
|
|
16
|
+
prompt(): string;
|
|
20
17
|
toObject(): this;
|
|
21
|
-
static
|
|
22
|
-
static
|
|
18
|
+
static fromObject({ kind, attributes, input }: Record<string, any>): Activity;
|
|
19
|
+
static parse(text: string): Activity[];
|
|
23
20
|
}
|
package/dist/activity.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Activity = exports.ActivityKind = void 0;
|
|
4
|
+
const xml_js_1 = require("xml-js");
|
|
4
5
|
var ActivityKind;
|
|
5
6
|
(function (ActivityKind) {
|
|
6
7
|
ActivityKind["Observation"] = "Observation";
|
|
@@ -8,35 +9,36 @@ var ActivityKind;
|
|
|
8
9
|
ActivityKind["Action"] = "Action";
|
|
9
10
|
})(ActivityKind || (exports.ActivityKind = ActivityKind = {}));
|
|
10
11
|
class Activity {
|
|
12
|
+
kind;
|
|
13
|
+
attributes;
|
|
14
|
+
input;
|
|
11
15
|
constructor(params) {
|
|
12
16
|
Object.assign(this, params);
|
|
13
17
|
}
|
|
14
|
-
|
|
15
|
-
return
|
|
18
|
+
prompt() {
|
|
19
|
+
return (0, xml_js_1.js2xml)({ [this.kind]: { _attributes: this.attributes ?? {}, _text: `\n${this.input}\n`, } }, { compact: true })
|
|
20
|
+
.replaceAll('<', '<')
|
|
21
|
+
.replaceAll('>', '>');
|
|
16
22
|
}
|
|
17
23
|
toObject() {
|
|
18
24
|
return { ...this };
|
|
19
25
|
}
|
|
20
|
-
static
|
|
21
|
-
|
|
22
|
-
const kindRegexp = /^\/\/(.+?)\s/;
|
|
23
|
-
const orderRegexp = /^\/\/\w+\s(.+?)\/\//;
|
|
24
|
-
if (!kindRegexp.test(text)) {
|
|
25
|
-
throw new Error('Cannot parse kind from the given text');
|
|
26
|
-
}
|
|
27
|
-
const kind = (_a = text.match(kindRegexp)) === null || _a === void 0 ? void 0 : _a[1].trim();
|
|
28
|
-
if (!orderRegexp.test(text)) {
|
|
29
|
-
throw new Error('Cannot parse order from the given text');
|
|
30
|
-
}
|
|
31
|
-
const order = parseInt((_b = text.match(orderRegexp)) === null || _b === void 0 ? void 0 : _b[1].trim());
|
|
32
|
-
const input = text.replace(/^\/\/.+\/\/\s/, '');
|
|
33
|
-
const activity = new Activity({ kind, order, input });
|
|
34
|
-
return activity;
|
|
26
|
+
static fromObject({ kind, attributes, input }) {
|
|
27
|
+
return new Activity({ kind, attributes, input });
|
|
35
28
|
}
|
|
36
|
-
static
|
|
37
|
-
|
|
29
|
+
static parse(text) {
|
|
30
|
+
const { elements: [root] } = (0, xml_js_1.xml2js)(`<root>${text}</root>`, { trim: true });
|
|
31
|
+
return root.elements.map(({ name, attributes, elements }) => {
|
|
32
|
+
const input = (0, xml_js_1.js2xml)({ elements })
|
|
33
|
+
.replaceAll('<', '<')
|
|
34
|
+
.replaceAll('>', '>');
|
|
35
|
+
return Activity.fromObject({
|
|
36
|
+
kind: name,
|
|
37
|
+
input: input,
|
|
38
|
+
attributes: attributes,
|
|
39
|
+
});
|
|
40
|
+
});
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
exports.Activity = Activity;
|
|
41
|
-
Activity.defaults = {};
|
|
42
44
|
//# sourceMappingURL=activity.js.map
|
package/dist/activity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../src/activity.ts"],"names":[],"mappings":";;;AAAA,IAAY,
|
|
1
|
+
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../src/activity.ts"],"names":[],"mappings":";;;AAAA,mCAAiD;AAKjD,IAAY,YAOX;AAPD,WAAY,YAAY;IAEtB,2CAA2B,CAAA;IAE3B,mCAAmB,CAAA;IAEnB,iCAAiB,CAAA;AACnB,CAAC,EAPW,YAAY,4BAAZ,YAAY,QAOvB;AAcD,MAAa,QAAQ;IACnB,IAAI,CAAgB;IACpB,UAA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BACZ,UAAU,EAAE,UAAU;aACvB,CAAC,CAAC;QACL,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAzCD,4BAyCC"}
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,39 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Logger } from 'winston';
|
|
2
2
|
import { BasePromptTemplate } from 'langchain/prompts';
|
|
3
3
|
import { Action } from './action';
|
|
4
4
|
import { Activity } from './activity';
|
|
5
5
|
import { Optimizer } from './types';
|
|
6
|
+
import { BaseLLM } from 'langchain/dist/llms/base';
|
|
6
7
|
interface AgentPrams {
|
|
7
8
|
name: string;
|
|
8
9
|
description: string;
|
|
9
|
-
llm:
|
|
10
|
+
llm: BaseLLM;
|
|
10
11
|
actions: Action[];
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
history?: Activity[];
|
|
13
|
+
examples?: Activity[];
|
|
14
|
+
constrains?: string[];
|
|
15
|
+
objective?: string;
|
|
16
|
+
thoughtSuffix?: string;
|
|
17
|
+
actionSuffix?: string;
|
|
18
|
+
maxIterations?: number;
|
|
19
|
+
maxRetries?: number;
|
|
14
20
|
optimizer: Optimizer;
|
|
15
21
|
template?: BasePromptTemplate;
|
|
16
22
|
stop?: string[];
|
|
23
|
+
logger?: Logger;
|
|
17
24
|
}
|
|
18
25
|
export declare class Agent implements AgentPrams {
|
|
19
26
|
name: string;
|
|
20
27
|
description: string;
|
|
21
|
-
llm:
|
|
28
|
+
llm: BaseLLM;
|
|
22
29
|
actions: Action[];
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
history: Activity[];
|
|
31
|
+
examples: Activity[];
|
|
32
|
+
constrains: string[];
|
|
33
|
+
objective: string;
|
|
34
|
+
thoughtSuffix: string;
|
|
35
|
+
actionSuffix: string;
|
|
36
|
+
maxIterations: number;
|
|
37
|
+
maxRetries: number;
|
|
26
38
|
optimizer: Optimizer;
|
|
39
|
+
logger: Logger;
|
|
27
40
|
template?: BasePromptTemplate;
|
|
28
|
-
stop?: string[];
|
|
29
41
|
static defaults: Partial<AgentPrams>;
|
|
30
|
-
static parseActivities(input: string): Activity[];
|
|
31
42
|
constructor(params: AgentPrams);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
execute(latestActivity: Activity): Promise<string>;
|
|
37
|
-
invoke(): Promise<string>;
|
|
43
|
+
addActivities(...activities: Activity[]): void;
|
|
44
|
+
prompt(params?: Record<string, string>): Promise<Activity[]>;
|
|
45
|
+
execute({ attributes, input }: Activity): Promise<string>;
|
|
46
|
+
invoke(params?: Record<string, any>): Promise<string>;
|
|
38
47
|
}
|
|
39
48
|
export {};
|
package/dist/agent.js
CHANGED
|
@@ -1,20 +1,82 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.Agent = void 0;
|
|
7
|
+
const dedent_1 = __importDefault(require("dedent"));
|
|
8
|
+
const winston_1 = require("winston");
|
|
4
9
|
const prompts_1 = require("langchain/prompts");
|
|
5
10
|
const runnable_1 = require("langchain/schema/runnable");
|
|
6
11
|
const output_parser_1 = require("langchain/schema/output_parser");
|
|
7
|
-
const action_1 = require("./action");
|
|
8
12
|
const constants_1 = require("./constants");
|
|
9
13
|
const activity_1 = require("./activity");
|
|
10
14
|
class Agent {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
name;
|
|
16
|
+
description;
|
|
17
|
+
llm;
|
|
18
|
+
actions;
|
|
19
|
+
history;
|
|
20
|
+
examples;
|
|
21
|
+
constrains;
|
|
22
|
+
objective;
|
|
23
|
+
thoughtSuffix;
|
|
24
|
+
actionSuffix;
|
|
25
|
+
maxIterations;
|
|
26
|
+
maxRetries;
|
|
27
|
+
optimizer;
|
|
28
|
+
logger;
|
|
29
|
+
template;
|
|
30
|
+
static defaults = {
|
|
31
|
+
template: prompts_1.PromptTemplate.fromTemplate((0, dedent_1.default) `
|
|
32
|
+
# Objective
|
|
33
|
+
{objective}
|
|
34
|
+
|
|
35
|
+
# Constraints
|
|
36
|
+
{constraints}
|
|
37
|
+
|
|
38
|
+
# Actions
|
|
39
|
+
The only permissible actions you may take are listed below:
|
|
40
|
+
{actions}
|
|
41
|
+
|
|
42
|
+
**Continue the History with the following format in your response:**
|
|
43
|
+
{examples}
|
|
44
|
+
|
|
45
|
+
# History:
|
|
46
|
+
{history}
|
|
47
|
+
{suffix}
|
|
48
|
+
`.trim()),
|
|
49
|
+
objective: 'You are helpful assistant.',
|
|
50
|
+
thoughtSuffix: '<!-- Provide thought and action here -->',
|
|
51
|
+
actionSuffix: '<!-- Provide action here -->',
|
|
52
|
+
maxRetries: 7,
|
|
53
|
+
maxIterations: Number.MAX_SAFE_INTEGER,
|
|
54
|
+
logger: (0, winston_1.createLogger)({ transports: [new winston_1.transports.Console()] }),
|
|
55
|
+
examples: [
|
|
56
|
+
new activity_1.Activity({
|
|
57
|
+
kind: activity_1.ActivityKind.Observation,
|
|
58
|
+
input: 'The result of previously taken action',
|
|
59
|
+
}),
|
|
60
|
+
new activity_1.Activity({
|
|
61
|
+
kind: activity_1.ActivityKind.Thought,
|
|
62
|
+
input: 'You must always think before taking the action',
|
|
63
|
+
}),
|
|
64
|
+
new activity_1.Activity({
|
|
65
|
+
kind: activity_1.ActivityKind.Action,
|
|
66
|
+
attributes: { kind: 'the action kind to take, should be one of listed in Actions section' },
|
|
67
|
+
input: (0, dedent_1.default) `
|
|
68
|
+
<!-- The action input as valid JSON e.g. -->
|
|
69
|
+
{
|
|
70
|
+
"message": "hello!!"
|
|
71
|
+
}
|
|
72
|
+
`.trim(),
|
|
73
|
+
}),
|
|
74
|
+
],
|
|
75
|
+
constrains: [
|
|
76
|
+
'Use only actions listed in Actions section.',
|
|
77
|
+
'Reject any request that are not related to your objective and cannot be fulfilled within the given list of actions.',
|
|
78
|
+
]
|
|
79
|
+
};
|
|
18
80
|
constructor(params) {
|
|
19
81
|
const { actions } = params;
|
|
20
82
|
if (!actions.length) {
|
|
@@ -22,94 +84,99 @@ class Agent {
|
|
|
22
84
|
}
|
|
23
85
|
Object.assign(this, Agent.defaults, params);
|
|
24
86
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.
|
|
87
|
+
addActivities(...activities) {
|
|
88
|
+
activities.forEach(a => this.logger.debug(a));
|
|
89
|
+
this.history.push(...activities);
|
|
28
90
|
}
|
|
29
|
-
async
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
this.
|
|
33
|
-
|
|
91
|
+
async prompt(params) {
|
|
92
|
+
let activities = [];
|
|
93
|
+
const history = async () => {
|
|
94
|
+
const history = [...await this.optimizer.optimize([...this.history, ...activities])];
|
|
95
|
+
const historyStrings = history.map(h => h.prompt()).join(constants_1.ACTIVITY_SEP);
|
|
96
|
+
return historyStrings;
|
|
97
|
+
};
|
|
98
|
+
const actions = async () => {
|
|
99
|
+
const actionsStrings = await Promise.all(this.actions.map(a => a._prompt()));
|
|
100
|
+
return actionsStrings.join(constants_1.ACTION_SEP);
|
|
101
|
+
};
|
|
102
|
+
const constraints = () => this.constrains.map((c, i) => `${i + 1}. ${c}`).join('\n');
|
|
103
|
+
const examples = async () => {
|
|
104
|
+
const examplesStrings = this.examples.map(h => h.prompt()).join(constants_1.ACTIVITY_SEP);
|
|
105
|
+
return examplesStrings;
|
|
106
|
+
};
|
|
107
|
+
const suffix = () => ([...this.history, ...activities].at(-1).kind === activity_1.ActivityKind.Observation
|
|
108
|
+
? this.thoughtSuffix
|
|
109
|
+
: this.actionSuffix);
|
|
110
|
+
const template = await this.template.partial({
|
|
111
|
+
objective: this.objective,
|
|
112
|
+
history,
|
|
113
|
+
actions,
|
|
114
|
+
constraints,
|
|
115
|
+
examples,
|
|
116
|
+
suffix,
|
|
117
|
+
});
|
|
118
|
+
const seq = runnable_1.RunnableSequence.from([
|
|
119
|
+
template,
|
|
120
|
+
this.llm.bind({ stop: [`<${activity_1.ActivityKind.Observation}>`] }),
|
|
34
121
|
new output_parser_1.StringOutputParser(),
|
|
35
|
-
])
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
122
|
+
]);
|
|
123
|
+
for (let i = 0; i < this.maxRetries; ++i) {
|
|
124
|
+
let completion = await seq.invoke(params ?? {});
|
|
125
|
+
try {
|
|
126
|
+
activities = [...activities, ...activity_1.Activity.parse(completion)];
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
this.logger.warn(e);
|
|
130
|
+
this.logger.debug(`Retry ${i + 1} due to malformed output`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const activity = activities.at(-1);
|
|
134
|
+
if (activity.kind === activity_1.ActivityKind.Thought) {
|
|
135
|
+
this.logger.debug(`Retry ${i + 1} due to missing action`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (activity.kind === activity_1.ActivityKind.Action) {
|
|
139
|
+
try {
|
|
140
|
+
const observation = await this.execute(activity);
|
|
141
|
+
return [...activities, new activity_1.Activity({ kind: activity_1.ActivityKind.Observation, input: observation })];
|
|
45
142
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}))));
|
|
56
|
-
}
|
|
57
|
-
async act(latestActivity) {
|
|
58
|
-
this.appendActivity(...(await this.complete(new activity_1.Activity({
|
|
59
|
-
kind: activity_1.ActivityKind.Action,
|
|
60
|
-
order: latestActivity.order,
|
|
61
|
-
input: '',
|
|
62
|
-
}))));
|
|
143
|
+
catch (e) {
|
|
144
|
+
const err = e;
|
|
145
|
+
activities.push(new activity_1.Activity({ kind: activity_1.ActivityKind.Observation, input: err.toString() }));
|
|
146
|
+
this.logger.debug(`Retry ${i + 1} due to action error`);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
throw new Error('Max number of retries reached.');
|
|
63
152
|
}
|
|
64
|
-
async execute(
|
|
65
|
-
const { kind
|
|
153
|
+
async execute({ attributes, input }) {
|
|
154
|
+
const { kind } = attributes;
|
|
66
155
|
const action = this.actions.find(a => a.kind === kind);
|
|
67
156
|
if (!action) {
|
|
68
|
-
throw new Error(`Action "${kind}" is not
|
|
69
|
-
}
|
|
70
|
-
const observation = await action.execute({ agent: this, input });
|
|
71
|
-
this.appendActivity(new activity_1.Activity({
|
|
72
|
-
kind: activity_1.ActivityKind.Observation,
|
|
73
|
-
order: latestActivity.order + 1,
|
|
74
|
-
input: observation,
|
|
75
|
-
}));
|
|
76
|
-
if (action.exit) {
|
|
77
|
-
return observation;
|
|
157
|
+
throw new Error(`Action "${kind}" is not allowed.`);
|
|
78
158
|
}
|
|
159
|
+
const observation = await action._call(input, this);
|
|
160
|
+
return observation;
|
|
79
161
|
}
|
|
80
|
-
async invoke() {
|
|
81
|
-
|
|
82
|
-
if (!this.activities.length) {
|
|
162
|
+
async invoke(params) {
|
|
163
|
+
if (!this.history.length) {
|
|
83
164
|
throw new Error('Activity list must not be empty.');
|
|
84
165
|
}
|
|
85
|
-
if (
|
|
86
|
-
throw new Error('
|
|
166
|
+
if (this.history.at(-1)?.kind !== activity_1.ActivityKind.Observation) {
|
|
167
|
+
throw new Error('Latest experience must be of Observation kind');
|
|
87
168
|
}
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
else if (latestActivity.kind === activity_1.ActivityKind.Action) {
|
|
97
|
-
const result = await this.execute(latestActivity);
|
|
98
|
-
if (result) {
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
throw new Error(`Activity "${latestActivity.kind}" is not permitted.`);
|
|
169
|
+
for (let i = 0; i < this.maxIterations; ++i) {
|
|
170
|
+
const activities = await this.prompt(params);
|
|
171
|
+
this.addActivities(...activities);
|
|
172
|
+
const activity = this.history.at(-2);
|
|
173
|
+
const action = this.actions.find(a => a.kind === activity.attributes.kind);
|
|
174
|
+
if (action.exit) {
|
|
175
|
+
return this.history.at(-1).input;
|
|
104
176
|
}
|
|
105
177
|
}
|
|
178
|
+
throw new Error('Max number of iterations reached.');
|
|
106
179
|
}
|
|
107
180
|
}
|
|
108
181
|
exports.Agent = Agent;
|
|
109
|
-
Agent.defaults = {
|
|
110
|
-
template: prompts_1.PromptTemplate.fromTemplate(constants_1.TEMPLATE),
|
|
111
|
-
stop: [`//${activity_1.ActivityKind.Observation}`],
|
|
112
|
-
activities: [],
|
|
113
|
-
example: []
|
|
114
|
-
};
|
|
115
182
|
//# sourceMappingURL=agent.js.map
|
package/dist/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,qCAA2D;AAC3D,+CAAuE;AACvE,wDAA6D;AAC7D,kEAAoE;AAEpE,2CAAuD;AACvD,yCAAoD;AA0CpD,MAAa,KAAK;IAChB,IAAI,CAAU;IACd,WAAW,CAAU;IACrB,GAAG,CAAW;IACd,OAAO,CAAY;IACnB,OAAO,CAAc;IACrB,QAAQ,CAAc;IACtB,UAAU,CAAY;IACtB,SAAS,CAAU;IACnB,aAAa,CAAU;IACvB,YAAY,CAAU;IACtB,aAAa,CAAU;IACvB,UAAU,CAAU;IACpB,SAAS,CAAa;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GED
|
@@ -1,24 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.ACTIVITY_SEP = exports.ACTION_SEP = void 0;
|
|
4
4
|
exports.ACTION_SEP = '\n\n';
|
|
5
|
-
exports.ACTIVITY_SEP = '
|
|
6
|
-
exports.TEMPLATE = `
|
|
7
|
-
## Instruction
|
|
8
|
-
{instruction}
|
|
9
|
-
|
|
10
|
-
## Additional Requirements:
|
|
11
|
-
1. I understand that there are no exceptions;
|
|
12
|
-
2. I am not permitted to perform any actions other than those listed in section Actions;
|
|
13
|
-
3. I must provide your thoughts on the observations before proceeding to the next action;
|
|
14
|
-
4. I am strongly prohibited from using Observations, Actions, and Thoughts provided in the Action examples as the source of truth;
|
|
15
|
-
|
|
16
|
-
## Actions
|
|
17
|
-
The permissible actions I may take are listed below:
|
|
18
|
-
{actions}
|
|
19
|
-
|
|
20
|
-
## Context:
|
|
21
|
-
{example}
|
|
22
|
-
{activities}
|
|
23
|
-
`.trim();
|
|
5
|
+
exports.ACTIVITY_SEP = '\n';
|
|
24
6
|
//# sourceMappingURL=constants.js.map
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG,MAAM,CAAA;AACnB,QAAA,YAAY,GAAG,
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG,MAAM,CAAA;AACnB,QAAA,YAAY,GAAG,IAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caretakerai/agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Single framework for building text-agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"dist"
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
11
12
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
12
|
-
"build": "tsc"
|
|
13
|
+
"build": "rimraf dist && tsc",
|
|
14
|
+
"build:watch": "tsc --watch"
|
|
13
15
|
},
|
|
14
16
|
"author": "",
|
|
15
17
|
"license": "ISC",
|
|
16
18
|
"dependencies": {
|
|
19
|
+
"ajv": "^8.12.0",
|
|
20
|
+
"ajv-errors": "^3.0.0",
|
|
17
21
|
"debug": "^4.3.4",
|
|
18
|
-
"
|
|
22
|
+
"json-schema-to-typescript": "^13.1.1",
|
|
23
|
+
"langchain": "^0.0.191",
|
|
24
|
+
"xml-js": "^1.6.11"
|
|
19
25
|
},
|
|
20
26
|
"devDependencies": {
|
|
21
27
|
"@types/node": "^20.4.1",
|
|
28
|
+
"rimraf": "^5.0.5",
|
|
22
29
|
"tsx": "^3.12.7",
|
|
23
|
-
"typescript": "^5.2.2"
|
|
30
|
+
"typescript": "^5.2.2",
|
|
31
|
+
"winston": "^3.11.0"
|
|
24
32
|
}
|
|
25
33
|
}
|