microsoft / Generative-AI-for-beginners-dotnet

Five lessons, learn how to really apply AI to your .NET Applications
https://aka.ms/genainet
MIT License
847 stars 571 forks source link

Azure.AI.Inference vs. Azure.AI.OpenAI with Azure AI Foundry #85

Closed dstarr closed 1 week ago

dstarr commented 1 week ago

I am working on the second lab here.

I have created a Hub and Project in Azure AI Foundry as per the first lab guided. Now I want to talk to the model I deployed.

The following code works:

// here we're calling the API using the Azure.AI.OpenAI namespace
Console.WriteLine("===============================");
ChatClient client = new AzureOpenAIClient(endpoint, credential)
                        .GetChatClient(azureAiModel);
ClientResult<ChatCompletion> response = await client.CompleteChatAsync(prompt.ToString());
foreach (var contentPart in response.Value.Content)
{
    Console.WriteLine(contentPart.Text);
}

The following code throw a "Resource not found" exception using the same endpoint, credential, and model.

// here we're calling the API using the Azure.AI.Inference namespace
Console.WriteLine("===============================");
IChatClient inferenceClient = new ChatCompletionsClient(endpoint, credential).AsChatClient(azureAiModel);
ChatResponse inferenceResponse = await inferenceClient.GetResponseAsync(prompt.ToString());
Console.WriteLine(inferenceResponse.Message);

Console.WriteLine("===============================");

Does this mean using Azure.AI.Inference will not work with AI Foundry? Your instructions seem to indicate it should work. Speciafially, you state about the Inference code:

If you want to use Azure AI Foundry you can use the same code, but you will need to change the endpoint and the credentials.

My full code file is here.

Thank you for having a look.

github-actions[bot] commented 1 week ago

👋 Thanks for contributing @dstarr! We will review the issue and get back to you soon.

mcp-net commented 1 week ago

### try to follow pattern:

var endpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_ENDPOINT")); var credential = new AzureKeyCredential(System.Environment.GetEnvironmentVariable("AZURE_AI_CHAT_KEY"));

var client = new ChatCompletionsClient(endpoint, credential, new AzureAIInferenceClientOptions());

var requestOptions = new ChatCompletionsOptions() { Messages = { new ChatRequestSystemMessage("You are a helpful assistant."), new ChatRequestUserMessage("How many feet are in a mile?"), }, };

Response response = client.CompleteAsync(requestOptions); System.Console.WriteLine(response.Value.Content);