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

微软的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'}})

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: GPT-3 API 是一种可以让你在程序中方便地使用 GPT-3 模型进行自然语言处理任务的接口。 它允许你通过简单的 API 调用来发送请求,获得 GPT-3 模型生成的文本。你可以通过 GPT-3 API 进行诸如文本生成、问答、翻译等多种自然语言处理任务。 使用 GPT-3 API 需要你先注册帐号,然后使用 API 密钥才能调用 API。你可以在官网(https://beta.openai.com/docs/quickstart/gpt3-api)上找到如何使用 GPT-3 API 的详细文档。 ### 回答2: GPT-3(Generative Pre-trained Transformer 3)是一种人工智能模型,具备极高的语言生成能力和智能对话能力。GPT-3的API接口调用可以帮助开发者将GPT-3集成到自己的应用程序中。 GPT-3 API接口调用的过程如下: 1. 注册:开发者需要在OpenAI官方网站上注册一个账号,并且申请API密钥。经过审核后,就可以获得访问GPT-3 API的权限。 2. 安装SDK:开发者可以根据自己的需求选择合适的SDK(软件开发工具包)来与GPT-3 API进行交互。目前,OpenAI官方支持多种编程语言的SDK,如Python、JavaScript等。 3. 配置环境:安装SDK后,需要根据官方文档的指示,进行相应的环境配置。这包括设置API密钥、安装相关依赖库等。 4. 创建API实例:在代码中,开发者需要创建一个GPT-3 API的实例对象,并通过传入API密钥等参数进行初始化。 5. 发起请求:调用实例对象的方法,向GPT-3 API发起请求。请求中包含输入的文本/问题等信息。 6. 处理响应:GPT-3 API会返回一个生成的文本作为响应。开发者可以对响应进行处理,提取所需信息等。 7. 优化和调试:在实际使用中,可能需要调整请求的参数、优化代码逻辑,以提升响应的质量和速度。 需要注意的是,GPT-3目前还处于测试阶段,OpenAI官方可能会对API进行调整和更新。因此,开发者应该及时关注官方文档和更新,以确保API调用的准确性和可用性。 总的来说,GPT-3 API接口调用是通过SDK与API进行交互,通过发送请求获取智能的语言生成和对话功能。这为开发者提供了一个强大的工具,可以应用于各种文本生成和自然语言处理的场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

曾小蛙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值