diff --git a/docs/ai/quickstarts/get-started-azure-openai.md b/docs/ai/quickstarts/get-started-azure-openai.md index ac1d1f66f8764..b88cb64e96a05 100644 --- a/docs/ai/quickstarts/get-started-azure-openai.md +++ b/docs/ai/quickstarts/get-started-azure-openai.md @@ -1,18 +1,33 @@ --- title: Quickstart - Build an Azure AI chat app with .NET -description: Create a simple chat app using the .NET Azure OpenAI SDK. +description: Create a simple chat app using Semantic Kernel or the .NET Azure OpenAI SDK. ms.date: 03/04/2024 ms.topic: quickstart ms.custom: devx-track-dotnet, devx-track-dotnet-ai author: fboucher ms.author: frbouche +zone_pivot_groups: openai-library # CustomerIntent: As a .NET developer new to Azure OpenAI, I want deploy and use sample code to interact to learn from the sample code. --- # Build an Azure AI chat app with .NET + +:::zone target="docs" pivot="semantic-kernel" + + +Get started with the Semantic Kernel by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `gpt-35-turbo` model deployed into an Azure OpenAI account. Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `gpt-35-turbo` model deployed into an Azure OpenAI account. Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK. +:::zone-end + [!INCLUDE [download-alert](includes/prerequisites-and-azure-deploy.md)] ## Trying HikerAI sample @@ -26,6 +41,82 @@ Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console c If you get an error message the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again. + +:::zone target="docs" pivot="semantic-kernel" + + +## Understanding the code + +Our application uses the `Microsoft.SemanticKernel` package, which is available on [NuGet](https://www.nuget.org/packages/Microsoft.SemanticKernel), to send and receive requests to an Azure OpenAI service deployed in Azure. + +The `AzureOpenAIChatCompletionService` service facilitates the requests and responses. + +```csharp +// == Create the Azure OpenAI Chat Completion Service ========== +AzureOpenAIChatCompletionService service = new(deployment, endpoint, key); +``` + +The entire application is contained within the **Program.cs** file. The first several lines of code loads up secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning. + +```csharp +// == Retrieve the local secrets saved during the Azure deployment ========== +var config = new ConfigurationBuilder().AddUserSecrets().Build(); +string endpoint = config["AZURE_OPENAI_ENDPOINT"]; +string deployment = config["AZURE_OPENAI_GPT_NAME"]; +string key = config["AZURE_OPENAI_KEY"]; +``` + +Once the `AzureOpenAIChatCompletionService` service is created, we provide more context to the model by adding a system prompt. This instructs the model how you'd like it to act during the conversation. + +```csharp +// Start the conversation with context for the AI model +ChatHistory chatHistory = new(""" + You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly. + You introduce yourself when first saying hello. When helping people out, you always ask them + for this information to inform the hiking recommendation you provide: + + 1. Where they are located + 2. What hiking intensity they are looking for + + You will then provide three suggestions for nearby hikes that vary in length after you get that information. + You will also share an interesting fact about the local nature on the hikes when making a recommendation. + """); +``` + +Then you can add a user message to the model by using the `AddUserMessage` function. + +To have the model generate a response based off the system prompt and the user request, use the `GetChatMessageContentAsync` function. + +```csharp + +// Add user message to chat history +chatHistory.AddUserMessage("Hi! Apparently you can help me find a hike that I will like?"); + +// Print User Message to console +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); + +// Get response +var response = await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 }); +``` + +To maintain the chat history, make sure you add the response from the model. + +```csharp +// Add response to chat history +chatHistory.Add(response); + +// Print Response to console +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); +``` + +Customize the system prompt and user message to see how the model responds to help you find a hike that you'll like. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + ## Understanding the code Our application uses the `Azure.AI.OpenAI` client SDK, which is available on [NuGet](https://www.nuget.org/packages/Azure.AI.OpenAI), to send and receive requests to an Azure OpenAI service deployed in Azure. @@ -102,6 +193,8 @@ To maintain the chat history or context, make sure you add the response from the Customize the system prompt and user message to see how the model responds to help you find a hike that you'll like. +:::zone-end + ## Clean up resources When you no longer need the sample application or resources, remove the corresponding deployment and all resources. diff --git a/docs/ai/quickstarts/quickstart-ai-chat-with-data.md b/docs/ai/quickstarts/quickstart-ai-chat-with-data.md index 0ef1f17f54525..eb9890b37a103 100644 --- a/docs/ai/quickstarts/quickstart-ai-chat-with-data.md +++ b/docs/ai/quickstarts/quickstart-ai-chat-with-data.md @@ -1,18 +1,32 @@ --- title: Quickstart - Get insight about your data from an .NET Azure AI chat app -description: Create a simple chat app using your data and the .NET Azure OpenAI SDK. +description: Create a simple chat app using your data and Semantic Kernel or the .NET Azure OpenAI SDK. ms.date: 03/04/2024 ms.topic: quickstart ms.custom: devx-track-dotnet, devx-track-dotnet-ai author: fboucher ms.author: frbouche +zone_pivot_groups: openai-library # CustomerIntent: As a .NET developer new to Azure OpenAI, I want deploy and use sample code and data to interact to learn from the sample code. --- # Get insight about your data from an .NET Azure AI chat app + +:::zone target="docs" pivot="semantic-kernel" + + +Get started with Semantic Kernel and the `gpt-35-turbo` model, from a simple .NET 8.0 console application. Use the AI model to get analytics and information about your previous hikes. It consists of a simple console application, running locally, that will read the file `hikes.md` and send request to an Azure OpenAI service deployed in your Azure subscription and provide the result in the console. Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel. +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + Get started with the .NET Azure OpenAI with a `gpt-35-turbo` model, from a simple .NET 8.0 console application. Use the AI model to get analytics and information about your previous hikes. It consists of a simple console application, running locally, that will read the file `hikes.md` and send request to an Azure OpenAI service deployed in your Azure subscription and provide the result in the console. Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK. +:::zone-end + [!INCLUDE [download-alert](includes/prerequisites-and-azure-deploy.md)] ## Try "Chatting About My Previous Hikes" sample @@ -26,6 +40,76 @@ Get started with the .NET Azure OpenAI with a `gpt-35-turbo` model, from a simpl If you get an error message the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again. + +:::zone target="docs" pivot="semantic-kernel" + + +## Explore the code + +Our application uses the `Microsoft.SemanticKernel` package, which is available on [NuGet](https://www.nuget.org/packages/Microsoft.SemanticKernel), to send and receive requests to an Azure OpenAI service deployed in Azure. + +The `AzureOpenAIChatCompletionService` service facilitates the requests and responses. + +```csharp +// == Create the Azure OpenAI Chat Completion Service ========== +AzureOpenAIChatCompletionService service = new(deployment, endpoint, key); +``` + +The entire application is contained within the **Program.cs** file. The first several lines of code loads up secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning. + +```csharp +// == Retrieve the local secrets saved during the Azure deployment ========== +var config = new ConfigurationBuilder().AddUserSecrets().Build(); +string endpoint = config["AZURE_OPENAI_ENDPOINT"]; +string deployment = config["AZURE_OPENAI_GPT_NAME"]; +string key = config["AZURE_OPENAI_KEY"]; +``` + +Once the `AzureOpenAIChatCompletionService` client is created, we read the content of the file `hikes.md` and use it to provide more context to the model by adding a system prompt. This instructs the model how you'd like it to act during the conversation. + +```csharp +// Provide context for the AI model +ChatHistory chatHistory = new($""" + You are upbeat and friendly. You introduce yourself when first saying hello. + Provide a short answer only based on the user hiking records below: + + {File.ReadAllText("hikes.md")} + """); +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); +``` + +Then you can add a user message to the model by using the `AddUserMessage` function. + +To have the model generate a response based off the system prompt and the user request, use the `GetChatMessageContentAsync` function. + +```csharp +// Start the conversation +chatHistory.AddUserMessage("Hi!"); +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); + +chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 })); +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); +``` + +To maintain the chat history or context, make sure you add the response from the model to the `chatHistory`. It's time to make our user request about our data again using the `AddUserMessage` and `GetChatMessageContentAsync` function. + +```csharp +// Continue the conversation with a question. +chatHistory.AddUserMessage("I would like to know the ratio of the hikes I've done in Canada compared to other countries."); +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); + +chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 })); +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); +``` + +Customize the system prompt and change the request, asking for different questions (ex: How many times did you hiked when it was raining? How many times did you hiked in 2021? etc.) to see how the model responds. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + ## Explore the code Our application uses the `Azure.AI.OpenAI` client SDK, which is available on [NuGet](https://www.nuget.org/packages/Azure.AI.OpenAI), to send and receive requests to an Azure OpenAI service deployed in Azure. @@ -109,6 +193,8 @@ response = await openAIClient.GetChatCompletionsAsync(completionOptions); Customize the system prompt and change the request, asking for different questions (ex: How many times did you hiked when it was raining? How many times did you hiked in 2021? etc.) to see how the model responds. +:::zone-end + ## Clean up resources When you no longer need the sample application or resources, remove the corresponding deployment and all resources. diff --git a/docs/ai/quickstarts/quickstart-azure-openai-tool.md b/docs/ai/quickstarts/quickstart-azure-openai-tool.md index f855c873ad852..956a7d67ef190 100644 --- a/docs/ai/quickstarts/quickstart-azure-openai-tool.md +++ b/docs/ai/quickstarts/quickstart-azure-openai-tool.md @@ -1,18 +1,33 @@ --- title: Quickstart - Extend Azure AI using Tools and execute a local Function with .NET -description: Create a simple chat app using the .NET Azure OpenAI SDK and extend the model to execute a local function. +description: Create a simple chat app using Semantic Kernel or the .NET Azure OpenAI SDK and extend the model to execute a local function. ms.date: 03/04/2024 ms.topic: quickstart ms.custom: devx-track-dotnet, devx-track-dotnet-ai author: fboucher ms.author: frbouche +zone_pivot_groups: openai-library # CustomerIntent: As a .NET developer new to Azure OpenAI, I want deploy and use sample code to interact to learn from the sample code how to extend the model using Tools. --- # Extend Azure AI using Tools and execute a local Function with .NET + +:::zone target="docs" pivot="semantic-kernel" + + +Get started with Semantic Kernel by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `gpt-35-turbo` model deployed into an Azure OpenAI account, however using Tool to extend the model capabilities it will call a local function. Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `gpt-35-turbo` model deployed into an Azure OpenAI account, however using Tool to extend the model capabilities it will call a local function. Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK. +:::zone-end + [!INCLUDE [download-alert](includes/prerequisites-and-azure-deploy.md)] ## Try HikerAI Pro sample @@ -26,6 +41,91 @@ Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console c If you get an error message the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again. + +:::zone target="docs" pivot="semantic-kernel" + + +## Understand the code + +Our application uses the `Microsoft.SemanticKernel` package, which is available on [NuGet](https://www.nuget.org/packages/Microsoft.SemanticKernel), to send and receive requests to an Azure OpenAI service deployed in Azure. + +```csharp +// Add a new plugin with a local .NET function that should be available to the AI model +// For convenience and clarity of into the code, this standalone local method handles tool call responses. It will fake a call to a weather API and return the current weather for the specified location. +kernel.ImportPluginFromFunctions("WeatherPlugin", +[ + KernelFunctionFactory.CreateFromMethod(([Description("The city, e.g. Montreal, Sidney")] string location, string unit = null) => + { + // Here you would call a weather API to get the weather for the location + return "Periods of rain or drizzle, 15 C"; + }, "get_current_weather", "Get the current weather in a given location") +]); +``` + +The `Kernel` class facilitates the requests and responses with the help of `AddAzureOpenAIChatCompletion` service. + +```csharp +// Create a Kernel containing the Azure OpenAI Chat Completion Service +IKernelBuilder b = Kernel.CreateBuilder(); + +Kernel kernel = b + .AddAzureOpenAIChatCompletion(deployment, endpoint, key) + .Build(); +``` + +The entire application is contained within the **Program.cs** file. The first several lines of code loads up secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning. + +```csharp +var config = new ConfigurationBuilder().AddUserSecrets().Build(); +string endpoint = config["AZURE_OPENAI_ENDPOINT"]; +string deployment = config["AZURE_OPENAI_GPT_NAME"]; +string key = config["AZURE_OPENAI_KEY"]; +``` + +Once the `kernel` client is created, we provide more context to the model by adding a system prompt. This instructs the model how you'd like it to act during the conversation. Note how the weather is emphasized in the system prompt. + +```csharp +ChatHistory chatHistory = new(""" + You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly. + A good weather is important for a good hike. Only make recommendations if the weather is good or if people insist. + You introduce yourself when first saying hello. When helping people out, you always ask them + for this information to inform the hiking recommendation you provide: + + 1. Where they are located + 2. What hiking intensity they are looking for + + You will then provide three suggestions for nearby hikes that vary in length after you get that information. + You will also share an interesting fact about the local nature on the hikes when making a recommendation. + """); +``` + +Then you can add a user message to the model by using the `AddUserMessage` functon. + +To have the model generate a response based off the system prompt and the user request, use the `GetChatMessageContentAsync` function. + +```csharp +chatHistory.AddUserMessage(""" + Is the weather is good today for a hike? + If yes, I live in the greater Montreal area and would like an easy hike. I don't mind driving a bit to get there. + I don't want the hike to be over 10 miles round trip. I'd consider a point-to-point hike. + I want the hike to be as isolated as possible. I don't want to see many people. + I would like it to be as bug free as possible. + """); + +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); + +chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 })); +Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}"); +``` + +Customize the system prompt and user message to see how the model responds to help you find a hike that you'll like. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + ## Understand the code Our application uses the `Azure.AI.OpenAI` client SDK, which is available on [NuGet](https://www.nuget.org/packages/Azure.AI.OpenAI), to send and receive requests to an Azure OpenAI service deployed in Azure. @@ -179,6 +279,8 @@ Console.WriteLine($"\n\nAssistant >>> {assistantResponse.Content}"); Customize the system prompt and user message to see how the model responds to help you find a hike that you'll like. +:::zone-end + ## Clean up resources When you no longer need the sample application or resources, remove the corresponding deployment and all resources. diff --git a/docs/ai/quickstarts/quickstart-openai-generate-images.md b/docs/ai/quickstarts/quickstart-openai-generate-images.md index fe690431d4d06..4a0839e991e11 100644 --- a/docs/ai/quickstarts/quickstart-openai-generate-images.md +++ b/docs/ai/quickstarts/quickstart-openai-generate-images.md @@ -1,18 +1,33 @@ --- title: Quickstart - Generate images using Azure AI with .NET -description: Create a simple app using the .NET Azure OpenAI SDK to generate postal card images. +description: Create a simple app using Semantic Kernel or the .NET Azure OpenAI SDK to generate postal card images. ms.date: 03/04/2024 ms.topic: quickstart ms.custom: devx-track-dotnet, devx-track-dotnet-ai author: fboucher ms.author: frbouche +zone_pivot_groups: openai-library # CustomerIntent: As a .NET developer new to Azure OpenAI, I want deploy and use sample code to interact to learn how to generate images from the sample code. --- # Generate images using Azure AI with .NET + +:::zone target="docs" pivot="semantic-kernel" + + +Get started with Semantic Kernel by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `dell-e-3` model to generate postal card and invite your friends for a hike! Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `dell-e-3` model to generate postal card and invite your friends for a hike! Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK. +:::zone-end + [!INCLUDE [download-alert](includes/prerequisites-and-azure-deploy.md)] ## Trying Generate Hiking Images sample @@ -26,6 +41,54 @@ Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console c If you get an error message the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again. + +:::zone target="docs" pivot="semantic-kernel" + + +## Understanding the code + +Our application uses the `Microsoft.SemanticKernel` package, which is available on [NuGet](https://www.nuget.org/packages/Microsoft.SemanticKernel), to send and receive requests to an Azure OpenAI service deployed in Azure. + +The `AzureOpenAITextToImageService` service facilitates the requests and responses. + +```csharp +AzureOpenAITextToImageService textToImageService = new(deployment, endpoint, key, null); +``` + +The entire application is contained within the _Program.cs_ file. The first several lines of code load secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning. + +```csharp +// == Retrieve the local secrets saved during the Azure deployment ========== +var config = new ConfigurationBuilder() + .AddUserSecrets() + .Build(); + +var config = new ConfigurationBuilder().AddUserSecrets().Build(); +string endpoint = config["AZURE_OPENAI_ENDPOINT"]; +string deployment = config["AZURE_OPENAI_GPT_NAME"]; +string key = config["AZURE_OPENAI_KEY"]; +``` + +Once the `textToImageService` service is created, we we provide more context to the model by adding a system prompt. A good prompt to generate images requires a clear description: what is in the images, specific color to use, style (drawing, painting, realistic or cartoony). The model will use this prompt to generate the image. To have the model generate a response based off the user request, use the `GenerateImageAsync` function, and specify the size and quality. + +```csharp +// Generate the image +string imageUrl = await textToImageService.GenerateImageAsync(""" + A postal card with an happy hiker waving and a beautiful mountain in the background. + There is a trail visible in the foreground. + The postal card has text in red saying: 'You are invited for a hike!' + """, 1024, 1024); +Console.WriteLine($"The generated image is ready at:\n{imageUrl}"); +``` + +Customize the prompt to personalize the images generated by the model. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + ## Understanding the code Our application uses the `Azure.AI.OpenAI` client SDK, which is available on [NuGet](https://www.nuget.org/packages/Azure.AI.OpenAI), to send and receive requests to an Azure OpenAI service deployed in Azure. @@ -95,6 +158,8 @@ Console.WriteLine($"\n\nThe generated image is ready at:\n {generatedImage.Url.A Customize the prompt to personalize the images generated by the model. +:::zone-end + ## Clean up resources When you no longer need the sample application or resources, remove the corresponding deployment and all resources. diff --git a/docs/ai/quickstarts/quickstart-openai-summarize-text.md b/docs/ai/quickstarts/quickstart-openai-summarize-text.md index 1a418510964b6..fc220d1b5651f 100644 --- a/docs/ai/quickstarts/quickstart-openai-summarize-text.md +++ b/docs/ai/quickstarts/quickstart-openai-summarize-text.md @@ -1,18 +1,33 @@ --- title: Quickstart - Summarize text using Azure AI chat app with .NET -description: Create a simple chat app using the .NET Azure OpenAI SDK to summarize a text. +description: Create a simple chat app using Semantic Kernel or the .NET Azure OpenAI SDK to summarize a text. ms.date: 03/04/2024 ms.topic: quickstart ms.custom: devx-track-dotnet, devx-track-dotnet-ai author: fboucher ms.author: frbouche +zone_pivot_groups: openai-library # CustomerIntent: As a .NET developer new to Azure OpenAI, I want deploy and use sample code to interact to learn from the sample code to summarize text. --- # Summarize text using Azure AI chat app with .NET + +:::zone target="docs" pivot="semantic-kernel" + + +Get started with Semantic Kernel by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `gpt-35-turbo` model deployed into an Azure OpenAI account. Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `gpt-35-turbo` model deployed into an Azure OpenAI account. Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK. +:::zone-end + [!INCLUDE [download-alert](includes/prerequisites-and-azure-deploy.md)] ## Trying Hiking Benefits Summary sample @@ -26,6 +41,66 @@ Get started with the .NET Azure OpenAI SDK by creating a simple .NET 8 console c If you get an error message the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again. + +:::zone target="docs" pivot="semantic-kernel" + + +## Understanding the code + +Our application uses the `Microsoft.SemanticKernel` package, which is available on [NuGet](https://www.nuget.org/packages/Microsoft.SemanticKernel), to send and receive requests to an Azure OpenAI service deployed in Azure. + +The `Kernel` class facilitates the requests and responses with the help of `AddAzureOpenAIChatCompletion` service. + +```csharp +Kernel kernel = Kernel.CreateBuilder() + .AddAzureOpenAIChatCompletion(deployment, endpoint, key) + .Build(); +``` + +The entire application is contained within the **Program.cs** file. The first several lines of code loads up secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning. + +```csharp +// == Retrieve the local secrets saved during the Azure deployment ========== +var config = new ConfigurationBuilder() + .AddUserSecrets() + .Build(); +string endpoint = config["AZURE_OPENAI_ENDPOINT"]; +string deployment = config["AZURE_OPENAI_GPT_NAME"]; +string key = config["AZURE_OPENAI_KEY"]; + +// Create a Kernel containing the Azure OpenAI Chat Completion Service +Kernel kernel = Kernel.CreateBuilder() + .AddAzureOpenAIChatCompletion(deployment, endpoint, key) + .Build(); +``` + +Once the `Kernel` is created, we read the contents of the file `benefits.md` and create a `prompt` to ask the the model to summarize that text. + +```csharp +// Create and print out the prompt +string prompt = $""" + Please summarize the the following text in 20 words or less: + {File.ReadAllText("benefits.md")} + """; +Console.WriteLine($"user >>> {prompt}"); +``` + +To have the model generate a response based off `prompt`, use the `InvokePromptAsync` function. + +```csharp +// Submit the prompt and print out the response +string response = await kernel.InvokePromptAsync(prompt, new(new OpenAIPromptExecutionSettings() { MaxTokens = 400 })); +Console.WriteLine($"assistant >>> {response}"); +``` + +Customize the text content of the file or the length of the summary to see the differences in the responses. + +:::zone-end + + +:::zone target="docs" pivot="azure-openai-sdk" + + ## Understanding the code Our application uses the `Azure.AI.OpenAI` client SDK, which is available on [NuGet](https://www.nuget.org/packages/Azure.AI.OpenAI), to send and receive requests to an Azure OpenAI service deployed in Azure. @@ -84,6 +159,8 @@ Console.WriteLine($"\n\nAssistant >>> {assistantResponse.Content}"); Customize the text content of the file or the length of the summary to see the differences in the responses. +:::zone-end + ## Clean up resources When you no longer need the sample application or resources, remove the corresponding deployment and all resources. diff --git a/docs/zone-pivot-groups.yml b/docs/zone-pivot-groups.yml index 97592d6befae2..d86ac0984914b 100644 --- a/docs/zone-pivot-groups.yml +++ b/docs/zone-pivot-groups.yml @@ -112,3 +112,11 @@ groups: title: C# and Visual Basic - id: lang-fsharp title: F# +- id: openai-library + title: OpenAI Library + prompt: Choose a library for OpenAI + pivots: + - id: semantic-kernel + title: Semantic Kernel + - id: azure-openai-sdk + title: Azure OpenAI SDK