forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* vector db qa chain * cr * add memory class * cr * conversation chain (langchain-ai#15)
- Loading branch information
Showing
6 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type InputValues = Record<string, any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type OutputValues = Record<string, any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type MemoryVariables = Record<string, any>; | ||
|
||
export abstract class BaseMemory { | ||
abstract loadMemoryVariables(values: InputValues): Promise<MemoryVariables>; | ||
|
||
abstract saveContext( | ||
inputValues: InputValues, | ||
OutputValues: Promise<OutputValues> | ||
): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { BaseMemory, InputValues, MemoryVariables, OutputValues } from "./base"; | ||
|
||
export interface BufferMemoryInput { | ||
humanPrefix: string; | ||
aiPrefix: string; | ||
memoryKey: string; | ||
} | ||
|
||
const getInputValue = (inputValues: InputValues) => { | ||
const keys = Object.keys(inputValues); | ||
if (keys.length === 1) { | ||
return inputValues[keys[0]]; | ||
} | ||
throw new Error( | ||
"input values have multiple keys, memory only supported when one key currently" | ||
); | ||
}; | ||
|
||
export class BufferMemory extends BaseMemory implements BufferMemoryInput { | ||
humanPrefix = "Human"; | ||
|
||
aiPrefix = "AI"; | ||
|
||
memoryKey = "history"; | ||
|
||
buffer = ""; | ||
|
||
constructor(fields?: Partial<BufferMemoryInput>) { | ||
super(); | ||
this.humanPrefix = fields?.humanPrefix ?? this.humanPrefix; | ||
this.aiPrefix = fields?.aiPrefix ?? this.aiPrefix; | ||
this.memoryKey = fields?.memoryKey ?? this.memoryKey; | ||
} | ||
|
||
async loadMemoryVariables(_values: InputValues): Promise<MemoryVariables> { | ||
const result = { [this.memoryKey]: this.buffer }; | ||
return result; | ||
} | ||
|
||
async saveContext( | ||
inputValues: InputValues, | ||
outputValues: Promise<OutputValues> | ||
): Promise<void> { | ||
const values = await outputValues; | ||
const human = `${this.humanPrefix}: ${getInputValue(inputValues)}`; | ||
const ai = `${this.aiPrefix}: ${getInputValue(values)}`; | ||
const newlines = [human, ai]; | ||
this.buffer += `\n${newlines.join("\n")}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { BufferMemory } from "./buffer_memory"; | ||
export { BaseMemory } from "./base"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { test, expect } from "@jest/globals"; | ||
import { BufferMemory } from "../buffer_memory"; | ||
import { OutputValues } from "../base"; | ||
|
||
test("Test buffer memory", async () => { | ||
const memory = new BufferMemory(); | ||
const result1 = await memory.loadMemoryVariables({}); | ||
expect(result1).toStrictEqual({ history: "" }); | ||
|
||
const result = new Promise<OutputValues>((resolve, _reject) => { | ||
resolve({ bar: "foo" }); | ||
}); | ||
await memory.saveContext({ foo: "bar" }, result); | ||
const expectedString = "\nHuman: bar\nAI: foo"; | ||
const result2 = await memory.loadMemoryVariables({}); | ||
expect(result2).toStrictEqual({ history: expectedString }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters