ninja icon indicating copy to clipboard operation
ninja copied to clipboard

can Chat-to-api support image input

Open zwbx opened this issue 1 year ago • 5 comments

import base64
import requests

# OpenAI 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 = "00000000_img_front.png"

# Getting the base64 string
base64_image = encode_image(image_path)

headers = {
  "Content-Type": "application/json",
  "Authorization": f"Bearer {api_key}"
}

payload = {
  "model": "gpt-4-vision-preview",
  "messages": [
    {
      "role": "user",
      "content": 
      [
        {
          "type": "text",
          "text": "What’s in this image?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": f"data:image/jpeg;base64,{base64_image}"
          }
        }
      ]
    }
  ],
  "max_tokens": 300
}

response = requests.post("http://0.0.0.0:7999/v1/chat/completions", headers=headers, json=payload)

print(response.json())

Thanks for the great work, I have successfully deployed Chat-to-api. But it seems to encounter bugs when call Chat-to-api by the image input mode. Response is "{'code': '500', 'msg': 'invalid type: sequence, expected a string at line 1 column 75'}" Could that be supported?

zwbx avatar Nov 27 '23 03:11 zwbx