Hi.
I have an app that allows me to ask questions about some images to GPT. I made the feature some time ago so i decided to clean my code a little.
At the begining i made my comm interface with the request method.
# Load each image from the paths
images = []
for path in image_paths:
image = cv2.imread(path)
if image is None:
raise FileNotFoundError(f"Unable to read image from {path}")
images.append(image)
payload = compose_payload(images=images, prompt=prompt)
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
response = response.json()
Something like this. Basicly send a list of images and a question about the images and get the response.
I read about the assistant new feature and i have develop a simple class that allows me to do the same.
I’m not quite sure in the difference between this to methods to ask questions.
Whats the assistant for?
Maybe in my case there’s no difference at all or I’m missing something.
Could someone explain to me the difference between both methods to get a response.
This is the code that i use to communicate with the assistant method:
def analize_equipment(self,images_path : list[str], questions_list : list[str]) -> dict:
""" Analize an equipment based on the images an questions list"""
# Thread contnetn (CHAT GPT MESSAGES)
thread_content = []
# Generate a single prompt with all the questions
entire_prompt = "Answer this questions: \n"
for i,question in enumerate(questions_list):
entire_prompt += f"{i}. {question} \n"
# Add the prompt to the prompt content
thread_content.append({"type": "text","text": entire_prompt})
# Uplad and store the files ID to acces them in the prompt and after analsis remove them.
files_ids = []
for path in images_path:
# Upload file
file = self.__client.files.create(
file=open(path, "rb"),
purpose="vision"
)
print(f"File {file.id} uploaded")
# Store file ID
files_ids.append(file.id)
# Add the file to the prompt content
thread_content.append(
{
"type": "image_file",
"image_file": {"file_id": file.id, "detail": "high"}
})
# Crete a thread to make the questions
thread = self.__client.beta.threads.create(
messages=[
{
"role": "user",
"content": thread_content
}
]
)
# Crete the assistant that will analize and send the request
assistant = self.__create_assistant()
run = self.__client.beta.threads.runs.create_and_poll(
thread_id = thread.id,
assistant_id=assistant.id
)
# If the run is cumpleted show the results
if run.status == 'completed':
messages = self.__client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages.data[0].content[0].text.value)
else:
print(run.status)