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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
---
title: "Reranking"
description: "Improve result relevance with secondary ranking algorithms"
---
Reranking applies a secondary ranking algorithm to improve the relevance order of search results. After the initial search returns results, the reranker analyzes the relationship between your query and each result to provide better ordering.
## How Reranking Works
Supermemory's reranking process:
1. **Initial search** returns results using standard semantic similarity
2. **Reranker model** analyzes query-result pairs
3. **Scores are recalculated** based on deeper semantic understanding
4. **Results are reordered** by the new relevance scores
5. **Final results** maintain the same structure but with improved ordering
The reranker is particularly effective at:
- **Understanding context** and nuanced relationships
- **Handling ambiguous queries** with multiple possible meanings
- **Improving precision** for complex technical topics
- **Better ranking** when results have similar initial scores
## Basic Reranking Comparison
<Tabs>
<Tab title="TypeScript">
```typescript
import Supermemory from 'supermemory';
const client = new Supermemory({
apiKey: process.env.SUPERMEMORY_API_KEY!
});
// Search without reranking
const standardResults = await client.search.documents({
q: "neural network optimization techniques",
rerank: false,
limit: 5
});
// Search with reranking
const rerankedResults = await client.search.documents({
q: "neural network optimization techniques",
rerank: true,
limit: 5
});
console.log("Standard top result:", standardResults.results[0].score);
console.log("Reranked top result:", rerankedResults.results[0].score);
```
</Tab>
<Tab title="Python">
```python
from supermemory import Supermemory
import os
client = Supermemory(api_key=os.environ.get("SUPERMEMORY_API_KEY"))
# Search without reranking
standard_results = client.search.documents(
q="neural network optimization techniques",
rerank=False,
limit=5
)
# Search with reranking
reranked_results = client.search.documents(
q="neural network optimization techniques",
rerank=True,
limit=5
)
print("Standard top result:", standard_results.results[0].score)
print("Reranked top result:", reranked_results.results[0].score)
```
</Tab>
<Tab title="cURL">
```bash
# Without reranking
echo "Standard ranking:"
curl -X POST "https://api.supermemory.ai/v3/search" \
-H "Authorization: Bearer $SUPERMEMORY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "neural network optimization techniques",
"rerank": false,
"limit": 3
}' | jq '.results[0] | {title, score}'
# With reranking
echo "Reranked results:"
curl -X POST "https://api.supermemory.ai/v3/search" \
-H "Authorization: Bearer $SUPERMEMORY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "neural network optimization techniques",
"rerank": true,
"limit": 3
}' | jq '.results[0] | {title, score}'
```
</Tab>
</Tabs>
**Sample Output Comparison:**
```json
// Without reranking - results ordered by semantic similarity
{
"results": [
{
"title": "Deep Learning Optimization Methods",
"score": 0.82,
"chunks": [
{
"content": "Various optimization algorithms like Adam, RMSprop, and SGD are used in neural network training...",
"score": 0.79
}
]
},
{
"title": "Neural Network Training Techniques",
"score": 0.81,
"chunks": [
{
"content": "Batch normalization and dropout are common regularization techniques for neural networks...",
"score": 0.78
}
]
}
],
"timing": 145
}
// With reranking - results reordered by contextual relevance
{
"results": [
{
"title": "Neural Network Training Techniques",
"score": 0.89, // Boosted by reranker
"chunks": [
{
"content": "Batch normalization and dropout are common regularization techniques for neural networks...",
"score": 0.85
}
]
},
{
"title": "Deep Learning Optimization Methods",
"score": 0.86, // Slightly adjusted
"chunks": [
{
"content": "Various optimization algorithms like Adam, RMSprop, and SGD are used in neural network training...",
"score": 0.83
}
]
}
],
"timing": 267 // Additional ~120ms for reranking
}
```
## Complex Query Reranking
Reranking excels with complex, multi-faceted queries:
<Tabs>
<Tab title="TypeScript">
```typescript
const results = await client.search.documents({
q: "sustainable machine learning carbon footprint energy efficiency",
rerank: true,
containerTags: ["research", "sustainability"],
limit: 8
});
// Reranker understands the connection between:
// - Machine learning computational costs
// - Environmental impact of AI training
// - Energy-efficient model architectures
// - Green computing practices in ML
```
</Tab>
<Tab title="Python">
```python
results = client.search.documents(
q="sustainable machine learning carbon footprint energy efficiency",
rerank=True,
container_tags=["research", "sustainability"],
limit=8
)
# Reranker understands the connection between:
# - Machine learning computational costs
# - Environmental impact of AI training
# - Energy-efficient model architectures
# - Green computing practices in ML
```
</Tab>
<Tab title="cURL">
```bash
curl -X POST "https://api.supermemory.ai/v3/search" \
-H "Authorization: Bearer $SUPERMEMORY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "sustainable machine learning carbon footprint energy efficiency",
"rerank": true,
"containerTags": ["research", "sustainability"],
"limit": 8
}'
```
</Tab>
</Tabs>
**Sample Output:**
```json
{
"results": [
{
"documentId": "doc_green_ai",
"title": "Green AI: Reducing the Carbon Footprint of Machine Learning",
"score": 0.94, // Highly relevant after reranking
"chunks": [
{
"content": "Training large neural networks can consume as much energy as several cars over their lifetime. Sustainable ML practices focus on model efficiency, pruning, and quantization to reduce computational demands...",
"score": 0.92,
"isRelevant": true
}
]
},
{
"documentId": "doc_efficient_models",
"title": "Energy-Efficient Neural Network Architectures",
"score": 0.91, // Boosted for strong topical relevance
"chunks": [
{
"content": "MobileNets and EfficientNets are designed specifically for energy-constrained environments, achieving high accuracy with minimal computational overhead...",
"score": 0.88,
"isRelevant": true
}
]
}
],
"total": 12,
"timing": 298
}
```
## Memory Search Reranking
Reranking also improves memory search results:
<Tabs>
<Tab title="TypeScript">
```typescript
const memoryResults = await client.search.memories({
q: "explain transformer architecture attention mechanism",
rerank: true,
containerTag: "ai_notes",
threshold: 0.6,
limit: 5
});
// Reranker identifies memories that best explain
// the relationship between transformers and attention
```
</Tab>
<Tab title="Python">
```python
memory_results = client.search.memories(
q="explain transformer architecture attention mechanism",
rerank=True,
container_tag="ai_notes",
threshold=0.6,
limit=5
)
# Reranker identifies memories that best explain
# the relationship between transformers and attention
```
</Tab>
<Tab title="cURL">
```bash
curl -X POST "https://api.supermemory.ai/v4/search" \
-H "Authorization: Bearer $SUPERMEMORY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "explain transformer architecture attention mechanism",
"rerank": true,
"containerTag": "ai_notes",
"threshold": 0.6,
"limit": 5
}'
```
</Tab>
</Tabs>
**Sample Output:**
```json
{
"results": [
{
"id": "mem_transformer_intro",
"memory": "The transformer architecture revolutionized NLP by replacing recurrent layers with self-attention mechanisms. The attention mechanism allows the model to focus on different parts of the input sequence when processing each token, enabling parallel processing and better long-range dependency modeling.",
"similarity": 0.93, // Reranked higher for comprehensive explanation
"title": "Transformer Architecture Overview",
"metadata": {
"topic": "deep-learning",
"subtopic": "transformers"
}
},
{
"id": "mem_attention_detail",
"memory": "Self-attention computes attention weights by taking dot products between query, key, and value vectors derived from the input embeddings. This allows each position to attend to all positions in the previous layer, capturing complex relationships in the data.",
"similarity": 0.91, // Boosted for technical detail
"title": "Self-Attention Mechanism Details"
}
],
"total": 8,
"timing": 198
}
```
## Domain-Specific Reranking
Reranking understands domain-specific relationships:
<Tabs>
<Tab title="TypeScript">
```typescript
// Medical domain query
const medicalResults = await client.search.documents({
q: "diabetes treatment insulin resistance metformin",
rerank: true,
filters: {
AND: [
{ key: "domain", value: "medical", negate: false }
]
},
limit: 10
});
// Reranker understands medical relationships:
// - Diabetes types and treatments
// - Insulin resistance mechanisms
// - Metformin's role in diabetes management
```
</Tab>
<Tab title="Python">
```python
# Medical domain query
medical_results = client.search.documents(
q="diabetes treatment insulin resistance metformin",
rerank=True,
filters={
"AND": [
{"key": "domain", "value": "medical", "negate": False}
]
},
limit=10
)
# Reranker understands medical relationships:
# - Diabetes types and treatments
# - Insulin resistance mechanisms
# - Metformin's role in diabetes management
```
</Tab>
<Tab title="cURL">
```bash
curl -X POST "https://api.supermemory.ai/v3/search" \
-H "Authorization: Bearer $SUPERMEMORY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "diabetes treatment insulin resistance metformin",
"rerank": true,
"filters": {
"AND": [
{"key": "domain", "value": "medical", "negate": false}
]
},
"limit": 10
}'
```
</Tab>
</Tabs>
|