Azure-Samples / azureai-samples

Official community-driven Azure AI Examples
MIT License
275 stars 182 forks source link

Outdated sample code for Assistant #124

Open RottenTangerine opened 3 months ago

RottenTangerine commented 3 months ago

Operating System

Windows

Version Information

python version: 3.10 openai package version: 1.36.0

Steps to reproduce

I tried to import the requirement libraries

from openai import AzureOpenAI
from openai.types.beta.threads.message_content_image_file import MessageContentImageFile
from openai.types.beta.threads.message_content_text import MessageContentText
from openai.types.beta.threads.messages import MessageFile
from PIL import Image

Expected behavior

No error while excuting

Actual behavior

No module found

Addition information

I try to downgrade the openai library version to 1.11.0, it works, but plz fix it in requirement.txt

RottenTangerine commented 3 months ago

Relevant file location: published documentation: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/assistants#see-also

Sample .ipynb: https://github.com/Azure-Samples/azureai-samples/tree/main/scenarios/Assistants/api-in-a-box/math_tutor Requiement.txt: https://github.com/Azure-Samples/azureai-samples/blob/main/scenarios/Assistants/api-in-a-box/requirements.txt

PurnaChandraPanda commented 3 months ago

@RottenTangerine

With latest openai sdk, following fix can manually be applied while repo is updated by owners.

from openai.types.beta.threads.message import Message
from openai.types.beta.threads.text_content_block import TextContentBlock
from openai.types.beta.threads.image_file_content_block import ImageFileContentBlock

Then, update format_message() to refer the new classes.

def format_messages(messages: Iterable[Message]) -> None:
    message_list = []

    # Get all the messages till the last user message
    for message in messages:
        message_list.append(message)
        if message.role == "user":
            break

    # Reverse the messages to show the last user message first
    message_list.reverse()

    # Print the user or Assistant messages or images
    for message in message_list:
        for item in message.content:
            # Determine the content type
            if isinstance(item, TextContentBlock):
                print(f"{message.role}:\n{item.text.value}\n")
            elif isinstance(item, ImageFileContentBlock):
                # Retrieve image from file id
                response_content = client.files.content(item.image_file.file_id)
                data_in_bytes = response_content.read()
                # Convert bytes to image
                readable_buffer = io.BytesIO(data_in_bytes)
                image = Image.open(readable_buffer)
                # Resize image to fit in terminal
                width, height = image.size
                image = image.resize((width // 2, height // 2), Image.LANCZOS)
                # Display image
                image.show()

With this much change, it worked in my end.