Skip to content

Instantly share code, notes, and snippets.

@quangnhat185
Last active July 10, 2024 19:09
Show Gist options
  • Save quangnhat185/e072df8a30109067ba6b84276bf9ce95 to your computer and use it in GitHub Desktop.
Save quangnhat185/e072df8a30109067ba6b84276bf9ce95 to your computer and use it in GitHub Desktop.
OpenAI API
from openai import OpenAI
import os
import ipdb
import base64
api_key = os.getenv('OPENAI_API_KEY')
client = OpenAI()
client.api_key = api_key
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Path to your image
image_path = "./dgh.jpeg"
# Getting the base64 string
base64_image = encode_image(image_path)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "There are A number of person and B number of animal in this image. What is A and B. Answer in this in json format"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
"detail": "low"
}
},
],
}
],
max_tokens=200,
)
response.model_dump().keys() # dict_keys(['id', 'choices', 'created', 'model', 'object', 'service_tier', 'system_fingerprint', 'usage'])
print(response.model_dump_json())
# Get content
print(response.choices[0].model_dump()['message']['content'])
from openai import OpenAI
import os
import ipdb
api_key = os.getenv('OPENAI_API_KEY')
client = OpenAI()
client.api_key = api_key
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,
)
response.model_dump().keys() # dict_keys(['id', 'choices', 'created', 'model', 'object', 'service_tier', 'system_fingerprint', 'usage'])
# Get content
print(response.choices[0].model_dump()['message']['content'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment