Vector DB for Agent Memory
Vector databases enable semantic search over agent memory.
Task
Implement SimpleVectorDB that:
- Embeds text using the provided character-frequency method.
- Stores embeddings with their source text.
- Searches by cosine similarity and returns
top_kresults.
Constraints
- Cosine similarity:
dot(a,b) / (|a| * |b|) - Return results sorted by descending similarity.
- Handle empty DB gracefully (return empty list).
Examples
Example 1:
Input:
db = SimpleVectorDB()
db.add('cat and dog')
db.add('financial markets')
db.search('pets and animals', top_k=1)Output:
['cat and dog']Explanation: Cat/dog text is more semantically similar to 'pets and animals'.
Starter Code
from typing import List, Tuple
import math
class SimpleVectorDB:
def __init__(self):
self.store: List[Tuple[List[float], str]] = []
def embed(self, text: str) -> List[float]:
# Simple character-frequency embedding (for testing only)
vec = [0.0] * 26
for c in text.lower():
if c.isalpha():
vec[ord(c) - ord('a')] += 1.0
norm = math.sqrt(sum(x**2 for x in vec)) or 1
return [x / norm for x in vec]
def add(self, text: str) -> None:
# TODO: Embed and store text
pass
def search(self, query: str, top_k: int = 3) -> List[str]:
# TODO: Return top_k most similar texts by cosine similarity
pass
Python3
ReadyLines: 1Characters: 0
Ready