Vector Search
Find content by semantic meaning using vector similarity.
Vector search finds content by meaning rather than exact word matches. When you search for "How do I reset my password?", it finds documents about "changing credentials" even if those exact words don't appear.
1from kern.knowledge.knowledge import Knowledge2from kern.vectordb.pgvector import PgVector, SearchType34knowledge = Knowledge(5 vector_db=PgVector(6 table_name="docs",7 db_url=db_url,8 search_type=SearchType.vector,9 ),10)How It Works
- Query embedding: Your search query is converted to a vector (list of numbers capturing meaning)
- Similarity matching: The system finds stored vectors closest to the query vector
- Ranking: Results are ordered by cosine similarity (how close the meanings are)
The embedding model determines how well semantic relationships are captured. General-purpose models like OpenAI's text-embedding-3-small work well for most content.
When to Use Vector Search
| Scenario | Why Vector Search Works |
|---|---|
| Conceptual questions | Matches meaning, not just words |
| Users phrase things differently | Finds relevant content regardless of terminology |
| Natural language queries | Understands intent behind questions |
| Content with varied vocabulary | Connects synonyms and related concepts |
Use hybrid search if you also need exact term matching (product names, error codes). Use keyword search if you need precise text matching only.
Configuration
Basic Setup
1from kern.knowledge.embedder.openai import OpenAIEmbedder23vector_db = PgVector(4 table_name="docs",5 db_url=db_url,6 search_type=SearchType.vector,7 embedder=OpenAIEmbedder(id="text-embedding-3-small"),8)With Reranking
Add a reranker to improve result ordering:
1from kern.knowledge.reranker.cohere import CohereReranker23vector_db = PgVector(4 table_name="docs",5 db_url=db_url,6 search_type=SearchType.vector,7 reranker=CohereReranker(),8)Example
1from kern.knowledge.knowledge import Knowledge2from kern.vectordb.pgvector import PgVector, SearchType34db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"56knowledge = Knowledge(7 vector_db=PgVector(8 table_name="recipes",9 db_url=db_url,10 search_type=SearchType.vector,11 embedder=OpenAIEmbedder(id="text-embedding-3-small"),12 ),13)1415# Load content16knowledge.insert(17 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",18)1920# Search by semantic meaning21results = knowledge.search("chicken coconut soup", max_results=5)22for doc in results:23 print(doc.content[:200])