How to build Perplexity-like search
Quick answer
Build a Perplexity-like search by combining
OpenAI embeddings to vectorize documents, a vector database like FAISS or Chroma for similarity search, and OpenAI chat completions to generate natural language answers from retrieved context. This retrieval-augmented generation (RAG) approach enables precise, conversational search results.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0 langchain-openai langchain-community faiss-cpu
Setup
Install required Python packages and set your OPENAI_API_KEY environment variable.
- Use
pip install openai langchain-openai langchain-community faiss-cpu - Export your API key:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) or set in your environment on Windows.
pip install openai langchain-openai langchain-community faiss-cpu output
Collecting openai Collecting langchain-openai Collecting langchain-community Collecting faiss-cpu Successfully installed openai langchain-openai langchain-community faiss-cpu
Step by step
This example loads documents, creates embeddings with OpenAIEmbeddings, indexes them in FAISS, performs similarity search, and uses OpenAI chat completions to answer queries based on retrieved context.
import os
from openai import OpenAI
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Sample documents to index
documents = [
"Python is a popular programming language.",
"OpenAI provides powerful AI models.",
"FAISS is a library for efficient similarity search.",
"Perplexity AI combines search with chat completions."
]
# Create embeddings for documents
embeddings = OpenAIEmbeddings(client=client, model="text-embedding-3-small")
# Build vector store index
vector_store = FAISS.from_texts(documents, embeddings)
# User query
query = "What is FAISS used for?"
# Embed query and retrieve top 2 similar docs
docs = vector_store.similarity_search(query, k=2)
# Prepare context from retrieved docs
context = "\n".join([doc.page_content for doc in docs])
# Define prompt template with context
prompt_template = ChatPromptTemplate.from_template(
"""
You are a helpful assistant. Use the following context to answer the question.
Context:\n{context}
Question: {question}
Answer:
"""
)
# Format prompt
messages = [
{"role": "user", "content": prompt_template.format(context=context, question=query)}
]
# Generate answer using chat completion
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
print("Answer:", response.choices[0].message.content) output
Answer: FAISS is a library for efficient similarity search, commonly used to quickly find similar vectors in large datasets.
Common variations
You can enhance your Perplexity-like search by:
- Using
Chromaor other vector stores instead ofFAISS. - Switching to a larger model like
gpt-4ofor more detailed answers. - Implementing async calls with
asyncioandawaitfor better performance. - Adding streaming responses for real-time answer generation.
import asyncio
from openai import OpenAI
async def async_search():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Async chat completion example
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=[{"role": "user", "content": "Explain vector search."}]
)
print("Async answer:", response.choices[0].message.content)
asyncio.run(async_search()) output
Async answer: Vector search is a technique to find similar items by comparing their vector embeddings, enabling fast and accurate retrieval in large datasets.
Troubleshooting
- If you get
InvalidRequestError, verify yourOPENAI_API_KEYis set correctly. - If similarity search returns no results, check that embeddings are generated and indexed properly.
- For slow responses, consider using smaller models or caching embeddings.
- Ensure your documents are clean and relevant for better retrieval quality.
Key Takeaways
- Use OpenAI embeddings with a vector store like FAISS to enable semantic search.
- Combine retrieved documents as context for chat completions to generate natural answers.
- Leverage LangChain's OpenAIEmbeddings and vectorstore integrations for rapid development.