Skip to content

Instantly share code, notes, and snippets.

@Terieyenike
Last active August 17, 2024 08:54
Show Gist options
  • Select an option

  • Save Terieyenike/a3849936279c06e1b5b3a0a9557c5769 to your computer and use it in GitHub Desktop.

Select an option

Save Terieyenike/a3849936279c06e1b5b3a0a9557c5769 to your computer and use it in GitHub Desktop.
Explore GPT-4o's Vision Capabilities
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