Building public GPTs for own PDFs

451 views Asked by At

I tried creating a GPT on OpenAI by uploading a PDF. It worked well from the UI and was able to answer questions. But when I sent the link to some one they need to be ChatGPT plus users to use it. So I tried using an API and linking assistant with ChatCompletion end point but that kept on giving me errors. I also tried passing file id to chatCompletion but that did not work. Below are relevant code snippets - please guide on suitable approach.

file = client.files.create(
  file=open("mydoc.pdf", "rb"),
  purpose='assistants'
)

assistant = client.beta.assistants.create(
  instructions="You will answer question on the pdf document that I have uploaded. ... ",
  model="gpt-4-1106-preview",
  tools=[{"type": "retrieval"}],
  file_ids=[file.id]
)

while True:
    user_input = input("You: ") #followed by exit code


#using assitant with chat. Commented this to use file id with chatCompletion
'''
response = client.assistants.chat(
        assistant_id=assistant_id,
        messages=[{"role": "user", "content": user_input}]
    )
'''
#using file id with ChatCompletion
response = client.ChatCompletion.create(
        model="gpt-4-1106-preview", 
        messages=[
            {"role": "system", "content": "You will answer question on the pdf document that I have uploaded. ..."},
            {"role": "user", "content": user_input}
        ],
        file=file.id
    )

Regards dbeings

1

There are 1 answers

0
dbeings On

This got sorted. I mixed up the chatCompletion and Assistants API as I was following chatGPT and not API docs :) The API docs provide details on assistants usage but are not clear extracting the end message post thread execution. Below is the code which worked:

import openai

api_key = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = openai.Client(api_key=api_key)

# Upload a file with an "assistants" purpose
file = client.files.create(
  file=open("mydoc.pdf", "rb"),
  purpose='assistants'
)

# Add the file to the assistant
assistant = client.beta.assistants.create(
  instructions="You will answer question on the pdf document that I have uploaded.....",
  model="gpt-4-1106-preview",
  tools=[{"type": "retrieval"}],
  file_ids=[file.id]
)

assistant_id = assistant.id

while True:
    # Get user input
    user_input = input("You: ")

    # Break the loop if the user types 'exit'
    if user_input.lower() == 'exit':
        break
    
    thread = client.beta.threads.create(
        messages=[
            {
                "role": "user",
                "content": user_input,
                "file_ids": [file.id]
            }
        ]
    )

    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id
    )
    
    while run.status != "completed":
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id
        )
    
    print("Searching...")
    messages = client.beta.threads.messages.list(
        thread_id=thread.id
    )

    message_content = messages.data[0].content[0].text.value
    
    print("Assistant:", message_content)