Last active
August 17, 2024 08:54
-
-
Save Terieyenike/a3849936279c06e1b5b3a0a9557c5769 to your computer and use it in GitHub Desktop.
Explore GPT-4o's Vision Capabilities
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 dotenv import load_dotenv | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| from openai import OpenAI | |
| client = OpenAI() | |
| model = 'gpt-4o' | |
| response = client.chat.completions.create( | |
| model = model, | |
| messages = [ | |
| {'role': 'system', 'content': 'You are a Sci-Fi writer'}, | |
| {'role': 'user', 'content': 'Write a short story about the life on Earth in the year 2120'} | |
| ] | |
| ) | |
| print(response.choices[0].message.content) | |
| # Using Local Base64 Images as Input | |
| import base64 | |
| import requests | |
| model = 'gpt-4o' | |
| system_message = 'You respond in english and then translate your response in dutch' | |
| prompt = 'Write a short and concise description of this image' | |
| image_url = 'https://images.pexels.com/photos/20659792/pexels-photo-20659792/free-photo-of-portrait-of-geese.jpeg' | |
| image_data = requests.get(image_url).content | |
| base64_image = base64.b64encode(image_data).decode('utf-8') | |
| 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