-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvercel_ai_sdk_example.ts
More file actions
97 lines (79 loc) · 2.38 KB
/
Copy pathvercel_ai_sdk_example.ts
File metadata and controls
97 lines (79 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Example: Vercel AI SDK + Mnemo persistent memory.
*
* Vercel AI SDK agents connect to Mnemo via the MnemoClient
* TypeScript SDK or directly via MCP.
*
* Requirements:
* npm install @mndfreek/mnemo-sdk
* # Or use MCP directly:
* npm install ai @ai-sdk/openai
* cargo build --release -p mnemo-cli
* export OPENAI_API_KEY=sk-...
*/
// === Option 1: Using Mnemo TypeScript SDK (REST API) ===
import { MnemoClient } from "@mndfreek/mnemo-sdk";
async function withMnemoSDK() {
const client = new MnemoClient({
command: "mnemo",
dbPath: "vercel_demo.db",
agentId: "vercel-agent",
});
await client.connect();
// Store knowledge
console.log("=== Store Knowledge ===");
const stored = await client.remember({
content: "The user Alice prefers TypeScript and Next.js",
tags: ["user-preference", "tech-stack"],
importance: 0.9,
});
console.log(`Stored: ${stored.id}`);
// Recall knowledge
console.log("\n=== Recall Knowledge ===");
const recalled = await client.recall({
query: "user tech preferences",
limit: 5,
});
for (const mem of recalled.memories) {
console.log(` [${mem.score.toFixed(2)}] ${mem.content}`);
}
// Forget
console.log("\n=== Forget ===");
const forgotten = await client.forget({
memoryIds: [stored.id],
});
console.log(`Forgotten: ${forgotten.forgotten}`);
await client.close();
}
// === Option 2: Using Vercel AI SDK with generateText ===
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
async function withGenerateText() {
const client = new MnemoClient({
command: "mnemo",
dbPath: "vercel_demo.db",
agentId: "vercel-agent",
});
await client.connect();
// Use Mnemo as context for AI generation
const memories = await client.recall({
query: "user preferences and background",
limit: 5,
});
const context = memories.memories
.map((m) => m.content)
.join("\n");
const result = await generateText({
model: openai("gpt-4o"),
system: `You are a helpful assistant. Here is what you know about the user:\n${context}`,
prompt: "Suggest a good project for me based on what you know.",
});
console.log(result.text);
// Store the suggestion in memory
await client.remember({
content: `Suggested project: ${result.text}`,
tags: ["suggestion"],
});
await client.close();
}
withMnemoSDK().catch(console.error);