How to add vector search to Semantic Kernel
Quick answer
Add vector search to
Semantic Kernel by integrating an embedding service like OpenAIEmbeddings and a vector store such as FAISS. Use langchain_community.vectorstores.FAISS with Semantic Kernel to perform semantic similarity search on documents or data.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install semantic-kernel openai langchain-openai langchain-community faiss-cpu
Setup
Install the required Python packages for Semantic Kernel, OpenAI embeddings, and FAISS vector store integration.
pip install semantic-kernel openai langchain-openai langchain-community faiss-cpu Step by step
This example shows how to create a Semantic Kernel instance, add OpenAIEmbeddings for vectorization, build a FAISS vector store, add documents, and perform a vector similarity search.
import os
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIEmbeddings
from langchain_openai import OpenAIEmbeddings as LcOpenAIEmbeddings
from langchain_community.vectorstores import FAISS
# Initialize Semantic Kernel
kernel = Kernel()
# Create OpenAI embeddings client
# Semantic Kernel expects an embeddings service, so we wrap LangChain's OpenAIEmbeddings
openai_api_key = os.environ["OPENAI_API_KEY"]
embeddings = OpenAIEmbeddings(
service_id="openai_embeddings",
api_key=openai_api_key,
ai_model_id="text-embedding-3-small"
)
# Sample documents to index
documents = [
{"id": "1", "text": "Semantic Kernel enables AI orchestration."},
{"id": "2", "text": "Vector search allows semantic similarity retrieval."},
{"id": "3", "text": "OpenAI provides powerful embedding models."}
]
# Generate embeddings for documents
texts = [doc["text"] for doc in documents]
vectors = [embeddings.get_embedding(text) for text in texts]
# Build FAISS index
index = FAISS.from_texts(texts, embeddings)
# Query vector search
query = "How to perform semantic search with AI?"
query_vector = embeddings.get_embedding(query)
# Search top 2 similar documents
results = index.similarity_search_by_vector(query_vector, k=2)
print("Top 2 similar documents:")
for res in results:
print(f"- {res.page_content}") output
Top 2 similar documents: - Semantic Kernel enables AI orchestration. - Vector search allows semantic similarity retrieval.
Common variations
- Use
gpt-4oor other embedding models by changingai_model_id. - Use async calls if your embedding client supports it.
- Replace
FAISSwith other vector stores likeChromaorWeaviatefor cloud-based vector search.
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If
FAISSinstallation fails, ensure you have the correct platform wheel or installfaiss-cpufor CPU-only support. - For embedding errors, confirm the model name
text-embedding-3-smallis supported and your API key has access.
Key Takeaways
- Use
OpenAIEmbeddingswithSemantic Kernelto generate vector representations. - Integrate
FAISSfromlangchain_community.vectorstoresfor efficient local vector search. - Ensure environment variables and dependencies are correctly configured for smooth embedding and search operations.