Wednesday, January 24, 2024

Using C# code generated by Azure OpenAI Studio

 In this article I will experience how one can use generated C# code in "Azure OpenAI Studio" to create your own application. Of course, it is assumed that you have an Azure subscription and access to Azure OpenAI.

Getting Started

Go to https://oai.azure.com.

Let us play in the “Chat playground”. Click on “Chat playground”.


We will start by grounding our AI bot. We can do that by telling the AI that it specializes in baking recipes. Enter the following to the "System Message":

You are an AI assistant that helps people with baking.

Next, add a prompt requesting a recipe:

Give me an easy recipe for baking a loaf of bread that takes about 30 minutes preparation and about 40 minutes in the oven.

The AI will suggest to you a recipe. This is the response I received:

Certainly! Here's a simple recipe for baking a loaf of bread with a total time of approximately 1 hour and 10 minutes:

Ingredients:
3 cups all-purpose flour
2 teaspoons active dry yeast
1 teaspoon salt
1 tablespoon sugar
1 cup warm water (around 110°F/43°C)
2 tablespoons olive oil

Instructions:
In a large mixing bowl, combine the warm water and sugar. Stir until the sugar is dissolved, then sprinkle the yeast over the water. Let it sit for about 5 minutes until the yeast becomes frothy.

Add the salt and olive oil to the yeast mixture, and gradually add the flour, one cup at a time. Stir well after each addition until a shaggy dough forms.

Once the dough starts to come together, transfer it onto a lightly floured surface and knead for about 5-7 minutes until it becomes smooth and elastic. If the dough is too sticky, you can add a little more flour as needed.

Shape the dough into a ball, and place it in a greased bowl. Cover the bowl with a clean kitchen towel or plastic wrap, and let the dough rise in a warm place for approximately 30 minutes or until it doubles in size.

Preheat your oven to 375°F (190°C) while the dough is rising.

After the dough has risen, punch it down gently to release any air bubbles. Transfer it to a greased loaf pan, and shape it into a loaf by tucking the edges under.

Cover the loaf pan again with the kitchen towel or plastic wrap, and let it rise for an additional 10 minutes.

Once the dough has risen for the second time, remove the cover and place the pan in the preheated oven. Bake for about 35-40 minutes or until the bread is golden brown and sounds hollow when tapped on the bottom.

Once baked, remove the bread from the oven and let it cool in the pan for a few minutes. Then transfer it to a wire rack to cool completely before slicing.

Enjoy your freshly baked bread!

Using generated C# code

Click on “View code”.


 Change the language to C#.

Note the message at the top. You are asked to add a specific version of a package in your C# app.

// Note: The Azure OpenAI client library for .NET is in preview.
// Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5

To get started with a basic console application, click on the “Learn more” link at the bottom.

Choose C#.


Under "Set up" you will be asked to create a new app and add a package to it.

dotnet new console -n azure-openai-quickstart
cd azure-openai-quickstart
dotnet add package Azure.AI.OpenAI --prerelease

Run the above commands, then replace the code in Program.cs with the code that was generated by “Azure AI Studio”.

You will need to enter the AZURE_OPENAI_API_KEY at around line 8 in Program.cs. This is given to you just below the sample code in “Azure OpenAI Studio”.

Copy and paste the key into your code. This is what my code looked like after pasting the key:

If you run “dotnet build”, you will see some errors. This is because we did not use the specific preview version of the Azure.AI.OpenAI package that was suggested. Most likely you have a more recent version. The version I have at the time of writing (January 2024) is 1.0.0-beta.12. All the errors pertain to the ChatMessage property when creating ChatCompletionsOptions. Replace the code for responseWithoutStream with the following:

Response<ChatCompletions> responseWithoutStream = await client.GetChatCompletionsAsync(
   new ChatCompletionsOptions() {
    DeploymentName="gpt-35-turbo",
    Messages =
    {
      new ChatRequestSystemMessage(@"You are an AI assistant that helps people find information."),
      new ChatRequestUserMessage(@"Give me an easy recipe for baking a loaf of bread that takes about 30 minutes preparation and about 40 minutes in the oven."),     
    },
    Temperature = (float)0.7,
    MaxTokens = 800,


    NucleusSamplingFactor = (float)0.95,
    FrequencyPenalty = 0,
    PresencePenalty = 0,
   });

Since nothing is output, let us display the AI response. Add the following code to the bottom of Program.cs:

Console.WriteLine(response.Choices[0].Message.Content);

Run the app and you will see the response from the AI. In my case I received a very similar response to what I previously got.

Conclusion

"Azure OpenAI Studio" can help you get started with the development of a C# app that utilizes services from "Azure OpenAI".


No comments:

Post a Comment