Wednesday, April 17, 2024

How to do RAG in OpenAI GPT4 Locally with File Search

 This video is a hands-on step-by-step primer about how to use RAG with Open AI File Search. OpenAI now supports RAG which means that now you can attach your own files and custom data to OpenAI assistant and talk to your documents with GPT4. 

Make sure you have installed latest version of openai on your local system:

pip install openai --upgrade

also make sure to have data.txt in the same folder as your script. 

from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
  name="Personal Assistant",
  instructions="You are an empathetic. Use you knowledge base to answer questions.",
  tools=[{"type": "file_search"}],

# Create a vector store caled "Personal Data"
vector_store = client.beta.vector_stores.create(
    name="Personal Data",
         "anchor": "last_active_at",
         "days": 1
# Ready the files for upload to OpenAI
file_paths = ["data.txt"]
file_streams = [open(path, "rb") for path in file_paths]
# Use the upload and poll SDK helper to upload the files, add them to the vector store,
# and poll the status of the file batch for completion.
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(, files=file_streams
# You can print the status and the file counts of the batch to see the result of this operation.

assistant = client.beta.assistants.update(,
  tool_resources={"file_search": {"vector_store_ids": []}},

thread = client.beta.threads.create(
  tool_resources={"file_search": {"vector_store_ids": []}},
      "role": "user",
      "content": "Who is Fahd Mirza?",
# The thread now has a vector store with that file in its tool resources.

run = client.beta.threads.runs.create_and_poll(,

messages = list(client.beta.threads.messages.list(,

message_content = messages[0].content[0].text
annotations = message_content.annotations
citations = []
for index, annotation in enumerate(annotations):
    message_content.value = message_content.value.replace(annotation.text, f"[{index}]")
    if file_citation := getattr(annotation, "file_citation", None):
        cited_file = client.files.retrieve(file_citation.file_id)
        citations.append(f"[{index}] {cited_file.filename}")


No comments: