@caretakerai/agent 0.0.21-beta.2 → 0.0.22
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 +136 -36
- package/dist/activity.d.ts +1 -0
- package/dist/activity.js +35 -17
- package/dist/activity.js.map +1 -1
- package/dist/agent.d.ts +4 -3
- package/dist/agent.js +18 -32
- package/dist/agent.js.map +1 -1
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -27,13 +27,15 @@ When creating an `Agent`, you must provide an `AgentPrams` object with the follo
|
|
|
27
27
|
- `instruction`: (Optional) Completion instruction for the language model.
|
|
28
28
|
- `maxIterations`: (Optional) The maximum number of iterations the agent can perform.
|
|
29
29
|
- `maxRetries`: (Optional) The maximum number of retries for actions.
|
|
30
|
-
- `
|
|
30
|
+
- `optimizers`: The optimizer pileline used to improve the agent's performance.
|
|
31
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
33
|
- `stop`: (Optional) A list of strings that, if generated by the agent, should cause it to stop.
|
|
34
34
|
- `logger`: (Optional) The logger the agent will use for outputting information.
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
|
|
36
|
+
### Objective
|
|
37
|
+
|
|
38
|
+
The `objective` is a crucial component of the Agent Framework. It defines the goal or purpose that the agent is trying to achieve. The objective is used to guide the agent's behavior and decision-making process.
|
|
37
39
|
|
|
38
40
|
```typescript
|
|
39
41
|
const agent = new Agent({
|
|
@@ -45,30 +47,108 @@ const agent = new Agent({
|
|
|
45
47
|
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
48
|
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
### Schema
|
|
51
|
+
|
|
52
|
+
The schema is a GraphQL schema that defines the structure of the data that the agent can act upon. It includes type definitions for queries and mutations that the agent can perform. The schema is used to validate the queries and mutations that the agent receives, ensuring that they are well-formed and consistent with the agent's objectives.
|
|
53
|
+
|
|
54
|
+
**Importance of a Good Schema**
|
|
55
|
+
|
|
56
|
+
A good schema is essential for the QnA Agent Framework to function effectively. The schema defines the structure of the data that the agent can act upon, and it provides a clear and consistent way for the agent to interact with the user. A well-designed schema can help to:
|
|
50
57
|
|
|
51
|
-
|
|
58
|
+
* Improve the accuracy and relevance of the agent's responses
|
|
59
|
+
* Reduce the complexity of the agent's decision-making process
|
|
60
|
+
* Increase the efficiency of the agent's processing loop
|
|
61
|
+
* Enhance the overall user experience
|
|
62
|
+
|
|
63
|
+
Think of the Agent as a developer that writes the queries for you while you talk to them. When writing queries on the fly, it is essential to use a good schema to ensure that the queries are well-formed and consistent with the agent's objectives. A good schema can help to prevent errors and inconsistencies in the agent's responses, and it can improve the overall performance of the agent.
|
|
52
64
|
|
|
53
65
|
## Usage
|
|
66
|
+
To use the QnA 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.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## Resolvers
|
|
70
|
+
Resolvers are functions that implement the logic for the queries and mutations defined in the schema. They are used to retrieve or update data in response to queries and mutations.
|
|
71
|
+
|
|
72
|
+
## History
|
|
73
|
+
Having at least one element in the history is important because it provides the agent with a starting point for its conversation with the user. This initial element in the history can be thought of as the "initial prompt" or "initial context" that sets the stage for the conversation.
|
|
74
|
+
|
|
75
|
+
Without an initial element in the history, the agent would not have any context or information to work with, and it would not know how to respond to the user's first message. By including at least one element in the history, you are providing the agent with a foundation for its conversation with the user, and it can use this information to generate more informed and relevant responses.
|
|
54
76
|
|
|
55
|
-
|
|
77
|
+
In the example provided, the initial element in the history is an observation that includes a message from the user asking "How can you help me?". This initial prompt provides the agent with a clear understanding of the user's intent and allows it to respond accordingly.
|
|
56
78
|
|
|
57
|
-
Here is an example of how
|
|
58
|
-
```
|
|
79
|
+
Here is an example of how the history is used in the agent's conversation:
|
|
80
|
+
```typescript
|
|
81
|
+
history: [
|
|
82
|
+
new Activity({ kind: ActivityKind.Observation, input: dedent /* yaml */`
|
|
83
|
+
data:
|
|
84
|
+
say:
|
|
85
|
+
reply: How can you help me?
|
|
86
|
+
` }),
|
|
87
|
+
],
|
|
88
|
+
```
|
|
89
|
+
This initial element in the history is used by the agent to generate its first response to the user. The agent can use this information to determine the user's intent and provide a relevant response.
|
|
90
|
+
|
|
91
|
+
In general, having at least one element in the history is important because it:
|
|
92
|
+
|
|
93
|
+
* Provides the agent with a starting point for its conversation with the user
|
|
94
|
+
* Gives the agent context and information to work with
|
|
95
|
+
* Allows the agent to generate more informed and relevant responses
|
|
96
|
+
* Helps to establish a clear understanding of the user's intent and goals
|
|
97
|
+
|
|
98
|
+
## Example
|
|
59
99
|
|
|
100
|
+
Here is an example of how to create a QnA agent that uses a language model to answer user queries:
|
|
101
|
+
```typescript
|
|
60
102
|
const agent = new Agent({
|
|
61
|
-
name: '
|
|
62
|
-
llm: new
|
|
63
|
-
|
|
64
|
-
|
|
103
|
+
name: 'QnA',
|
|
104
|
+
llm: new ChatOpenAI({
|
|
105
|
+
modelName: 'gpt-3.5-turbo-16k',
|
|
106
|
+
temperature: 0.7,
|
|
107
|
+
maxTokens: 2000,
|
|
108
|
+
}),
|
|
109
|
+
objective: dedent`
|
|
110
|
+
1. Help the user with finding information.
|
|
111
|
+
2. Search no more than 7 times before providing the answer.
|
|
112
|
+
3. Subsequent searches must be different from one another.
|
|
113
|
+
4. Prefer multiple searches to answer complex questions.
|
|
114
|
+
5. Prefer user language in making search queries and providing answers.
|
|
115
|
+
6. Prefer answers up to 300 words long.
|
|
116
|
+
7. Prefer descriptive answers split to paragraphs instead of lists.
|
|
117
|
+
`.trim(),
|
|
118
|
+
typeDefs: dedent /* GraphQL */`
|
|
65
119
|
schema {
|
|
66
120
|
query: Query
|
|
67
121
|
mutation: Mutation
|
|
68
122
|
}
|
|
69
123
|
|
|
70
124
|
type Query {
|
|
71
|
-
|
|
125
|
+
"""
|
|
126
|
+
Perform text-searches in the knowledge base and return the results as strings.
|
|
127
|
+
|
|
128
|
+
The following example shows the method of searching for information on complex topics:
|
|
129
|
+
<Observation>
|
|
130
|
+
data:
|
|
131
|
+
say:
|
|
132
|
+
reply: 'What is the difference between a white hole and a black hole?'
|
|
133
|
+
<Observation>
|
|
134
|
+
<Thought>
|
|
135
|
+
The user is looking for distinctive features of separate entities of the universe. I should split my search into 2.
|
|
136
|
+
</Thought>
|
|
137
|
+
<Action>
|
|
138
|
+
query {
|
|
139
|
+
blackHole: search(input: { query: "What is the nature of a black hole?" }) { result }
|
|
140
|
+
whiteHole: search(input: { query: "What is the nature of a white hole?" }) { result }
|
|
141
|
+
}
|
|
142
|
+
</Action>
|
|
143
|
+
<Observation>
|
|
144
|
+
data:
|
|
145
|
+
blackHole:
|
|
146
|
+
result: 'The nature of a black hole is...'
|
|
147
|
+
whiteHole:
|
|
148
|
+
result: 'The nature of a white hole is...'
|
|
149
|
+
<Observation>
|
|
150
|
+
"""
|
|
151
|
+
search(input: SearchInput!): SearchResult
|
|
72
152
|
}
|
|
73
153
|
|
|
74
154
|
type Mutation {
|
|
@@ -76,45 +156,65 @@ const agent = new Agent({
|
|
|
76
156
|
Relay information to the user and wait for the reply. Note that this is only way of communicating information to the user.
|
|
77
157
|
"""
|
|
78
158
|
say(input: SayInput!): SayResult
|
|
79
|
-
add(input: MathInput!): MathResult
|
|
80
|
-
# ... Rest of mutations
|
|
81
159
|
}
|
|
82
160
|
|
|
83
|
-
#
|
|
161
|
+
# Inputs for mathematical operations
|
|
162
|
+
input SearchInput {
|
|
163
|
+
query: String!
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
# Inputs for say operation
|
|
167
|
+
input SayInput {
|
|
168
|
+
message: String! # The message to say to the user
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
# Result for say results
|
|
172
|
+
type SayResult {
|
|
173
|
+
reply: String # The user's reply
|
|
174
|
+
error: String # can be used to describe any error that occurred during the computation
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
# Result for mathematical operations
|
|
178
|
+
type SearchResult {
|
|
179
|
+
result: String
|
|
180
|
+
error: String # can be used to describe any error that occurred during the computation
|
|
181
|
+
}
|
|
84
182
|
`.trim(),
|
|
85
183
|
resolvers: {
|
|
184
|
+
Query: {
|
|
185
|
+
search: async (_, { input: { query } }) => {
|
|
186
|
+
try {
|
|
187
|
+
const chain = RetrievalQAChain.fromLLM(llm, retriever);
|
|
188
|
+
const { text } = await chain.invoke({ query });
|
|
189
|
+
return { result: text };
|
|
190
|
+
} catch (error) {
|
|
191
|
+
return { error };
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
},
|
|
86
195
|
Mutation: {
|
|
87
196
|
say: async (_, { input: { message } }) => {
|
|
88
197
|
console.log(`${chalk.bold(`${agent.name}:`)} ${message}`);
|
|
89
198
|
|
|
90
199
|
const reply = await inputPrompt({
|
|
91
|
-
message: 'Human:'
|
|
200
|
+
message: 'Human:',
|
|
92
201
|
});
|
|
93
202
|
|
|
94
203
|
return { reply };
|
|
95
204
|
},
|
|
96
|
-
|
|
97
|
-
try {
|
|
98
|
-
return { result: left + right };
|
|
99
|
-
} catch (error) {
|
|
100
|
-
return { error };
|
|
101
|
-
}
|
|
102
|
-
},
|
|
103
|
-
// ... rest of resolvers
|
|
104
|
-
}
|
|
205
|
+
},
|
|
105
206
|
},
|
|
106
207
|
history: [
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
})
|
|
208
|
+
new Activity({ kind: ActivityKind.Observation, input: dedent /* yaml */`
|
|
209
|
+
data:
|
|
210
|
+
say:
|
|
211
|
+
reply: How can you help me?
|
|
212
|
+
` }),
|
|
113
213
|
],
|
|
114
|
-
|
|
214
|
+
optimizers: [new RemoveErrorActivitiesOptimizer(), new LengthOptimizer(16)],
|
|
115
215
|
});
|
|
116
216
|
|
|
117
217
|
// Invoke the agent
|
|
118
218
|
await agent.invoke();
|
|
119
|
-
|
|
120
|
-
|
|
219
|
+
```
|
|
220
|
+
This example demonstrates how to create a QnA agent that uses a language model to answer user queries. The agent is configured with a set of objectives, type definitions, and resolvers that define its behavior. The `agent.invoke()` method is called to start the agent's processing loop.
|
package/dist/activity.d.ts
CHANGED
package/dist/activity.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
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");
|
|
5
4
|
var ActivityKind;
|
|
6
5
|
(function (ActivityKind) {
|
|
7
6
|
ActivityKind["Observation"] = "Observation";
|
|
@@ -16,9 +15,7 @@ class Activity {
|
|
|
16
15
|
Object.assign(this, params);
|
|
17
16
|
}
|
|
18
17
|
prompt() {
|
|
19
|
-
return
|
|
20
|
-
.replaceAll('<', '<')
|
|
21
|
-
.replaceAll('>', '>');
|
|
18
|
+
return `<${this.kind}>\n${this.input}\n</${this.kind}>`;
|
|
22
19
|
}
|
|
23
20
|
toObject() {
|
|
24
21
|
return { ...this };
|
|
@@ -27,19 +24,40 @@ class Activity {
|
|
|
27
24
|
return new Activity({ kind, input });
|
|
28
25
|
}
|
|
29
26
|
static parse(text) {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
27
|
+
const pattern = new RegExp(`<(${ActivityKind.Thought}|${ActivityKind.Action}|${ActivityKind.Observation})>(.*?)<\\/\\1>`);
|
|
28
|
+
const match = text.match(new RegExp(pattern, 'gs'));
|
|
29
|
+
if (!match) {
|
|
30
|
+
throw new Error(`Could not extract activities from "${text}"`);
|
|
31
|
+
}
|
|
32
|
+
const activities = match.map(str => {
|
|
33
|
+
const [, kind, input] = str.match(new RegExp(pattern, 's'));
|
|
34
|
+
try {
|
|
35
|
+
return new Activity({
|
|
36
|
+
kind: kind,
|
|
37
|
+
input: input.trim(),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
const err = e;
|
|
42
|
+
throw new Error(`Could not extract activity from "${str}": ${err}`);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
Activity.validateSequence(activities);
|
|
46
|
+
return activities;
|
|
47
|
+
}
|
|
48
|
+
static validateSequence(activities) {
|
|
49
|
+
activities.slice(0, -1).forEach((activity, index) => {
|
|
50
|
+
const next = activities[index + 1];
|
|
51
|
+
if (activity.kind === ActivityKind.Observation && next.kind !== ActivityKind.Thought) {
|
|
52
|
+
throw new Error(`Observation at index ${index} must be followed by Thought`);
|
|
53
|
+
}
|
|
54
|
+
if (activity.kind === ActivityKind.Thought && next.kind !== ActivityKind.Action) {
|
|
55
|
+
throw new Error(`Thought at index ${index} must be followed by Action`);
|
|
56
|
+
}
|
|
57
|
+
if (activity.kind === ActivityKind.Action && next.kind !== ActivityKind.Observation) {
|
|
58
|
+
throw new Error(`Action at index ${index} must be followed by Observation`);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
43
61
|
}
|
|
44
62
|
}
|
|
45
63
|
exports.Activity = Activity;
|
package/dist/activity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../src/activity.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../src/activity.ts"],"names":[],"mappings":";;;AAIA,IAAY,YAOX;AAPD,WAAY,YAAY;IAEtB,2CAA2B,CAAA;IAE3B,mCAAmB,CAAA;IAEnB,iCAAiB,CAAA;AACnB,CAAC,EAPW,YAAY,4BAAZ,YAAY,QAOvB;AAYD,MAAa,QAAQ;IACnB,IAAI,CAA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CAAC;gBAEvB,MAAM,IAAI,KAAK,CAAC,oCAAoC,GAAG,MAAM,GAAG,EAAE,CAAC,CAAC;aACrE;QACH,CAAC,CAAC,CAAC;QAGH,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;QAEtC,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,gBAAgB,CAAC,UAAsB;QAC5C,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE;YAClD,MAAM,IAAI,GAAG,UAAU,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;YAEnC,IAAI,QAAQ,CAAC,IAAI,KAAK,YAAY,CAAC,WAAW,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,CAAC,OAAO,EAAE;gBACpF,MAAM,IAAI,KAAK,CAAC,wBAAwB,KAAK,8BAA8B,CAAC,CAAC;aAC9E;YAED,IAAI,QAAQ,CAAC,IAAI,KAAK,YAAY,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,CAAC,MAAM,EAAE;gBAC/E,MAAM,IAAI,KAAK,CAAC,oBAAoB,KAAK,6BAA6B,CAAC,CAAC;aACzE;YAED,IAAI,QAAQ,CAAC,IAAI,KAAK,YAAY,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,CAAC,WAAW,EAAE;gBACnF,MAAM,IAAI,KAAK,CAAC,mBAAmB,KAAK,kCAAkC,CAAC,CAAC;aAC7E;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAzED,4BAyEC"}
|
package/dist/agent.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface AgentPrams {
|
|
|
20
20
|
instruction?: string;
|
|
21
21
|
maxIterations?: number;
|
|
22
22
|
maxRetries?: number;
|
|
23
|
-
|
|
23
|
+
optimizers: Optimizer[];
|
|
24
24
|
signal?: AbortSignal;
|
|
25
25
|
template?: PromptTemplate;
|
|
26
26
|
stop?: string[];
|
|
@@ -44,14 +44,15 @@ export declare class Agent implements AgentPrams {
|
|
|
44
44
|
maxRetries: number;
|
|
45
45
|
isChatModel: boolean;
|
|
46
46
|
signal: AbortSignal;
|
|
47
|
-
|
|
47
|
+
optimizers: Optimizer[];
|
|
48
48
|
logger: Logger;
|
|
49
49
|
template?: PromptTemplate;
|
|
50
50
|
executor?: GraphQLExecutor;
|
|
51
51
|
readonly schema: GraphQLSchema;
|
|
52
52
|
static defaults: Partial<AgentPrams>;
|
|
53
53
|
constructor(params: AgentPrams);
|
|
54
|
-
|
|
54
|
+
private sanitize;
|
|
55
|
+
private addActivities;
|
|
55
56
|
prompt(params?: Record<string, string>): Promise<void>;
|
|
56
57
|
invoke(params?: Record<string, any>): Promise<void>;
|
|
57
58
|
}
|
package/dist/agent.js
CHANGED
|
@@ -35,7 +35,7 @@ class Agent {
|
|
|
35
35
|
maxRetries;
|
|
36
36
|
isChatModel;
|
|
37
37
|
signal;
|
|
38
|
-
|
|
38
|
+
optimizers;
|
|
39
39
|
logger;
|
|
40
40
|
template;
|
|
41
41
|
executor;
|
|
@@ -68,7 +68,7 @@ class Agent {
|
|
|
68
68
|
new activity_1.Activity({
|
|
69
69
|
kind: activity_1.ActivityKind.Observation,
|
|
70
70
|
input: (0, dedent_1.default) `
|
|
71
|
-
data
|
|
71
|
+
data:
|
|
72
72
|
theBestNumber:
|
|
73
73
|
result: 73
|
|
74
74
|
`.trim(),
|
|
@@ -96,6 +96,11 @@ class Agent {
|
|
|
96
96
|
this.schema = (0, schema_1.makeExecutableSchema)({ typeDefs, resolvers });
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
+
sanitize(content) {
|
|
100
|
+
const thought = content.match(new RegExp(`<${activity_1.ActivityKind.Thought}>(.*?)</${activity_1.ActivityKind.Thought}>`, 's'))[1].trim();
|
|
101
|
+
const action = content.match(new RegExp(`<${activity_1.ActivityKind.Thought}>(.*?)</${activity_1.ActivityKind.Thought}>`, 's'))[1].trim();
|
|
102
|
+
return content;
|
|
103
|
+
}
|
|
99
104
|
addActivities(...activities) {
|
|
100
105
|
activities.forEach(a => this.logger.debug(a));
|
|
101
106
|
this.history.push(...activities);
|
|
@@ -103,15 +108,17 @@ class Agent {
|
|
|
103
108
|
async prompt(params) {
|
|
104
109
|
let activities = [];
|
|
105
110
|
const retryErrors = [];
|
|
111
|
+
let history = [...this.history];
|
|
112
|
+
if (history.length < this.examples.length) {
|
|
113
|
+
history = [...this.examples, ...history];
|
|
114
|
+
}
|
|
115
|
+
for (const opt of this.optimizers) {
|
|
116
|
+
history = await opt.optimize(history);
|
|
117
|
+
}
|
|
106
118
|
for (let i = 0; i < this.maxRetries; ++i) {
|
|
107
|
-
let history = [...this.history];
|
|
108
|
-
if (history.length < this.examples.length) {
|
|
109
|
-
history = [...this.examples, ...history];
|
|
110
|
-
}
|
|
111
|
-
history = await this.optimizer.optimize(history);
|
|
112
119
|
const messages = [];
|
|
113
120
|
let aiActivities = [];
|
|
114
|
-
history.forEach(activity => {
|
|
121
|
+
[...history, ...activities].forEach(activity => {
|
|
115
122
|
if (activity.kind === activity_1.ActivityKind.Observation) {
|
|
116
123
|
aiActivities.length && messages.push(new messages_1.AIMessage(aiActivities.map(a => a.prompt()).join(constants_1.ACTIVITY_SEP)));
|
|
117
124
|
aiActivities = [];
|
|
@@ -143,20 +150,11 @@ class Agent {
|
|
|
143
150
|
retryErrors.push(new Error('Generation finished due to length reason.'));
|
|
144
151
|
continue;
|
|
145
152
|
}
|
|
146
|
-
[, content] = content.split(`<${activity_1.ActivityKind.Thought}>`);
|
|
147
|
-
content = `<${activity_1.ActivityKind.Thought}>` + content;
|
|
148
|
-
[content] = content.split(`</${activity_1.ActivityKind.Action}>`);
|
|
149
|
-
content = content + `</${activity_1.ActivityKind.Action}>`;
|
|
150
153
|
try {
|
|
151
154
|
let newActivities = activity_1.Activity.parse(content).slice(0, 2);
|
|
152
155
|
if (!newActivities.length) {
|
|
153
156
|
throw new Error('No activities generated!');
|
|
154
157
|
}
|
|
155
|
-
if (newActivities.filter(a => a.kind === activity_1.ActivityKind.Thought).length > 1) {
|
|
156
|
-
throw new Error('Multiple thoughts generated!');
|
|
157
|
-
}
|
|
158
|
-
const activityIndex = newActivities.findIndex(a => a.kind === activity_1.ActivityKind.Action);
|
|
159
|
-
newActivities = newActivities.slice(0, activityIndex + 1);
|
|
160
158
|
activities.push(...newActivities);
|
|
161
159
|
}
|
|
162
160
|
catch (e) {
|
|
@@ -177,7 +175,7 @@ class Agent {
|
|
|
177
175
|
const result = this.executor
|
|
178
176
|
? await this.executor(source)
|
|
179
177
|
: await (0, graphql_1.graphql)({ schema: this.schema, source });
|
|
180
|
-
|
|
178
|
+
activities.push(new activity_1.Activity({
|
|
181
179
|
kind: activity_1.ActivityKind.Observation,
|
|
182
180
|
input: (0, yaml_1.stringify)(result),
|
|
183
181
|
}));
|
|
@@ -185,6 +183,7 @@ class Agent {
|
|
|
185
183
|
retryErrors.push(...result.errors);
|
|
186
184
|
continue;
|
|
187
185
|
}
|
|
186
|
+
this.addActivities(...activities);
|
|
188
187
|
return;
|
|
189
188
|
}
|
|
190
189
|
catch (e) {
|
|
@@ -193,8 +192,6 @@ class Agent {
|
|
|
193
192
|
kind: activity_1.ActivityKind.Observation,
|
|
194
193
|
input: err.toString(),
|
|
195
194
|
}));
|
|
196
|
-
this.addActivities(...activities);
|
|
197
|
-
activities = [];
|
|
198
195
|
const message = `Retry ${i + 1} due to action error: ${err}`;
|
|
199
196
|
this.logger.debug(message);
|
|
200
197
|
retryErrors.push(err);
|
|
@@ -207,18 +204,7 @@ class Agent {
|
|
|
207
204
|
if (!this.history.length) {
|
|
208
205
|
throw new Error('History must not be empty.');
|
|
209
206
|
}
|
|
210
|
-
|
|
211
|
-
const next = this.history[index + 1];
|
|
212
|
-
if (activity.kind === activity_1.ActivityKind.Observation && next.kind !== activity_1.ActivityKind.Thought) {
|
|
213
|
-
throw new Error(`Observation at index ${index} must be followed by Thought`);
|
|
214
|
-
}
|
|
215
|
-
if (activity.kind === activity_1.ActivityKind.Thought && next.kind !== activity_1.ActivityKind.Action) {
|
|
216
|
-
throw new Error(`Thought at index ${index} must be followed by Action`);
|
|
217
|
-
}
|
|
218
|
-
if (activity.kind === activity_1.ActivityKind.Action && next.kind !== activity_1.ActivityKind.Observation) {
|
|
219
|
-
throw new Error(`Action at index ${index} must be followed by Observation`);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
207
|
+
activity_1.Activity.validateSequence(this.history);
|
|
222
208
|
if (this.history.at(-1)?.kind !== activity_1.ActivityKind.Observation) {
|
|
223
209
|
throw new Error('Latest experience must be of Observation kind');
|
|
224
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,+BAAiC;AACjC,gDAAoC;AAEpC,qDAAwD;AAGxD,kDAA6D;AAE7D,qCAAkE;AAElE,2CAA2C;AAC3C,yCAAoD;AAEpD,uDAA+F;AA8C/F,MAAa,eAAgB,SAAQ,KAAK;IAG/B;IAFT,YACE,OAAe,EACR,MAAe;QAEtB,KAAK,CAAC,OAAO,CAAC,CAAA;QAFP,WAAM,GAAN,MAAM,CAAS;IAGxB,CAAC;CACF;AAPD,0CAOC;AAED,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,
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,+BAAiC;A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1B,IAAI,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;SAC/B;QAED,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvD,CAAC;;AA/OH,sBAgPC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caretakerai/agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "Single framework for building text-agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
"debug": "^4.3.4",
|
|
23
23
|
"json-schema-to-typescript": "^13.1.1",
|
|
24
24
|
"pino": "^9.0.0",
|
|
25
|
-
"xml-js": "^1.6.11",
|
|
26
25
|
"yaml": "^2.4.0"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|