I am using chatgpt assistant for pdf answering , but it is taking around 20 seconds for answering one question, but when i upload the same files on" create gpt" on gpt console it takes lesser time , why is that so?
Below is my piece of code:
assistant = client.beta.assistants.create(
name="DocAI",
instructions='''You are my assistant who can answer questions from multiple pdfs. Frame answers from the pdf provided to you. Always Give the name of files ids extracted answer from.
Answer questions related to data story from data storytelling pdf, Answer questions relate to work from netflix pdf and braking from Regenerative brakinf pdf.
''',
model="gpt-3.5-turbo",
tools=[{"type": "file_search"}],
)
vector_store = client.beta.vector_stores.create(name="Knowledge Store")
file_paths = ["/Users/aadyagaur/Documents/untitled folder 2/British_pound.pdf", "/Users/aadyagaur/Documents/untitled folder 2/Data storytelling_Roadmap.pdf", "/Users/aadyagaur/Documents/untitled folder 2/Netflix.pdf"]
file_streams = [open(path, "rb") for path in file_paths]
,
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id, files=file_streams
)
# You can print the status and the file counts of the batch to see the result of this operation.
print(file_batch.status)
print(file_batch.file_counts)
assistant = client.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)
# Upload the user provided file to OpenAI
# message_file = client.files.create(
# file=open("/Users/aadyagaur/Documents/untitled folder 2/Netflix.pdf", "rb"), purpose="assistants"
# )
# Create a thread and attach the file to the message
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": "Explain what is the state of UK economy",
# Attach the new file to the message.
# "attachments": [
# { "file_id": message_file.id, "tools": [{"type": "file_search"}] }
# ],
}
]
)
# The thread now has a vector store with that file in its tool resources.
print(thread)
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id, assistant_id=assistant.id
)
messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
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}")
end_time = time.time()
response_time = end_time - start_time
print(message_content.value)
print(f"Assistant response time: {response_time:.2f} seconds")