Created
August 17, 2024 08:55
-
-
Save Terieyenike/daca29cd117a96c8834847d9083efca4 to your computer and use it in GitHub Desktop.
Local Base64 Images as Input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from IPython.display import Image, display | |
| image_path = 'images/triangle2.jpg' | |
| display(Image(image_path, width=400)) | |
| import base64 | |
| def encode_image(image_path): | |
| with open(image_path, 'rb') as image_file: | |
| image_binary_data = image_file.read() | |
| return base64.b64encode(image_binary_data).decode('utf-8') | |
| base64_image = encode_image(image_path) | |
| system_message = 'You are a math tutor' | |
| # prompt = 'What is the perimeter of the triangle?' | |
| prompt = 'Solve this problem.' | |
| model = 'gpt-4o' | |
| response = client.chat.completions.create( | |
| model = model, | |
| messages = [ | |
| {'role': 'system', 'content': system_message}, | |
| {'role': 'user', 'content': [ | |
| {'type': 'text', 'text': prompt}, | |
| {'type': 'image_url', 'image_url': { | |
| 'url': f'data:image/jpg;base64,{base64_image}'} | |
| } | |
| ]} | |
| ], | |
| temperature = 0.0 | |
| ) | |
| print(response.choices[0].message.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment