Added support for Ollama in TS SDK (#2345)

Co-authored-by: Dev Khant <devkhant24@gmail.com>
This commit is contained in:
Gaurav Agerwala
2025-03-19 21:38:19 +05:30
committed by GitHub
co-authored by Dev Khant
parent 540ada489b
commit 2ffe9922f3
7 changed files with 299 additions and 7 deletions
+34 -1
View File
@@ -116,6 +116,36 @@ async function runTests(memory: Memory) {
}
}
async function demoLocalMemory() {
console.log("\n=== Testing In-Memory Vector Store with Ollama===\n");
const memory = new Memory({
version: "v1.1",
embedder: {
provider: "ollama",
config: {
model: "nomic-embed-text:latest",
},
},
vectorStore: {
provider: "memory",
config: {
collectionName: "memories",
dimension: 768, // 768 is the dimension of the nomic-embed-text model
},
},
llm: {
provider: "ollama",
config: {
model: "llama3.1:8b",
},
},
// historyDbPath: "memory.db",
});
await runTests(memory);
}
async function demoMemoryStore() {
console.log("\n=== Testing In-Memory Vector Store ===\n");
@@ -346,6 +376,9 @@ async function main() {
// Test in-memory store
await demoMemoryStore();
// Test in-memory store with Ollama
await demoLocalMemory();
// Test graph memory if Neo4j environment variables are set
if (
process.env.NEO4J_URL &&
@@ -384,4 +417,4 @@ async function main() {
}
}
// main();
main();
+93
View File
@@ -0,0 +1,93 @@
import { Memory } from "../src";
import { Ollama } from "ollama";
import * as readline from "readline";
const memory = new Memory({
embedder: {
provider: "ollama",
config: {
model: "nomic-embed-text:latest",
},
},
vectorStore: {
provider: "memory",
config: {
collectionName: "memories",
dimension: 768, // since we are using nomic-embed-text
},
},
llm: {
provider: "ollama",
config: {
model: "llama3.1:8b",
},
},
historyDbPath: "local-llms.db",
});
async function chatWithMemories(message: string, userId = "default_user") {
const relevantMemories = await memory.search(message, { userId: userId });
const memoriesStr = relevantMemories.results
.map((entry) => `- ${entry.memory}`)
.join("\n");
const systemPrompt = `You are a helpful AI. Answer the question based on query and memories.
User Memories:
${memoriesStr}`;
const messages = [
{ role: "system", content: systemPrompt },
{ role: "user", content: message },
];
const ollama = new Ollama();
const response = await ollama.chat({
model: "llama3.1:8b",
messages: messages,
});
const assistantResponse = response.message.content || "";
messages.push({ role: "assistant", content: assistantResponse });
await memory.add(messages, { userId: userId });
return assistantResponse;
}
async function main() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log("Chat with AI (type 'exit' to quit)");
const askQuestion = (): Promise<string> => {
return new Promise((resolve) => {
rl.question("You: ", (input) => {
resolve(input.trim());
});
});
};
try {
while (true) {
const userInput = await askQuestion();
if (userInput.toLowerCase() === "exit") {
console.log("Goodbye!");
rl.close();
break;
}
const response = await chatWithMemories(userInput, "sample_user");
console.log(`AI: ${response}`);
}
} catch (error) {
console.error("An error occurred:", error);
rl.close();
}
}
main().catch(console.error);