forked from wrtnlabs/agentica
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrtnAgent.ts
More file actions
280 lines (260 loc) · 8.25 KB
/
WrtnAgent.ts
File metadata and controls
280 lines (260 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import OpenAI from "openai";
import { ChatGptAgent } from "./chatgpt/ChatGptAgent";
import { WrtnAgentCostAggregator } from "./internal/WrtnAgentCostAggregator";
import { WrtnAgentOperationComposer } from "./internal/WrtnAgentOperationComposer";
import { WrtnAgentPromptTransformer } from "./internal/WrtnAgentPromptTransformer";
import { __map_take } from "./internal/__map_take";
import { IWrtnAgentConfig } from "./structures/IWrtnAgentConfig";
import { IWrtnAgentContext } from "./structures/IWrtnAgentContext";
import { IWrtnAgentController } from "./structures/IWrtnAgentController";
import { IWrtnAgentEvent } from "./structures/IWrtnAgentEvent";
import { IWrtnAgentOperationCollection } from "./structures/IWrtnAgentOperationCollection";
import { IWrtnAgentOperationSelection } from "./structures/IWrtnAgentOperationSelection";
import { IWrtnAgentPrompt } from "./structures/IWrtnAgentPrompt";
import { IWrtnAgentProps } from "./structures/IWrtnAgentProps";
import { IWrtnAgentProvider } from "./structures/IWrtnAgentProvider";
import { IWrtnAgentTokenUsage } from "./structures/IWrtnAgentTokenUsage";
/**
* Nestia A.I. chatbot agent.
*
* `WrtnChatAgent` is a facade class for the super A.I. chatbot agent
* which performs the {@link converstate user's conversation function}
* with LLM (Large Language Model) function calling and manages the
* {@link getPromptHistories prompt histories}.
*
* To understand and compose the `WrtnAgent` class exactly, reference
* below types concentrating on the documentation comments please.
* Especially, you have to be careful about the {@link IWrtnAgentProps}
* type which is used in the {@link constructor} function.
*
* - Constructors
* - {@link IWrtnAgentProps}
* - {@link IWrtnAgentProvider}
* - {@link IWrtnAgentController}
* - {@link IWrtnAgentConfig}
* - {@link IWrtnAgentSystemPrompt}
* - Accessors
* - {@link IWrtnAgentOperation}
* - {@link IWrtnAgentPrompt}
* - {@link IWrtnAgentEvent}
* - {@link IWrtnAgentTokenUsage}
*
* @author Samchon
*/
export class WrtnAgent {
// THE OPERATIONS
private readonly operations_: IWrtnAgentOperationCollection;
// STACK
private readonly stack_: IWrtnAgentOperationSelection[];
private readonly prompt_histories_: IWrtnAgentPrompt[];
private readonly listeners_: Map<string, Set<Function>>;
// STATUS
private readonly token_usage_: IWrtnAgentTokenUsage;
private ready_: boolean;
private readonly agentExecutionPlan_: (
ctx: IWrtnAgentContext,
) => Promise<IWrtnAgentPrompt[]>;
/* -----------------------------------------------------------
CONSTRUCTOR
----------------------------------------------------------- */
/**
* Initializer constructor.
*
* @param props Properties to construct the agent
*/
public constructor(private readonly props: IWrtnAgentProps) {
// OPERATIONS
this.operations_ = WrtnAgentOperationComposer.compose({
controllers: props.controllers,
config: props.config,
});
// STATUS
this.stack_ = [];
this.listeners_ = new Map();
this.prompt_histories_ = (props.histories ?? []).map((input) =>
WrtnAgentPromptTransformer.transform({
operations: this.operations_.group,
input,
}),
);
// STATUS
this.token_usage_ = {
total: 0,
prompt: {
total: 0,
audio: 0,
cached: 0,
},
completion: {
total: 0,
accepted_prediction: 0,
audio: 0,
reasoning: 0,
rejected_prediction: 0,
},
};
this.ready_ = false;
this.agentExecutionPlan_ = props.agentExecutionPlan ?? ChatGptAgent.execute;
}
/* -----------------------------------------------------------
ACCESSORS
----------------------------------------------------------- */
/**
* Conversate with the A.I. chatbot.
*
* User talks to the A.I. chatbot with the content.
*
* When the user's conversation implies the A.I. chatbot to execute a
* function calling, the returned chat prompts will contain the
* function calling information like {@link IWrtnAgentPrompt.IExecute}.
*
* @param content The content to talk
* @returns List of newly created chat prompts
*/
public async conversate(content: string): Promise<IWrtnAgentPrompt[]> {
const prompt: IWrtnAgentPrompt.IText = {
type: "text",
role: "user",
text: content,
};
await this.dispatch(prompt);
const newbie: IWrtnAgentPrompt[] = await this.agentExecutionPlan_({
// APPLICATION
operations: this.operations_,
config: this.props.config,
// STATES
histories: this.prompt_histories_,
stack: this.stack_,
ready: () => this.ready_,
prompt,
// HANDLERS
dispatch: (event) => this.dispatch(event),
request: async (source, body) => {
// request information
const event: IWrtnAgentEvent.IRequest = {
type: "request",
source,
body: {
...body,
model: this.props.provider.model,
},
options: this.props.provider.options,
};
await this.dispatch(event);
// completion
const value: OpenAI.ChatCompletion =
await this.props.provider.api.chat.completions.create(
event.body,
event.options,
);
WrtnAgentCostAggregator.aggregate(this.token_usage_, value);
await this.dispatch({
type: "response",
source,
body: event.body,
options: event.options,
value,
});
return value;
},
initialize: async () => {
this.ready_ = true;
await this.dispatch({
type: "initialize",
});
},
});
this.prompt_histories_.push(prompt, ...newbie);
return [prompt, ...newbie];
}
/**
* Get configuration.
*/
public getConfig(): IWrtnAgentConfig | undefined {
return this.props.config;
}
/**
* Get LLM Provider.
*/
public getProvider(): IWrtnAgentProvider {
return this.props.provider;
}
/**
* Get controllers.
*
* Get list of controllers, which are the collection of functions that
* the "Super A.I. Chatbot" can execute.
*/
public getControllers(): ReadonlyArray<IWrtnAgentController> {
return this.props.controllers;
}
/**
* Get the chatbot's prompt histories.
*
* Get list of chat prompts that the chatbot has been conversated.
*
* @returns List of chat prompts
*/
public getPromptHistories(): IWrtnAgentPrompt[] {
return this.prompt_histories_;
}
/**
* Get token usage of the A.I. chatbot.
*
* Entire token usage of the A.I. chatbot during the conversating
* with the user by {@link conversate} method callings.
*
* @returns Cost of the A.I. chatbot
*/
public getTokenUsage(): IWrtnAgentTokenUsage {
return this.token_usage_;
}
/* -----------------------------------------------------------
EVENT HANDLERS
----------------------------------------------------------- */
/**
* Add an event listener.
*
* Add an event listener to be called whenever the event is emitted.
*
* @param type Type of event
* @param listener Callback function to be called whenever the event is emitted
*/
public on<Type extends IWrtnAgentEvent.Type>(
type: Type,
listener: (event: IWrtnAgentEvent.Mapper[Type]) => void | Promise<void>,
): void {
__map_take(this.listeners_, type, () => new Set()).add(listener);
}
/**
* Erase an event listener.
*
* Erase an event listener to stop calling the callback function.
*
* @param type Type of event
* @param listener Callback function to erase
*/
public off<Type extends IWrtnAgentEvent.Type>(
type: Type,
listener: (event: IWrtnAgentEvent.Mapper[Type]) => void | Promise<void>,
): void {
const set: Set<Function> | undefined = this.listeners_.get(type);
if (set) {
set.delete(listener);
if (set.size === 0) this.listeners_.delete(type);
}
}
private async dispatch<Event extends IWrtnAgentEvent>(
event: Event,
): Promise<void> {
const set: Set<Function> | undefined = this.listeners_.get(event.type);
if (set)
await Promise.all(
Array.from(set).map(async (listener) => {
try {
await listener(event);
} catch {}
}),
);
}
}