Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[minor],community[minor]: Add message id tests #5827

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface StoredMessageData {
/** Response metadata. For example: response headers, logprobs, token counts. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response_metadata?: Record<string, any>;
id?: string;
}

export interface StoredMessage {
Expand Down
20 changes: 20 additions & 0 deletions langchain-core/src/messages/tests/base_message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,23 @@ test("Deserialisation and serialisation of tool_call_id", async () => {
const deserialized: ToolMessage = await load(JSON.stringify(message), config);
expect(deserialized).toEqual(message);
});

test("Deserialisation and serialisation of messages with ID", async () => {
const config = {
importMap: { messages: { AIMessage } },
optionalImportEntrypoints: [],
optionalImportsMap: {},
secretsMap: {},
};

const messageId = "uuid-1234";

const message = new AIMessage({
content: "The sky is blue because...",
id: messageId,
});

const deserialized: AIMessage = await load(JSON.stringify(message), config);
expect(deserialized).toEqual(message);
expect(deserialized.id).toBe(messageId);
});
21 changes: 20 additions & 1 deletion libs/langchain-community/src/stores/tests/postgres.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe.skip("Postgres Chat History", () => {
beforeAll(async () => {
pool = new pg.Pool({
host: "127.0.0.1",
port: 5433,
port: 5432,
user: "myuser",
password: "ChangeMe",
database: "api",
Expand Down Expand Up @@ -125,4 +125,23 @@ describe.skip("Postgres Chat History", () => {
await newChatHistory.clear();
}
});

test("Can store & retrieve message IDs", async () => {
const blankResult = await chatHistory.getMessages();
expect(blankResult).toStrictEqual([]);

const aiMessageId = "ai-message-id";
const aiMessage = new AIMessage({
content: "Ozzy Osbourne",
id: aiMessageId,
});
await chatHistory.addMessage(aiMessage);

const expectedMessages = [aiMessage];

const resultWithHistory = await chatHistory.getMessages();
expect(resultWithHistory).toHaveLength(1);
expect(resultWithHistory).toEqual(expectedMessages);
expect(resultWithHistory[0].id).toEqual(aiMessageId);
});
});
Loading