Image handling with base64 url encoded images

I am trying to use

def ask_with_image(self,
        prompt_text: str,
        image_data: str,
        model: str = "gpt-4-vision",
        temperature: float = 0.7,
        max_tokens: int = 300) -> str:
        """
        Ask a question about an image

        Args:
            prompt_text (str): The text of the prompt to send to the model
            image_data (str): Base64 encoded image data
            model (str): The model to use
            temperature (float): Controls randomness in the response
            max_tokens (int): Maximum number of tokens for the response

        Returns:
            str: The model's response
        """
        messages = [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": prompt_text},
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{image_data}",
                            "detail": "auto"
                        }
                    }
                ]
            }
        ]

according to https://platform.openai.com/docs/guides/vision
is should be able to handle images up to 20 MB in size. But the url content is somehow counted and even with very small images Debug 256x133 size:61277 base64: 81704 image saved to: /tmp/resized_001.jpg_256x133.png i get openai.BadRequestError: Error code: 400 - {‘error’: {‘message’: “This model’s maximum context length is 128000 tokens. However, you requested 128459 tokens (459 in the messages, 128000 in the completion). Please reduce the length of the messages or completion.”, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages’, ‘code’: ‘context_length_exceeded’}}

2 Likes

I think instead of the function default, you are trying to use a max_tokens parameter of 128000. Try 2000 instead.

Also, look here for real AI model names.

https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4

2 Likes

thx. Indeed i only had to fix the max_tokens parameter

1 Like