-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of core memory (#307)
* Initial implementation of core memory * stop reseting the db in every build * stop reseting the db in every build * delete unused code * changed prompt * readded commented verbose * changed prompt²
- Loading branch information
Showing
8 changed files
with
159 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,12 @@ | |
"scripts": { | ||
"start": "ts-node src/index.ts", | ||
"dev": "nodemon src/index.ts", | ||
"generate": "prisma generate", | ||
"migrate": "prisma migrate reset", | ||
"studio": "prisma studio", | ||
"docker:build": "docker build -t sydney-whatsapp-chatbot .", | ||
"docker:run": "docker run -it -d --name sydney-whatsapp-chatbot sydney-whatsapp-chatbot", | ||
"docker:build:run": "pnpm docker:build && pnpm docker:run", | ||
"docker:stop": "docker stop sydney-whatsapp-chatbot", | ||
"build": "npm run generate && npm run migrate" | ||
"build": "prisma generate && prisma migrate" | ||
}, | ||
"repository": "https://github.com/WAppAI/assistant.git", | ||
"author": "Luis Otavio <[email protected]> and Matheus Veiga <[email protected]>", | ||
|
13 changes: 13 additions & 0 deletions
13
prisma/migrations/20240918062447_add_core_memory_to_open_router_conversation/migration.sql
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,13 @@ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_OpenRouterConversation" ( | ||
"waChatId" TEXT NOT NULL PRIMARY KEY, | ||
"memory" TEXT NOT NULL, | ||
"coreMemory" TEXT NOT NULL DEFAULT '', | ||
CONSTRAINT "OpenRouterConversation_waChatId_fkey" FOREIGN KEY ("waChatId") REFERENCES "WAChat" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); | ||
INSERT INTO "new_OpenRouterConversation" ("memory", "waChatId") SELECT "memory", "waChatId" FROM "OpenRouterConversation"; | ||
DROP TABLE "OpenRouterConversation"; | ||
ALTER TABLE "new_OpenRouterConversation" RENAME TO "OpenRouterConversation"; | ||
PRAGMA foreign_key_check; | ||
PRAGMA foreign_keys=ON; |
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
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,98 @@ | ||
// src/clients/tools/tool-core-memory.ts | ||
|
||
import { StructuredTool } from "langchain/tools"; | ||
import { z } from "zod"; | ||
import { getCoreMemoryFor, updateCoreMemory } from "../../crud/conversation"; | ||
|
||
const AddToCoreMemorySchema = z.object({ | ||
chat: z.string().describe("The chat ID to which the message will be added."), | ||
message: z.string().describe("The message to add to the core memory."), | ||
}); | ||
|
||
export class AddToCoreMemoryTool extends StructuredTool { | ||
name = "AddToCoreMemoryTool"; | ||
description = "Adds a message to the core memory for a given chat."; | ||
schema = AddToCoreMemorySchema; | ||
|
||
async _call({ | ||
chat, | ||
message, | ||
}: z.infer<typeof AddToCoreMemorySchema>): Promise<string> { | ||
try { | ||
let coreMemory = await getCoreMemoryFor(chat); | ||
if (!coreMemory) { | ||
coreMemory = ""; | ||
} | ||
coreMemory += ` ${message}`; | ||
await updateCoreMemory(chat, coreMemory.trim()); | ||
return `Message added to core memory for chat: ${chat}`; | ||
} catch (error) { | ||
console.error("Error adding to core memory:", error); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
const DeleteFromCoreMemorySchema = z.object({ | ||
chat: z.string().describe("The chat ID from which the part will be deleted."), | ||
part: z.string().describe("The specific part of the core memory to delete."), | ||
}); | ||
|
||
export class DeleteFromCoreMemoryTool extends StructuredTool { | ||
name = "DeleteFromCoreMemoryTool"; | ||
description = "Deletes a specific part of the core memory for a given chat."; | ||
schema = DeleteFromCoreMemorySchema; | ||
|
||
async _call({ | ||
chat, | ||
part, | ||
}: z.infer<typeof DeleteFromCoreMemorySchema>): Promise<string> { | ||
try { | ||
let coreMemory = await getCoreMemoryFor(chat); | ||
if (!coreMemory) { | ||
return `No core memory found for chat: ${chat}`; | ||
} | ||
coreMemory = coreMemory.replace(part, "").trim(); | ||
await updateCoreMemory(chat, coreMemory); | ||
return `Part deleted from core memory for chat: ${chat}`; | ||
} catch (error) { | ||
console.error("Error deleting from core memory:", error); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
const ReplaceInCoreMemorySchema = z.object({ | ||
chat: z | ||
.string() | ||
.describe("The chat ID for which the core memory will be replaced."), | ||
oldPart: z | ||
.string() | ||
.describe("The specific part of the core memory to replace."), | ||
newPart: z.string().describe("The new part to replace the old part with."), | ||
}); | ||
|
||
export class ReplaceInCoreMemoryTool extends StructuredTool { | ||
name = "ReplaceInCoreMemoryTool"; | ||
description = "Replaces a specific part of the core memory for a given chat."; | ||
schema = ReplaceInCoreMemorySchema; | ||
|
||
async _call({ | ||
chat, | ||
oldPart, | ||
newPart, | ||
}: z.infer<typeof ReplaceInCoreMemorySchema>): Promise<string> { | ||
try { | ||
let coreMemory = await getCoreMemoryFor(chat); | ||
if (!coreMemory) { | ||
return `No core memory found for chat: ${chat}`; | ||
} | ||
coreMemory = coreMemory.replace(oldPart, newPart).trim(); | ||
await updateCoreMemory(chat, coreMemory); | ||
return `Part replaced in core memory for chat: ${chat}`; | ||
} catch (error) { | ||
console.error("Error replacing in core memory:", error); | ||
throw error; | ||
} | ||
} | ||
} |
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
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