mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-18 23:36:00 +00:00
pkg(tools): Expose raw search results in MemoryPromptData for prompt templates (#787)
Expose raw search results in `MemoryPromptData` so prompt templates can traverse, filter, and selectively include results based on metadata (e.g. score, source).
##### Usage example
```typescript
const promptTemplate = (data: MemoryPromptData) => {
const relevant = data.searchResults.filter(
(r) => (r.metadata?.score as number) > 0.7
)
return `${data.userMemories}\n${relevant.map(r => r.memory).join('\n')}`
}
```
This commit is contained in:
parent
c534008001
commit
a00a751e10
8 changed files with 40 additions and 8 deletions
|
|
@ -154,6 +154,7 @@ The `MemoryPromptData` object passed to your template function provides:
|
|||
|
||||
- `userMemories`: Pre-formatted markdown combining static profile facts (name, preferences, goals) and dynamic context (current projects, recent interests)
|
||||
- `generalSearchMemories`: Pre-formatted search results based on semantic similarity to the current query (empty string if mode is "profile")
|
||||
- `searchResults`: Raw search results array (`Array<{ memory: string; metadata?: Record<string, unknown> }>`) for traversing, filtering, or selectively including results based on metadata
|
||||
|
||||
### XML-Based Prompting for Claude
|
||||
|
||||
|
|
@ -179,6 +180,31 @@ const model = withSupermemory(anthropic("claude-3-sonnet"), "user-123", {
|
|||
})
|
||||
```
|
||||
|
||||
### Filtering Search Results
|
||||
|
||||
Use `searchResults` to traverse the raw data and pick what's important:
|
||||
|
||||
```typescript
|
||||
const selectivePrompt = (data: MemoryPromptData) => {
|
||||
const relevant = data.searchResults.filter(
|
||||
(r) => (r.metadata?.score as number) > 0.7
|
||||
)
|
||||
return `
|
||||
<user_memories>
|
||||
${data.userMemories}
|
||||
</user_memories>
|
||||
<relevant_context>
|
||||
${relevant.map((r) => `- ${r.memory}`).join("\n")}
|
||||
</relevant_context>
|
||||
`.trim()
|
||||
}
|
||||
|
||||
const model = withSupermemory(openai("gpt-4"), "user-123", {
|
||||
mode: "full",
|
||||
promptTemplate: selectivePrompt
|
||||
})
|
||||
```
|
||||
|
||||
### Custom Branding
|
||||
|
||||
Remove "supermemories" references and use your own branding:
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ const model = withSupermemory(openai("gpt-4"), "user-123", { mode: "full" })
|
|||
|
||||
### Custom Prompt Templates
|
||||
|
||||
Customize how memories are formatted:
|
||||
Customize how memories are formatted. The template receives `userMemories`, `generalSearchMemories`, and `searchResults` (raw array for filtering by metadata):
|
||||
|
||||
```typescript
|
||||
import { withSupermemory, type MemoryPromptData } from "@supermemory/tools/ai-sdk"
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ await agent.generate("My favorite framework is Next.js")
|
|||
|
||||
## Custom Prompt Templates
|
||||
|
||||
Customize how memories are formatted and injected:
|
||||
Customize how memories are formatted and injected. The template receives `userMemories`, `generalSearchMemories`, and `searchResults` (raw array for filtering by metadata):
|
||||
|
||||
```typescript
|
||||
import { Agent } from "@mastra/core/agent"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue