【API调用gpt-4 (vision-preview)】基于微软的Azure OpenAI API

本文介绍了如何在Azure平台上使用OpenAI的GPT-4服务,通过Python代码解析本地图片,并生成相应的中文和英文描述。步骤包括安装OpenAI库,设置APIkey和端点,以及提供示例代码来展示GPT-4对图像的理解和生成能力。

微软的Azure页面 : https://learn.microsoft.com/zh-cn/azure/ai-services/openai/concepts/models
调用代码:https://learn.microsoft.com/zh-cn/azure/ai-services/openai/how-to/switching-endpoints
openai说明: https://platform.openai.com/docs/guides/vision

一、服务器区域选择与购买 (略)

不同区域的服务器开通不同模型 美国西部
在这里插入图片描述

二、上传本地图片解析

先安装openai

pip install -U openai

代码 + 自己api

api_key=“yourkey”
azure_endpoint=“xxxx/chat/completions?api-version=2023-07-01-preview”
api_version=“2023-12-01-preview”,


'''
https://platform.openai.com/docs/guides/vision
https://learn.microsoft.com/zh-cn/azure/ai-services/openai/concepts/models
https://learn.microsoft.com/zh-cn/azure/ai-services/openai/how-to/chatgpt?tabs=python&pivots=programming-language-chat-completions
https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/gpt-with-vision
'''

from openai import AzureOpenAI
api_key="yourkey"
import base64


azure_endpoint="xxxx/chat/completions?api-version=2023-07-01-preview"
client = AzureOpenAI(
    api_key=api_key,
    api_version="2023-12-01-preview",
    azure_endpoint=azure_endpoint
)

# 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')
  


def request_base64_gpt4(image_path):
  base64_image=encode_image(image_path)
  response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    messages=[
      {
        "role": "user",
        "content": [
          {"type": "text", "text": "这个是chibi的僵尸题材,生成prompt,以便用来进行text2img的模型训练,先输出中文描述,再输出对应的应为描述"},
          {
            "type": "image_url",
            "image_url": {
              "url": f"data:image/jpeg;base64,{base64_image}",
            },
          },
        ],
      }
    ],
    max_tokens=300,
  )
  print("response",response)
  print(response.choices[0])

if  __name__ == "__main__":
  request_base64_gpt4("test.png")

输入图片

在这里插入图片描述

返回值

这是一个以chibi风格画的僵尸题材插图。画面中的僵尸角色是一只卡通化的狐狸,它有着白紫相间的毛发,头上戴着一个大蝴蝶结,眼睛是闪亮的蓝色。它身穿一件粉蓝色的和服,和服上有粉色的花朵装饰。它的手臂下垂,手掌朝上,似乎在展示一个暗紫色的瓶子,瓶子上系着一个粉色的蝴蝶结。背景是深紫色,上方有一些红色的液体滴落

filter_results={‘hate’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘self_harm’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘sexual’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘violence’: {‘filtered’: False, ‘severity’: ‘safe’}})], created=1709793219, model=‘gpt-4’, object=‘chat.completion’, system_fingerprint=None, usage=CompletionUsage(completion_tokens=300, prompt_tokens=820,
total_tokens=1120), prompt_filter_results=[{‘prompt_index’: 0, ‘content_filter_results’: {‘hate’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘self_harm’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘sexual’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘violence’: {‘filtered’: False, ‘severity’: ‘safe’}}}])
Choice(finish_reason=‘length’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘中文描述:这是一个以chibi风格画的僵尸题材插图。画面中的僵尸角色是一只卡通化的狐狸,它有着白紫
相间的毛发,头上戴着一个大蝴蝶结,眼睛是闪亮的蓝色。它身穿一件粉蓝色的和服,和服上有粉色的花朵装饰。它的手臂下垂,手掌朝上,似乎在展示一个暗紫色的瓶子,瓶子上系着一个粉色的蝴蝶结。背景是深
紫色,上方有一些红色的液体滴落。\n\n英文描述:This is a chibi-style zombie-themed illustration. The zombie character in the picture is a cartoonized fox with white and purple fur and a big bow on its head, with shiny blue eyes. It is wearing a light blue kimono with pink flower decorations. Its arms are drooping, palms facing up, seemingly showing off a dark purple bottle tied with a pink bow. The background is dark’, role=‘assistant’, function_call=None, tool_calls=None), content_filter_results={‘hate’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘self_harm’:
{‘filtered’: False, ‘severity’: ‘safe’}, ‘sexual’: {‘filtered’: False, ‘severity’: ‘safe’}, ‘violence’: {‘filtered’: False, ‘severity’: ‘safe’}})

参考代码,GPT4识别图片,并中文回复

prompt=“What’s in this image? 并使用中文回答”
需要解析的远程图片
在这里插入图片描述

完整代码

from openai import AzureOpenAI
api_key="your_key"
azure_endpoint="your_model_url"
client = AzureOpenAI(
    api_key=api_key,
    api_version="2023-12-01-preview",
    azure_endpoint=azure_endpoint
)

response = client.chat.completions.create(
  model="gpt-4-vision-preview",
  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,
)

print(response.choices[0])

回应

这张图片是一个木制的步道穿过一片绿色的草地,远处有一些树木,天空是蓝色的,有一些白云。

Choice(finish_reason=None, index=0, logprobs=None, message=ChatCompletionMessage(
content='这张图片是一个木制的步道穿过一片绿色的草地,远处有一些树木,天空是蓝色的,有一些白云。', role='assistant', function_call=None, tool_calls=None), 
finish_details={'type': 'stop', 'stop': '<|fim_suffix|>'}, 
content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})

### GPT-4 Visual Preview 特性和功能 GPT-4 Turbo with Vision 是由 OpenAI 推出的一款多模态模型,能够处理图像输入并生成相应的文本回复[^2]。此版本支持对图片的理解与分析,并能基于视觉内容回答复杂问题。如果进一步启用[视觉增强]特性,则可扩展至视频素材的解析。 #### 功能概述 该模型不仅限于简单的对象识别,还具备深入场景解读的能力。例如,在科学研究领域中,它被测试用于解决涉及跨学科知识的问题。一项对比实验显示,当面对需要空气动力学专业知识的任务时,GPT-4V 和其他先进模型均给出了合理解释;而在另一项关于生物化学的任务里,尽管两者表现各有千秋,但 GPT-4V 展现出了较强的推测能力[^3]。 以下是几个具体的功能特点: 1. **图像问答** 用户上传一张照片后,可以就这张照片的内容提问,比如描述其中发生了什么或者指出特定细节。 2. **情景理解** 不仅局限于静态物体检测,还能捕捉动态事件及其背景意义,这有助于更全面地把握画面故事线。 3. **技术文档辅助阅读** 对含有图表或结构化信息的技术资料进行快速扫描总结,帮助技术人员高效获取关键要点。 ```python from openai import Client, ImageAnalysisRequest client = Client(api_key="your_api_key") def analyze_image(image_path): request = ImageAnalysisRequest(file=open(image_path, 'rb')) response = client.images.analyze(request) return response.text print(analyze_image('example.jpg')) ``` 上述代码片段展示了如何调用OpenAI API 来实现基本的图像分析功能。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾小蛙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值