分享方式:


使用包含視覺功能的 GPT-4 Turbo

包含視覺功能的 GPT-4 Turbo 是由 OpenAI 開發的大型多模態模型 (LMM),可分析影像並針對影像問題提供文字回應。 它同時包含自然語言處理和視覺理解。

包含視覺功能的 GPT-4 Turbo 模型可回答影像中呈現的一般問題。 如果您使用視覺增強功能,您也可以向它顯示影片。

提示

若要使用包含視覺功能的 GPT-4 Turbo,請在已部署之包含視覺功能的 GPT-4 Turbo 模型上呼叫聊天完成 API。 如果您不熟悉聊天完成 API,請參閱 GPT-4 Turbo & GPT-4 操作指南

GPT-4 Turbo 模型升級

GPT-4 Turbo 的最新 GA 版本為:

  • gpt-4 版本:turbo-2024-04-09

這是下列預覽模型的取代:

  • gpt-4 版本:1106-Preview
  • gpt-4 版本:0125-Preview
  • gpt-4 版本:vision-preview

OpenAI 與 Azure OpenAI GPT-4 Turbo GA 模型之間的差異

  • OpenAI 的最新 0409 turbo 模型版本支援所有推斷要求的 JSON 模式和函式呼叫。
  • Azure OpenAI 的最新 turbo-2024-04-09 版本目前不支援以影像 (視覺) 輸入進行推斷要求時使用 JSON 模式和函式呼叫。 以文字為基礎的輸入要求 (沒有 image_url 和內嵌影像的要求) 支援 JSON 模式和函式呼叫。

GPT-4 視覺預覽的差異

  • Azure AI 特定的視覺增強功能與含有視覺功能的 GPT-4 Turbo 整合,不支援 gpt-4 版本: turbo-2024-04-09。 這包括光學字元辨識 (OCR)、物件基礎、影片提示,以及使用影像改善資料的處理。

GPT-4 Turbo 佈建的受控可用性

  • gpt-4 版本:turbo-2024-04-09 可用於標準和佈建的部署。 目前此模型的佈建版本不支援影像/視覺推斷要求。 此模型的佈建部署只接受文字輸入。 標準模型部署同時接受文字和影像/視覺推斷要求。

區域可用性

如需模型區域可用性的資訊,請參閱適用於標準的模型矩陣,以及佈建的部署

部署包含視覺功能的 GPT-4 Turbo GA

若要從 Studio UI 部署 GA 模型,請選取 [GPT-4],然後從下拉式功能表中選擇 turbo-2024-04-09 版本。 gpt-4-turbo-2024-04-09 模型的預設配額會與 GPT-4-Turbo 目前的配額相同。 請參閱區域配額限制。

呼叫聊天完成 API

下列命令顯示利用程式碼來使用包含視覺功能之 GPT-4 Turbo 模型的最基本方式。 如果這是您第一次以程式設計方式使用這些模型,建議您從包含視覺功能的 GPT-4 Turbo 快速入門開始。

請傳送 POST 要求至 https://{RESOURCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}/chat/completions?api-version=2024-02-15-preview,其中

  • RESOURCE_NAME 是您 Azure OpenAI 資源的名稱
  • DEPLOYMENT_NAME 是包含視覺功能的 GPT-4 Turbo 模型的部署名稱

必要標頭:

  • Content-Type: application/json
  • api-key: {API_KEY}

本文: 下列是範例要求本文。 格式與 GPT-4 的聊天完成 API 相同,不同之處在於訊息內容可以是包含文字和影像的陣列 (不論是影像的有效 HTTP 或 HTTPS URL,或是 base-64 編碼的影像皆可)。

重要

請記得設定 "max_tokens" 值,否則傳回輸出會被切斷。

重要

上傳影像時,每個聊天要求以 10 個影像為限。

{
    "messages": [ 
        {
            "role": "system", 
            "content": "You are a helpful assistant." 
        },
        {
            "role": "user", 
            "content": [
	            {
	                "type": "text",
	                "text": "Describe this picture:"
	            },
	            {
	                "type": "image_url",
	                "image_url": {
                        "url": "<image URL>"
                    }
                } 
           ] 
        }
    ],
    "max_tokens": 100, 
    "stream": false 
} 

提示

使用本機影像

如果您想要使用本機影像,您可以使用下列 Python 程式碼將它轉換成 base64,以便將其傳遞至 API。 替代檔案轉換工具可在線上取得。

import base64
from mimetypes import guess_type

# Function to encode a local image into data URL 
def local_image_to_data_url(image_path):
    # Guess the MIME type of the image based on the file extension
    mime_type, _ = guess_type(image_path)
    if mime_type is None:
        mime_type = 'application/octet-stream'  # Default MIME type if none is found

    # Read and encode the image file
    with open(image_path, "rb") as image_file:
        base64_encoded_data = base64.b64encode(image_file.read()).decode('utf-8')

    # Construct the data URL
    return f"data:{mime_type};base64,{base64_encoded_data}"

# Example usage
image_path = '<path_to_image>'
data_url = local_image_to_data_url(image_path)
print("Data URL:", data_url)

當您的 base64 影像資料準備就緒時,您可以將它傳遞給要求本文中的 API,如下所示:

...
"type": "image_url",
"image_url": {
   "url": "data:image/jpeg;base64,<your_image_data>"
}
...

輸出

API 回應應該看起來如下所示。

{
    "id": "chatcmpl-8VAVx58veW9RCm5K1ttmxU6Cm4XDX",
    "object": "chat.completion",
    "created": 1702439277,
    "model": "gpt-4",
    "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"
                }
            }
        }
    ],
    "choices": [
        {
            "finish_reason":"stop",
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "The picture shows an individual dressed in formal attire, which includes a black tuxedo with a black bow tie. There is an American flag on the left lapel of the individual's jacket. The background is predominantly blue with white text that reads \"THE KENNEDY PROFILE IN COURAGE AWARD\" and there are also visible elements of the flag of the United States placed behind the individual."
            },
            "content_filter_results": {
                "hate": {
                    "filtered": false,
                    "severity": "safe"
                },
                "self_harm": {
                    "filtered": false,
                    "severity": "safe"
                },
                "sexual": {
                    "filtered": false,
                    "severity": "safe"
                },
                "violence": {
                    "filtered": false,
                    "severity": "safe"
                }
            }
        }
    ],
    "usage": {
        "prompt_tokens": 1156,
        "completion_tokens": 80,
        "total_tokens": 1236
    }
}

每個回應都包含 "finish_details" 欄位。 可能有以下的值:

  • stop: API 傳回的完整模型輸出。
  • length: 因為 max_tokens 輸入參數或模型的權杖限制,所以模型輸出不完整。
  • content_filter: 由於內容篩選中的旗標而省略了內容。

影像處理中的詳細資料參數設定:低、高、自動

模型中的詳細資料參數提供三個選項: lowhighauto,以調整模型解譯和處理影像的方式。 預設設定為自動,其中模型會根據影像輸入的大小來決定低或高。

  • low 設定: 模型不會啟用「high res」模式,而是處理解析度較低的 512x512 版本,進而產生更快速的回應,並在詳細資料不重要的案例中降低權杖的使用量。
  • high 設定: 模型會啟動「high res」模式。 在此,模型一開始會檢視低解析度影像,然後從輸入影像產生詳細的 512x512 區段。 每個區段都會使用兩倍的權杖預算,以更詳細的解譯影像。

如需影像參數如何影響使用的權杖和定價的詳細資料,請參閱 - 什麼是 OpenAI?影像權杖搭配包含視覺功能的 GPT-4 Turbo

透過影像使用視覺增強功能

包含視覺功能的 GPT-4 Turbo 提供對 Azure AI 服務量身打造之增強功能的專屬存取權。 與 Azure AI 視覺結合時,它會提供聊天模型有關影像中可見文字和物件位置的更詳細資訊,以增強您的聊天體驗。

光學字元辨識 (OCR) 整合讓模型可以為密集文字、轉換的影像和大量數字的財務文件產生更高品質的回應。 它也涵蓋更廣泛的語言。

物件基礎整合為資料分析和使用者互動帶來了新的層面,因為此功能可以在視覺上區分和醒目提示它所處理影像中的重要元素。

重要

若要透過 Azure OpenAI 資源使用視覺增強功能,您必須指定電腦視覺資源。 它必須位於付費 (S1) 階層,且位於與包含視覺功能之 GPT-4 Turbo 資源相同的 Azure 區域中。 如果您使用 Azure AI 服務資源,則不需要額外的電腦視覺資源。

警告

適用於包含視覺功能之 GPT-4 Turbo 的 Azure AI 增強功能將會與核心功能分開計費。 每個適用於包含視覺功能之 GPT-4 Turbo 的特定 Azure AI 增強功能都有不同的費用。 如需詳細資訊,請參閱特殊價格資訊

請傳送 POST 要求至 https://{RESOURCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}/chat/completions?api-version=2024-02-15-preview,其中

  • RESOURCE_NAME 是您 Azure OpenAI 資源的名稱
  • DEPLOYMENT_NAME 是包含視覺功能的 GPT-4 Turbo 模型的部署名稱

必要標頭:

  • Content-Type: application/json
  • api-key: {API_KEY}

本文

格式與 GPT-4 的聊天完成 API 相同,不同之處在於訊息內容可以是包含字串和影像的陣列 (不論是影像的有效 HTTP 或 HTTPS URL,或是 base-64 編碼的影像皆可)。

您也必須包含 enhancementsdataSources 物件。 enhancements 代表聊天中要求的特定視覺增強功能。 它有 groundingocr 屬性,兩者都有布林值 enabled 屬性。 使用這些來要求 OCR 服務和/或物件偵測/基礎服務。 dataSources 代表視覺增強功能所需的電腦視覺資源資料。 它有 type 屬性,其應為 "AzureComputerVision"parameters 屬性。 將 endpointkey 設定為電腦視覺資源的端點 URL 和存取金鑰。

重要

請記得設定 "max_tokens" 值,否則傳回輸出會被切斷。

{
    "enhancements": {
            "ocr": {
              "enabled": true
            },
            "grounding": {
              "enabled": true
            }
    },
    "dataSources": [
    {
        "type": "AzureComputerVision",
        "parameters": {
            "endpoint": "<your_computer_vision_endpoint>",
            "key": "<your_computer_vision_key>"
        }
    }],
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": [
	            {
	                "type": "text",
	                "text": "Describe this picture:"
	            },
	            {
	                "type": "image_url",
	                "image_url": {
                        "url":"<image URL>" 
                    }
                }
           ] 
        }
    ],
    "max_tokens": 100, 
    "stream": false 
} 

輸出

您從模型收到的聊天回應現在應該包含影像的增強資訊,例如物件標籤和週框方塊,以及 OCR 結果。 API 回應應該看起來如下所示。

{
    "id": "chatcmpl-8UyuhLfzwTj34zpevT3tWlVIgCpPg",
    "object": "chat.completion",
    "created": 1702394683,
    "model": "gpt-4",
    "choices":
    [
        {
            "finish_details": {
                "type": "stop",
                "stop": "<|fim_suffix|>"
            },
            "index": 0,
            "message":
            {
                "role": "assistant",
                "content": "The image shows a close-up of an individual with dark hair and what appears to be a short haircut. The person has visible ears and a bit of their neckline. The background is a neutral light color, providing a contrast to the dark hair."
            },
            "enhancements":
            {
                "grounding":
                {
                    "lines":
                    [
                        {
                            "text": "The image shows a close-up of an individual with dark hair and what appears to be a short haircut. The person has visible ears and a bit of their neckline. The background is a neutral light color, providing a contrast to the dark hair.",
                            "spans":
                            [
                                {
                                    "text": "the person",
                                    "length": 10,
                                    "offset": 99,
                                    "polygon": [{"x":0.11950000375509262,"y":0.4124999940395355},{"x":0.8034999370574951,"y":0.4124999940395355},{"x":0.8034999370574951,"y":0.6434999704360962},{"x":0.11950000375509262,"y":0.6434999704360962}]
                                }
                            ]
                        }
                    ],
                    "status": "Success"
                }
            }
        }
    ],
    "usage":
    {
        "prompt_tokens": 816,
        "completion_tokens": 49,
        "total_tokens": 865
    }
}

每個回應都包含 "finish_details" 欄位。 可能有以下的值:

  • stop: API 傳回的完整模型輸出。
  • length: 因為 max_tokens 輸入參數或模型的權杖限制,所以模型輸出不完整。
  • content_filter: 由於內容篩選中的旗標而省略了內容。

透過影片使用視覺增強功能

包含視覺功能的 GPT-4 Turbo 提供對 Azure AI 服務量身打造之增強功能的專屬存取權。 影片提示整合會使用 Azure AI 視覺影片擷取來取樣影片中的一組畫面,並在影片中建立語音文字記錄。 它可以讓 AI 模型針對影片內容提供摘要和解答。

請遵循下列步驟來設定影片擷取系統,並將它與您的 AI 聊天模型整合。

重要

若要透過 Azure OpenAI 資源使用視覺增強功能,您必須指定電腦視覺資源。 它必須位於付費 (S1) 階層,且位於與包含視覺功能之 GPT-4 Turbo 資源相同的 Azure 區域中。 如果您使用 Azure AI 服務資源,則不需要額外的電腦視覺資源。

警告

適用於包含視覺功能之 GPT-4 Turbo 的 Azure AI 增強功能將會與核心功能分開計費。 每個適用於包含視覺功能之 GPT-4 Turbo 的特定 Azure AI 增強功能都有不同的費用。 如需詳細資訊,請參閱特殊價格資訊

提示

如果您想要,可以改為使用 Jupyter Notebook 來執行下列步驟: 視訊聊天完成筆記本

將影片上傳至 Azure Blob 儲存體

您必須將影片上傳至 Azure Blob 儲存體容器。 如果您還沒有儲存體帳戶,請建立一個新的儲存體帳戶

影片上傳後,您可以取得其 SAS URL,在稍後的步驟中用來存取影片。

確定適當的讀取存取

根據您的驗證方法,您可能需要執行一些額外步驟,以授予存取 Azure Blob 儲存體容器之權利。 如果您使用 Azure AI 服務資源,而不是 Azure OpenAI 資源,您必須使用受控識別來授與它讀取 Azure Blob 儲存體的存取權:

遵循下列步驟,在 Azure AI 服務資源上啟用系統指派的身分識別:

  1. 從 Azure 入口網站中的 AI 服務資源選取 [資源管理] -> [身分識別],並將狀態切換為 [ON]
  2. 指派儲存體 Blob 資料讀取對 AI 服務資源的存取權: 從 [身分識別] 頁面中,選取 [Azure 角色指派],然後使用下列設定 [新增角色指派]:
    • 範圍: 儲存體
    • 訂用帳戶: {您的訂用帳戶}
    • 資源: {選取 Azure Blob 儲存體資源}
    • 角色: 儲存體 Blob 資料讀者
  3. 儲存您的設定。

建立影片擷取索引

  1. 在與您所使用之 Azure OpenAI 資源相同的區域中取得 Azure AI 視覺資源。

  2. 建立索引來儲存和組織影片檔案及其中繼資料。 下列範例命令示範如何使用建立索引API 來建立名為 my-video-index 的索引。 將索引名稱儲存至暫存位置; 您將在後續步驟中用到它。

    提示

    如需建立影片索引的詳細指示,請參閱使用向量化進行影片擷取

    重要

    影片索引名稱長度最多可達 24 個字元,除非它是 GUID,則可為 36 個字元。

    curl.exe -v -X PUT "https://<YOUR_ENDPOINT_URL>/computervision/retrieval/indexes/my-video-index?api-version=2023-05-01-preview" -H "Ocp-Apim-Subscription-Key: <YOUR_SUBSCRIPTION_KEY>" -H "Content-Type: application/json" --data-ascii "
    {
      'metadataSchema': {
        'fields': [
          {
            'name': 'cameraId',
            'searchable': false,
            'filterable': true,
            'type': 'string'
          },
          {
            'name': 'timestamp',
            'searchable': false,
            'filterable': true,
            'type': 'datetime'
          }
        ]
      },
      'features': [
        {
          'name': 'vision',
          'domain': 'surveillance'
        },
        {
          'name': 'speech'
        }
      ]
    }"
    
  3. 使用影片檔案相關聯的中繼資料將其新增至索引。 下列範例示範如何透過建立擷取 API 使用 SAS URL 將兩個影片檔案新增至索引。 將 SAS URL和 documentId 值儲存至暫存位置;您將在後續步驟中用到它們。

    curl.exe -v -X PUT "https://<YOUR_ENDPOINT_URL>/computervision/retrieval/indexes/my-video-index/ingestions/my-ingestion?api-version=2023-05-01-preview" -H "Ocp-Apim-Subscription-Key: <YOUR_SUBSCRIPTION_KEY>" -H "Content-Type: application/json" --data-ascii "
    {
      'videos': [
        {
          'mode': 'add',
          'documentId': '02a504c9cd28296a8b74394ed7488045',
          'documentUrl': 'https://example.blob.core.windows.net/videos/02a504c9cd28296a8b74394ed7488045.mp4?sas_token_here',
          'metadata': {
            'cameraId': 'camera1',
            'timestamp': '2023-06-30 17:40:33'
          }
        },
        {
          'mode': 'add',
          'documentId': '043ad56daad86cdaa6e493aa11ebdab3',
          'documentUrl': '[https://example.blob.core.windows.net/videos/043ad56daad86cdaa6e493aa11ebdab3.mp4?sas_token_here',
          'metadata': {
            'cameraId': 'camera2'
          }
        }
      ]
    }"
    
  4. 將影片檔案新增至索引後,擷取流程即啟動。 視檔案的大小和數目而定,這可能需要一些時間。 為了確保擷取在執行搜尋之前已完成,您可以使用取得擷取 API 來檢查狀態。 請等候此呼叫傳回 "state" = "Completed",再繼續進行下一個步驟。

    curl.exe -v -X GET "https://<YOUR_ENDPOINT_URL>/computervision/retrieval/indexes/my-video-index/ingestions?api-version=2023-05-01-preview&$top=20" -H "ocp-apim-subscription-key: <YOUR_SUBSCRIPTION_KEY>"
    

將影片索引與包含視覺功能的 GPT-4 Turbo 整合

  1. 請準備對 https://{RESOURCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}/chat/completions?api-version=2024-02-15-preview 的 POST 要求,其中

    • RESOURCE_NAME 是您 Azure OpenAI 資源的名稱
    • DEPLOYMENT_NAME 是您的 GPT-4 視覺模型部署的名稱

    必要標頭:

    • Content-Type: application/json
    • api-key: {API_KEY}
  2. 在要求本文中,新增下列 JSON 結構:

    {
        "enhancements": {
                "video": {
                  "enabled": true
                }
        },
        "dataSources": [
        {
            "type": "AzureComputerVisionVideoIndex",
            "parameters": {
                "computerVisionBaseUrl": "<your_computer_vision_endpoint>",
                "computerVisionApiKey": "<your_computer_vision_key>",
                "indexName": "<name_of_your_index>",
                "videoUrls": ["<your_video_SAS_URL>"]
            }
        }],
        "messages": [ 
            {
                "role": "system", 
                "content": "You are a helpful assistant." 
            },
            {
                "role": "user",
                "content": [
                        {
                            "type": "acv_document_id",
                            "acv_document_id": "<your_video_ID>"
                        },
                        {
                            "type": "text",
                            "text": "Describe this video:"
                        }
                    ]
            }
        ],
        "max_tokens": 100, 
    } 
    

    要求包含 enhancementsdataSources 物件。 enhancements 代表聊天中要求的特定視覺增強功能。 dataSources 代表視覺增強功能所需的電腦視覺資源資料。 它有 type 屬性 (其應為 "AzureComputerVisionVideoIndex"),以及包含 AI 視覺和影片資訊的 parameters 屬性。

  3. 在上述所有 <placeholder> 欄位中填入您自己的資訊: 視需要輸入 OpenAI 和 AI 視覺資源的端點 URL 及金鑰,並從先前的步驟擷取影片索引資訊。

  4. 將 POST 要求傳送至 API 端點。 它應該包含 OpenAI 和 AI 視覺認證、影片索引的名稱,以及單一影片的識別碼和 SAS URL。

重要

"data_sources" 物件的內容會根據您使用的 Azure 資源類型和驗證方法而有所不同。 請參閱下列參考:

"data_sources": [
{
    "type": "AzureComputerVisionVideoIndex",
    "parameters": {
    "endpoint": "<your_computer_vision_endpoint>",
    "computerVisionApiKey": "<your_computer_vision_key>",
    "indexName": "<name_of_your_index>",
    "videoUrls": ["<your_video_SAS_URL>"]
    }
}],

輸出

您從模型收到的聊天回應應包含影片的相關資訊。 API 回應應該看起來如下所示。

{
    "id": "chatcmpl-8V4J2cFo7TWO7rIfs47XuDzTKvbct",
    "object": "chat.completion",
    "created": 1702415412,
    "model": "gpt-4",
    "choices":
    [
        {
            "finish_reason":"stop",
            "index": 0,
            "message":
            {
                "role": "assistant",
                "content": "The advertisement video opens with a blurred background that suggests a serene and aesthetically pleasing environment, possibly a workspace with a nature view. As the video progresses, a series of frames showcase a digital interface with search bars and prompts like \"Inspire new ideas,\" \"Research a topic,\" and \"Organize my plans,\" suggesting features of a software or application designed to assist with productivity and creativity.\n\nThe color palette is soft and varied, featuring pastel blues, pinks, and purples, creating a calm and inviting atmosphere. The backgrounds of some frames are adorned with abstract, organically shaped elements and animations, adding to the sense of innovation and modernity.\n\nMidway through the video, the focus shifts to what appears to be a browser or software interface with the phrase \"Screens simulated, subject to change; feature availability and timing may vary,\" indicating the product is in development and that the visuals are illustrative of its capabilities.\n\nThe use of text prompts continues with \"Help me relax,\" followed by a demonstration of a 'dark mode' feature, providing a glimpse into the software's versatility and user-friendly design.\n\nThe video concludes by revealing the product name, \"Copilot,\" and positioning it as \"Your everyday AI companion,\" implying the use of artificial intelligence to enhance daily tasks. The final frames feature the Microsoft logo, associating the product with the well-known technology company.\n\nIn summary, the advertisement video is for a Microsoft product named \"Copilot,\" which seems to be an AI-powered software tool aimed at improving productivity, creativity, and organization for its users. The video conveys a message of innovation, ease, and support in daily digital interactions through a visually appealing and calming presentation."
            }
        }
    ],
    "usage":
    {
        "prompt_tokens": 2068,
        "completion_tokens": 341,
        "total_tokens": 2409
    }
}

每個回應都包含 "finish_details" 欄位。 可能有以下的值:

  • stop: API 傳回的完整模型輸出。
  • length: 因為 max_tokens 輸入參數或模型的權杖限制,所以模型輸出不完整。
  • content_filter: 由於內容篩選中的旗標而省略了內容。

影片提示的價格範例

包含視覺功能的 GPT-4 Turbo 價格是動態的,取決於所使用的特定功能和輸入。 如需 Azure OpenAI 價格的完整檢視,請參閱 Azure OpenAI 價格

基本費用和其他功能如下所述:

包含視覺功能的 GPT-4 Turbo 基本價格為:

  • 輸入:每 1000 個權杖美金 $0.01 元
  • 輸出: 每 1000 個權杖美金 $0.03 元

影片提示與影片擷取附加元件整合:

  • 擷取: 每分鐘影片美金 $0.05 元
  • 交易: 每 1000 個影片擷取查詢美金 $0.25 元

下一步