Can anyone help me with a problem when sending an image to vision models? Is there a specific configuration in the code for it to receive it in a specific format? Could someone help me and give me an example?
1 Like
Hi @deyvidhblack, Welcome to the community.
Can you please try this example code:
import openai
import base64
# 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')
client = openai.OpenAI(
api_key='Your_sambanova_api_key',
base_url="https://api.sambanova.ai/v1",
)
# Path to your image
image_path = "/Users/omkarg/Downloads/IMG_2117.jpg"
# Getting the base64 string
base64_image = encode_image(image_path)
response = client.chat.completions.create(
model="Llama-3.2-11B-Vision-Instruct",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
)
print(response.choices[0].message.content)
Please let us know if you face any errors.
Thanks & Regards