From c96c5eca458214909046d854c30fcf00ec6f11a5 Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 12:58:30 -0700 Subject: [PATCH 1/7] Add ID --- langchain-core/index.cjs | 1 + langchain-core/index.d.ts | 1 + langchain-core/index.js | 1 + langchain-core/src/messages/base.ts | 5 +++++ 4 files changed, 8 insertions(+) create mode 100644 langchain-core/index.cjs create mode 100644 langchain-core/index.d.ts create mode 100644 langchain-core/index.js diff --git a/langchain-core/index.cjs b/langchain-core/index.cjs new file mode 100644 index 000000000000..c84256f7a3b5 --- /dev/null +++ b/langchain-core/index.cjs @@ -0,0 +1 @@ +module.exports = require('./dist/index.cjs'); \ No newline at end of file diff --git a/langchain-core/index.d.ts b/langchain-core/index.d.ts new file mode 100644 index 000000000000..485d17256319 --- /dev/null +++ b/langchain-core/index.d.ts @@ -0,0 +1 @@ +export * from './dist/index.js' \ No newline at end of file diff --git a/langchain-core/index.js b/langchain-core/index.js new file mode 100644 index 000000000000..485d17256319 --- /dev/null +++ b/langchain-core/index.js @@ -0,0 +1 @@ +export * from './dist/index.js' \ No newline at end of file diff --git a/langchain-core/src/messages/base.ts b/langchain-core/src/messages/base.ts index 1135c75b84f0..013eb622b351 100644 --- a/langchain-core/src/messages/base.ts +++ b/langchain-core/src/messages/base.ts @@ -98,6 +98,11 @@ export interface ToolCall { export type BaseMessageFields = { content: MessageContent; name?: string; + /** + * An optional unique identifier for the message. This should ideally be + * provided by the provider/model which created the message. + */ + id?: string; additional_kwargs?: { function_call?: FunctionCall; tool_calls?: ToolCall[]; From 55f77861ce027dff5ed0b4dac7849f2b7c638c16 Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 13:03:18 -0700 Subject: [PATCH 2/7] Delete langchain-core/index.js --- langchain-core/index.js | 1 - 1 file changed, 1 deletion(-) delete mode 100644 langchain-core/index.js diff --git a/langchain-core/index.js b/langchain-core/index.js deleted file mode 100644 index 485d17256319..000000000000 --- a/langchain-core/index.js +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/index.js' \ No newline at end of file From 48a87a927196526081ddecf4d9a6afd0c3519f0c Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 13:03:27 -0700 Subject: [PATCH 3/7] Delete langchain-core/index.d.ts --- langchain-core/index.d.ts | 1 - 1 file changed, 1 deletion(-) delete mode 100644 langchain-core/index.d.ts diff --git a/langchain-core/index.d.ts b/langchain-core/index.d.ts deleted file mode 100644 index 485d17256319..000000000000 --- a/langchain-core/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/index.js' \ No newline at end of file From 4ddf389d1e563675be046e08f2a04639c1be448d Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 13:03:35 -0700 Subject: [PATCH 4/7] Delete langchain-core/index.cjs --- langchain-core/index.cjs | 1 - 1 file changed, 1 deletion(-) delete mode 100644 langchain-core/index.cjs diff --git a/langchain-core/index.cjs b/langchain-core/index.cjs deleted file mode 100644 index c84256f7a3b5..000000000000 --- a/langchain-core/index.cjs +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dist/index.cjs'); \ No newline at end of file From 254f27fab16c39a86a0d0624277cab7cc6f074d7 Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 13:20:16 -0700 Subject: [PATCH 5/7] add ID --- langchain-core/src/messages/base.ts | 9 ++++++ .../src/messages/tests/base_message.test.ts | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/langchain-core/src/messages/base.ts b/langchain-core/src/messages/base.ts index 013eb622b351..6642cb9619e4 100644 --- a/langchain-core/src/messages/base.ts +++ b/langchain-core/src/messages/base.ts @@ -5,6 +5,8 @@ export interface StoredMessageData { content: string; role: string | undefined; name: string | undefined; + /** An optional unique identifier for the message. */ + id: string | undefined; tool_call_id: string | undefined; // eslint-disable-next-line @typescript-eslint/no-explicit-any additional_kwargs?: Record; @@ -169,6 +171,12 @@ export abstract class BaseMessage /** The name of the message sender in a multi-user chat. */ name?: string; + /** + * An optional unique identifier for the message. This should ideally be + * provided by the provider/model which created the message. + */ + id?: string; + /** Additional keyword arguments */ additional_kwargs: NonNullable; @@ -201,6 +209,7 @@ export abstract class BaseMessage fields.response_metadata = {}; } super(fields); + this.id = fields.id; this.name = fields.name; this.content = fields.content; this.additional_kwargs = fields.additional_kwargs; diff --git a/langchain-core/src/messages/tests/base_message.test.ts b/langchain-core/src/messages/tests/base_message.test.ts index 504777c2681b..ddf3415a9df3 100644 --- a/langchain-core/src/messages/tests/base_message.test.ts +++ b/langchain-core/src/messages/tests/base_message.test.ts @@ -107,3 +107,31 @@ 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 id", async () => { + const config = { + importMap: { messages: { AIMessage } }, + optionalImportEntrypoints: [], + optionalImportsMap: {}, + secretsMap: {}, + }; + + const message = new AIMessage({ + content: "I am a messages", + id: "my-cool-id", + }); + + const deserialized: AIMessage = await load(JSON.stringify(message), config); + expect(deserialized).toEqual(message); + + // Ensure it works if you mutate the id after creation + const newMessage = new AIMessage({ + content: "I am a messages", + }); + newMessage.id = "my-other-cool-id"; + const deserializedNew: AIMessage = await load( + JSON.stringify(newMessage), + config + ); + expect(deserializedNew).toEqual(newMessage); +}); From 4c4ac643232db9ced193fd5b19ddb06fc38bb5dd Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 13:57:31 -0700 Subject: [PATCH 6/7] message format --- langchain-core/src/messages/base.ts | 4 ++++ langchain-core/src/messages/tests/base_message.test.ts | 6 +++--- langchain-core/src/prompts/tests/chat.test.ts | 9 --------- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/langchain-core/src/messages/base.ts b/langchain-core/src/messages/base.ts index 6642cb9619e4..f4ca04b21ecb 100644 --- a/langchain-core/src/messages/base.ts +++ b/langchain-core/src/messages/base.ts @@ -208,6 +208,10 @@ export abstract class BaseMessage // eslint-disable-next-line no-param-reassign fields.response_metadata = {}; } + if (!fields.id) { + // eslint-disable-next-line no-param-reassign + fields.id = undefined; + } super(fields); this.id = fields.id; this.name = fields.name; diff --git a/langchain-core/src/messages/tests/base_message.test.ts b/langchain-core/src/messages/tests/base_message.test.ts index ddf3415a9df3..aba242f747ac 100644 --- a/langchain-core/src/messages/tests/base_message.test.ts +++ b/langchain-core/src/messages/tests/base_message.test.ts @@ -117,7 +117,7 @@ test("Deserialisation and serialisation of id", async () => { }; const message = new AIMessage({ - content: "I am a messages", + content: "I am a message 1", id: "my-cool-id", }); @@ -126,12 +126,12 @@ test("Deserialisation and serialisation of id", async () => { // Ensure it works if you mutate the id after creation const newMessage = new AIMessage({ - content: "I am a messages", + content: "I am a message 2", }); newMessage.id = "my-other-cool-id"; const deserializedNew: AIMessage = await load( JSON.stringify(newMessage), config ); - expect(deserializedNew).toEqual(newMessage); + expect(deserializedNew.id).toEqual(newMessage.id); }); diff --git a/langchain-core/src/prompts/tests/chat.test.ts b/langchain-core/src/prompts/tests/chat.test.ts index 975e503f4d17..bf020c8caca4 100644 --- a/langchain-core/src/prompts/tests/chat.test.ts +++ b/langchain-core/src/prompts/tests/chat.test.ts @@ -33,15 +33,6 @@ function createChatPromptTemplate() { template: "I'm a generic message. I'm {foo}. I'm {bar}.", inputVariables: ["foo", "bar"], }); - // return new ChatPromptTemplate({ - // promptMessages: [ - // new SystemMessagePromptTemplate(systemPrompt), - // new HumanMessagePromptTemplate(userPrompt), - // new AIMessagePromptTemplate({ prompt: aiPrompt }), - // new ChatMessagePromptTemplate(genericPrompt, "test"), - // ], - // inputVariables: ["context", "foo", "bar"], - // }); return ChatPromptTemplate.fromMessages<{ foo: string; bar: string; From 0f3f80d2188ddd37a1b72d1d53ef507398931006 Mon Sep 17 00:00:00 2001 From: William Fu-Hinthorn <13333726+hinthornw@users.noreply.github.com> Date: Fri, 17 May 2024 14:06:51 -0700 Subject: [PATCH 7/7] type --- langchain-core/src/messages/utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/langchain-core/src/messages/utils.ts b/langchain-core/src/messages/utils.ts index 64f6489ed60f..c8c03ca999ec 100644 --- a/langchain-core/src/messages/utils.ts +++ b/langchain-core/src/messages/utils.ts @@ -97,6 +97,7 @@ function mapV1MessageToStoredMessage( role: v1Message.role, name: undefined, tool_call_id: undefined, + id: undefined, }, }; }