GPT 进行图像理解时候的token消耗2025-01-19

使用两种方法来对图像进行分析,第一种是使用在线的URL 第二种使用base64进行图像编码,将编码后的图像作为prompt上传,这两种形式的编码可以参考官方API的实例,
另外可以参考这个https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
经过测试这两种方法进行回答一个使用869 另外一个用了870 tokens基本相差不大。

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

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),  # This is the default and can be omitted
)

#==============方法一================   
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://s2.loli.net/2025/01/18/n9QFZSyjihrGsf5.jpg",
                    },
                },
            ],
        }
    ],
    max_tokens=300,
)

print(f'{response.usage.prompt_tokens} prompt tokens counted by the OpenAI API.')

print( response.choices[0].message.content+ "\n")




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

image_path = "E:\\dataset\\stage3\\3vqa\\images\\data_RAD\\synpic41119.jpg"

# 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": "What is in this image?",
                },
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
                },
            ],
        }
    ],
)

print(f'{response.usage.prompt_tokens} prompt tokens counted by the OpenAI API.')
print(response.choices[0].message.content+ "\n")

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。