Skip to content

Commit 0d8dc4e

Browse files
committed
dotnet image quickstart
1 parent dbcf8c7 commit 0d8dc4e

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

articles/ai-services/openai/gpt-v-quickstart.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Get started using GPT-4 Turbo with images with the Azure OpenAI Service.
4545

4646
::: zone-end
4747

48+
::: zone pivot="programming-language-dotnet"
49+
50+
[!INCLUDE [.NET quickstart](includes/gpt-v-dotnet.md)]
51+
52+
::: zone-end
53+
4854
## Next steps
4955

5056
* Learn more about these APIs in the [GPT-4 Turbo with Vision how-to guide](./gpt-v-quickstart.md)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: 'Quickstart: Use GPT-4 Turbo with Vision on your images and videos with the Python SDK'
3+
titleSuffix: Azure OpenAI
4+
description: Get started using the Azure OpenAI Python SDK to deploy and use the GPT-4 Turbo with Vision model.
5+
services: cognitive-services
6+
manager: nitinme
7+
ms.service: azure-ai-openai
8+
ms.topic: include
9+
ms.custom: references_regions
10+
ms.date: 01/22/2024
11+
---
12+
13+
Use this article to get started using the Azure OpenAI Python SDK to deploy and use the GPT-4 Turbo with Vision model.
14+
15+
[Library source code](https://github.com/openai/openai-python?azure-portal=true) | [Package (PyPi)](https://pypi.org/project/openai?azure-portal=true) |
16+
17+
18+
## Prerequisites
19+
20+
- An Azure subscription. You can [create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true).
21+
- [The .NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download)
22+
- An Azure OpenAI Service resource with a GPT-4 Turbo with Vision model deployed. See [GPT-4 and GPT-4 Turbo Preview model availability](../concepts/models.md#gpt-4-and-gpt-4-turbo-model-availability) for available regions. For more information about resource creation, see the [resource deployment guide](/azure/ai-services/openai/how-to/create-resource).
23+
24+
## Set up
25+
26+
### Retrieve key and endpoint
27+
28+
To successfully make a call against Azure OpenAI, you need an **endpoint** and a **key**.
29+
30+
|Variable name | Value |
31+
|--------------------------|-------------|
32+
| `AZURE_OPENAI_ENDPOINT` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in the **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://aoai-docs.openai.azure.com/`.|
33+
| `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`.|
34+
35+
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.
36+
37+
:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview UI for an Azure OpenAI resource in the Azure portal with the endpoint & access keys location highlighted." lightbox="../media/quickstarts/endpoint.png":::
38+
39+
40+
## Create the .NET app
41+
42+
1. Create a .NET app using the `dotnet new` command:
43+
44+
```dotnetcli
45+
dotnet new console -n OpenAISpeech
46+
```
47+
48+
1. Change into the directory of the new app:
49+
50+
```dotnetcli
51+
cd OpenAISpeech
52+
```
53+
54+
## Install the client library
55+
56+
Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library:
57+
58+
```dotnetcli
59+
dotnet add package Azure.AI.OpenAI
60+
```
61+
62+
## Passwordless authentication is recommended
63+
64+
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:
65+
66+
1. Add the [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) package.
67+
68+
```dotnetcli
69+
dotnet add package Azure.Identity
70+
```
71+
72+
1. 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**.
73+
1. Sign-in to Azure using Visual Studio or the Azure CLI via `az login`.
74+
75+
## Update the app code
76+
77+
1. Replace the contents of `program.cs` with the following code and update the placeholder values with your own.
78+
79+
```csharp
80+
using Azure;
81+
using Azure.AI.OpenAI;
82+
using Azure.Identity;
83+
using OpenAI.Chat; // Required for Passwordless auth
84+
85+
var endpoint = new Uri("YOUR_AZURE_OPENAI_ENDPOINT");
86+
var credentials = new AzureKeyCredential("YOUR_AZURE_OPENAI_KEY");
87+
// var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth
88+
var deploymentName = "gpt-4"; // Default name, update with your own if needed
89+
90+
var openAIClient = new AzureOpenAIClient(endpoint, credentials);
91+
var chatClient = openAIClient.GetChatClient(deploymentName);
92+
93+
var imageUri = "YOUR_IMAGE_URL";
94+
95+
List<ChatMessage> messages = [
96+
new UserChatMessage(
97+
ChatMessageContentPart.CreateTextMessageContentPart("Please describe the following image:"),
98+
ChatMessageContentPart.CreateImageMessageContentPart(new Uri(imageUri), "image/png"))
99+
];
100+
101+
ChatCompletion chatCompletion = await chatClient.CompleteChatAsync(messages);
102+
103+
Console.WriteLine($"[ASSISTANT]:");
104+
Console.WriteLine($"{chatCompletion.Content[0].Text}");
105+
```
106+
107+
> [!IMPORTANT]
108+
> For production, store and access your credentials using a secure method, such as [Azure Key Vault](/azure/key-vault/general/overview). For more information about credential security, see [Azure AI services security](../../security-features.md).
109+
110+
1. Run the application using the `dotnet run` command or the run button at the top of Visual Studio:
111+
112+
```dotnetcli
113+
dotnet run
114+
```
115+
116+
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.
117+
118+
## Clean up resources
119+
120+
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.
121+
122+
- [Azure portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources)
123+
- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources)
124+
125+

zone-pivots/zone-pivot-groups.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ groups:
183183
title: Python
184184
- id: programming-language-javascript
185185
title: JavaScript
186+
- id: programming-language-dotnet
187+
title: C#
186188
- id: rest-api
187189
title: REST
188190
# Owner: mbullwin

0 commit comments

Comments
 (0)