Skip to content

Latest commit

 

History

History
202 lines (140 loc) · 7.24 KB

quickstart-ai-chat-with-data.md

File metadata and controls

202 lines (140 loc) · 7.24 KB
title description ms.date ms.topic ms.custom author ms.author zone_pivot_groups
Quickstart - Get insight about your data from a .NET AI chat app
Create a simple chat app using your data, Semantic Kernel, and OpenAI.
07/17/2024
quickstart
devx-track-dotnet, devx-track-dotnet-ai
fboucher
frbouche
openai-library

Get insight about your data from a .NET AI chat app

:::zone target="docs" pivot="openai"

Get started with AI development using a .NET 8 console app to connect to an OpenAI gpt-3.5-turbo model. You'll connect to the AI model using Semantic Kernel to analyze hiking data and provide insights.

[!INCLUDE download-alert] :::zone-end

:::zone target="docs" pivot="azure-openai"

Get started with AI development using a .NET 8 console app to connect to an OpenAI gpt-3.5-turbo model deployed on Azure. You'll connect to the AI model using Semantic Kernel to analyze hiking data and provide insights.

[!INCLUDE download-alert] :::zone-end

Get the sample project

[!INCLUDE clone-sample-repo]

:::zone target="docs" pivot="azure-openai"

[!INCLUDE deploy-azd]

:::zone-end

Try the hiking chat sample

:::zone target="docs" pivot="openai"

  1. From a terminal or command prompt, navigate to the openai\03-ChattingAboutMyHikes directory.

  2. Run the following commands to configure your OpenAI API key as a secret for the sample app:

    dotnet user-secrets init
    dotnet user-secrets set OpenAIKey <your-openai-key>
  3. Use the dotnet run command to run the app:

    dotnet run
    

:::zone-end

:::zone target="docs" pivot="azure-openai"

  1. From a terminal or command prompt, navigate to the azure-openai\02-HikerAI directory.

  2. Use the dotnet run command to run the app:

    dotnet run
    

    [!TIP] If you get an error message, the Azure OpenAI resources might not have finished deploying. Wait a couple of minutes and try again.

:::zone-end

Explore the code

:::zone target="docs" pivot="openai"

The application uses the Microsoft.SemanticKernel package to send and receive requests to an OpenAI service.

The entire application is contained within the Program.cs file. The first several lines of code set configuration values and gets the OpenAI Key that was previously set using the dotnet user-secrets command.

var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string model = "gpt-3.5-turbo";
string key = config["OpenAIKey"];

The OpenAIChatCompletionService service facilitates the requests and responses.

// Create the OpenAI Chat Completion Service
OpenAIChatCompletionService service = new(model, key);

Once the OpenAIChatCompletionService client is created, the app reads the content of the file hikes.md and uses it to provide more context to the model by adding a system prompt. This influences model behavior and the generated completions during the conversation. :::zone-end

:::zone target="docs" pivot="azure-openai"

The application uses the Microsoft.SemanticKernel package to send and receive requests to an Azure OpenAI service deployed in Azure.

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.

// == Retrieve the local secrets saved during the Azure deployment ==========
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
string deployment = config["AZURE_OPENAI_GPT_NAME"];
string key = config["AZURE_OPENAI_KEY"];

The AzureOpenAIChatCompletionService service facilitates the requests and responses.

// == Create the Azure OpenAI Chat Completion Service  ==========
AzureOpenAIChatCompletionService service = new(deployment, endpoint, key);

Once the OpenAIChatCompletionService client is created, the app reads the content of the file hikes.md and uses it to provide more context to the model by adding a system prompt. This influences model behavior and the generated completions during the conversation. :::zone-end

// 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}");

The following code adds a user prompt to the model using the AddUserMessage function. The GetChatMessageContentAsync function instructs the model to generate a response based off the system and user prompts.

// 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}");

The app adds the response from the model to the chatHistory to maintain the chat history or context.

// 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 or user prompts to provide different questions and context:

  • How many times did I hike when it was raining?
  • How many times did I hike in 2021?

The model generates a relevant response to each prompt based on your inputs.

:::zone target="docs" pivot="azure-openai"

Clean up resources

When you no longer need the sample application or resources, remove the corresponding deployment and all resources.

azd down

[!INCLUDE troubleshoot]

:::zone-end

Next steps