Thursday, July 18, 2024

Use GPT-4o Mini Locally with Text and Images

  This video introduces and shows how to use GPT-4o mini by OpenAI which is quite cost efficient and performant.


Code:

from openai import OpenAI
import base64
import requests
import os

## Set the API key and model name
MODEL="gpt-4o-mini"
os.environ.get('OPENAI_API_KEY')
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")

IMAGE_PATH="nm.png"
base64_image = encode_image(IMAGE_PATH)

response = client.chat.completions.create(
    model=MODEL,
    messages=[
        {"role": "system", "content": "You are a helpful assistant that responds in Markdown. Help me with this image!"},
        {"role": "user", "content": [
            {"type": "text", "text": "Describe the image? how many girls are there?"},
            {"type": "image_url", "image_url": {
                "url": f"data:image/png;base64,{base64_image}"}
            }
        ]}
    ],
    temperature=0.0,
)

print(response.choices[0].message.content)

-

#pip install -U openai
#export OPENAI_API_KEY=""

from openai import OpenAI
import os

## Set the API key and model name
MODEL="gpt-4o-mini"
os.environ.get('OPENAI_API_KEY')
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))

completion = client.chat.completions.create(
  model=MODEL,
  messages=[
    {"role": "system", "content": "You are a helpful assistant. Help me with my question!"},
    {"role": "user", "content": "A bat and a ball together cost $1.10. The bat costs $1.00 more than the ball. How much does the ball cost?"}  
  ]
)

print("Assistant: " + completion.choices[0].message.content)

No comments: