Hello everyone,
I’m developing a chatbot. Currently, I provide it with scanned documents in PDF format, but it’s not able to read them—it responds with irrelevant content.
The documents I mention were originally created in Word, then printed and later scanned; they do not contain images (they are just scanned pages with printed text).
I’m using the GPT-4o model (with fine-tuning), o1, and o3-mini.
I’m sharing parts of the code so you can tell me if something is wrong.
def upload_files_to_openai(self, files):
uploaded_ids = []
for upload in files:
try:
upload.file.seek(0)
file_tupla = (
upload.filename,
upload.file,
upload.content_type,
)
message_file = self.openai_handler.client.files.create(
file=file_tupla,
purpose='user_data'
)
uploaded_ids.append(message_file.id)
except Exception as e:
print(f"Error uploading the file: {e}")
return uploaded_ids
def send_message(self, thread_id: str, prompt: str, token: Optional[str] = None,
files_ids: Optional[List[str]] = None,
role: str = "user"
) -> Optional[str]:
'''
Sends a message to a thread in OpenAI.
'''
try:
attachments = []
if files_ids:
attachments = [{"file_id": fid, "tools": [{"type": "file_search"}]} for fid in files_ids]
logging.info(f"Sending message to thread {thread_id} in OpenAI...")
message = self.client.beta.threads.messages.create(
thread_id=thread_id,
role=role,
content=prompt,
attachments=attachments,
)
return message.id
except Exception as e:
logging.error(f"Error sending message to OpenAI: {e}")
return self.handle_error(e)
async def create_run(self, thread_id: str, assistant_id: str, model: str,
instructions: str = None):
'''
Creates a run in a thread in OpenAI.
'''
if model == "ft:gpt-4o-2024-08-06:fine:tuning":
reasoning = None
elif model == "o1":
reasoning = "low"
else:
reasoning = "high"
try:
logging.info(f"Creating run in thread {thread_id} in OpenAI...")
buffer = []
in_citation = False
citation_buffer = []
with self.client.beta.threads.runs.stream(
thread_id=thread_id,
assistant_id=assistant_id,
additional_instructions=instructions,
tool_choice={"type": "file_search"},
model=model,
reasoning_effort=reasoning,
tools=[{
"type": "file_search",
"file_search": {"max_num_results": 50}
}],
event_handler=AssistantEventHandler(),
) as stream:
for chunk in stream:
text = ""
if hasattr(chunk, "data") and hasattr(chunk.data, "delta"):
delta = chunk.data.delta
if hasattr(delta, "content") and delta.content:
for block in delta.content:
if hasattr(block, "text") and hasattr(block.text, "value"):
text += block.text.value
for char in text:
if not in_citation:
if char == "":
in_citation = True
citation_buffer.append(char)
else:
buffer.append(char)
else:
citation_buffer.append(char)
if char == "":
citation_buffer = []
in_citation = False
if not in_citation and buffer:
cleaned = ''.join(buffer)
yield cleaned
buffer = []
if buffer:
yield ''.join(buffer)
logging.info("Run created successfully.")
except Exception as e:
print(f"Error creating the run in OpenAI: {e}")
logging.error(f"Error creating the run in OpenAI: {e}")
yield self.handle_error(e)