#Assistants files

1 messages · Page 1 of 1 (latest)

steep hawkBOT
#

Hi @woven widget. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Sure, I can guide you through the process of making your assistant read your PDFs. Here are the steps:

  1. Upload the PDFs: You can upload your PDF files to OpenAI using the files.create method. This will allow your assistant to use these files as a knowledge base when answering questions. Here's an example of how to do this:
# Upload the file
file = client.files.create(
    file=open(
        "data/language_models_are_unsupervised_multitask_learners.pdf",
        "rb",
    ),
    purpose="assistants",
)

Source (https://cookbook.openai.com/examples/assistants_api_overview_python#retrieval)

  1. Update the Assistant: After uploading the files, you need to update your assistant to use these files. You can do this using the assistants.update method. Here's an example:
# Update Assistant
assistant = client.beta.assistants.update(
    MATH_ASSISTANT_ID,
    tools=[{"type": "code_interpreter"}, {"type": "retrieval"}],
    file_ids=[file.id],
)

Source (https://cookbook.openai.com/examples/assistants_api_overview_python#retrieval)

  1. Parse the PDFs: If your PDFs have complex formatting, you might need to parse them into a more readable format. You can use libraries like pdfminer or pdf2image to extract text or convert the PDF pages to images. Here's an example of how to do this:
# Imports
from pdf2image import convert_from_path
from pdfminer.high_level import extract_text

# File processing
def convert_doc_to_images(path):
    images = convert_from_path(path)
    return images

def extract_text_from_doc(path):
    text = extract_text(path)
    return text

Source (https://cookbook.openai.com/examples/parse_pdf_docs_for_rag#data-preparation)

#
  1. Use the Assistant: Now you can use your assistant to answer questions based on the content of the uploaded PDFs. Here's an example:
thread, run = create_thread_and_run(
    "What are some cool math concepts behind this ML paper pdf? Explain in two sentences."
)
run = wait_on_run(run, thread)

Source (https://cookbook.openai.com/examples/assistants_api_overview_python#retrieval)

Remember, the file parser works best with simple formatting. A single column of text is best. The parser can struggle with multi-column PDFs, and won’t understand the nuance conveyed by the relative positions of text on a PowerPoint slide. Source (https://help.openai.com/en/articles/8843948-knowledge-in-gpts#h_21e68a9515)

If you want to explore further, you can check out the Assistants Quickstart app (https://github.com/openai/openai-assistants-quickstart) on GitHub. Source (https://platform.openai.com/docs/assistants/quickstart)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@woven widget Kapa.ai is still learning and improving, please let me know how I did by reacting below.

woven widget
#

@steep hawk ok

steep hawkBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Sure, here's a full Python example of how to use PDFs with OpenAI Assistants:

import openai

# Initialize the OpenAI client
client = openai.OpenAI(api_key="your-api-key")

# Upload the PDF file
with open("your-file.pdf", "rb") as file:
    uploaded_file = client.files.create(file=file, purpose="assistants")

# Create or update the assistant with the uploaded file
assistant = client.assistants.create(
    model="gpt-4-1106-preview",
    instructions="You are an assistant that uses the uploaded PDFs to answer questions.",
    tools=[{"type": "retrieval"}],
    file_ids=[uploaded_file.id]
)

# Create a thread with a question
thread = client.threads.create(
    assistant_id=assistant.id,
    messages=[
        {
            "role": "user",
            "content": "What is the main topic of the uploaded PDF?"
        }
    ]
)

# Execute the run of the thread
run = client.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
)

# Wait for the run to complete and then retrieve the response
while run.status != "completed":
    time.sleep(1)
    run = client.threads.runs.retrieve(run.id)

# Print the assistant's response
print(run.outputs[0].message['content'])

Please replace "your-api-key" with your actual OpenAI API key and "your-file.pdf" with the path to your PDF file.

This script uploads a PDF file, creates an assistant that uses the uploaded file as a knowledge base, asks a question about the content of the PDF, and prints the assistant's response.

#

Please note that the assistant's ability to understand and answer questions about the content of the PDF depends on the complexity of the PDF's formatting and content. The assistant works best with simple, single-column text. It may struggle with multi-column PDFs or PDFs with complex layouts. Source (https://cookbook.openai.com/examples/assistants_api_overview_python#retrieval)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@woven widget Kapa.ai is still learning and improving, please let me know how I did by reacting below.

woven widget
#

@steep hawk going back to your first message, what if it is 2 files?

steep hawkBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

If you want to upload and use two files, you can simply upload each file separately and then include both file IDs when creating or updating the assistant. Here's how you can do it:

import openai

# Initialize the OpenAI client
client = openai.OpenAI(api_key="your-api-key")

# Upload the first PDF file
with open("your-first-file.pdf", "rb") as file:
    uploaded_file1 = client.files.create(file=file, purpose="assistants")

# Upload the second PDF file
with open("your-second-file.pdf", "rb") as file:
    uploaded_file2 = client.files.create(file=file, purpose="assistants")

# Create or update the assistant with the uploaded files
assistant = client.assistants.create(
    model="gpt-4-1106-preview",
    instructions="You are an assistant that uses the uploaded PDFs to answer questions.",
    tools=[{"type": "retrieval"}],
    file_ids=[uploaded_file1.id, uploaded_file2.id]
)

# Create a thread with a question
thread = client.threads.create(
    assistant_id=assistant.id,
    messages=[
        {
            "role": "user",
            "content": "What are the main topics of the uploaded PDFs?"
        }
    ]
)

# Execute the run of the thread
run = client.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
)

# Wait for the run to complete and then retrieve the response
while run.status != "completed":
    time.sleep(1)
    run = client.threads.runs.retrieve(run.id)

# Print the assistant's response
print(run.outputs[0].message['content'])

Please replace "your-api-key" with your actual OpenAI API key and "your-first-file.pdf" and "your-second-file.pdf" with the paths to your PDF files.

This script uploads two PDF files, creates an assistant that uses the uploaded files as a knowledge base, asks a question about the content of the PDFs, and prints the assistant's response.

#

Please note that the assistant's ability to understand and answer questions about the content of the PDFs depends on the complexity of the PDFs' formatting and content. The assistant works best with simple, single-column text. It may struggle with multi-column PDFs or PDFs with complex layouts. Source (https://cookbook.openai.com/examples/assistants_api_overview_python#retrieval)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: