【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

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

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

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

猜你喜欢

转载自blog.csdn.net/imwaters/article/details/135347788