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
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: