From f7594bcb5ff59e1f4394e52c5ba6aac8e37a192b Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:05:23 -0700 Subject: [PATCH 1/9] Add roletype --- examples/memory/history.ts | 36 +++++++++++++++++++++++++++++-- examples/memory/memory_example.ts | 4 ++-- src/message_models.ts | 8 +++++++ yarn.lock | 30 +++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 5 deletions(-) diff --git a/examples/memory/history.ts b/examples/memory/history.ts index fb6c9bd..3e7850e 100644 --- a/examples/memory/history.ts +++ b/examples/memory/history.ts @@ -1,54 +1,72 @@ +import { RoleType } from "../../src/message_models"; + export const history = [ { role: "human", + role_type: "UserRole" as RoleType, content: "Who was Octavia Butler?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" + " science fiction author.", }, { role: "human", + role_type: "UserRole" as RoleType, content: "Which books of hers were made into movies?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "The most well-known adaptation of Octavia Butler's work is the FX series" + " Kindred, based on her novel of the same name.", }, - { role: "human", content: "Who were her contemporaries?" }, + { + role: "human", + role_type: "UserRole" as RoleType, + content: "Who were her contemporaries?" }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R." + " Delany, and Joanna Russ.", }, - { role: "human", content: "What awards did she win?" }, + { + role: "human", + role_type: "UserRole" as RoleType, + content: "What awards did she win?" }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur" + " Fellowship.", }, { role: "human", + role_type: "UserRole" as RoleType, content: "Which other women sci-fi writers might I want to read?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "You might want to read Ursula K. Le Guin or Joanna Russ.", }, { role: "human", + role_type: "UserRole" as RoleType, content: "Write a short synopsis of Butler's book, Parable of the Sower. What is it" + " about?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Parable of the Sower is a science fiction novel by Octavia Butler," + " published in 1993. It follows the story of Lauren Olamina, a young woman" + @@ -57,10 +75,12 @@ export const history = [ }, { role: "human", + role_type: "UserRole" as RoleType, content: "What inspired Octavia Butler's writing style?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Octavia Butler was inspired by a wide range of influences, including her" + " passion for science fiction, her interest in sociology, and her desire to" + @@ -68,20 +88,24 @@ export const history = [ }, { role: "human", + role_type: "UserRole" as RoleType, content: "Did Octavia Butler write only science fiction?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "While Octavia Butler is best known for her contributions to science fiction," + " she also wrote in other genres, including fantasy and speculative fiction.", }, { role: "human", + role_type: "UserRole" as RoleType, content: "Tell me more about the Xenogenesis series.", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "The Xenogenesis series, also known as the Lilith's Brood trilogy, consists" + " of three novels: Dawn (1987), Adulthood Rites (1988), and Imago (1989)." + @@ -90,10 +114,12 @@ export const history = [ }, { role: "human", + role_type: "UserRole" as RoleType, content: "Are there any upcoming adaptations of Octavia Butler's work?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "As of my last update, there were discussions about potential adaptations," + " but no confirmed upcoming projects. It's advisable to check the latest" + @@ -101,10 +127,12 @@ export const history = [ }, { role: "human", + role_type: "UserRole" as RoleType, content: "What is the Earthseed series about?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "The Earthseed series consists of Parable of the Sower (1993) and Parable of" + " the Talents (1998). It follows the protagonist, Lauren Olamina, as she" + @@ -113,10 +141,12 @@ export const history = [ }, { role: "human", + role_type: "UserRole" as RoleType, content: "How did Octavia Butler impact the science fiction genre?", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Octavia Butler made significant contributions to the science fiction genre" + " by challenging conventions and addressing social issues. Her works" + @@ -125,10 +155,12 @@ export const history = [ }, { role: "human", + role_type: "UserRole" as RoleType, content: "Tell me about Octavia Butler's writing process.", }, { role: "ai", + role_type: "AssistantRole" as RoleType, content: "Octavia Butler was known for her disciplined and dedicated writing" + " process. She often wrote in the early morning hours and set specific" + diff --git a/examples/memory/memory_example.ts b/examples/memory/memory_example.ts index c82f85b..847bde2 100644 --- a/examples/memory/memory_example.ts +++ b/examples/memory/memory_example.ts @@ -68,8 +68,8 @@ async function main() { // Add memory. We could do this in a batch, but we'll do it one by one rather to // ensure that summaries and other artifacts are generated correctly. try { - for (const { role, content } of history) { - const message = new Message({ role, content }); + for (const { role, role_type, content } of history) { + const message = new Message({ role, role_type, content }); const memory = new Memory({ messages: [message] }); await client.memory.addMemory(sessionID, memory); diff --git a/src/message_models.ts b/src/message_models.ts index 339e981..669c5ce 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -1,3 +1,6 @@ +export type RoleType = "UserRole" | "AssistantRole" | "SystemRole" | "FunctionRole"; + + /** * IMessage interface for providing input to create a Message instance. */ @@ -5,6 +8,7 @@ export interface IMessage { uuid?: string; created_at?: string; role: string; + role_type: RoleType; content: string; token_count?: number; metadata?: Record; @@ -20,6 +24,8 @@ export class Message { role: string; + role_type: RoleType; + content: string; token_count?: number; @@ -34,6 +40,7 @@ export class Message { this.uuid = data.uuid; this.created_at = data.created_at; this.role = data.role; + this.role_type = data.role_type; this.content = data.content; this.token_count = data.token_count; this.metadata = data.metadata; @@ -48,6 +55,7 @@ export class Message { uuid: this.uuid, created_at: this.created_at, role: this.role, + role_type: this.role_type, content: this.content, token_count: this.token_count, metadata: this.metadata, diff --git a/yarn.lock b/yarn.lock index 718a95c..9876647 100644 --- a/yarn.lock +++ b/yarn.lock @@ -647,7 +647,24 @@ uuid "^9.0.0" zod "^3.22.3" -"@langchain/core@^0.1.23", "@langchain/core@~0.1.13", "@langchain/core@~0.1.29": +"@langchain/core@^0.1.19", "@langchain/core@~0.1.13", "@langchain/core@~0.1.29": + version "0.1.36" + resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.36.tgz#795722a52e2d99efe5000cc5f57084b5b0ea5938" + integrity sha512-uZP1Y5gGysSHKT7D5IT+fNJCuWU76msiYuQuQvMQJZyu+MAHAJQrBuokdwxjyMSE1cumGhn4tBnpcFoq5JAm1w== + dependencies: + ansi-styles "^5.0.0" + camelcase "6" + decamelize "1.2.0" + js-tiktoken "^1.0.8" + langsmith "~0.1.7" + ml-distance "^4.0.0" + p-queue "^6.6.2" + p-retry "4" + uuid "^9.0.0" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + +"@langchain/core@^0.1.23": version "0.1.29" resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.29.tgz#4337a9db35ee0281af3d50d6fbb9c84c5dd6be77" integrity sha512-MVrrb2J1Y08Yy6JnRDy/C/yxZj2PvO6wNkMs8657Q345SHs8iCbRX7Dy7rC69isUrpMjR7PvADFxV9rGvdoMBg== @@ -3137,6 +3154,17 @@ langsmith@~0.1.1: p-retry "4" uuid "^9.0.0" +langsmith@~0.1.7: + version "0.1.7" + resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.7.tgz#6ae6006e051b149e713dc7d1c6acb6d422ad4612" + integrity sha512-yPhpsppcqV1LBDO3Eh3PdiAS5/1Z5pAvgECXQbx77aJjEYqZ4+sm4WBuSHSAdj8on0SG6xXkA+lDsQU8nM+k/w== + dependencies: + "@types/uuid" "^9.0.1" + commander "^10.0.1" + p-queue "^6.6.2" + p-retry "4" + uuid "^9.0.0" + leven@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/leven/-/leven-3.1.0.tgz#77891de834064cccba82ae7842bb6b14a13ed7f2" From d0f6897d9a2c3902b8701b544f598bbcdc598db8 Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Wed, 28 Feb 2024 17:10:50 -0700 Subject: [PATCH 2/9] linter --- src/message_models.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/message_models.ts b/src/message_models.ts index 669c5ce..72dcd78 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -1,5 +1,8 @@ -export type RoleType = "UserRole" | "AssistantRole" | "SystemRole" | "FunctionRole"; - +export type RoleType = + | "UserRole" + | "AssistantRole" + | "SystemRole" + | "FunctionRole"; /** * IMessage interface for providing input to create a Message instance. From 2ceda3987b39904ba5c118ba7918750b946eaa6c Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:00:23 -0700 Subject: [PATCH 3/9] Roletype --- src/langchain/memory.ts | 11 ++++++++--- src/tests/memory_test.ts | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/langchain/memory.ts b/src/langchain/memory.ts index 47e4a44..95c20d3 100644 --- a/src/langchain/memory.ts +++ b/src/langchain/memory.ts @@ -144,11 +144,14 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { if (memory) { messages = messages.concat( memory.messages.map((message) => { - const { content, role } = message; - if (role === this.humanPrefix) { + const { content, role_type, role } = message; + if (role_type === "UserRole") { return new HumanMessage(content); } - if (role === this.aiPrefix) { + if ( + role_type === "AssistantRole" || + role_type === "SystemRole" + ) { return new AIMessage(content); } // default to generic ChatMessage @@ -189,10 +192,12 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { messages: [ new Message({ role: this.humanPrefix, + role_type: "UserRole", content: `${input}`, }), new Message({ role: this.aiPrefix, + role_type: "AssistantRole", content: `${output}`, }), ], diff --git a/src/tests/memory_test.ts b/src/tests/memory_test.ts index fb0281b..a922d90 100644 --- a/src/tests/memory_test.ts +++ b/src/tests/memory_test.ts @@ -9,6 +9,7 @@ import { ZepClient, } from "../"; import { FetchMock } from "jest-fetch-mock"; +import { RoleType } from "../message_models"; const BASE_URL = "http://localhost:8000"; @@ -212,6 +213,7 @@ describe("ZepClient", () => { it("should retrieve a message for a session", async () => { const responseData = { role: "human", + role_type: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -259,6 +261,7 @@ describe("ZepClient", () => { messages: [ { role: "human", + role_type: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -314,6 +317,7 @@ describe("ZepClient", () => { it("should update metadata for a message in a session", async () => { const responseData = { role: "human", + role_type: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -360,7 +364,7 @@ describe("ZepClient", () => { // Test for retrieving memory for a session it("should retrieve memory for a session", async () => { const responseData = { - messages: [{ role: "human", content: "Hello" }], + messages: [{ role: "human", role_type: "UserRole" as RoleType, content: "Hello" }], summary: { uuid: "", created_at: "", @@ -376,7 +380,7 @@ describe("ZepClient", () => { expect(memory).toEqual( new Memory({ - messages: [new Message({ role: "human", content: "Hello" })], + messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello" })], summary: new Summary({ content: "Memory summary", created_at: "", @@ -444,8 +448,8 @@ describe("ZepClient", () => { it("should retrieve last 'n' memories for a session when 'lastn' parameter is used", async () => { const responseData = { messages: [ - { role: "system", content: "How can I assist you?" }, - { role: "human", content: "What's the weather like?" }, + { role: "system", role_type: "SystemRole" as RoleType, content: "How can I assist you?" }, + { role: "human", role_type: "UserRole" as RoleType, content: "What's the weather like?" }, ], summary: { uuid: "", @@ -476,10 +480,12 @@ describe("ZepClient", () => { messages: [ new Message({ role: "system", + role_type: "SystemRole" as RoleType, content: "How can I assist you?", }), new Message({ role: "human", + role_type: "UserRole" as RoleType, content: "What's the weather like?", }), ], @@ -499,7 +505,7 @@ describe("ZepClient", () => { describe("addMemory", () => { it("should add a memory to a session", async () => { const memoryData = new Memory({ - messages: [new Message({ role: "human", content: "Hello again!" })], + messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello again!" })], summary: new Summary({ uuid: "", created_at: "", @@ -524,7 +530,7 @@ describe("ZepClient", () => { it("should throw APIError if !200 OK", async () => { const memoryData = new Memory({ messages: [ - new Message({ role: "system", content: "System message" }), + new Message({ role: "system", role_type: "SystemRole" as RoleType, content: "System message" }), ], summary: new Summary({ uuid: "summary_uuid", @@ -594,6 +600,7 @@ describe("ZepClient", () => { { message: { role: "system", + role_type: "SystemRole", content: "system message", uuid: "message_uuid", created_at: "2023-01-01T00:00:00Z", From 4f7740d033ee15758b3341e0456a0ce5be87b988 Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:13:13 -0700 Subject: [PATCH 4/9] role_type -> roleType --- examples/memory/history.ts | 52 +++++++++++++++---------------- examples/memory/memory_example.ts | 4 +-- src/langchain/memory.ts | 13 +++----- src/message_models.ts | 8 ++--- src/tests/memory_test.ts | 24 +++++++------- 5 files changed, 49 insertions(+), 52 deletions(-) diff --git a/examples/memory/history.ts b/examples/memory/history.ts index 3e7850e..ec81059 100644 --- a/examples/memory/history.ts +++ b/examples/memory/history.ts @@ -3,70 +3,70 @@ import { RoleType } from "../../src/message_models"; export const history = [ { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Who was Octavia Butler?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" + " science fiction author.", }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Which books of hers were made into movies?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "The most well-known adaptation of Octavia Butler's work is the FX series" + " Kindred, based on her novel of the same name.", }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Who were her contemporaries?" }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R." + " Delany, and Joanna Russ.", }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "What awards did she win?" }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur" + " Fellowship.", }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Which other women sci-fi writers might I want to read?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "You might want to read Ursula K. Le Guin or Joanna Russ.", }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Write a short synopsis of Butler's book, Parable of the Sower. What is it" + " about?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Parable of the Sower is a science fiction novel by Octavia Butler," + " published in 1993. It follows the story of Lauren Olamina, a young woman" + @@ -75,12 +75,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "What inspired Octavia Butler's writing style?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Octavia Butler was inspired by a wide range of influences, including her" + " passion for science fiction, her interest in sociology, and her desire to" + @@ -88,24 +88,24 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Did Octavia Butler write only science fiction?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "While Octavia Butler is best known for her contributions to science fiction," + " she also wrote in other genres, including fantasy and speculative fiction.", }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Tell me more about the Xenogenesis series.", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "The Xenogenesis series, also known as the Lilith's Brood trilogy, consists" + " of three novels: Dawn (1987), Adulthood Rites (1988), and Imago (1989)." + @@ -114,12 +114,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Are there any upcoming adaptations of Octavia Butler's work?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "As of my last update, there were discussions about potential adaptations," + " but no confirmed upcoming projects. It's advisable to check the latest" + @@ -127,12 +127,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "What is the Earthseed series about?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "The Earthseed series consists of Parable of the Sower (1993) and Parable of" + " the Talents (1998). It follows the protagonist, Lauren Olamina, as she" + @@ -141,12 +141,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "How did Octavia Butler impact the science fiction genre?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Octavia Butler made significant contributions to the science fiction genre" + " by challenging conventions and addressing social issues. Her works" + @@ -155,12 +155,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Tell me about Octavia Butler's writing process.", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + roleType: "AssistantRole" as RoleType, content: "Octavia Butler was known for her disciplined and dedicated writing" + " process. She often wrote in the early morning hours and set specific" + diff --git a/examples/memory/memory_example.ts b/examples/memory/memory_example.ts index 847bde2..a7f9262 100644 --- a/examples/memory/memory_example.ts +++ b/examples/memory/memory_example.ts @@ -68,8 +68,8 @@ async function main() { // Add memory. We could do this in a batch, but we'll do it one by one rather to // ensure that summaries and other artifacts are generated correctly. try { - for (const { role, role_type, content } of history) { - const message = new Message({ role, role_type, content }); + for (const { role, roleType, content } of history) { + const message = new Message({ role, roleType, content }); const memory = new Memory({ messages: [message] }); await client.memory.addMemory(sessionID, memory); diff --git a/src/langchain/memory.ts b/src/langchain/memory.ts index 95c20d3..ac6c8f4 100644 --- a/src/langchain/memory.ts +++ b/src/langchain/memory.ts @@ -144,14 +144,11 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { if (memory) { messages = messages.concat( memory.messages.map((message) => { - const { content, role_type, role } = message; - if (role_type === "UserRole") { + const { content, roleType, role } = message; + if (roleType === "UserRole") { return new HumanMessage(content); } - if ( - role_type === "AssistantRole" || - role_type === "SystemRole" - ) { + if (roleType === "AssistantRole" || roleType === "SystemRole") { return new AIMessage(content); } // default to generic ChatMessage @@ -192,12 +189,12 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { messages: [ new Message({ role: this.humanPrefix, - role_type: "UserRole", + roleType: "UserRole", content: `${input}`, }), new Message({ role: this.aiPrefix, - role_type: "AssistantRole", + roleType: "AssistantRole", content: `${output}`, }), ], diff --git a/src/message_models.ts b/src/message_models.ts index 72dcd78..4691ba0 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -11,7 +11,7 @@ export interface IMessage { uuid?: string; created_at?: string; role: string; - role_type: RoleType; + roleType: RoleType; content: string; token_count?: number; metadata?: Record; @@ -27,7 +27,7 @@ export class Message { role: string; - role_type: RoleType; + roleType: RoleType; content: string; @@ -43,7 +43,7 @@ export class Message { this.uuid = data.uuid; this.created_at = data.created_at; this.role = data.role; - this.role_type = data.role_type; + this.roleType = data.roleType; this.content = data.content; this.token_count = data.token_count; this.metadata = data.metadata; @@ -58,7 +58,7 @@ export class Message { uuid: this.uuid, created_at: this.created_at, role: this.role, - role_type: this.role_type, + roleType: this.roleType, content: this.content, token_count: this.token_count, metadata: this.metadata, diff --git a/src/tests/memory_test.ts b/src/tests/memory_test.ts index a922d90..a592b7a 100644 --- a/src/tests/memory_test.ts +++ b/src/tests/memory_test.ts @@ -213,7 +213,7 @@ describe("ZepClient", () => { it("should retrieve a message for a session", async () => { const responseData = { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -261,7 +261,7 @@ describe("ZepClient", () => { messages: [ { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -317,7 +317,7 @@ describe("ZepClient", () => { it("should update metadata for a message in a session", async () => { const responseData = { role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -364,7 +364,7 @@ describe("ZepClient", () => { // Test for retrieving memory for a session it("should retrieve memory for a session", async () => { const responseData = { - messages: [{ role: "human", role_type: "UserRole" as RoleType, content: "Hello" }], + messages: [{ role: "human", roleType: "UserRole" as RoleType, content: "Hello" }], summary: { uuid: "", created_at: "", @@ -380,7 +380,7 @@ describe("ZepClient", () => { expect(memory).toEqual( new Memory({ - messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello" })], + messages: [new Message({ role: "human", roleType: "UserRole" as RoleType, content: "Hello" })], summary: new Summary({ content: "Memory summary", created_at: "", @@ -448,8 +448,8 @@ describe("ZepClient", () => { it("should retrieve last 'n' memories for a session when 'lastn' parameter is used", async () => { const responseData = { messages: [ - { role: "system", role_type: "SystemRole" as RoleType, content: "How can I assist you?" }, - { role: "human", role_type: "UserRole" as RoleType, content: "What's the weather like?" }, + { role: "system", roleType: "SystemRole" as RoleType, content: "How can I assist you?" }, + { role: "human", roleType: "UserRole" as RoleType, content: "What's the weather like?" }, ], summary: { uuid: "", @@ -480,12 +480,12 @@ describe("ZepClient", () => { messages: [ new Message({ role: "system", - role_type: "SystemRole" as RoleType, + roleType: "SystemRole" as RoleType, content: "How can I assist you?", }), new Message({ role: "human", - role_type: "UserRole" as RoleType, + roleType: "UserRole" as RoleType, content: "What's the weather like?", }), ], @@ -505,7 +505,7 @@ describe("ZepClient", () => { describe("addMemory", () => { it("should add a memory to a session", async () => { const memoryData = new Memory({ - messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello again!" })], + messages: [new Message({ role: "human", roleType: "UserRole" as RoleType, content: "Hello again!" })], summary: new Summary({ uuid: "", created_at: "", @@ -530,7 +530,7 @@ describe("ZepClient", () => { it("should throw APIError if !200 OK", async () => { const memoryData = new Memory({ messages: [ - new Message({ role: "system", role_type: "SystemRole" as RoleType, content: "System message" }), + new Message({ role: "system", roleType: "SystemRole" as RoleType, content: "System message" }), ], summary: new Summary({ uuid: "summary_uuid", @@ -600,7 +600,7 @@ describe("ZepClient", () => { { message: { role: "system", - role_type: "SystemRole", + roleType: "SystemRole", content: "system message", uuid: "message_uuid", created_at: "2023-01-01T00:00:00Z", From 54bab862caf20c0c50dfcd7e7ef4c895f22b43aa Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:34:02 -0700 Subject: [PATCH 5/9] getzepmessageroletype --- src/langchain/message_history.ts | 5 +++-- src/message_models.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/langchain/message_history.ts b/src/langchain/message_history.ts index f3da3e3..31ca623 100644 --- a/src/langchain/message_history.ts +++ b/src/langchain/message_history.ts @@ -10,7 +10,7 @@ import { BaseChatMessageHistory } from "@langchain/core/chat_history"; import ZepClient from "../zep-client"; import { Memory } from "../memory_models"; import { NotFoundError } from "../errors"; -import { Message } from "../message_models"; +import { getZepMessageRoleType, Message, RoleType } from "../message_models"; import type { MemoryType } from "../interfaces"; /** @@ -87,7 +87,7 @@ export class ZepChatMessageHistory token_count: msg.token_count, metadata: msg.metadata, }; - if (msg.role === "ai") { + if (msg.roleType === ("AssistantRole" as RoleType)) { return new AIMessage(msg.content, metadata); } return new HumanMessage(msg.content, metadata); @@ -117,6 +117,7 @@ export class ZepChatMessageHistory content: message.content, // eslint-disable-next-line no-underscore-dangle role: message._getType(), + roleType: getZepMessageRoleType(message._getType()), metadata, }); diff --git a/src/message_models.ts b/src/message_models.ts index 4691ba0..6d6aaad 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -1,3 +1,5 @@ +import { MessageType } from "@langchain/core/messages"; + export type RoleType = | "UserRole" | "AssistantRole" @@ -65,3 +67,20 @@ export class Message { }; } } + +// export type MessageType = "human" | "ai" | "generic" | "system" | "function" | "tool"; + +export const getZepMessageRoleType = (role: MessageType): RoleType => { + switch (role) { + case "human": + return "UserRole"; + case "ai": + return "AssistantRole"; + case "system": + return "SystemRole"; + case "function": + return "FunctionRole"; + default: + return "SystemRole"; + } +}; From 525103cfa4dfcbebb08aa58057f91814d09dad09 Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:05:51 -0700 Subject: [PATCH 6/9] Fixing cases --- .eslintrc.json | 1 + examples/memory/history.ts | 52 +++++++++++++++---------------- examples/memory/memory_example.ts | 4 +-- src/langchain/memory.ts | 10 +++--- src/langchain/message_history.ts | 5 +-- src/message_models.ts | 10 +++--- src/tests/memory_test.ts | 24 +++++++------- 7 files changed, 53 insertions(+), 53 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index f88a46f..e3d72d9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -40,6 +40,7 @@ } ], "max-classes-per-file": 0, + "camelcase": "warn", "multiline-ternary": 0, "no-unused-vars": "off", "no-underscore-dangle": "off", diff --git a/examples/memory/history.ts b/examples/memory/history.ts index ec81059..3e7850e 100644 --- a/examples/memory/history.ts +++ b/examples/memory/history.ts @@ -3,70 +3,70 @@ import { RoleType } from "../../src/message_models"; export const history = [ { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Who was Octavia Butler?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" + " science fiction author.", }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Which books of hers were made into movies?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "The most well-known adaptation of Octavia Butler's work is the FX series" + " Kindred, based on her novel of the same name.", }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Who were her contemporaries?" }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R." + " Delany, and Joanna Russ.", }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "What awards did she win?" }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur" + " Fellowship.", }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Which other women sci-fi writers might I want to read?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "You might want to read Ursula K. Le Guin or Joanna Russ.", }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Write a short synopsis of Butler's book, Parable of the Sower. What is it" + " about?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Parable of the Sower is a science fiction novel by Octavia Butler," + " published in 1993. It follows the story of Lauren Olamina, a young woman" + @@ -75,12 +75,12 @@ export const history = [ }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "What inspired Octavia Butler's writing style?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Octavia Butler was inspired by a wide range of influences, including her" + " passion for science fiction, her interest in sociology, and her desire to" + @@ -88,24 +88,24 @@ export const history = [ }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Did Octavia Butler write only science fiction?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "While Octavia Butler is best known for her contributions to science fiction," + " she also wrote in other genres, including fantasy and speculative fiction.", }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Tell me more about the Xenogenesis series.", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "The Xenogenesis series, also known as the Lilith's Brood trilogy, consists" + " of three novels: Dawn (1987), Adulthood Rites (1988), and Imago (1989)." + @@ -114,12 +114,12 @@ export const history = [ }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Are there any upcoming adaptations of Octavia Butler's work?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "As of my last update, there were discussions about potential adaptations," + " but no confirmed upcoming projects. It's advisable to check the latest" + @@ -127,12 +127,12 @@ export const history = [ }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "What is the Earthseed series about?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "The Earthseed series consists of Parable of the Sower (1993) and Parable of" + " the Talents (1998). It follows the protagonist, Lauren Olamina, as she" + @@ -141,12 +141,12 @@ export const history = [ }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "How did Octavia Butler impact the science fiction genre?", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Octavia Butler made significant contributions to the science fiction genre" + " by challenging conventions and addressing social issues. Her works" + @@ -155,12 +155,12 @@ export const history = [ }, { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Tell me about Octavia Butler's writing process.", }, { role: "ai", - roleType: "AssistantRole" as RoleType, + role_type: "AssistantRole" as RoleType, content: "Octavia Butler was known for her disciplined and dedicated writing" + " process. She often wrote in the early morning hours and set specific" + diff --git a/examples/memory/memory_example.ts b/examples/memory/memory_example.ts index a7f9262..847bde2 100644 --- a/examples/memory/memory_example.ts +++ b/examples/memory/memory_example.ts @@ -68,8 +68,8 @@ async function main() { // Add memory. We could do this in a batch, but we'll do it one by one rather to // ensure that summaries and other artifacts are generated correctly. try { - for (const { role, roleType, content } of history) { - const message = new Message({ role, roleType, content }); + for (const { role, role_type, content } of history) { + const message = new Message({ role, role_type, content }); const memory = new Memory({ messages: [message] }); await client.memory.addMemory(sessionID, memory); diff --git a/src/langchain/memory.ts b/src/langchain/memory.ts index ac6c8f4..71d2ce6 100644 --- a/src/langchain/memory.ts +++ b/src/langchain/memory.ts @@ -144,11 +144,11 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { if (memory) { messages = messages.concat( memory.messages.map((message) => { - const { content, roleType, role } = message; - if (roleType === "UserRole") { + const { content, role_type, role } = message; + if (role_type === "UserRole") { return new HumanMessage(content); } - if (roleType === "AssistantRole" || roleType === "SystemRole") { + if (role_type === "AssistantRole") { return new AIMessage(content); } // default to generic ChatMessage @@ -189,12 +189,12 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { messages: [ new Message({ role: this.humanPrefix, - roleType: "UserRole", + role_type: "UserRole", content: `${input}`, }), new Message({ role: this.aiPrefix, - roleType: "AssistantRole", + role_type: "AssistantRole", content: `${output}`, }), ], diff --git a/src/langchain/message_history.ts b/src/langchain/message_history.ts index 31ca623..5e0c37a 100644 --- a/src/langchain/message_history.ts +++ b/src/langchain/message_history.ts @@ -87,7 +87,8 @@ export class ZepChatMessageHistory token_count: msg.token_count, metadata: msg.metadata, }; - if (msg.roleType === ("AssistantRole" as RoleType)) { + + if (msg.role_type === ("AssistantRole" as RoleType)) { return new AIMessage(msg.content, metadata); } return new HumanMessage(msg.content, metadata); @@ -117,7 +118,7 @@ export class ZepChatMessageHistory content: message.content, // eslint-disable-next-line no-underscore-dangle role: message._getType(), - roleType: getZepMessageRoleType(message._getType()), + role_type: getZepMessageRoleType(message._getType()), metadata, }); diff --git a/src/message_models.ts b/src/message_models.ts index 6d6aaad..e0430c0 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -13,7 +13,7 @@ export interface IMessage { uuid?: string; created_at?: string; role: string; - roleType: RoleType; + role_type: RoleType; content: string; token_count?: number; metadata?: Record; @@ -29,7 +29,7 @@ export class Message { role: string; - roleType: RoleType; + role_type: RoleType; content: string; @@ -45,7 +45,7 @@ export class Message { this.uuid = data.uuid; this.created_at = data.created_at; this.role = data.role; - this.roleType = data.roleType; + this.role_type = data.role_type; this.content = data.content; this.token_count = data.token_count; this.metadata = data.metadata; @@ -60,7 +60,7 @@ export class Message { uuid: this.uuid, created_at: this.created_at, role: this.role, - roleType: this.roleType, + role_type: this.role_type, content: this.content, token_count: this.token_count, metadata: this.metadata, @@ -68,8 +68,6 @@ export class Message { } } -// export type MessageType = "human" | "ai" | "generic" | "system" | "function" | "tool"; - export const getZepMessageRoleType = (role: MessageType): RoleType => { switch (role) { case "human": diff --git a/src/tests/memory_test.ts b/src/tests/memory_test.ts index a592b7a..a922d90 100644 --- a/src/tests/memory_test.ts +++ b/src/tests/memory_test.ts @@ -213,7 +213,7 @@ describe("ZepClient", () => { it("should retrieve a message for a session", async () => { const responseData = { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -261,7 +261,7 @@ describe("ZepClient", () => { messages: [ { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -317,7 +317,7 @@ describe("ZepClient", () => { it("should update metadata for a message in a session", async () => { const responseData = { role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -364,7 +364,7 @@ describe("ZepClient", () => { // Test for retrieving memory for a session it("should retrieve memory for a session", async () => { const responseData = { - messages: [{ role: "human", roleType: "UserRole" as RoleType, content: "Hello" }], + messages: [{ role: "human", role_type: "UserRole" as RoleType, content: "Hello" }], summary: { uuid: "", created_at: "", @@ -380,7 +380,7 @@ describe("ZepClient", () => { expect(memory).toEqual( new Memory({ - messages: [new Message({ role: "human", roleType: "UserRole" as RoleType, content: "Hello" })], + messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello" })], summary: new Summary({ content: "Memory summary", created_at: "", @@ -448,8 +448,8 @@ describe("ZepClient", () => { it("should retrieve last 'n' memories for a session when 'lastn' parameter is used", async () => { const responseData = { messages: [ - { role: "system", roleType: "SystemRole" as RoleType, content: "How can I assist you?" }, - { role: "human", roleType: "UserRole" as RoleType, content: "What's the weather like?" }, + { role: "system", role_type: "SystemRole" as RoleType, content: "How can I assist you?" }, + { role: "human", role_type: "UserRole" as RoleType, content: "What's the weather like?" }, ], summary: { uuid: "", @@ -480,12 +480,12 @@ describe("ZepClient", () => { messages: [ new Message({ role: "system", - roleType: "SystemRole" as RoleType, + role_type: "SystemRole" as RoleType, content: "How can I assist you?", }), new Message({ role: "human", - roleType: "UserRole" as RoleType, + role_type: "UserRole" as RoleType, content: "What's the weather like?", }), ], @@ -505,7 +505,7 @@ describe("ZepClient", () => { describe("addMemory", () => { it("should add a memory to a session", async () => { const memoryData = new Memory({ - messages: [new Message({ role: "human", roleType: "UserRole" as RoleType, content: "Hello again!" })], + messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello again!" })], summary: new Summary({ uuid: "", created_at: "", @@ -530,7 +530,7 @@ describe("ZepClient", () => { it("should throw APIError if !200 OK", async () => { const memoryData = new Memory({ messages: [ - new Message({ role: "system", roleType: "SystemRole" as RoleType, content: "System message" }), + new Message({ role: "system", role_type: "SystemRole" as RoleType, content: "System message" }), ], summary: new Summary({ uuid: "summary_uuid", @@ -600,7 +600,7 @@ describe("ZepClient", () => { { message: { role: "system", - roleType: "SystemRole", + role_type: "SystemRole", content: "system message", uuid: "message_uuid", created_at: "2023-01-01T00:00:00Z", From 3c7ef6897d7cda18d90835625b1e6a51976ec127 Mon Sep 17 00:00:00 2001 From: paulpaliychuk Date: Tue, 5 Mar 2024 17:23:14 -0500 Subject: [PATCH 7/9] feat: Add support for human and ai prefix in zep message history --- src/langchain/message_history.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/langchain/message_history.ts b/src/langchain/message_history.ts index 5e0c37a..9c4da16 100644 --- a/src/langchain/message_history.ts +++ b/src/langchain/message_history.ts @@ -22,6 +22,8 @@ interface ZepMemoryInput { sessionId: string; client: ZepClient; memoryType: MemoryType; + humanPrefix?: string; + aiPrefix?: string; } /** @@ -43,11 +45,21 @@ export class ZepChatMessageHistory memoryType: MemoryType; + humanPrefix = "human"; + + aiPrefix = "ai"; + constructor(fields: ZepMemoryInput) { super(); this.sessionId = fields.sessionId; this.memoryType = fields.memoryType; this.client = fields.client; + if (fields.humanPrefix) { + this.humanPrefix = fields.humanPrefix; + } + if (fields.aiPrefix) { + this.aiPrefix = fields.aiPrefix; + } } private async getMemory(): Promise { @@ -99,13 +111,17 @@ export class ZepChatMessageHistory return messages; } - // @ts-ignore async addAIChatMessage(message: string, metadata?: any): Promise { await this.addMessage(new AIMessage({ content: message }), metadata); } - // @ts-ignore async addMessage(message: BaseMessage, metadata?: any): Promise { + const messageToSave = message; + if (message._getType() === "ai") { + messageToSave.name = this.aiPrefix; + } else if (message._getType() === "human") { + messageToSave.name = this.humanPrefix; + } if (message.content === null) { throw new Error("Message content cannot be null"); } @@ -116,8 +132,7 @@ export class ZepChatMessageHistory const zepMessage = new Message({ content: message.content, - // eslint-disable-next-line no-underscore-dangle - role: message._getType(), + role: message.name ?? message._getType(), role_type: getZepMessageRoleType(message._getType()), metadata, }); From 524de70e3682e687dfaddb0d8ffda9fc8ebdbb08 Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Thu, 7 Mar 2024 09:03:09 -0700 Subject: [PATCH 8/9] updating role types --- examples/memory/history.ts | 52 ++++++++++++++++---------------- src/langchain/memory.ts | 8 ++--- src/langchain/message_history.ts | 2 +- src/message_models.ts | 21 +++++++------ src/tests/memory_test.ts | 24 +++++++-------- 5 files changed, 55 insertions(+), 52 deletions(-) diff --git a/examples/memory/history.ts b/examples/memory/history.ts index 3e7850e..712dec9 100644 --- a/examples/memory/history.ts +++ b/examples/memory/history.ts @@ -3,70 +3,70 @@ import { RoleType } from "../../src/message_models"; export const history = [ { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Who was Octavia Butler?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" + " science fiction author.", }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Which books of hers were made into movies?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "The most well-known adaptation of Octavia Butler's work is the FX series" + " Kindred, based on her novel of the same name.", }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Who were her contemporaries?" }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R." + " Delany, and Joanna Russ.", }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "What awards did she win?" }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur" + " Fellowship.", }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Which other women sci-fi writers might I want to read?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "You might want to read Ursula K. Le Guin or Joanna Russ.", }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Write a short synopsis of Butler's book, Parable of the Sower. What is it" + " about?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Parable of the Sower is a science fiction novel by Octavia Butler," + " published in 1993. It follows the story of Lauren Olamina, a young woman" + @@ -75,12 +75,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "What inspired Octavia Butler's writing style?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Octavia Butler was inspired by a wide range of influences, including her" + " passion for science fiction, her interest in sociology, and her desire to" + @@ -88,24 +88,24 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Did Octavia Butler write only science fiction?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "While Octavia Butler is best known for her contributions to science fiction," + " she also wrote in other genres, including fantasy and speculative fiction.", }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Tell me more about the Xenogenesis series.", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "The Xenogenesis series, also known as the Lilith's Brood trilogy, consists" + " of three novels: Dawn (1987), Adulthood Rites (1988), and Imago (1989)." + @@ -114,12 +114,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Are there any upcoming adaptations of Octavia Butler's work?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "As of my last update, there were discussions about potential adaptations," + " but no confirmed upcoming projects. It's advisable to check the latest" + @@ -127,12 +127,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "What is the Earthseed series about?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "The Earthseed series consists of Parable of the Sower (1993) and Parable of" + " the Talents (1998). It follows the protagonist, Lauren Olamina, as she" + @@ -141,12 +141,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "How did Octavia Butler impact the science fiction genre?", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Octavia Butler made significant contributions to the science fiction genre" + " by challenging conventions and addressing social issues. Her works" + @@ -155,12 +155,12 @@ export const history = [ }, { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Tell me about Octavia Butler's writing process.", }, { role: "ai", - role_type: "AssistantRole" as RoleType, + role_type: "assistant" as RoleType, content: "Octavia Butler was known for her disciplined and dedicated writing" + " process. She often wrote in the early morning hours and set specific" + diff --git a/src/langchain/memory.ts b/src/langchain/memory.ts index 71d2ce6..f02354e 100644 --- a/src/langchain/memory.ts +++ b/src/langchain/memory.ts @@ -145,10 +145,10 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { messages = messages.concat( memory.messages.map((message) => { const { content, role_type, role } = message; - if (role_type === "UserRole") { + if (role_type === "user") { return new HumanMessage(content); } - if (role_type === "AssistantRole") { + if (role_type === "assistant") { return new AIMessage(content); } // default to generic ChatMessage @@ -189,12 +189,12 @@ export class ZepMemory extends BaseChatMemory implements ZepMemoryInput { messages: [ new Message({ role: this.humanPrefix, - role_type: "UserRole", + role_type: "user", content: `${input}`, }), new Message({ role: this.aiPrefix, - role_type: "AssistantRole", + role_type: "assistant", content: `${output}`, }), ], diff --git a/src/langchain/message_history.ts b/src/langchain/message_history.ts index 5e0c37a..04d9f65 100644 --- a/src/langchain/message_history.ts +++ b/src/langchain/message_history.ts @@ -88,7 +88,7 @@ export class ZepChatMessageHistory metadata: msg.metadata, }; - if (msg.role_type === ("AssistantRole" as RoleType)) { + if (msg.role_type === ("assistant" as RoleType)) { return new AIMessage(msg.content, metadata); } return new HumanMessage(msg.content, metadata); diff --git a/src/message_models.ts b/src/message_models.ts index e0430c0..3ffb02e 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -1,10 +1,11 @@ import { MessageType } from "@langchain/core/messages"; export type RoleType = - | "UserRole" - | "AssistantRole" - | "SystemRole" - | "FunctionRole"; + | "user" + | "assistant" + | "system" + | "function" + | "tool"; /** * IMessage interface for providing input to create a Message instance. @@ -71,14 +72,16 @@ export class Message { export const getZepMessageRoleType = (role: MessageType): RoleType => { switch (role) { case "human": - return "UserRole"; + return "user"; case "ai": - return "AssistantRole"; + return "assistant"; case "system": - return "SystemRole"; + return "system"; case "function": - return "FunctionRole"; + return "function"; + case "tool": + return "tool"; default: - return "SystemRole"; + return "system"; } }; diff --git a/src/tests/memory_test.ts b/src/tests/memory_test.ts index a922d90..f3c1f3c 100644 --- a/src/tests/memory_test.ts +++ b/src/tests/memory_test.ts @@ -213,7 +213,7 @@ describe("ZepClient", () => { it("should retrieve a message for a session", async () => { const responseData = { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -261,7 +261,7 @@ describe("ZepClient", () => { messages: [ { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -317,7 +317,7 @@ describe("ZepClient", () => { it("should update metadata for a message in a session", async () => { const responseData = { role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "Hello", uuid: "message_uuid", created_at: "2022-01-01T00:00:00Z", @@ -364,7 +364,7 @@ describe("ZepClient", () => { // Test for retrieving memory for a session it("should retrieve memory for a session", async () => { const responseData = { - messages: [{ role: "human", role_type: "UserRole" as RoleType, content: "Hello" }], + messages: [{ role: "human", role_type: "user" as RoleType, content: "Hello" }], summary: { uuid: "", created_at: "", @@ -380,7 +380,7 @@ describe("ZepClient", () => { expect(memory).toEqual( new Memory({ - messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello" })], + messages: [new Message({ role: "human", role_type: "user" as RoleType, content: "Hello" })], summary: new Summary({ content: "Memory summary", created_at: "", @@ -448,8 +448,8 @@ describe("ZepClient", () => { it("should retrieve last 'n' memories for a session when 'lastn' parameter is used", async () => { const responseData = { messages: [ - { role: "system", role_type: "SystemRole" as RoleType, content: "How can I assist you?" }, - { role: "human", role_type: "UserRole" as RoleType, content: "What's the weather like?" }, + { role: "system", role_type: "system" as RoleType, content: "How can I assist you?" }, + { role: "human", role_type: "user" as RoleType, content: "What's the weather like?" }, ], summary: { uuid: "", @@ -480,12 +480,12 @@ describe("ZepClient", () => { messages: [ new Message({ role: "system", - role_type: "SystemRole" as RoleType, + role_type: "system" as RoleType, content: "How can I assist you?", }), new Message({ role: "human", - role_type: "UserRole" as RoleType, + role_type: "user" as RoleType, content: "What's the weather like?", }), ], @@ -505,7 +505,7 @@ describe("ZepClient", () => { describe("addMemory", () => { it("should add a memory to a session", async () => { const memoryData = new Memory({ - messages: [new Message({ role: "human", role_type: "UserRole" as RoleType, content: "Hello again!" })], + messages: [new Message({ role: "human", role_type: "user" as RoleType, content: "Hello again!" })], summary: new Summary({ uuid: "", created_at: "", @@ -530,7 +530,7 @@ describe("ZepClient", () => { it("should throw APIError if !200 OK", async () => { const memoryData = new Memory({ messages: [ - new Message({ role: "system", role_type: "SystemRole" as RoleType, content: "System message" }), + new Message({ role: "system", role_type: "system" as RoleType, content: "System message" }), ], summary: new Summary({ uuid: "summary_uuid", @@ -600,7 +600,7 @@ describe("ZepClient", () => { { message: { role: "system", - role_type: "SystemRole", + role_type: "system", content: "system message", uuid: "message_uuid", created_at: "2023-01-01T00:00:00Z", From bc7e2d4486827c7bd30f535d0323166d91cebfb9 Mon Sep 17 00:00:00 2001 From: Peter Garbers <225740+petergarbers@users.noreply.github.com> Date: Thu, 7 Mar 2024 18:25:33 -0700 Subject: [PATCH 9/9] prettifier --- src/message_models.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/message_models.ts b/src/message_models.ts index 3ffb02e..e286dbb 100644 --- a/src/message_models.ts +++ b/src/message_models.ts @@ -1,11 +1,6 @@ import { MessageType } from "@langchain/core/messages"; -export type RoleType = - | "user" - | "assistant" - | "system" - | "function" - | "tool"; +export type RoleType = "user" | "assistant" | "system" | "function" | "tool"; /** * IMessage interface for providing input to create a Message instance.