repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import { Memory } from "../src"; |
| #2 | import dotenv from "dotenv"; |
| #3 | |
| #4 | // Load environment variables |
| #5 | dotenv.config(); |
| #6 | |
| #7 | async function demoDefaultConfig() { |
| #8 | console.log("\n=== Testing Default Config ===\n"); |
| #9 | |
| #10 | const memory = new Memory(); |
| #11 | await runTests(memory); |
| #12 | } |
| #13 | |
| #14 | async function run_examples() { |
| #15 | // Test default config |
| #16 | await demoDefaultConfig(); |
| #17 | } |
| #18 | |
| #19 | run_examples(); |
| #20 | |
| #21 | async function runTests(memory: Memory) { |
| #22 | try { |
| #23 | // Reset all memories |
| #24 | console.log("\nResetting all memories..."); |
| #25 | await memory.reset(); |
| #26 | console.log("All memories reset"); |
| #27 | |
| #28 | // Add a single memory |
| #29 | console.log("\nAdding a single memory..."); |
| #30 | const result1 = await memory.add( |
| #31 | "Hi, my name is John and I am a software engineer.", |
| #32 | { |
| #33 | userId: "john", |
| #34 | }, |
| #35 | ); |
| #36 | console.log("Added memory:", result1); |
| #37 | |
| #38 | // Add multiple messages |
| #39 | console.log("\nAdding multiple messages..."); |
| #40 | const result2 = await memory.add( |
| #41 | [ |
| #42 | { role: "user", content: "What is your favorite city?" }, |
| #43 | { role: "assistant", content: "I love Paris, it is my favorite city." }, |
| #44 | ], |
| #45 | { |
| #46 | userId: "john", |
| #47 | }, |
| #48 | ); |
| #49 | console.log("Added messages:", result2); |
| #50 | |
| #51 | // Trying to update the memory |
| #52 | const result3 = await memory.add( |
| #53 | [ |
| #54 | { role: "user", content: "What is your favorite city?" }, |
| #55 | { |
| #56 | role: "assistant", |
| #57 | content: "I love New York, it is my favorite city.", |
| #58 | }, |
| #59 | ], |
| #60 | { |
| #61 | userId: "john", |
| #62 | }, |
| #63 | ); |
| #64 | console.log("Updated messages:", result3); |
| #65 | |
| #66 | // Get a single memory |
| #67 | console.log("\nGetting a single memory..."); |
| #68 | if (result1.results && result1.results.length > 0) { |
| #69 | const singleMemory = await memory.get(result1.results[0].id); |
| #70 | console.log("Single memory:", singleMemory); |
| #71 | } else { |
| #72 | console.log("No memory was added in the first step"); |
| #73 | } |
| #74 | |
| #75 | // Updating this memory |
| #76 | const result4 = await memory.update( |
| #77 | result1.results[0].id, |
| #78 | "I love India, it is my favorite country.", |
| #79 | ); |
| #80 | console.log("Updated memory:", result4); |
| #81 | |
| #82 | // Get all memories |
| #83 | console.log("\nGetting all memories..."); |
| #84 | const allMemories = await memory.getAll({ |
| #85 | userId: "john", |
| #86 | }); |
| #87 | console.log("All memories:", allMemories); |
| #88 | |
| #89 | // Search for memories |
| #90 | console.log("\nSearching memories..."); |
| #91 | const searchResult = await memory.search("What do you know about Paris?", { |
| #92 | userId: "john", |
| #93 | }); |
| #94 | console.log("Search results:", searchResult); |
| #95 | |
| #96 | // Get memory history |
| #97 | if (result1.results && result1.results.length > 0) { |
| #98 | console.log("\nGetting memory history..."); |
| #99 | const history = await memory.history(result1.results[0].id); |
| #100 | console.log("Memory history:", history); |
| #101 | } |
| #102 | |
| #103 | // Delete a memory |
| #104 | if (result1.results && result1.results.length > 0) { |
| #105 | console.log("\nDeleting a memory..."); |
| #106 | await memory.delete(result1.results[0].id); |
| #107 | console.log("Memory deleted successfully"); |
| #108 | } |
| #109 | |
| #110 | // Reset all memories |
| #111 | console.log("\nResetting all memories..."); |
| #112 | await memory.reset(); |
| #113 | console.log("All memories reset"); |
| #114 | } catch (error) { |
| #115 | console.error("Error:", error); |
| #116 | } |
| #117 | } |
| #118 | |
| #119 | async function demoLocalMemory() { |
| #120 | console.log("\n=== Testing In-Memory Vector Store with Ollama===\n"); |
| #121 | |
| #122 | const memory = new Memory({ |
| #123 | version: "v1.1", |
| #124 | embedder: { |
| #125 | provider: "ollama", |
| #126 | config: { |
| #127 | model: "nomic-embed-text:latest", |
| #128 | }, |
| #129 | }, |
| #130 | vectorStore: { |
| #131 | provider: "memory", |
| #132 | config: { |
| #133 | collectionName: "memories", |
| #134 | dimension: 768, // 768 is the dimension of the nomic-embed-text model |
| #135 | }, |
| #136 | }, |
| #137 | llm: { |
| #138 | provider: "ollama", |
| #139 | config: { |
| #140 | model: "llama3.1:8b", |
| #141 | }, |
| #142 | }, |
| #143 | // historyDbPath: "memory.db", |
| #144 | }); |
| #145 | |
| #146 | await runTests(memory); |
| #147 | } |
| #148 | |
| #149 | async function demoMemoryStore() { |
| #150 | console.log("\n=== Testing In-Memory Vector Store ===\n"); |
| #151 | |
| #152 | const memory = new Memory({ |
| #153 | version: "v1.1", |
| #154 | embedder: { |
| #155 | provider: "openai", |
| #156 | config: { |
| #157 | apiKey: process.env.OPENAI_API_KEY || "", |
| #158 | model: "text-embedding-3-small", |
| #159 | }, |
| #160 | }, |
| #161 | vectorStore: { |
| #162 | provider: "memory", |
| #163 | config: { |
| #164 | collectionName: "memories", |
| #165 | dimension: 1536, |
| #166 | }, |
| #167 | }, |
| #168 | llm: { |
| #169 | provider: "openai", |
| #170 | config: { |
| #171 | apiKey: process.env.OPENAI_API_KEY || "", |
| #172 | model: "gpt-4-turbo-preview", |
| #173 | }, |
| #174 | }, |
| #175 | historyDbPath: "memory.db", |
| #176 | }); |
| #177 | |
| #178 | await runTests(memory); |
| #179 | } |
| #180 | |
| #181 | async function demoPGVector() { |
| #182 | console.log("\n=== Testing PGVector Store ===\n"); |
| #183 | |
| #184 | const memory = new Memory({ |
| #185 | version: "v1.1", |
| #186 | embedder: { |
| #187 | provider: "openai", |
| #188 | config: { |
| #189 | apiKey: process.env.OPENAI_API_KEY || "", |
| #190 | model: "text-embedding-3-small", |
| #191 | }, |
| #192 | }, |
| #193 | vectorStore: { |
| #194 | provider: "pgvector", |
| #195 | config: { |
| #196 | collectionName: "memories", |
| #197 | dimension: 1536, |
| #198 | dbname: process.env.PGVECTOR_DB || "vectordb", |
| #199 | user: process.env.PGVECTOR_USER || "postgres", |
| #200 | password: process.env.PGVECTOR_PASSWORD || "postgres", |
| #201 | host: process.env.PGVECTOR_HOST || "localhost", |
| #202 | port: parseInt(process.env.PGVECTOR_PORT || "5432"), |
| #203 | embeddingModelDims: 1536, |
| #204 | hnsw: true, |
| #205 | }, |
| #206 | }, |
| #207 | llm: { |
| #208 | provider: "openai", |
| #209 | config: { |
| #210 | apiKey: process.env.OPENAI_API_KEY || "", |
| #211 | model: "gpt-4-turbo-preview", |
| #212 | }, |
| #213 | }, |
| #214 | historyDbPath: "memory.db", |
| #215 | }); |
| #216 | |
| #217 | await runTests(memory); |
| #218 | } |
| #219 | |
| #220 | async function demoQdrant() { |
| #221 | console.log("\n=== Testing Qdrant Store ===\n"); |
| #222 | |
| #223 | const memory = new Memory({ |
| #224 | version: "v1.1", |
| #225 | embedder: { |
| #226 | provider: "openai", |
| #227 | config: { |
| #228 | apiKey: process.env.OPENAI_API_KEY || "", |
| #229 | model: "text-embedding-3-small", |
| #230 | }, |
| #231 | }, |
| #232 | vectorStore: { |
| #233 | provider: "qdrant", |
| #234 | config: { |
| #235 | collectionName: "memories", |
| #236 | embeddingModelDims: 1536, |
| #237 | url: process.env.QDRANT_URL, |
| #238 | apiKey: process.env.QDRANT_API_KEY, |
| #239 | path: process.env.QDRANT_PATH, |
| #240 | host: process.env.QDRANT_HOST, |
| #241 | port: process.env.QDRANT_PORT |
| #242 | ? parseInt(process.env.QDRANT_PORT) |
| #243 | : undefined, |
| #244 | onDisk: true, |
| #245 | }, |
| #246 | }, |
| #247 | llm: { |
| #248 | provider: "openai", |
| #249 | config: { |
| #250 | apiKey: process.env.OPENAI_API_KEY || "", |
| #251 | model: "gpt-4-turbo-preview", |
| #252 | }, |
| #253 | }, |
| #254 | historyDbPath: "memory.db", |
| #255 | }); |
| #256 | |
| #257 | await runTests(memory); |
| #258 | } |
| #259 | |
| #260 | async function demoRedis() { |
| #261 | console.log("\n=== Testing Redis Store ===\n"); |
| #262 | |
| #263 | const memory = new Memory({ |
| #264 | version: "v1.1", |
| #265 | embedder: { |
| #266 | provider: "openai", |
| #267 | config: { |
| #268 | apiKey: process.env.OPENAI_API_KEY || "", |
| #269 | model: "text-embedding-3-small", |
| #270 | }, |
| #271 | }, |
| #272 | vectorStore: { |
| #273 | provider: "redis", |
| #274 | config: { |
| #275 | collectionName: "memories", |
| #276 | embeddingModelDims: 1536, |
| #277 | redisUrl: process.env.REDIS_URL || "redis://localhost:6379", |
| #278 | username: process.env.REDIS_USERNAME, |
| #279 | password: process.env.REDIS_PASSWORD, |
| #280 | }, |
| #281 | }, |
| #282 | llm: { |
| #283 | provider: "openai", |
| #284 | config: { |
| #285 | apiKey: process.env.OPENAI_API_KEY || "", |
| #286 | model: "gpt-4-turbo-preview", |
| #287 | }, |
| #288 | }, |
| #289 | historyDbPath: "memory.db", |
| #290 | }); |
| #291 | |
| #292 | await runTests(memory); |
| #293 | } |
| #294 | |
| #295 | async function demoGraphMemory() { |
| #296 | console.log("\n=== Testing Graph Memory Store ===\n"); |
| #297 | |
| #298 | const memory = new Memory({ |
| #299 | version: "v1.1", |
| #300 | embedder: { |
| #301 | provider: "openai", |
| #302 | config: { |
| #303 | apiKey: process.env.OPENAI_API_KEY || "", |
| #304 | model: "text-embedding-3-small", |
| #305 | }, |
| #306 | }, |
| #307 | vectorStore: { |
| #308 | provider: "memory", |
| #309 | config: { |
| #310 | collectionName: "memories", |
| #311 | dimension: 1536, |
| #312 | }, |
| #313 | }, |
| #314 | llm: { |
| #315 | provider: "openai", |
| #316 | config: { |
| #317 | apiKey: process.env.OPENAI_API_KEY || "", |
| #318 | model: "gpt-4-turbo-preview", |
| #319 | }, |
| #320 | }, |
| #321 | graphStore: { |
| #322 | provider: "neo4j", |
| #323 | config: { |
| #324 | url: process.env.NEO4J_URL || "neo4j://localhost:7687", |
| #325 | username: process.env.NEO4J_USERNAME || "neo4j", |
| #326 | password: process.env.NEO4J_PASSWORD || "password", |
| #327 | }, |
| #328 | llm: { |
| #329 | provider: "openai", |
| #330 | config: { |
| #331 | model: "gpt-4-turbo-preview", |
| #332 | }, |
| #333 | }, |
| #334 | }, |
| #335 | historyDbPath: "memory.db", |
| #336 | }); |
| #337 | |
| #338 | try { |
| #339 | // Reset all memories |
| #340 | await memory.reset(); |
| #341 | |
| #342 | // Add memories with relationships |
| #343 | const result = await memory.add( |
| #344 | [ |
| #345 | { |
| #346 | role: "user", |
| #347 | content: "Alice is Bob's sister and works as a doctor.", |
| #348 | }, |
| #349 | { |
| #350 | role: "assistant", |
| #351 | content: |
| #352 | "I understand that Alice and Bob are siblings and Alice is a medical professional.", |
| #353 | }, |
| #354 | { role: "user", content: "Bob is married to Carol who is a teacher." }, |
| #355 | ], |
| #356 | { |
| #357 | userId: "john", |
| #358 | }, |
| #359 | ); |
| #360 | console.log("Added memories with relationships:", result); |
| #361 | |
| #362 | // Search for connected information |
| #363 | const searchResult = await memory.search( |
| #364 | "Tell me about Bob's family connections", |
| #365 | { |
| #366 | userId: "john", |
| #367 | }, |
| #368 | ); |
| #369 | console.log("Search results with graph relationships:", searchResult); |
| #370 | } catch (error) { |
| #371 | console.error("Error in graph memory demo:", error); |
| #372 | } |
| #373 | } |
| #374 | |
| #375 | async function main() { |
| #376 | // Test in-memory store |
| #377 | await demoMemoryStore(); |
| #378 | |
| #379 | // Test in-memory store with Ollama |
| #380 | await demoLocalMemory(); |
| #381 | |
| #382 | // Test graph memory if Neo4j environment variables are set |
| #383 | if ( |
| #384 | process.env.NEO4J_URL && |
| #385 | process.env.NEO4J_USERNAME && |
| #386 | process.env.NEO4J_PASSWORD |
| #387 | ) { |
| #388 | await demoGraphMemory(); |
| #389 | } else { |
| #390 | console.log( |
| #391 | "\nSkipping Graph Memory test - Neo4j environment variables not set", |
| #392 | ); |
| #393 | } |
| #394 | |
| #395 | // Test PGVector store if environment variables are set |
| #396 | if (process.env.PGVECTOR_DB) { |
| #397 | await demoPGVector(); |
| #398 | } else { |
| #399 | console.log("\nSkipping PGVector test - environment variables not set"); |
| #400 | } |
| #401 | |
| #402 | // Test Qdrant store if environment variables are set |
| #403 | if ( |
| #404 | process.env.QDRANT_URL || |
| #405 | (process.env.QDRANT_HOST && process.env.QDRANT_PORT) |
| #406 | ) { |
| #407 | await demoQdrant(); |
| #408 | } else { |
| #409 | console.log("\nSkipping Qdrant test - environment variables not set"); |
| #410 | } |
| #411 | |
| #412 | // Test Redis store if environment variables are set |
| #413 | if (process.env.REDIS_URL) { |
| #414 | await demoRedis(); |
| #415 | } else { |
| #416 | console.log("\nSkipping Redis test - environment variables not set"); |
| #417 | } |
| #418 | } |
| #419 | |
| #420 | main(); |
| #421 |