Skip to content

Commit

Permalink
Move to message stores
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 4, 2024
1 parent cb14829 commit 20676f7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 49 deletions.
8 changes: 4 additions & 4 deletions libs/langchain-community/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ stores/message/firestore.cjs
stores/message/firestore.js
stores/message/firestore.d.ts
stores/message/firestore.d.cts
stores/message/file_system.cjs
stores/message/file_system.js
stores/message/file_system.d.ts
stores/message/file_system.d.cts
stores/message/in_memory.cjs
stores/message/in_memory.js
stores/message/in_memory.d.ts
Expand Down Expand Up @@ -818,10 +822,6 @@ memory/chat_memory.cjs
memory/chat_memory.js
memory/chat_memory.d.ts
memory/chat_memory.d.cts
memory/file_history.cjs
memory/file_history.js
memory/file_history.d.ts
memory/file_history.d.cts
memory/motorhead_memory.cjs
memory/motorhead_memory.js
memory/motorhead_memory.d.ts
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-community/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export const config = {
"stores/message/convex": "stores/message/convex",
"stores/message/dynamodb": "stores/message/dynamodb",
"stores/message/firestore": "stores/message/firestore",
"stores/message/file_system": "stores/message/file_system",
"stores/message/in_memory": "stores/message/in_memory",
"stores/message/ipfs_datastore": "stores/message/ipfs_datastore",
"stores/message/ioredis": "stores/message/ioredis",
Expand All @@ -255,7 +256,6 @@ export const config = {
"stores/message/zep_cloud": "stores/message/zep_cloud",
// memory
"memory/chat_memory": "memory/chat_memory",
"memory/file_history": "memory/file_chat_history",
"memory/motorhead_memory": "memory/motorhead_memory",
"memory/zep": "memory/zep",
"memory/zep_cloud": "memory/zep_cloud",
Expand Down
26 changes: 13 additions & 13 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,15 @@
"import": "./stores/message/firestore.js",
"require": "./stores/message/firestore.cjs"
},
"./stores/message/file_system": {
"types": {
"import": "./stores/message/file_system.d.ts",
"require": "./stores/message/file_system.d.cts",
"default": "./stores/message/file_system.d.ts"
},
"import": "./stores/message/file_system.js",
"require": "./stores/message/file_system.cjs"
},
"./stores/message/in_memory": {
"types": {
"import": "./stores/message/in_memory.d.ts",
Expand Down Expand Up @@ -2557,15 +2566,6 @@
"import": "./memory/chat_memory.js",
"require": "./memory/chat_memory.cjs"
},
"./memory/file_history": {
"types": {
"import": "./memory/file_history.d.ts",
"require": "./memory/file_history.d.cts",
"default": "./memory/file_history.d.ts"
},
"import": "./memory/file_history.js",
"require": "./memory/file_history.cjs"
},
"./memory/motorhead_memory": {
"types": {
"import": "./memory/motorhead_memory.d.ts",
Expand Down Expand Up @@ -3882,6 +3882,10 @@
"stores/message/firestore.js",
"stores/message/firestore.d.ts",
"stores/message/firestore.d.cts",
"stores/message/file_system.cjs",
"stores/message/file_system.js",
"stores/message/file_system.d.ts",
"stores/message/file_system.d.cts",
"stores/message/in_memory.cjs",
"stores/message/in_memory.js",
"stores/message/in_memory.d.ts",
Expand Down Expand Up @@ -3930,10 +3934,6 @@
"memory/chat_memory.js",
"memory/chat_memory.d.ts",
"memory/chat_memory.d.cts",
"memory/file_history.cjs",
"memory/file_history.js",
"memory/file_history.d.ts",
"memory/file_history.d.cts",
"memory/motorhead_memory.cjs",
"memory/motorhead_memory.js",
"memory/motorhead_memory.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-community/src/load/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export * as caches__upstash_redis from "../caches/upstash_redis.js";
export * as stores__doc__base from "../stores/doc/base.js";
export * as stores__doc__gcs from "../stores/doc/gcs.js";
export * as stores__doc__in_memory from "../stores/doc/in_memory.js";
export * as stores__message__file_system from "../stores/message/file_system.js";
export * as stores__message__in_memory from "../stores/message/in_memory.js";
export * as memory__chat_memory from "../memory/chat_memory.js";
export * as memory__file_history from "../memory/file_chat_history.js";
export * as indexes__base from "../indexes/base.js";
export * as indexes__memory from "../indexes/memory.js";
export * as document_loaders__web__airtable from "../document_loaders/web/airtable.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises as fs } from "node:fs";
import { dirname } from "node:path";

import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
BaseMessage,
Expand Down Expand Up @@ -33,9 +34,9 @@ export type FileChatStore = {
};

/**
* Type for the input to the `FileChatMessageHistory` constructor.
* Type for the input to the `FileSystemChatMessageHistory` constructor.
*/
export interface FileChatMessageHistoryInput {
export interface FileSystemChatMessageHistoryInput {
sessionId: string;
userId?: string;
filePath?: string;
Expand All @@ -45,6 +46,7 @@ let store: FileChatStore;

/**
* Store chat message history using a local JSON file.
* For demo and development purposes only.
*
* @example
* ```typescript
Expand All @@ -57,7 +59,7 @@ let store: FileChatStore;
* "system",
* "You are a helpful assistant. Answer all questions to the best of your ability.",
* ],
* new MessagesPlaceholder("chat_history"),
* ["placeholder", "chat_history"],
* ["human", "{input}"],
* ]);
*
Expand All @@ -67,7 +69,7 @@ let store: FileChatStore;
* inputMessagesKey: "input",
* historyMessagesKey: "chat_history",
* getMessageHistory: async (sessionId) => {
* const chatHistory = new FileChatMessageHistory({
* const chatHistory = new FileSystemChatMessageHistory({
* sessionId: sessionId,
* userId: "userId", // Optional
* })
Expand All @@ -80,7 +82,7 @@ let store: FileChatStore;
* );
* ```
*/
export class FileChatMessageHistory extends BaseListChatMessageHistory {
export class FileSystemChatMessageHistory extends BaseListChatMessageHistory {
lc_namespace = ["langchain", "stores", "message", "file"];

private sessionId: string;
Expand All @@ -89,12 +91,12 @@ export class FileChatMessageHistory extends BaseListChatMessageHistory {

private filePath: string;

constructor(chatHistoryInput: FileChatMessageHistoryInput) {
constructor(chatHistoryInput: FileSystemChatMessageHistoryInput) {
super();

this.sessionId = chatHistoryInput.sessionId;
this.userId = chatHistoryInput.userId ?? "";
this.filePath = chatHistoryInput.filePath || FILE_HISTORY_DEFAULT_FILE_PATH;
this.filePath = chatHistoryInput.filePath ?? FILE_HISTORY_DEFAULT_FILE_PATH;
}

private async init(): Promise<void> {
Expand All @@ -104,7 +106,7 @@ export class FileChatMessageHistory extends BaseListChatMessageHistory {
try {
store = await this.loadStore();
} catch (error) {
console.error("Error initializing FileChatMessageHistory:", error);
console.error("Error initializing FileSystemChatMessageHistory:", error);
throw error;
}
}
Expand All @@ -119,7 +121,9 @@ export class FileChatMessageHistory extends BaseListChatMessageHistory {
if (error.code === "ENOENT") {
return {};
}
throw new Error(`Error loading FileChatMessageHistory store: ${error}`);
throw new Error(
`Error loading FileSystemChatMessageHistory store: ${error}`
);
}
}

Expand All @@ -128,7 +132,9 @@ export class FileChatMessageHistory extends BaseListChatMessageHistory {
await fs.mkdir(dirname(this.filePath), { recursive: true });
await fs.writeFile(this.filePath, JSON.stringify(store));
} catch (error) {
throw new Error(`Error saving FileChatMessageHistory store: ${error}`);
throw new Error(
`Error saving FileSystemChatMessageHistory store: ${error}`
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { HumanMessage, AIMessage } from "@langchain/core/messages";
import { v4 as uuid } from "uuid";
import {
FILE_HISTORY_DEFAULT_FILE_PATH,
FileChatMessageHistory,
} from "../file_chat_history.js";
FileSystemChatMessageHistory,
} from "../message/file_system.js";

afterAll(async () => {
try {
Expand All @@ -17,11 +17,11 @@ afterAll(async () => {
}
});

test("FileChatMessageHistory works", async () => {
test("FileSystemChatMessageHistory works", async () => {
const input = {
sessionId: uuid(),
};
const chatHistory = new FileChatMessageHistory(input);
const chatHistory = new FileSystemChatMessageHistory(input);
const blankResult = await chatHistory.getMessages();
expect(blankResult).toStrictEqual([]);

Expand All @@ -36,18 +36,18 @@ test("FileChatMessageHistory works", async () => {
expect(resultWithHistory).toEqual(expectedMessages);
});

test("FileChatMessageHistory persist sessions", async () => {
test("FileSystemChatMessageHistory persist sessions", async () => {
const input = {
sessionId: uuid(),
};
const chatHistory1 = new FileChatMessageHistory(input);
const chatHistory1 = new FileSystemChatMessageHistory(input);
const blankResult = await chatHistory1.getMessages();
expect(blankResult).toStrictEqual([]);

await chatHistory1.addUserMessage("Who is the best vocalist?");
await chatHistory1.addAIMessage("Ozzy Osbourne");

const chatHistory2 = new FileChatMessageHistory(input);
const chatHistory2 = new FileSystemChatMessageHistory(input);
const expectedMessages = [
new HumanMessage("Who is the best vocalist?"),
new AIMessage("Ozzy Osbourne"),
Expand All @@ -56,12 +56,12 @@ test("FileChatMessageHistory persist sessions", async () => {
expect(resultWithHistory).toEqual(expectedMessages);
});

test("FileChatMessageHistory clear session", async () => {
test("FileSystemChatMessageHistory clear session", async () => {
const input = {
sessionId: uuid(),
userId: uuid(),
};
const chatHistory = new FileChatMessageHistory(input);
const chatHistory = new FileSystemChatMessageHistory(input);

await chatHistory.addUserMessage("Who is the best vocalist?");
await chatHistory.addAIMessage("Ozzy Osbourne");
Expand All @@ -79,12 +79,12 @@ test("FileChatMessageHistory clear session", async () => {
expect(blankResult).toStrictEqual([]);
});

test("FileChatMessageHistory clear all sessions", async () => {
test("FileSystemChatMessageHistory clear all sessions", async () => {
const input1 = {
sessionId: uuid(),
userId: "user1",
};
const chatHistory1 = new FileChatMessageHistory(input1);
const chatHistory1 = new FileSystemChatMessageHistory(input1);

await chatHistory1.addUserMessage("Who is the best vocalist?");
await chatHistory1.addAIMessage("Ozzy Osbourne");
Expand All @@ -93,7 +93,7 @@ test("FileChatMessageHistory clear all sessions", async () => {
sessionId: uuid(),
userId: "user1",
};
const chatHistory2 = new FileChatMessageHistory(input2);
const chatHistory2 = new FileSystemChatMessageHistory(input2);

await chatHistory2.addUserMessage("Who is the best vocalist?");
await chatHistory2.addAIMessage("Ozzy Osbourne");
Expand All @@ -117,19 +117,19 @@ test("FileChatMessageHistory clear all sessions", async () => {
expect(deletedResult2).toStrictEqual([]);
});

test("FileChatMessageHistory set context and get all sessions", async () => {
test("FileSystemChatMessageHistory set context and get all sessions", async () => {
const session1 = {
sessionId: uuid(),
userId: "user1",
};
const context1 = { title: "Best vocalist" };
const chatHistory1 = new FileChatMessageHistory(session1);
const chatHistory1 = new FileSystemChatMessageHistory(session1);

await chatHistory1.setContext(context1);
await chatHistory1.addUserMessage("Who is the best vocalist?");
await chatHistory1.addAIMessage("Ozzy Osbourne");

const chatHistory2 = new FileChatMessageHistory({
const chatHistory2 = new FileSystemChatMessageHistory({
sessionId: uuid(),
userId: "user1",
});
Expand Down
5 changes: 0 additions & 5 deletions libs/langchain-scripts/src/_data/importMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3355,11 +3355,6 @@ export const importMap: Array<DeprecatedEntrypoint> = [
new: "@langchain/community/retrievers/remote",
namedImport: "RemoteRetrieverValues",
},
{
old: "langchain/memory/file_history",
new: "@langchain/community/memory/file_history",
namedImport: "FileChatMessageHistory",
},
{
old: "langchain/memory/zep",
new: "@langchain/community/memory/zep",
Expand Down

0 comments on commit 20676f7

Please sign in to comment.