0
0
Agentic AIml~5 mins

Long-term memory with vector stores in Agentic AI

Choose your learning style9 modes available
Introduction

Long-term memory with vector stores helps AI remember and find important information quickly by storing it as numbers.

You want an AI to recall past conversations or facts over time.
You need to search large amounts of text or data fast.
You want to build a chatbot that remembers user preferences.
You want to organize documents so AI can find related info easily.
You want to improve AI answers by using stored knowledge.
Syntax
Agentic AI
vector_store = VectorStore()
vector_store.add(data_vector, metadata)
results = vector_store.search(query_vector, top_k=5)

VectorStore() creates a place to save data as vectors (lists of numbers).

add() saves a vector with extra info (metadata).

Examples
Adds one vector and searches for the closest match.
Agentic AI
vector_store = VectorStore()
vector_store.add([0.1, 0.2, 0.3], {'text': 'Hello world'})
results = vector_store.search([0.1, 0.2, 0.3], top_k=1)
Adds many vectors in a loop and searches top 3 matches.
Agentic AI
vector_store = VectorStore()
for vec, meta in data:
    vector_store.add(vec, meta)
results = vector_store.search(query_vec, top_k=3)
Sample Model

This program creates a simple vector store that saves vectors and their text labels. It then searches for the two closest vectors to a query and prints their text and similarity scores.

Agentic AI
from sklearn.metrics.pairwise import cosine_similarity

class VectorStore:
    def __init__(self):
        self.vectors = []
        self.metadata = []

    def add(self, vector, meta):
        self.vectors.append(vector)
        self.metadata.append(meta)

    def search(self, query_vector, top_k=1):
        import numpy as np
        if not self.vectors:
            return []
        sims = cosine_similarity([query_vector], self.vectors)[0]
        top_indices = sims.argsort()[::-1][:top_k]
        return [(self.metadata[i], sims[i]) for i in top_indices]

# Create vector store
store = VectorStore()

# Add some vectors with text metadata
store.add([0.1, 0.2, 0.3], {'text': 'Hello world'})
store.add([0.4, 0.5, 0.6], {'text': 'Goodbye world'})
store.add([0.1, 0.0, 0.1], {'text': 'Hello again'})

# Search for vectors similar to query
query = [0.1, 0.2, 0.25]
results = store.search(query, top_k=2)

# Print results
for meta, score in results:
    print(f"Text: {meta['text']}, Similarity: {score:.3f}")
OutputSuccess
Important Notes

Vectors are lists of numbers that represent information in a way AI can understand.

Cosine similarity measures how close two vectors are, from -1 (opposite) to 1 (same).

Vector stores help AI remember and find info quickly without reading everything again.

Summary

Long-term memory with vector stores saves info as number lists for fast recall.

It is useful for chatbots, search, and remembering past data.

Adding and searching vectors uses similarity to find the best matches.