Uploading Files to a Vector Store
In this lesson, you'll learn how to create a new vector store, upload a file into it, and then query it using GPT-4o with the file_search tool.
Prerequisites
Make sure you have:
- A PDF file ready to upload (e.g.,
Nexovious Project Info.pdf) - OpenAI SDK installed
pip install openai
The Code
from openai import OpenAI
client = OpenAI()
file_path = "../files/Nexovious Project Info.pdf"
# 1. Create vector store
vector_store = client.vector_stores.create(
name="Nexovious File Store",
metadata={
"description": "Vector store for Nexovious project information",
"type": "product information"
}
)
print(f"Created vector store: {vector_store.id}")
# 2. Upload file content directly to vector store for indexing
with open(file_path, "rb") as f:
client.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id,
files=[f]
)
print("File uploaded and indexed in vector store.")
# 3. Query the vector store
response = client.responses.create(
model="gpt-4o-mini",
input="What services Nexovious provides?",
tools=[{
"type": "file_search",
"vector_store_ids": [vector_store.id],
"max_num_results": 1
}]
)
# 4. Extract assistant response text
for item in response.output:
if getattr(item, "type", None) == "message":
for content in item.content:
if getattr(content, "type", None) == "output_text":
print(content.text)
Explanation
client.vector_stores.create(...): Initializes a new vector store.upload_and_poll(...): Uploads and waits until the file is fully indexed.vector_store.id: Used for querying the indexed content.response.output: Contains the AI's grounded response.
Use Case
This setup is ideal for:
- Uploading internal knowledge bases
- Allowing GPT to answer questions from custom documents
- Integrating document search into your chat agents