MicrosoftLearning / mslearn-openai

https://microsoftlearning.github.io/mslearn-openai/
MIT License
214 stars 325 forks source link

ai-102 module 6 #63

Closed mctfabrice closed 6 months ago

mctfabrice commented 6 months ago

Step 5 Add code to use the Azure OpenAI service I try to update the code like you can see but i have error

image

// Implicit using statements are included using System.Text; using System.Text.Json; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Azure;

// Add Azure OpenAI package using Azure.AI.OpenAI;

// Build a config object and retrieve user settings. IConfiguration config = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); string? oaiEndpoint = config["AzureOAIEndpoint"]; string? oaiKey = config["AzureOAIKey"]; string? oaiDeploymentName = config["AzureOAIDeploymentName"];

if(string.IsNullOrEmpty(oaiEndpoint) || string.IsNullOrEmpty(oaiKey) || string.IsNullOrEmpty(oaiDeploymentName) ) { Console.WriteLine("Please check your appsettings.json file for missing or incorrect values."); return; }

// Initialize the Azure OpenAI client... // Initialize the Azure OpenAI client OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey));

// System message to provide context to the model string systemMessage = "I am a hiking enthusiast named Forest who helps people discover hikes in their area. If no area is specified, I will default to near Rainier National Park. I will then provide three suggestions for nearby hikes that vary in length. I will also share an interesting fact about the local nature on the hikes when making a recommendation.";

do { Console.WriteLine("Enter your prompt text (or type 'quit' to exit): "); string? inputText = Console.ReadLine(); if (inputText == "quit") break;

// Generate summary from Azure OpenAI
if (inputText == null) {
    Console.WriteLine("Please enter a prompt.");
    continue;
}

Console.WriteLine("\nSending request for summary to Azure OpenAI endpoint...\n\n");

// Add code to send request... // Build completion options object ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions() { Messages = { new ChatRequestSystemMessage(systemMessage), new ChatRequestUserMessage(inputText), }, MaxTokens = 400, Temperature = 0.7f, DeploymentName = oaiDeploymentName };

// Send request to Azure OpenAI model ChatCompletions response = client.GetChatCompletions(chatCompletionsOptions);

// Print the response string completion = response.Choices[0].Message.Content; Console.WriteLine("Response: " + completion + "\n");

} while (true);

ivorb commented 6 months ago

it's unclear from this what the issue is - it could be not installing the correct package, not including the library, or a different issue all together. Also, the title says module 6, but the code image looks like module 2?

ivorb commented 6 months ago

This lab tested fine in the hosted environment. We're unfortunately unable to debug issues specific to your use case - please retry this lab from the beginning, taking special note of adding the packages. If you find issues with the exercise when following all instructions, please open a new issue with more details.

mctfabrice commented 6 months ago

Hello all is ok now !!! Tks

On Mon, Apr 8, 2024, 4:42 PM Ivor Berry @.***> wrote:

This lab tested fine in the hosted environment. We're unfortunately unable to debug issues specific to your use case - please retry this lab from the beginning, taking special note of adding the packages. If you find issues with the exercise when following all instructions, please open a new issue with more details.

— Reply to this email directly, view it on GitHub https://github.com/MicrosoftLearning/mslearn-openai/issues/63#issuecomment-2042942884, or unsubscribe https://github.com/notifications/unsubscribe-auth/BHSAXLNWTS523TUIZT3APTLY4KUFTAVCNFSM6AAAAABFVYGS7WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBSHE2DEOBYGQ . You are receiving this because you authored the thread.Message ID: @.***>