Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outdated sample code for Assistant #124

Open
RottenTangerine opened this issue Aug 2, 2024 · 2 comments
Open

Outdated sample code for Assistant #124

RottenTangerine opened this issue Aug 2, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@RottenTangerine
Copy link

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 RottenTangerine added the bug Something isn't working label Aug 2, 2024
@PurnaChandraPanda
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants