Quickstart: Use images in your AI chats
Get started using GPT-4 Turbo with images with the Azure OpenAI Service.
Note
Model choice
The latest vision-capable models are gpt-4o
and gpt-4o mini
. These are in public preview. The latest available GA model is gpt-4
version turbo-2024-04-09
.
Start using images in your AI chats with a no-code approach through Azure AI Foundry.
Prerequisites
- An Azure subscription. Create one for free.
- An Azure OpenAI Service resource. For more information about resource creation, see the resource deployment guide.
Go to Azure AI Foundry
Browse to Azure AI Foundry and sign in with the credentials associated with your Azure OpenAI resource. During or after the sign-in workflow, select the appropriate directory, Azure subscription, and Azure OpenAI resource.
Create a project or select an existing one. Navigate to the Models + endpoints option on the left, and select Deploy model. Choose an image-capable deployment by selecting model name: gpt-4o or gpt-4o-mini. For more information about model deployment, see the resource deployment guide.
Select the new deployment and select Open in playground.
Playground
From this page, you can quickly iterate and experiment with the model's capabilities.
For general help with assistant setup, chat sessions, settings, and panels, refer to the Chat quickstart.
Start a chat session to analyze images
In this chat session, you're instructing the assistant to aid in understanding images that you input.
To start, make sure your image-capable deployment is selected in the Deployment dropdown.
In the Setup pane, provide a System Message to guide the assistant. The default System Message is: "You are an AI assistant that helps people find information." You can tailor the System Message to the image or scenario that you're uploading.
Note
We recommend you update the System Message to be specific to the task in order to avoid unhelpful responses from the model.
Save your changes, and when prompted to confirm updating the system message, select Continue.
In the Chat session pane, enter a text prompt like "Describe this image," and upload an image with the attachment button. You can use a different text prompt for your use case. Then select Send.
Observe the output provided. Consider asking follow-up questions related to the analysis of your image to learn more.
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this article to get started using the Azure OpenAI REST APIs to deploy and use the GPT-4 Turbo with Vision model.
Prerequisites
- An Azure subscription. Create one for free.
- Python 3.8 or later version.
- The following Python libraries:
requests
,json
. - An Azure OpenAI Service resource with a GPT-4 Turbo with Vision model deployed. See GPT-4 and GPT-4 Turbo Preview model availability for available regions. For more information about resource creation, see the resource deployment guide.
Note
It is currently not supported to turn off content filtering for the GPT-4 Turbo with Vision model.
Retrieve key and endpoint
To successfully call the Azure OpenAI APIs, you need the following information about your Azure OpenAI resource:
Variable | Name | Value |
---|---|---|
Endpoint | api_base |
The endpoint value is located under Keys and Endpoint for your resource in the Azure portal. You can also find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
Key | api_key |
The key value is also located under Keys and Endpoint for your resource in the Azure portal. Azure generates two keys for your resource. You can use either value. |
Go to your resource in the Azure portal. On the navigation pane, select Keys and Endpoint under Resource Management. Copy the Endpoint value and an access key value. You can use either the KEY 1 or KEY 2 value. Having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create a new Python application
Create a new Python file named quickstart.py. Open the new file in your preferred editor or IDE.
Replace the contents of quickstart.py with the following code.
# Packages required: import requests import json api_base = '<your_azure_openai_endpoint>' deployment_name = '<your_deployment_name>' API_KEY = '<your_azure_openai_key>' base_url = f"{api_base}openai/deployments/{deployment_name}" headers = { "Content-Type": "application/json", "api-key": API_KEY } # Prepare endpoint, headers, and request body endpoint = f"{base_url}/chat/completions?api-version=2023-12-01-preview" data = { "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": 2000 } # Make the API call response = requests.post(endpoint, headers=headers, data=json.dumps(data)) print(f"Status Code: {response.status_code}") print(response.text)
Make the following changes:
- Enter your endpoint URL and key in the appropriate fields.
- Enter your GPT-4 Turbo with Vision deployment name in the appropriate field.
- Change the value of the
"image"
field to the URL of your image.Tip
You can also use a base 64 encoded image data instead of a URL. For more information, see the GPT-4 Turbo with Vision how-to guide.
Run the application with the
python
command:python quickstart.py
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this article to get started using the Azure OpenAI Python SDK to deploy and use the GPT-4 Turbo with Vision model.
Library source code | Package (PyPi) |
Prerequisites
- An Azure subscription. Create one for free.
- Python 3.8 or later version.
- The following Python libraries:
os
- An Azure OpenAI Service resource with a GPT-4 Turbo with Vision model deployed. See GPT-4 and GPT-4 Turbo Preview model availability for available regions. For more information about resource creation, see the resource deployment guide.
Set up
Install the OpenAI Python client library with:
pip install openai
Note
This library is maintained by OpenAI. Refer to the release history to track the latest updates to the library.
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Environment variables
Create and assign persistent environment variables for your key and endpoint.
Important
If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Create a new Python application
Create a new Python file named quickstart.py. Open the new file in your preferred editor or IDE.
Replace the contents of quickstart.py with the following code.
from openai import AzureOpenAI api_base = os.getenv("AZURE_OPENAI_ENDPOINT") api_key= os.getenv("AZURE_OPENAI_API_KEY") deployment_name = '<your_deployment_name>' api_version = '2023-12-01-preview' # this might change in the future client = AzureOpenAI( api_key=api_key, api_version=api_version, base_url=f"{api_base}/openai/deployments/{deployment_name}" ) response = client.chat.completions.create( model=deployment_name, 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=2000 ) print(response)
Make the following changes:
- Enter the name of your GPT-4 Turbo with Vision deployment in the appropriate field.
- Change the value of the
"url"
field to the URL of your image.Tip
You can also use a base 64 encoded image data instead of a URL. For more information, see the GPT-4 Turbo with Vision how-to guide.
Run the application with the
python
command:python quickstart.py
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this article to get started using the OpenAI JavaScript SDK to deploy and use the GPT-4 Turbo with Vision model.
This SDK is provided by OpenAI with Azure specific types provided by Azure.
Reference documentation | Library source code | Package (npm) | Samples
Prerequisites
- An Azure subscription - Create one for free
- LTS versions of Node.js
- Azure CLI used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
- An Azure OpenAI resource created in a supported region (see Region availability). For more information, see Create a resource and deploy a model with Azure OpenAI.
Note
This library is maintained by OpenAI. Refer to the release history to track the latest updates to the library.
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Environment variables
Create and assign persistent environment variables for your key and endpoint.
Important
If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Create a Node application
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it. Then run the npm init
command to create a node application with a package.json file.
npm init
Install the client library
Install the client libraries with:
npm install openai @azure/identity
Your app's package.json file will be updated with the dependencies.
Create a new JavaScript application for image prompts
Select an image from the azure-samples/cognitive-services-sample-data-files and set the URL for an image in the environment variables.
Replace the contents of quickstart.js with the following code.
const AzureOpenAI = require('openai').AzureOpenAI; const { DefaultAzureCredential, getBearerTokenProvider } = require('@azure/identity'); // You will need to set these environment variables or edit the following values const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>"; const imageUrl = process.env["IMAGE_URL"] || "<image url>"; // Required Azure OpenAI deployment name and API version const apiVersion = "2024-07-01-preview"; const deploymentName = "gpt-4-with-turbo"; // keyless authentication const credential = new DefaultAzureCredential(); const scope = "https://cognitiveservices.azure.com/.default"; const azureADTokenProvider = getBearerTokenProvider(credential, scope); function getClient(): AzureOpenAI { return new AzureOpenAI({ endpoint, azureADTokenProvider, apiVersion, deployment: deploymentName, }); } function createMessages() { return { messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: [ { type: "text", text: "Describe this picture:", }, { type: "image_url", image_url: { url: imageUrl, }, }, ], }, ], model: "", max_tokens: 2000, }; } async function printChoices(completion) { for (const choice of completion.choices) { console.log(choice.message); } } export async function main() { console.log("== Get GPT-4 Turbo with vision Sample =="); const client = getClient(); const messages = createMessages(); const completion = await client.chat.completions.create(messages); await printChoices(completion); } main().catch((err) => { console.error("Error occurred:", err); });
Make the following changes:
- Enter the name of your GPT-4 Turbo with Vision deployment in the appropriate field.
- Change the value of the
"url"
field to the URL of your image.Tip
You can also use a base 64 encoded image data instead of a URL. For more information, see the GPT-4 Turbo with Vision how-to guide.
Run the application with the following command:
node quickstart.js
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this article to get started using the OpenAI JavaScript SDK to deploy and use the GPT-4 Turbo with Vision model.
This SDK is provided by OpenAI with Azure specific types provided by Azure.
Reference documentation | Library source code | Package (npm) | Samples
Prerequisites
- An Azure subscription - Create one for free
- LTS versions of Node.js
- TypeScript
- Azure CLI used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
- An Azure OpenAI resource created in a supported region (see Region availability). For more information, see Create a resource and deploy a model with Azure OpenAI.
Note
This library is maintained by OpenAI. Refer to the release history to track the latest updates to the library.
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
API-KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Environment variables
Create and assign persistent environment variables for your key and endpoint.
Important
If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
Create a Node application
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it. Then run the npm init
command to create a node application with a package.json file.
npm init
Install the client library
Install the client libraries with:
npm install openai @azure/identity
Your app's package.json file will be updated with the dependencies.
Create a new JavaScript application for image prompts
Select an image from the azure-samples/cognitive-services-sample-data-files and set the URL for an image in the environment variables.
Create a quickstart.ts and paste in the following code.
import { AzureOpenAI } from "openai"; import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity"; import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming, } from "openai/resources/index"; // You will need to set these environment variables or edit the following values const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>"; const imageUrl = process.env["IMAGE_URL"] || "<image url>"; // Required Azure OpenAI deployment name and API version const apiVersion = "2024-07-01-preview"; const deploymentName = "gpt-4-with-turbo"; // keyless authentication const credential = new DefaultAzureCredential(); const scope = "https://cognitiveservices.azure.com/.default"; const azureADTokenProvider = getBearerTokenProvider(credential, scope); function getClient(): AzureOpenAI { return new AzureOpenAI({ endpoint, azureADTokenProvider, apiVersion, deployment: deploymentName, }); } function createMessages(): ChatCompletionCreateParamsNonStreaming { return { messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: [ { type: "text", text: "Describe this picture:", }, { type: "image_url", image_url: { url: imageUrl, }, }, ], }, ], model: "", max_tokens: 2000, }; } async function printChoices(completion: ChatCompletion): Promise<void> { for (const choice of completion.choices) { console.log(choice.message); } } export async function main() { console.log("== Get GPT-4 Turbo with vision Sample =="); const client = getClient(); const messages = createMessages(); const completion = await client.chat.completions.create(messages); await printChoices(completion); } main().catch((err) => { console.error("Error occurred:", err); });
Make the following changes:
- Enter the name of your GPT-4 Turbo with Vision deployment in the appropriate field.
- Change the value of the
"url"
field to the URL of your image.Tip
You can also use a base 64 encoded image data instead of a URL. For more information, see the GPT-4 Turbo with Vision how-to guide.
Build the application with the following command:
tsc
Run the application with the following command:
node quickstart.js
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Use this article to get started using the Azure OpenAI .NET SDK to deploy and use the GPT-4 Turbo with Vision model.
Prerequisites
- An Azure subscription. You can create one for free.
- The .NET 8.0 SDK
- An Azure OpenAI Service resource with a GPT-4 Turbo with Vision model deployed. See GPT-4 and GPT-4 Turbo Preview model availability for available regions. For more information about resource creation, see the resource deployment guide.
Set up
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
AZURE_OPENAI_API_KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create the .NET app
Create a .NET app using the
dotnet new
command:dotnet new console -n OpenAISpeech
Change into the directory of the new app:
cd OpenAISpeech
Install the client library
Install the Azure.OpenAI
client library:
dotnet add package Azure.AI.OpenAI
Passwordless authentication is recommended
Passwordless authentication is more secure than key-based alternatives and is the recommended approach for connecting to Azure services. If you choose to use Passwordless authentication, you'll need to complete the following:
Add the
Azure.Identity
package.dotnet add package Azure.Identity
Assign the
Cognitive Services User
role to your user account. This can be done in the Azure portal on your OpenAI resource under Access control (IAM) > Add role assignment.Sign-in to Azure using Visual Studio or the Azure CLI via
az login
.
Update the app code
Replace the contents of
program.cs
with the following code and update the placeholder values with your own.using Azure; using Azure.AI.OpenAI; using Azure.Identity; using OpenAI.Chat; // Required for Passwordless auth var endpoint = new Uri("YOUR_AZURE_OPENAI_ENDPOINT"); var credentials = new AzureKeyCredential("YOUR_AZURE_OPENAI_KEY"); // var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth var deploymentName = "gpt-4"; // Default name, update with your own if needed var openAIClient = new AzureOpenAIClient(endpoint, credentials); var chatClient = openAIClient.GetChatClient(deploymentName); var imageUri = "YOUR_IMAGE_URL"; List<ChatMessage> messages = [ new UserChatMessage( ChatMessageContentPart.CreateTextMessageContentPart("Please describe the following image:"), ChatMessageContentPart.CreateImageMessageContentPart(new Uri(imageUri), "image/png")) ]; ChatCompletion chatCompletion = await chatClient.CompleteChatAsync(messages); Console.WriteLine($"[ASSISTANT]:"); Console.WriteLine($"{chatCompletion.Content[0].Text}");
Important
For production, store and access your credentials using a secure method, such as Azure Key Vault. For more information about credential security, see Azure AI services security.
Run the application using the
dotnet run
command or the run button at the top of Visual Studio:dotnet run
The app generates an audio file at the location you specified for the speechFilePath
variable. Play the file on your device to hear the generated audio.
Clean up resources
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.
Next steps
- Learn more about these APIs in the Vision-enabled models how-to guide
- GPT-4 Turbo with Vision frequently asked questions
- GPT-4 Turbo with Vision API reference