@caretakerai/agent 0.0.13 → 0.0.15
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 +68 -114
- package/dist/activity.d.ts +1 -2
- package/dist/activity.js +2 -2
- package/dist/activity.js.map +1 -1
- package/dist/agent.d.ts +14 -7
- package/dist/agent.js +51 -47
- package/dist/agent.js.map +1 -1
- package/dist/constants.d.ts +0 -1
- package/dist/constants.js +1 -2
- package/dist/constants.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
- package/dist/action.d.ts +0 -23
- package/dist/action.js +0 -86
- package/dist/action.js.map +0 -1
package/README.md
CHANGED
|
@@ -8,8 +8,6 @@ The Agent Framework provides a flexible and extensible environment for building
|
|
|
8
8
|
|
|
9
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
10
|
|
|
11
|
-
- `Action`: A subclass of `Activity` that represents a concrete action the agent can take.
|
|
12
|
-
|
|
13
11
|
- `Optimizer`: A component used to improve the agent's performance by optimizing its activities and decisions.
|
|
14
12
|
|
|
15
13
|
## Initialization Parameters
|
|
@@ -17,21 +15,23 @@ The Agent Framework provides a flexible and extensible environment for building
|
|
|
17
15
|
When creating an `Agent`, you must provide an `AgentPrams` object with the following properties:
|
|
18
16
|
|
|
19
17
|
- `name`: The name of the agent.
|
|
18
|
+
- `description`: A description of the agent's purpose and capabilities.
|
|
20
19
|
- `llm`: The language model the agent will use for understanding and generating text.
|
|
21
|
-
- `
|
|
22
|
-
- `
|
|
20
|
+
- `isChatModel`: (Optional) A flag to indicate if a chat model is used, affecting prompt formatting.
|
|
21
|
+
- `typeDefs`: GraphQL type definitions for the agent's actions.
|
|
22
|
+
- `resolvers`: (Optional) GraphQL resolvers for implementing the actions.
|
|
23
|
+
- `executor`: (Optional) A custom GraphQL executor to handle agent actions.
|
|
23
24
|
- `history`: (Optional) A list of `Activity` objects representing the agent's past experiences.
|
|
24
25
|
- `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
26
|
- `objective`: (Optional) The goal the agent is trying to achieve.
|
|
27
|
-
- `
|
|
28
|
-
- `
|
|
29
|
-
- `maxIterations`: (Optional) The maximum number of iterations the agent can perform in a single run.
|
|
27
|
+
- `instruction`: (Optional) Completion instruction for the language model.
|
|
28
|
+
- `maxIterations`: (Optional) The maximum number of iterations the agent can perform.
|
|
30
29
|
- `maxRetries`: (Optional) The maximum number of retries for actions.
|
|
31
30
|
- `optimizer`: The optimizer used to improve the agent's performance.
|
|
31
|
+
- `signal`: (Optional) An abort signal to stop the agent's operation.
|
|
32
32
|
- `template`: (Optional) The template for generating prompts for the agent.
|
|
33
|
+
- `stop`: (Optional) A list of strings that, if generated by the agent, should cause it to stop.
|
|
33
34
|
- `logger`: (Optional) The logger the agent will use for outputting information.
|
|
34
|
-
|
|
35
35
|
## Objective
|
|
36
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
37
|
|
|
@@ -45,122 +45,76 @@ const agent = new Agent({
|
|
|
45
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
46
|
|
|
47
47
|
|
|
48
|
-
##
|
|
49
|
-
|
|
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
|
-
|
|
48
|
+
## GraphQL Actions
|
|
49
|
+
In the Agent Framework, actions are defined within a GraphQL schema, which provides a structured and enforceable method for specifying the data the agent can act upon. This schema-driven approach facilitates the declaration of queries and mutations that correspond to the actions available to the agent.
|
|
90
50
|
|
|
91
|
-
|
|
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
|
-
```
|
|
51
|
+
Actions are expressed as mutations within the GraphQL schema, and their execution is handled through resolvers. These resolvers implement the logic that the agent performs when an action is invoked. The parameters for these actions are defined using GraphQL's strong typing system, ensuring that the agent receives and acts upon well-structured and validated data.
|
|
134
52
|
|
|
135
53
|
## Usage
|
|
136
54
|
|
|
137
|
-
To use the Agent Framework,
|
|
55
|
+
To use the Agent Framework, instantiate the `Agent` class with the necessary initialization parameters, including the language model, GraphQL type definitions, and resolvers. You can also include optional configurations such as history, objectives, and optimizers. Once the agent is configured, call the `agent.invoke()` method to begin the agent's processing loop. The agent will execute actions defined in the GraphQL schema to interact with the user and perform tasks in line with its objectives.
|
|
138
56
|
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
});
|
|
57
|
+
Here is an example of how to set up and use the Agent Framework:
|
|
58
|
+
```ts
|
|
149
59
|
|
|
150
60
|
const agent = new Agent({
|
|
151
61
|
name: 'CalculatorAI',
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
62
|
+
llm: new OpenAI(), // Define LLM
|
|
63
|
+
objective: 'Help the user with math.', // Describe the objective for the agent.
|
|
64
|
+
typeDefs: dedent`
|
|
65
|
+
schema {
|
|
66
|
+
query: Query
|
|
67
|
+
mutation: Mutation
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type Query {
|
|
71
|
+
# No queries for this case
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type Mutation {
|
|
75
|
+
"""
|
|
76
|
+
Relay information to the user and wait for the reply. Note that this is only way of communicating information to the user.
|
|
77
|
+
"""
|
|
78
|
+
say(input: SayInput!): SayResult
|
|
79
|
+
add(input: MathInput!): MathResult
|
|
80
|
+
# ... Rest of mutations
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
# ... Rest of schema
|
|
84
|
+
`.trim(),
|
|
85
|
+
resolvers: {
|
|
86
|
+
Mutation: {
|
|
87
|
+
say: async (_, { input: { message } }) => {
|
|
88
|
+
console.log(`${chalk.bold(`${agent.name}:`)} ${message}`);
|
|
89
|
+
|
|
90
|
+
const reply = await inputPrompt({
|
|
91
|
+
message: 'Human:'
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return { reply };
|
|
95
|
+
},
|
|
96
|
+
add: (_, { input: { left, right } }) => {
|
|
97
|
+
try {
|
|
98
|
+
return { result: left + right };
|
|
99
|
+
} catch (error) {
|
|
100
|
+
return { error };
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
// ... rest of resolvers
|
|
104
|
+
}
|
|
105
|
+
},
|
|
158
106
|
history: [
|
|
159
|
-
|
|
107
|
+
// Add some agent primer
|
|
108
|
+
new Activity({
|
|
109
|
+
kind: ActivityKind.Observation, input: JSON.stringify({
|
|
110
|
+
data: { say: { reply: 'Hi!, how can you help me?' } }
|
|
111
|
+
}, null, 2)
|
|
112
|
+
})
|
|
160
113
|
],
|
|
161
|
-
//
|
|
114
|
+
optimizer: // Add history optimizer
|
|
162
115
|
});
|
|
163
116
|
|
|
117
|
+
// Invoke the agent
|
|
164
118
|
await agent.invoke();
|
|
165
|
-
```
|
|
166
119
|
|
|
120
|
+
```
|
package/dist/activity.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ export declare enum ActivityKind {
|
|
|
6
6
|
export type ActivityParams = {
|
|
7
7
|
kind: ActivityKind;
|
|
8
8
|
input: string;
|
|
9
|
-
attributes?: Record<string, string>;
|
|
10
9
|
};
|
|
11
10
|
export declare class Activity implements ActivityParams {
|
|
12
11
|
kind: ActivityKind;
|
|
@@ -15,6 +14,6 @@ export declare class Activity implements ActivityParams {
|
|
|
15
14
|
constructor(params: ActivityParams);
|
|
16
15
|
prompt(): string;
|
|
17
16
|
toObject(): this;
|
|
18
|
-
static fromObject({ kind,
|
|
17
|
+
static fromObject({ kind, input }: Record<string, any>): Activity;
|
|
19
18
|
static parse(text: string): Activity[];
|
|
20
19
|
}
|
package/dist/activity.js
CHANGED
|
@@ -23,8 +23,8 @@ class Activity {
|
|
|
23
23
|
toObject() {
|
|
24
24
|
return { ...this };
|
|
25
25
|
}
|
|
26
|
-
static fromObject({ kind,
|
|
27
|
-
return new Activity({ kind,
|
|
26
|
+
static fromObject({ kind, input }) {
|
|
27
|
+
return new Activity({ kind, input });
|
|
28
28
|
}
|
|
29
29
|
static parse(text) {
|
|
30
30
|
const { elements: [root] } = (0, xml_js_1.xml2js)(`<root>${text}</root>`, { trim: true });
|
package/dist/activity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;
|
|
1
|
+
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../src/activity.ts"],"names":[],"mappings":";;;AAAA,mCAAiD;AAKjD,IAAY,YAOX;AAPD,WAAY,YAAY;IAEtB,2CAA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AC,IAAI,KAAK,MAAM,CAAE,EAAE,CAAC;iBAChF,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC;iBACvB,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YAE3B,OAAO,QAAQ,CAAC,UAAU,CAAC;gBACzB,IAAI,EAAE,IAAI;gBACV,KAAK,EAAE,KAAK;gBACZ,UAAU,EAAE,UAAU;aACvB,CAAC,CAAC;QACL,CAAC,CAAC;aACD,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,YAAY,CAAC,MAAM,EAAE,YAAY,CAAC,WAAW,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1G,CAAC;CACF;AA3CD,4BA2CC"}
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import { Logger } from '
|
|
1
|
+
import { Logger } from 'pino';
|
|
2
2
|
import { BasePromptTemplate } from '@langchain/core/prompts';
|
|
3
3
|
import { BaseLanguageModel } from '@langchain/core/language_models/base';
|
|
4
|
-
import {
|
|
4
|
+
import type { TypeSource, IResolvers } from '@graphql-tools/utils';
|
|
5
|
+
import { GraphQLSchema } from 'graphql';
|
|
5
6
|
import { Activity } from './activity';
|
|
6
7
|
import { Optimizer } from './types';
|
|
8
|
+
type GraphQLExecutor = (query: string) => Promise<Record<string, unknown>>;
|
|
7
9
|
interface AgentPrams {
|
|
8
10
|
name: string;
|
|
9
11
|
description: string;
|
|
10
12
|
llm: BaseLanguageModel;
|
|
11
13
|
isChatModel?: boolean;
|
|
12
|
-
|
|
14
|
+
typeDefs: TypeSource;
|
|
15
|
+
resolvers?: IResolvers;
|
|
16
|
+
executor?: GraphQLExecutor;
|
|
13
17
|
history?: Activity[];
|
|
14
18
|
examples?: Activity[];
|
|
15
19
|
objective?: string;
|
|
@@ -17,6 +21,7 @@ interface AgentPrams {
|
|
|
17
21
|
maxIterations?: number;
|
|
18
22
|
maxRetries?: number;
|
|
19
23
|
optimizer: Optimizer;
|
|
24
|
+
signal?: AbortSignal;
|
|
20
25
|
template?: BasePromptTemplate;
|
|
21
26
|
stop?: string[];
|
|
22
27
|
logger?: Logger;
|
|
@@ -25,23 +30,25 @@ export declare class Agent implements AgentPrams {
|
|
|
25
30
|
name: string;
|
|
26
31
|
description: string;
|
|
27
32
|
llm: BaseLanguageModel;
|
|
28
|
-
|
|
33
|
+
typeDefs: TypeSource;
|
|
34
|
+
resolvers: IResolvers;
|
|
29
35
|
history: Activity[];
|
|
30
36
|
examples: Activity[];
|
|
31
37
|
objective: string;
|
|
32
38
|
instruction: string;
|
|
33
|
-
actionSuffix: string;
|
|
34
39
|
maxIterations: number;
|
|
35
40
|
maxRetries: number;
|
|
36
41
|
isChatModel: boolean;
|
|
42
|
+
signal: AbortSignal;
|
|
37
43
|
optimizer: Optimizer;
|
|
38
44
|
logger: Logger;
|
|
39
45
|
template?: BasePromptTemplate;
|
|
46
|
+
executor?: GraphQLExecutor;
|
|
47
|
+
readonly schema: GraphQLSchema;
|
|
40
48
|
static defaults: Partial<AgentPrams>;
|
|
41
49
|
constructor(params: AgentPrams);
|
|
42
50
|
addActivities(...activities: Activity[]): void;
|
|
43
51
|
prompt(params?: Record<string, string>): Promise<void>;
|
|
44
|
-
|
|
45
|
-
invoke(params?: Record<string, any>): Promise<string>;
|
|
52
|
+
invoke(params?: Record<string, any>): Promise<void>;
|
|
46
53
|
}
|
|
47
54
|
export {};
|
package/dist/agent.js
CHANGED
|
@@ -5,37 +5,45 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Agent = void 0;
|
|
7
7
|
const dedent_1 = __importDefault(require("dedent"));
|
|
8
|
-
const
|
|
8
|
+
const yaml_1 = require("yaml");
|
|
9
|
+
const pino_1 = __importDefault(require("pino"));
|
|
9
10
|
const prompts_1 = require("@langchain/core/prompts");
|
|
10
11
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
12
|
+
const schema_1 = require("@graphql-tools/schema");
|
|
13
|
+
const graphql_1 = require("graphql");
|
|
11
14
|
const constants_1 = require("./constants");
|
|
12
15
|
const activity_1 = require("./activity");
|
|
13
16
|
class Agent {
|
|
14
17
|
name;
|
|
15
18
|
description;
|
|
16
19
|
llm;
|
|
17
|
-
|
|
20
|
+
typeDefs;
|
|
21
|
+
resolvers;
|
|
18
22
|
history;
|
|
19
23
|
examples;
|
|
20
24
|
objective;
|
|
21
25
|
instruction;
|
|
22
|
-
actionSuffix;
|
|
23
26
|
maxIterations;
|
|
24
27
|
maxRetries;
|
|
25
28
|
isChatModel;
|
|
29
|
+
signal;
|
|
26
30
|
optimizer;
|
|
27
31
|
logger;
|
|
28
32
|
template;
|
|
33
|
+
executor;
|
|
34
|
+
schema;
|
|
29
35
|
static defaults = {
|
|
30
36
|
template: prompts_1.PromptTemplate.fromTemplate((0, dedent_1.default) `
|
|
31
37
|
# Objective
|
|
32
38
|
{objective}
|
|
33
39
|
|
|
34
|
-
#
|
|
40
|
+
# GraphQL Schema
|
|
35
41
|
The only permissible actions you may take are listed below:
|
|
36
|
-
|
|
42
|
+
\`\`\`graphql
|
|
43
|
+
{schema}
|
|
44
|
+
\`\`\`
|
|
37
45
|
|
|
38
|
-
**Continue the History with
|
|
46
|
+
**Continue the History with your thoughts and actions following format in your response:**
|
|
39
47
|
{examples}
|
|
40
48
|
|
|
41
49
|
# History:
|
|
@@ -45,43 +53,52 @@ class Agent {
|
|
|
45
53
|
`),
|
|
46
54
|
objective: 'You are helpful assistant.',
|
|
47
55
|
instruction: (0, dedent_1.default) `
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
Use only actions listed in Actions section.
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
Plan your further actions step by step before taking any.
|
|
57
|
+
Always explain your choice in your thoughts.
|
|
58
|
+
Use only actions listed in the Actions section.
|
|
59
|
+
Do multiple queries or mutations in a single request if possible.
|
|
60
|
+
Reject any requests that are not related to your objective and cannot be fulfilled within the given list of actions.
|
|
61
|
+
Thought and Action goes here.
|
|
53
62
|
`,
|
|
54
63
|
maxRetries: 7,
|
|
55
64
|
isChatModel: false,
|
|
56
65
|
maxIterations: Number.MAX_SAFE_INTEGER,
|
|
57
|
-
logger: (0,
|
|
66
|
+
logger: (0, pino_1.default)(),
|
|
58
67
|
examples: [
|
|
59
68
|
new activity_1.Activity({
|
|
60
69
|
kind: activity_1.ActivityKind.Observation,
|
|
61
|
-
input:
|
|
70
|
+
input: (0, dedent_1.default) `
|
|
71
|
+
<!-- The result of the previous action e.g. -->
|
|
72
|
+
data
|
|
73
|
+
theBestNumber:
|
|
74
|
+
result: 73
|
|
75
|
+
`.trim(),
|
|
62
76
|
}),
|
|
63
77
|
new activity_1.Activity({
|
|
64
78
|
kind: activity_1.ActivityKind.Thought,
|
|
65
|
-
input: '
|
|
79
|
+
input: 'Now I know that the best number is 73. I should share this information with the user immediately.',
|
|
66
80
|
}),
|
|
67
81
|
new activity_1.Activity({
|
|
68
82
|
kind: activity_1.ActivityKind.Action,
|
|
69
|
-
attributes: { kind: 'the action kind to take, should be one of listed in Actions section' },
|
|
70
83
|
input: (0, dedent_1.default) `
|
|
71
|
-
<!-- The action
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
<!-- The action must be a single executable GraphQL request e.g. -->
|
|
85
|
+
\`\`\`graphql
|
|
86
|
+
mutation {
|
|
87
|
+
say(input: { message: "The best number is 73!" }) {
|
|
88
|
+
reply
|
|
89
|
+
}
|
|
74
90
|
}
|
|
91
|
+
\`\`\`
|
|
75
92
|
`.trim(),
|
|
76
93
|
}),
|
|
77
94
|
],
|
|
78
95
|
};
|
|
79
96
|
constructor(params) {
|
|
80
|
-
const { actions } = params;
|
|
81
|
-
if (!actions.length) {
|
|
82
|
-
throw new Error('Actions list must be non empty');
|
|
83
|
-
}
|
|
84
97
|
Object.assign(this, Agent.defaults, params);
|
|
98
|
+
const { typeDefs, resolvers, executor } = params;
|
|
99
|
+
if (!executor) {
|
|
100
|
+
this.schema = (0, schema_1.makeExecutableSchema)({ typeDefs, resolvers });
|
|
101
|
+
}
|
|
85
102
|
}
|
|
86
103
|
addActivities(...activities) {
|
|
87
104
|
activities.forEach(a => this.logger.debug(a));
|
|
@@ -101,10 +118,6 @@ class Agent {
|
|
|
101
118
|
const activitiesStrings = activities.map(a => a.prompt()).join(constants_1.ACTIVITY_SEP);
|
|
102
119
|
return activitiesStrings;
|
|
103
120
|
};
|
|
104
|
-
const actions = async () => {
|
|
105
|
-
const actionsStrings = await Promise.all(this.actions.map(a => a._prompt()));
|
|
106
|
-
return actionsStrings.join(constants_1.ACTION_SEP);
|
|
107
|
-
};
|
|
108
121
|
const instruction = () => this.instruction
|
|
109
122
|
.split('\n')
|
|
110
123
|
.filter(i => i)
|
|
@@ -116,15 +129,15 @@ class Agent {
|
|
|
116
129
|
};
|
|
117
130
|
const template = await this.template.partial({
|
|
118
131
|
objective: this.objective,
|
|
132
|
+
schema: this.typeDefs.toString(),
|
|
119
133
|
history,
|
|
120
|
-
actions,
|
|
121
134
|
examples,
|
|
122
135
|
instruction,
|
|
123
136
|
completions,
|
|
124
137
|
});
|
|
125
138
|
const chain = template
|
|
126
139
|
.pipe((prompt) => prompt.toString().trim())
|
|
127
|
-
.pipe(this.llm.bind({ stop: [`<${activity_1.ActivityKind.Observation}
|
|
140
|
+
.pipe(this.llm.bind({ stop: [`<${activity_1.ActivityKind.Observation}>`] }))
|
|
128
141
|
.pipe(new output_parsers_1.StringOutputParser());
|
|
129
142
|
for (let i = 0; i < this.maxRetries; ++i) {
|
|
130
143
|
let completion = await chain.invoke(params ?? {});
|
|
@@ -155,11 +168,16 @@ class Agent {
|
|
|
155
168
|
continue;
|
|
156
169
|
}
|
|
157
170
|
try {
|
|
158
|
-
const
|
|
171
|
+
const source = activity.input
|
|
172
|
+
.replace(/.*?```graphql\s*/, '')
|
|
173
|
+
.replace(/```$/, '')
|
|
174
|
+
.trim();
|
|
175
|
+
const result = this.executor
|
|
176
|
+
? await this.executor(source)
|
|
177
|
+
: await (0, graphql_1.graphql)({ schema: this.schema, source });
|
|
159
178
|
this.addActivities(...activities, new activity_1.Activity({
|
|
160
179
|
kind: activity_1.ActivityKind.Observation,
|
|
161
|
-
input:
|
|
162
|
-
attributes: { of: activity.attributes.kind }
|
|
180
|
+
input: (0, yaml_1.stringify)(result),
|
|
163
181
|
}));
|
|
164
182
|
return;
|
|
165
183
|
}
|
|
@@ -168,7 +186,6 @@ class Agent {
|
|
|
168
186
|
activities.push(new activity_1.Activity({
|
|
169
187
|
kind: activity_1.ActivityKind.Observation,
|
|
170
188
|
input: err.toString(),
|
|
171
|
-
attributes: { of: activity.attributes.kind }
|
|
172
189
|
}));
|
|
173
190
|
this.addActivities(...activities);
|
|
174
191
|
activities = [];
|
|
@@ -178,29 +195,16 @@ class Agent {
|
|
|
178
195
|
}
|
|
179
196
|
throw new Error('Max number of retries reached.');
|
|
180
197
|
}
|
|
181
|
-
async execute({ attributes, input }) {
|
|
182
|
-
const { kind } = attributes;
|
|
183
|
-
const action = this.actions.find(a => a.kind === kind);
|
|
184
|
-
if (!action) {
|
|
185
|
-
throw new Error(`Action "${kind}" is not allowed. Correct your spelling.`);
|
|
186
|
-
}
|
|
187
|
-
const observation = await action._call(input, this);
|
|
188
|
-
return observation;
|
|
189
|
-
}
|
|
190
198
|
async invoke(params) {
|
|
191
199
|
if (!this.history.length) {
|
|
192
|
-
throw new Error('
|
|
200
|
+
throw new Error('History must not be empty.');
|
|
193
201
|
}
|
|
194
202
|
if (this.history.at(-1)?.kind !== activity_1.ActivityKind.Observation) {
|
|
195
203
|
throw new Error('Latest experience must be of Observation kind');
|
|
196
204
|
}
|
|
197
205
|
for (let i = 0; i < this.maxIterations; ++i) {
|
|
198
206
|
await this.prompt(params);
|
|
199
|
-
|
|
200
|
-
const action = this.actions.find(a => a.kind === activity.attributes.kind);
|
|
201
|
-
if (action.exit) {
|
|
202
|
-
return this.history.at(-1).input;
|
|
203
|
-
}
|
|
207
|
+
this.signal?.throwIfAborted();
|
|
204
208
|
}
|
|
205
209
|
throw new Error('Max number of iterations reached.');
|
|
206
210
|
}
|
package/dist/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,+BAAiC;AACjC,gDAAoC;AAEpC,qDAA6E;AAC7E,mEAAoE;AAGpE,kDAA6D;AAE7D,qCAAiD;AAEjD,2CAA2C;AAC3C,yCAAoD;AA+CpD,MAAa,KAAK;IAChB,IAAI,CAAU;IACd,WAAW,CAAU;IACrB,GAAG,CAAqB;IACxB,QAAQ,CAAc;IACtB,SAAS,CAAa;IACtB,OAAO,CAAc;IACrB,QAAQ,CAAa;IACrB,SAAS,CAAS;IAClB,WAAW,CAAS;IACpB,aAAa,CAAS;IACtB,UAAU,CAAS;IACnB,WAAW,CAAU;IACrB,MAAM,CAAc;IACpB,SAAS,CAAa;IACtB,MAAM,CAAU;IAChB,QAAQ,CAAsB;IAC9B,QAAQ,CAAmB;IAElB,MAAM,CAAgB;IAE/B,MAAM,CAAC,QAAQ,GAAwB;QACrC,QAAQ,EAAE,wBAAc,CAAC,YAAY,CAAC,IAAA,gBAAM,EAAA;;;;;;;;;;;;;;;;;KAiB3C,CAAC;QACF,SAAS,EAAE,4BAA4B;QACvC,WAAW,EAAE,IAAA,gBAAM,EAAA;;;;;;;KAOlB;QACD,UAAU,EAAE,CAAC;QACb,WAAW,EAAE,KAAK;QAClB,aAAa,EAAE,MAAM,CAAC,gBAAgB;QACtC,MAAM,EAAE,IAAA,cAAI,GAAE;QACd,QAAQ,EAAE;YACR,IAAI,mBAAQ,CAAC;gBACX,IAAI,EAAE,u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,CAAC;gBAEvB,UAAU,CAAC,IAAI,CAAC,IAAI,mBAAQ,CAAC;oBAC3B,IAAI,EAAE,uBAAY,CAAC,WAAW;oBAC9B,KAAK,EAAE,GAAG,CAAC,QAAQ,EAAE;iBACtB,CAAC,CAAC,CAAC;gBAEJ,IAAI,CAAC,aAAa,CAAC,GAAG,UAAU,CAAC,CAAC;gBAClC,UAAU,GAAG,EAAE,CAAC;gBAChB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,yBAAyB,GAAG,EAAE,CAAC,CAAC;gBAChE,SAAS;aACV;SACF;QAED,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,MAA4B;QACvC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACxB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAC/C;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,KAAK,uBAAY,CAAC,WAAW,EAAE;YAC1D,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;SAClE;QAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,aAAa,EAAE,EAAE,CAAC,EAAE;YAC3C,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC1B,IAAI,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;SAC/B;QAED,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvD,CAAC;;AA9OH,sBA+OC"}
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,IAAI,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,7 +14,6 @@ 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("./action"), exports);
|
|
18
17
|
__exportStar(require("./agent"), exports);
|
|
19
18
|
__exportStar(require("./constants"), exports);
|
|
20
19
|
__exportStar(require("./activity"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,8CAA4B;AAC5B,6CAA2B;AAC3B,0CAAwB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caretakerai/agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "Single framework for building text-agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,11 +21,14 @@
|
|
|
21
21
|
"ajv-errors": "^3.0.0",
|
|
22
22
|
"debug": "^4.3.4",
|
|
23
23
|
"json-schema-to-typescript": "^13.1.1",
|
|
24
|
-
"
|
|
25
|
-
"xml-js": "^1.6.11"
|
|
24
|
+
"pino": "^9.0.0",
|
|
25
|
+
"xml-js": "^1.6.11",
|
|
26
|
+
"yaml": "^2.4.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
29
|
+
"@graphql-tools/schema": "^10.0.2",
|
|
28
30
|
"@types/node": "^20.4.1",
|
|
31
|
+
"graphql": "^16.8.1",
|
|
29
32
|
"rimraf": "^5.0.5",
|
|
30
33
|
"tsx": "^3.12.7",
|
|
31
34
|
"typescript": "^5.2.2"
|
package/dist/action.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Agent } from './agent';
|
|
2
|
-
import { type JSONSchema } from 'json-schema-to-typescript';
|
|
3
|
-
import { Activity } from './activity';
|
|
4
|
-
export interface ActionInput<T> {
|
|
5
|
-
params: T;
|
|
6
|
-
agent: Agent;
|
|
7
|
-
}
|
|
8
|
-
export interface ActionExample {
|
|
9
|
-
description?: string;
|
|
10
|
-
activities: Activity[];
|
|
11
|
-
}
|
|
12
|
-
export declare abstract class Action<P = any, R = any> {
|
|
13
|
-
abstract get exit(): boolean;
|
|
14
|
-
abstract get kind(): string;
|
|
15
|
-
abstract get description(): string;
|
|
16
|
-
abstract get params(): JSONSchema;
|
|
17
|
-
abstract get result(): JSONSchema;
|
|
18
|
-
abstract get examples(): ActionExample[];
|
|
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>;
|
|
23
|
-
}
|
package/dist/action.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
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.Action = void 0;
|
|
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/core/prompts");
|
|
11
|
-
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
|
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
|
-
/**
|
|
21
|
-
* @kind {kind}
|
|
22
|
-
{description}
|
|
23
|
-
* /
|
|
24
|
-
|
|
25
|
-
{params}
|
|
26
|
-
|
|
27
|
-
{result}
|
|
28
|
-
{examples}\`\`\`
|
|
29
|
-
`).trim();
|
|
30
|
-
class Action {
|
|
31
|
-
_examplesPrompt() {
|
|
32
|
-
return this.examples
|
|
33
|
-
.map(({ activities, description }) => [
|
|
34
|
-
(0, dedent_1.default) `
|
|
35
|
-
/**
|
|
36
|
-
* @example ${description}`,
|
|
37
|
-
activities.map(a => a.prompt())
|
|
38
|
-
.join(constants_1.ACTIVITY_SEP)
|
|
39
|
-
.split('\n')
|
|
40
|
-
.map(s => ` * ${s}`)
|
|
41
|
-
.join('\n'),
|
|
42
|
-
' */'
|
|
43
|
-
].join('\n'))
|
|
44
|
-
.join('\n\n') + '\n';
|
|
45
|
-
}
|
|
46
|
-
async _prompt(template = ACTION_TEMPLATE) {
|
|
47
|
-
const paramsType = `${this.kind}Params`;
|
|
48
|
-
const resultType = `${this.kind}Result`;
|
|
49
|
-
const partial = await prompts_1.PromptTemplate.fromTemplate(template).partial({
|
|
50
|
-
params: async () => {
|
|
51
|
-
const ts = await (0, json_schema_to_typescript_1.compile)(this.params, paramsType, { bannerComment: '', additionalProperties: false });
|
|
52
|
-
return ts.replace(/^(export\s*)/gm, '').trim();
|
|
53
|
-
},
|
|
54
|
-
result: async () => {
|
|
55
|
-
const ts = await (0, json_schema_to_typescript_1.compile)(this.result, resultType, { bannerComment: '', additionalProperties: false });
|
|
56
|
-
return ts.replace(/^(export\s*)/gm, '').trim();
|
|
57
|
-
},
|
|
58
|
-
examples: () => this._examplesPrompt(),
|
|
59
|
-
kind: this.kind,
|
|
60
|
-
description: this.description
|
|
61
|
-
.split('\n')
|
|
62
|
-
.map(s => ` * ${s}`)
|
|
63
|
-
.join('\n'),
|
|
64
|
-
paramsType,
|
|
65
|
-
resultType,
|
|
66
|
-
});
|
|
67
|
-
return partial
|
|
68
|
-
.pipe(prompt => prompt.toString().trim())
|
|
69
|
-
.invoke({});
|
|
70
|
-
}
|
|
71
|
-
async _call(input, agent) {
|
|
72
|
-
const params = JSON.parse(input);
|
|
73
|
-
const validator = ajv.compile(this.params);
|
|
74
|
-
const isValid = validator(params);
|
|
75
|
-
if (!isValid) {
|
|
76
|
-
throw new Error(`Action "${this.kind}" params are not valid: ${ajv.errorsText(validator.errors)} `);
|
|
77
|
-
}
|
|
78
|
-
const result = await this.call({ params, agent });
|
|
79
|
-
if (typeof result === 'string') {
|
|
80
|
-
return result;
|
|
81
|
-
}
|
|
82
|
-
return JSON.stringify(result);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
exports.Action = Action;
|
|
86
|
-
//# sourceMappingURL=action.js.map
|
package/dist/action.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"action.js","sourceRoot":"","sources":["../src/action.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,8CAAsB;AACtB,4DAAyC;AACzC,qDAAyD;AAEzD,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;;;;;;;;;;;CAWxB,CAAC,CAAC,IAAI,EAAE,CAAC;AAaV,MAAsB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