Open alex-deus opened 3 days ago
I came up with a solution, but it doesn’t work well for large JSON files (around 5MB), and sometimes for smaller simple file, it returns: {"n": "I couldn't find your given name in the provided documents."}
:-D
import json
import os
import time
from openai import OpenAI
TEMPERATURE = 0.2
def ask(client: OpenAI, assistant_id: str, thread_id: str, file_id: str, instruction: str) -> str:
client.beta.threads.messages.create(
thread_id=thread_id,
content=instruction,
role="user",
attachments=[{"file_id": file_id, "tools": [{"type": "file_search"}]}],
)
run = client.beta.threads.runs.create(
thread_id=thread_id,
model="gpt-4o",
tools=[{"type": "file_search"}],
assistant_id=assistant_id,
temperature=TEMPERATURE
)
count = 0
while True:
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
if run.status == "completed":
break
elif run.status == "failed":
raise Exception(f"{run.last_error.code}: {run.last_error.message}")
count += 1
if count > 30:
raise Exception("Too many requests")
else:
time.sleep(4)
answer = ""
messages = client.beta.threads.messages.list(thread_id=thread_id)
for message in messages.data:
if run.id != message.run_id:
continue
for content in message.content:
if content.type == "text":
answer = content.text.value
break
return answer
def main() -> None:
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
# Create assistant
assistant = client.beta.assistants.create(
description="Test",
instructions="Return all answer in JSON format",
model="gpt-4o",
tools=[{"type": "file_search"}],
temperature=TEMPERATURE
)
# Upload file
data = ("data.json", json.dumps({"given_name": "John", "family_name": "Smit"}).encode())
file = client.files.create(file=data, purpose="assistants")
thread = client.beta.threads.create() # Create thread
instruction = "What is my given_name? Answer format {'n': '<name>'}"
answer = ask(client, assistant.id, thread.id, file.id, instruction)
print(answer) # Sometimes answer could be {"n": "I couldn't find your given name in the provided documents."}
instruction = "What is my last family_name? Answer format {'l': '<last name>'}"
answer = ask(client, assistant.id, thread.id, file.id, instruction)
print(answer)
if __name__ == '__main__':
main()
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
I uploaded file to vector storage, but I couldn't use the file at threads:
It seems there was an error while trying to search the uploaded files. Could you please try uploading the file again, or let me know if there is a specific file you want me to look into?
.To Reproduce
Code snippets
OS
Linux
Python version
3.11.1
Library version
openai v1.51.0