A Document Searcher Using Version 2 of the GPT Assistant API Software

Below is a script I’ve created using version 2 of the GPT Assistant API software. It’s a document searcher. You can use either pdf or txt files with it.

I began this project by first studying and playing with the script submitted by @GEScott71, My very simple GPT Assistant Walk-Thru

I’ve used quite a bit of it in my own script. Thanks extremely much for your help, Scott.

I’d very much appreciate any suggestions for improvements to the script.

from openai import OpenAI
import time

client = OpenAI(api_key=<your api key>)

vector_store = client.beta.vector_stores.create(name="Founders Documents")
# Ready the files for upload to OpenAI
file_paths = ["/home/tom/InventoryManagementAPI/SelectedJefferson/Jefferson_0054-03_EBk_v6.0.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(
  vector_store_id=vector_store.id, files=file_streams
assistant = client.beta.assistants.create(
  instructions="You are a documents researcher.",
  name="Documents Assistant",
  tools=[{"type": "file_search"}],
  tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},


def run_assistant(message_body): # Create a message, run the assistant on it, monitor it for completion, and display the output
    # Create a message in an existing thread
    message = client.beta.threads.messages.create(
        thread_id = thread.id,

    # Run the existing assistant on the existing thread
    run = client.beta.threads.runs.create(

    # Monitor the assistant and report status
    while run.status != "completed":
        run = client.beta.threads.runs.retrieve(

    # Extract the messages from the thread
    messages = client.beta.threads.messages.list(

    # Display the output
    for message in reversed(messages.data):
        print(message.role + ": " + message.content[0].text.value)
    return messages

# ************************

thread = client.beta.threads.create() 

print("thread_id:",thread.id, "assistant_id:", assistant.id)

while True:
    user_input = input("Enter a question, or type 'exit' to end: ").strip().lower()
    if user_input == 'exit':

1 Like