How to Intermediate · 3 min read

How to use file search with OpenAI Assistants

Quick answer
Use OpenAI Assistants to perform file search by embedding file contents with OpenAIEmbeddings and indexing them in a vector store like FAISS. Query the vector store with user input, then pass the retrieved relevant file snippets to gpt-4o for context-aware answers.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai langchain langchain_openai langchain_community faiss-cpu

Setup

Install required packages and set your OpenAI API key as an environment variable.

  • Install packages: pip install openai langchain langchain_openai langchain_community faiss-cpu
  • Set environment variable: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
bash
pip install openai langchain langchain_openai langchain_community faiss-cpu

Step by step

This example loads text files, creates embeddings with OpenAIEmbeddings, indexes them in FAISS, and queries the index to find relevant file content. The retrieved content is then passed to gpt-4o for a contextual answer.

python
import os
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain_core.prompts import ChatPromptTemplate

# Load documents from files
loader = TextLoader("./documents/sample.txt")  # Replace with your file path
documents = loader.load()

# Create embeddings
embeddings = OpenAIEmbeddings(openai_api_key=os.environ["OPENAI_API_KEY"])

# Create FAISS vector store from documents
vectorstore = FAISS.from_documents(documents, embeddings)

# Query vector store
query = "Explain the main topic of the document."
relevant_docs = vectorstore.similarity_search(query, k=2)

# Prepare context from retrieved docs
context = "\n\n".join([doc.page_content for doc in relevant_docs])

# Setup OpenAI chat client
chat = ChatOpenAI(model_name="gpt-4o", openai_api_key=os.environ["OPENAI_API_KEY"])

# Create prompt with context
prompt_template = ChatPromptTemplate.from_template(
    "You are an assistant that answers questions based on the following document excerpts:\n{context}\n\nQuestion: {question}\nAnswer:")

prompt = prompt_template.format_prompt(context=context, question=query)

# Get completion
response = chat(prompt.to_messages())
print(response[0].content)
output
The main topic of the document is ... (based on your file content)

Common variations

  • Use async calls with asyncio and ChatOpenAI.acreate() for non-blocking queries.
  • Switch models by changing model_name to gpt-4o-mini or gemini-1.5-pro for cost or speed trade-offs.
  • Use other vector stores like Chroma or Weaviate for scalable file search.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If no relevant documents are found, increase k in similarity_search or check your document loading path.
  • For slow responses, consider using smaller models like gpt-4o-mini.

Key Takeaways

  • Use OpenAIEmbeddings with a vector store like FAISS to index file contents for search.
  • Query the vector store to retrieve relevant file snippets before passing context to gpt-4o for accurate answers.
  • Adjust model and vector store parameters to balance cost, speed, and accuracy for your file search use case.
Verified 2026-04 · gpt-4o, gpt-4o-mini, gemini-1.5-pro
Verify ↗