Skip to main content

File Search with GPT-4o

In this lesson, we’ll use the file_search tool to allow GPT-4o to pull information from a specific vector store. This is perfect for querying private documents securely and intelligently.

Prerequisites

You must:

  • Have uploaded documents to OpenAI's vector store.
  • Have a valid vector_store_id ready (e.g., from your project dashboard).

Install the OpenAI SDK:

pip install openai

The Code

from openai import OpenAI

client = OpenAI()

response = client.responses.create(
model="gpt-4o-mini",
input="What services Nexovious provides?",
tools=[{
"type": "file_search",
"vector_store_ids": ["vs_68328938caac819194fefb25d125d497"]
}]
)

# Extract and print only the assistant's message content
for item in response.output:
if item.type == "message":
for content_item in item.content:
if content_item.type == "output_text":
print(content_item.text)

Explanation

  • tools=[{"type": "file_search"}]: Activates retrieval-augmented generation (RAG) from vector stores.
  • vector_store_ids: List of vector store IDs to query.
  • response.output: Includes both assistant replies and tool calls.
  • content_item.text: Extracts only assistant-visible message content.

Use Case

Perfect for:

  • Internal knowledge base Q&A
  • Support chatbots using your company docs
  • Searching indexed manuals, PDFs, and policies with AI