当前位置: 代码网 > 科技>操作系统>Windows > 【API调用gpt-4 (vision-preview)】基于微软的Azure OpenAI API

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

2024年08月02日 Windows 我要评论
微软的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-endpointsopenai说明: https://platform.openai.com/docs/guides/vision不同区域的服务器开通不同模型

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

参考代码,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'}})

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com