Code Extender | Excel

ExtendedOpenAI Chat with Image

This code extends the OpenAI class to include a method for chatting with an AI model using text and an image URL, and provides a unit test for the method.


Empty image or helper icon

This Query related with thread "Advanced OpenAI Chat Integration & Code Analysis"

Prompt

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What’s in this image?"},
        {
          "type": "image_url",
          "image_url": {
            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
          },
        },
      ],
    }
  ],
  max_tokens=300,
)

Answer

class ExtendedOpenAI(OpenAI):
    def chat_with_image(self, model, user_message, image_url, max_tokens=300):
        message = {
            "role": "user",
            "content": [
                {"type": "text", "text": user_message},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": image_url,
                    },
                },
            ],
        }
        
        response = self.chat.completions.create(
            model=model,
            messages=[message],
            max_tokens=max_tokens
        )
        
        return response

client = ExtendedOpenAI()

response = client.chat_with_image(
    model="gpt-4o",
    user_message="What’s in this image?",
    image_url="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
    max_tokens=300
)

Unit Testing:

import unittest

class TestExtendedOpenAI(unittest.TestCase):
    def test_chat_with_image(self):
        extended_client = ExtendedOpenAI()
        model = "gpt-4o"
        user_message = "What’s in this image?"
        image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
        max_tokens = 300
        
        response = extended_client.chat_with_image(model, user_message, image_url, max_tokens)
        
        self.assertIsNotNone(response)
        # Add more specific assertion based on the expected structure/content of the response
        # self.assertEqual(response["field"], expected_value)

if __name__ == '__main__':
    unittest.main()

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This code extends the OpenAI class to include a method for chatting with an AI model using text and an image URL, and provides a unit test for the method.