-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.ts
915 lines (789 loc) Β· 23.7 KB
/
index.ts
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
import type { OpenAI as OpenAIClient } from "openai";
import { Document } from "../document.js";
import { Serializable, SerializedConstructor } from "../load/serializable.js";
import type { StringWithAutocomplete } from "../util/types.js";
export const RUN_KEY = "__run";
export type Example = Record<string, string>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type InputValues<K extends string = string> = Record<K, any>;
export type PartialValues<K extends string = string> = Record<
K,
string | (() => Promise<string>) | (() => string)
>;
/**
* Output of a single generation.
*/
export interface Generation {
/**
* Generated text output
*/
text: string;
/**
* Raw generation info response from the provider.
* May include things like reason for finishing (e.g. in {@link OpenAI})
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
generationInfo?: Record<string, any>;
}
export type GenerationChunkFields = {
text: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
generationInfo?: Record<string, any>;
};
/**
* Chunk of a single generation. Used for streaming.
*/
export class GenerationChunk implements Generation {
public text: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public generationInfo?: Record<string, any>;
constructor(fields: GenerationChunkFields) {
this.text = fields.text;
this.generationInfo = fields.generationInfo;
}
concat(chunk: GenerationChunk): GenerationChunk {
return new GenerationChunk({
text: this.text + chunk.text,
generationInfo: {
...this.generationInfo,
...chunk.generationInfo,
},
});
}
}
/**
* Contains all relevant information returned by an LLM.
*/
export type LLMResult = {
/**
* List of the things generated. Each input could have multiple {@link Generation | generations}, hence this is a list of lists.
*/
generations: Generation[][];
/**
* Dictionary of arbitrary LLM-provider specific output.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
llmOutput?: Record<string, any>;
/**
* Dictionary of run metadata
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[RUN_KEY]?: Record<string, any>;
};
export interface StoredMessageData {
content: string;
role: string | undefined;
name: string | undefined;
tool_call_id: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
additional_kwargs?: Record<string, any>;
}
export interface StoredMessage {
type: string;
data: StoredMessageData;
}
export interface StoredGeneration {
text: string;
message?: StoredMessage;
}
export type MessageType =
| "human"
| "ai"
| "generic"
| "system"
| "function"
| "tool";
export type MessageContent =
| string
| {
type: "text" | "image_url";
text?: string;
image_url?: string | { url: string; detail?: "low" | "high" };
}[];
export interface BaseMessageFields {
content: MessageContent;
name?: string;
additional_kwargs?: {
function_call?: OpenAIClient.Chat.ChatCompletionMessage.FunctionCall;
tool_calls?: OpenAIClient.Chat.ChatCompletionMessageToolCall[];
[key: string]: unknown;
};
}
export interface ChatMessageFieldsWithRole extends BaseMessageFields {
role: string;
}
export interface FunctionMessageFieldsWithName extends BaseMessageFields {
name: string;
}
export interface ToolMessageFieldsWithToolCallId extends BaseMessageFields {
tool_call_id: string;
}
function mergeContent(
firstContent: MessageContent,
secondContent: MessageContent
): MessageContent {
// If first content is a string
if (typeof firstContent === "string") {
if (typeof secondContent === "string") {
return firstContent + secondContent;
} else {
return [{ type: "text", text: firstContent }, ...secondContent];
}
// If both are arrays
} else if (Array.isArray(secondContent)) {
return [...firstContent, ...secondContent];
// If the first content is a list and second is a string
} else {
// Otherwise, add the second content as a new element of the list
return [...firstContent, { type: "text", text: secondContent }];
}
}
/**
* Base class for all types of messages in a conversation. It includes
* properties like `content`, `name`, and `additional_kwargs`. It also
* includes methods like `toDict()` and `_getType()`.
*/
export abstract class BaseMessage
extends Serializable
implements BaseMessageFields
{
lc_namespace = ["langchain", "schema"];
lc_serializable = true;
/**
* @deprecated
* Use {@link BaseMessage.content} instead.
*/
get text(): string {
return typeof this.content === "string" ? this.content : "";
}
/** The content of the message. */
content: MessageContent;
/** The name of the message sender in a multi-user chat. */
name?: string;
/** Additional keyword arguments */
additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;
/** The type of the message. */
abstract _getType(): MessageType;
constructor(
fields: string | BaseMessageFields,
/** @deprecated */
kwargs?: Record<string, unknown>
) {
if (typeof fields === "string") {
// eslint-disable-next-line no-param-reassign
fields = { content: fields, additional_kwargs: kwargs };
}
// Make sure the default value for additional_kwargs is passed into super() for serialization
if (!fields.additional_kwargs) {
// eslint-disable-next-line no-param-reassign
fields.additional_kwargs = {};
}
super(fields);
this.name = fields.name;
this.content = fields.content;
this.additional_kwargs = fields.additional_kwargs;
}
toDict(): StoredMessage {
return {
type: this._getType(),
data: (this.toJSON() as SerializedConstructor)
.kwargs as StoredMessageData,
};
}
toChunk(): BaseMessageChunk {
const type = this._getType();
if (type === "human") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new HumanMessageChunk({ ...this });
} else if (type === "ai") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new AIMessageChunk({ ...this });
} else if (type === "system") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new SystemMessageChunk({ ...this });
} else if (type === "function") {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new FunctionMessageChunk({ ...this });
// eslint-disable-next-line @typescript-eslint/no-use-before-define
} else if (ChatMessage.isInstance(this)) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new ChatMessageChunk({ ...this });
} else {
throw new Error("Unknown message type.");
}
}
}
// TODO: Deprecate when SDK typing is updated
export type OpenAIToolCall = OpenAIClient.ChatCompletionMessageToolCall & {
index: number;
};
function isOpenAIToolCallArray(value?: unknown): value is OpenAIToolCall[] {
return (
Array.isArray(value) &&
value.every((v) => typeof (v as OpenAIToolCall).index === "number")
);
}
/**
* Represents a chunk of a message, which can be concatenated with other
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
* additional keyword arguments from another `BaseMessageChunk` into this
* one. It also overrides the `__add__()` method to support concatenation
* of `BaseMessageChunk` instances.
*/
export abstract class BaseMessageChunk extends BaseMessage {
abstract concat(chunk: BaseMessageChunk): BaseMessageChunk;
static _mergeAdditionalKwargs(
left: NonNullable<BaseMessageFields["additional_kwargs"]>,
right: NonNullable<BaseMessageFields["additional_kwargs"]>
): NonNullable<BaseMessageFields["additional_kwargs"]> {
const merged = { ...left };
for (const [key, value] of Object.entries(right)) {
if (merged[key] === undefined) {
merged[key] = value;
} else if (typeof merged[key] !== typeof value) {
throw new Error(
`additional_kwargs[${key}] already exists in the message chunk, but with a different type.`
);
} else if (typeof merged[key] === "string") {
merged[key] = (merged[key] as string) + value;
} else if (
!Array.isArray(merged[key]) &&
typeof merged[key] === "object"
) {
merged[key] = this._mergeAdditionalKwargs(
merged[key] as NonNullable<BaseMessageFields["additional_kwargs"]>,
value as NonNullable<BaseMessageFields["additional_kwargs"]>
);
} else if (
key === "tool_calls" &&
isOpenAIToolCallArray(merged[key]) &&
isOpenAIToolCallArray(value)
) {
for (const toolCall of value) {
if (merged[key]?.[toolCall.index] !== undefined) {
merged[key] = merged[key]?.map((value, i) => {
if (i !== toolCall.index) {
return value;
}
return {
...value,
...toolCall,
function: {
name: toolCall.function.name ?? value.function.name,
arguments:
(value.function.arguments ?? "") +
(toolCall.function.arguments ?? ""),
},
};
});
} else {
(merged[key] as OpenAIToolCall[])[toolCall.index] = toolCall;
}
}
} else {
throw new Error(
`additional_kwargs[${key}] already exists in this message chunk.`
);
}
}
return merged;
}
}
/**
* Represents a human message in a conversation.
*/
export class HumanMessage extends BaseMessage {
static lc_name() {
return "HumanMessage";
}
_getType(): MessageType {
return "human";
}
}
/**
* Represents a chunk of a human message, which can be concatenated with
* other human message chunks.
*/
export class HumanMessageChunk extends BaseMessageChunk {
static lc_name() {
return "HumanMessageChunk";
}
_getType(): MessageType {
return "human";
}
concat(chunk: HumanMessageChunk) {
return new HumanMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: HumanMessageChunk._mergeAdditionalKwargs(
this.additional_kwargs,
chunk.additional_kwargs
),
});
}
}
/**
* Represents an AI message in a conversation.
*/
export class AIMessage extends BaseMessage {
static lc_name() {
return "AIMessage";
}
_getType(): MessageType {
return "ai";
}
}
/**
* Represents a chunk of an AI message, which can be concatenated with
* other AI message chunks.
*/
export class AIMessageChunk extends BaseMessageChunk {
static lc_name() {
return "AIMessageChunk";
}
_getType(): MessageType {
return "ai";
}
concat(chunk: AIMessageChunk) {
return new AIMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: AIMessageChunk._mergeAdditionalKwargs(
this.additional_kwargs,
chunk.additional_kwargs
),
});
}
}
/**
* Represents a system message in a conversation.
*/
export class SystemMessage extends BaseMessage {
static lc_name() {
return "SystemMessage";
}
_getType(): MessageType {
return "system";
}
}
/**
* Represents a chunk of a system message, which can be concatenated with
* other system message chunks.
*/
export class SystemMessageChunk extends BaseMessageChunk {
static lc_name() {
return "SystemMessageChunk";
}
_getType(): MessageType {
return "system";
}
concat(chunk: SystemMessageChunk) {
return new SystemMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: SystemMessageChunk._mergeAdditionalKwargs(
this.additional_kwargs,
chunk.additional_kwargs
),
});
}
}
/**
* @deprecated
* Use {@link BaseMessage} instead.
*/
export const BaseChatMessage = BaseMessage;
/**
* @deprecated
* Use {@link HumanMessage} instead.
*/
export const HumanChatMessage = HumanMessage;
/**
* @deprecated
* Use {@link AIMessage} instead.
*/
export const AIChatMessage = AIMessage;
/**
* @deprecated
* Use {@link SystemMessage} instead.
*/
export const SystemChatMessage = SystemMessage;
/**
* Represents a function message in a conversation.
*/
export class FunctionMessage extends BaseMessage {
static lc_name() {
return "FunctionMessage";
}
constructor(fields: FunctionMessageFieldsWithName);
constructor(
fields: string | BaseMessageFields,
/** @deprecated */
name: string
);
constructor(
fields: string | FunctionMessageFieldsWithName,
/** @deprecated */
name?: string
) {
if (typeof fields === "string") {
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
fields = { content: fields, name: name! };
}
super(fields);
}
_getType(): MessageType {
return "function";
}
}
/**
* Represents a chunk of a function message, which can be concatenated
* with other function message chunks.
*/
export class FunctionMessageChunk extends BaseMessageChunk {
static lc_name() {
return "FunctionMessageChunk";
}
_getType(): MessageType {
return "function";
}
concat(chunk: FunctionMessageChunk) {
return new FunctionMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: FunctionMessageChunk._mergeAdditionalKwargs(
this.additional_kwargs,
chunk.additional_kwargs
),
name: this.name ?? "",
});
}
}
/**
* Represents a tool message in a conversation.
*/
export class ToolMessage extends BaseMessage {
static lc_name() {
return "ToolMessage";
}
tool_call_id: string;
constructor(fields: ToolMessageFieldsWithToolCallId);
constructor(
fields: string | BaseMessageFields,
tool_call_id: string,
name?: string
);
constructor(
fields: string | ToolMessageFieldsWithToolCallId,
tool_call_id?: string,
name?: string
) {
if (typeof fields === "string") {
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
fields = { content: fields, name, tool_call_id: tool_call_id! };
}
super(fields);
this.tool_call_id = fields.tool_call_id;
}
_getType(): MessageType {
return "tool";
}
}
/**
* Represents a chunk of a tool message, which can be concatenated
* with other tool message chunks.
*/
export class ToolMessageChunk extends BaseMessageChunk {
tool_call_id: string;
constructor(fields: ToolMessageFieldsWithToolCallId) {
super(fields);
this.tool_call_id = fields.tool_call_id;
}
static lc_name() {
return "ToolMessageChunk";
}
_getType(): MessageType {
return "tool";
}
concat(chunk: ToolMessageChunk) {
return new ToolMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: ToolMessageChunk._mergeAdditionalKwargs(
this.additional_kwargs,
chunk.additional_kwargs
),
tool_call_id: this.tool_call_id,
});
}
}
/**
* Represents a chat message in a conversation.
*/
export class ChatMessage
extends BaseMessage
implements ChatMessageFieldsWithRole
{
static lc_name() {
return "ChatMessage";
}
role: string;
constructor(content: string, role: string);
constructor(fields: ChatMessageFieldsWithRole);
constructor(fields: string | ChatMessageFieldsWithRole, role?: string) {
if (typeof fields === "string") {
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
fields = { content: fields, role: role! };
}
super(fields);
this.role = fields.role;
}
_getType(): MessageType {
return "generic";
}
static isInstance(message: BaseMessage): message is ChatMessage {
return message._getType() === "generic";
}
}
export type BaseMessageLike =
| BaseMessage
| [StringWithAutocomplete<MessageType | "user" | "assistant">, string]
| string;
export function isBaseMessage(
messageLike?: unknown
): messageLike is BaseMessage {
return typeof (messageLike as BaseMessage)?._getType === "function";
}
export function isBaseMessageChunk(
messageLike?: unknown
): messageLike is BaseMessageChunk {
return (
isBaseMessage(messageLike) &&
typeof (messageLike as BaseMessageChunk).concat === "function"
);
}
export function coerceMessageLikeToMessage(
messageLike: BaseMessageLike
): BaseMessage {
if (typeof messageLike === "string") {
return new HumanMessage(messageLike);
} else if (isBaseMessage(messageLike)) {
return messageLike;
}
const [type, content] = messageLike;
if (type === "human" || type === "user") {
return new HumanMessage({ content });
} else if (type === "ai" || type === "assistant") {
return new AIMessage({ content });
} else if (type === "system") {
return new SystemMessage({ content });
} else {
throw new Error(
`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.`
);
}
}
/**
* Represents a chunk of a chat message, which can be concatenated with
* other chat message chunks.
*/
export class ChatMessageChunk extends BaseMessageChunk {
static lc_name() {
return "ChatMessageChunk";
}
role: string;
constructor(content: string, role: string);
constructor(fields: ChatMessageFieldsWithRole);
constructor(fields: string | ChatMessageFieldsWithRole, role?: string) {
if (typeof fields === "string") {
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
fields = { content: fields, role: role! };
}
super(fields);
this.role = fields.role;
}
_getType(): MessageType {
return "generic";
}
concat(chunk: ChatMessageChunk) {
return new ChatMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: ChatMessageChunk._mergeAdditionalKwargs(
this.additional_kwargs,
chunk.additional_kwargs
),
role: this.role,
});
}
}
export interface ChatGeneration extends Generation {
message: BaseMessage;
}
export type ChatGenerationChunkFields = GenerationChunkFields & {
message: BaseMessageChunk;
};
export class ChatGenerationChunk
extends GenerationChunk
implements ChatGeneration
{
public message: BaseMessageChunk;
constructor(fields: ChatGenerationChunkFields) {
super(fields);
this.message = fields.message;
}
concat(chunk: ChatGenerationChunk) {
return new ChatGenerationChunk({
text: this.text + chunk.text,
generationInfo: {
...this.generationInfo,
...chunk.generationInfo,
},
message: this.message.concat(chunk.message),
});
}
}
interface StoredMessageV1 {
type: string;
role: string | undefined;
text: string;
}
/**
* Maps messages from an older format (V1) to the current `StoredMessage`
* format. If the message is already in the `StoredMessage` format, it is
* returned as is. Otherwise, it transforms the V1 message into a
* `StoredMessage`. This function is important for maintaining
* compatibility with older message formats.
*/
function mapV1MessageToStoredMessage(
message: StoredMessage | StoredMessageV1
): StoredMessage {
// TODO: Remove this mapper when we deprecate the old message format.
if ((message as StoredMessage).data !== undefined) {
return message as StoredMessage;
} else {
const v1Message = message as StoredMessageV1;
return {
type: v1Message.type,
data: {
content: v1Message.text,
role: v1Message.role,
name: undefined,
tool_call_id: undefined,
},
};
}
}
export function mapStoredMessageToChatMessage(message: StoredMessage) {
const storedMessage = mapV1MessageToStoredMessage(message);
switch (storedMessage.type) {
case "human":
return new HumanMessage(storedMessage.data);
case "ai":
return new AIMessage(storedMessage.data);
case "system":
return new SystemMessage(storedMessage.data);
case "function":
if (storedMessage.data.name === undefined) {
throw new Error("Name must be defined for function messages");
}
return new FunctionMessage(
storedMessage.data as FunctionMessageFieldsWithName
);
case "tool":
if (storedMessage.data.tool_call_id === undefined) {
throw new Error("Tool call ID must be defined for tool messages");
}
return new ToolMessage(
storedMessage.data as ToolMessageFieldsWithToolCallId
);
case "chat": {
if (storedMessage.data.role === undefined) {
throw new Error("Role must be defined for chat messages");
}
return new ChatMessage(storedMessage.data as ChatMessageFieldsWithRole);
}
default:
throw new Error(`Got unexpected type: ${storedMessage.type}`);
}
}
export interface ChatResult {
generations: ChatGeneration[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
llmOutput?: Record<string, any>;
}
/**
* Base PromptValue class. All prompt values should extend this class.
*/
export abstract class BasePromptValue extends Serializable {
abstract toString(): string;
abstract toChatMessages(): BaseMessage[];
}
export type AgentAction = {
tool: string;
toolInput: string;
log: string;
};
export type AgentFinish = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
returnValues: Record<string, any>;
log: string;
};
export type AgentStep = {
action: AgentAction;
observation: string;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ChainValues = Record<string, any>;
/**
* Base class for all chat message histories. All chat message histories
* should extend this class.
*/
export abstract class BaseChatMessageHistory extends Serializable {
public abstract getMessages(): Promise<BaseMessage[]>;
public abstract addMessage(message: BaseMessage): Promise<void>;
public abstract addUserMessage(message: string): Promise<void>;
public abstract addAIChatMessage(message: string): Promise<void>;
public abstract clear(): Promise<void>;
}
/**
* Base class for all list chat message histories. All list chat message
* histories should extend this class.
*/
export abstract class BaseListChatMessageHistory extends Serializable {
public abstract addMessage(message: BaseMessage): Promise<void>;
public addUserMessage(message: string): Promise<void> {
return this.addMessage(new HumanMessage(message));
}
public addAIChatMessage(message: string): Promise<void> {
return this.addMessage(new AIMessage(message));
}
}
/**
* Base class for all caches. All caches should extend this class.
*/
export abstract class BaseCache<T = Generation[]> {
abstract lookup(prompt: string, llmKey: string): Promise<T | null>;
abstract update(prompt: string, llmKey: string, value: T): Promise<void>;
}
/**
* Base class for all file stores. All file stores should extend this
* class.
*/
export abstract class BaseFileStore extends Serializable {
abstract readFile(path: string): Promise<string>;
abstract writeFile(path: string, contents: string): Promise<void>;
}
/**
* Base class for all entity stores. All entity stores should extend this
* class.
*/
export abstract class BaseEntityStore extends Serializable {
abstract get(key: string, defaultValue?: string): Promise<string | undefined>;
abstract set(key: string, value?: string): Promise<void>;
abstract delete(key: string): Promise<void>;
abstract exists(key: string): Promise<boolean>;
abstract clear(): Promise<void>;
}
/**
* Abstract class for a document store. All document stores should extend
* this class.
*/
export abstract class Docstore {
abstract search(search: string): Promise<Document>;
abstract add(texts: Record<string, Document>): Promise<void>;
}