Exercise - Connect to Azure OpenAI
The OpenAiService
class contains a stub implementation of a service that can send prompts to an AI assistant and parse the responses.
There are a few key requirements to tackle in this exercise:
- Import the .NET SDK for Azure OpenAI
- Add the Azure OpenAI endpoint and key to the application settings
- Modify the service class with various members and a client instance
Import the .NET SDK
The Azure.AI.OpenAI
package on NuGet provides a typed SDK to access various model deployments from your account endpoint.
Open a new terminal.
Use
dotnet add package
to import theAzure.AI.OpenAI
package from NuGet specifying a prerelease version of1.0.0-beta.14
.dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.14
Build the .NET project again.
dotnet build
Close the terminal.
Add application settings
In a .NET application, it's common to use the configuration providers to inject new settings into your application. For this application, use the appsettings.Development.json
file to provide the most current values for the Azure OpenAI endpoint and key.
In the root of the project, create a new file named appsettings.Development.json.
Important
On Linux, files are case-sensitive. The .NET environment for this project is named Development and the filename must match that environment name to work.
Within the file, create a new JSON object with a placeholder property for
OpenAi
settings.{ "OpenAi": { } }
Within the
OpenAi
property, create two new properties for theEndpoint
andKey
. Use the Azure OpenAI endpoint and key settings you recorded earlier in this project.{ "OpenAi": { "Endpoint": "<your-azure-openai-endpoint>", "Key": "<your-azure-openai-key>" } }
Assuming the name of the Azure OpenAI account is nybncrsna76fo-openai and the key is
4bf98cb194cdf0f9001eae3259a76ed8
, you would configure the JSON object like this example.{ "OpenAi": { "Endpoint": "https://nybncrsna76fo-openai.openai.azure.com/", "Key": "4bf98cb194cdf0f9001eae3259a76ed8" } }
Note
The key in this example is fictitious.
Save the appsettings.Development.json file.
Add required members and a client instance
Finally, implement the class variables required to use the Azure OpenAI client. At this step, implement a few static prompts and create a new instance of the OpenAIClient
class.
Open the Services/OpenAiService.cs file.
Add using directives for the
Azure
andAzure.AI.OpenAI
namespaces.using Azure; using Azure.AI.OpenAI;
Within the
OpenAiService
class, add a new variable named_client
that's of typeOpenAIClient
.private readonly OpenAIClient _client;
Define a static block of text to send to the AI assistant before each prompt with a new string variable named
_systemPromptText
.private readonly string _systemPrompt = @" You are an AI assistant that helps people find information. Provide concise answers that are polite and professional." + Environment.NewLine;
Define a second static block of text to send to the AI with instructions on how to summarize a conversation with a new string variable named
_summarizePrompt
.private readonly string _summarizePrompt = @" Summarize this prompt in one or two words to use as a label in a button on a web page. Do not use any punctuation." + Environment.NewLine;
Within the constructor of the class, add two extra lines of code to check if the endpoint or key is null. Use
ArgumentNullException.ThrowIfNullOrEmpty
to throw an error early if either of these values are null.ArgumentNullException.ThrowIfNullOrEmpty(endpoint); ArgumentNullException.ThrowIfNullOrEmpty(key);
Tip
When you run the application, this will throw an error right away if either of these settings don't have a valid value provided through the appsettings.Development.json file.
Next, take the model name that is a parameter of the constructor and save it to the
_modelName
variable._modelName = modelName;
Finally, create a new instance of the
OpenAIClient
class using the endpoint to build aUri
and the key to build anAzureKeyCredential
.Uri uri = new(endpoint); AzureKeyCredential credential = new(key); _client = new( endpoint: uri, keyCredential: credential );
Save the Services/OpenAiService.cs file.
Check your work
At this point, your constructor should include enough logic to create a client instance. Since the class doesn't do anything with the client yet, there's no point in running the web application, but there's value in building the application to make sure your code doesn't have any omissions or errors.
Open a new terminal.
Build the .NET project.
dotnet build
Observe the build output and check to make sure there aren't any build errors.
MSBuild version 17.5.1+f6fdcf537 for .NET Determining projects to restore... All projects are up-to-date for restore. cosmoschatgpt -> /workspaces/cosmosdb-chatgpt/bin/Debug/net8.0/cosmoschatgpt.dll Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:02.93
Close the terminal.