Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66cea27

Browse files
committed
Update Program.cs for Azure/OpenAI integration
Refactor using directives and remove commented-out AI client registrations. Enhance chat client setup with endpoint and API key configurations, and add guidance for using Visual Studio's "Manage User Secrets." Modify service registration to include logging and function invocation, and ensure consistent formatting in the HTTP request pipeline.
1 parent 8d9bf09 commit 66cea27

File tree

2 files changed

+82
-41
lines changed
  • common/microsoft-extensions-ai-integration/AIPromptIntegration

2 files changed

+82
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
1-
using AIPromptIntegration.Components;
1+
using AIPromptIntegration.Components;
22
using Microsoft.Extensions.AI;
3-
using Azure;
4-
using OpenAI;
3+
4+
// Optional model provider implementations:
5+
// Azure models
6+
using Azure;
57
using Azure.AI.OpenAI;
8+
// OpenAI models
9+
using OpenAI;
10+
// GitHub models
11+
using System.ClientModel;
612

713
var builder = WebApplication.CreateBuilder(args);
814

915
// Add services to the container.
1016
builder.Services.AddRazorComponents()
11-
.AddInteractiveServerComponents();
17+
.AddInteractiveServerComponents();
1218

1319
builder.Services.AddTelerikBlazor();
1420

15-
#region Azure AI Inference Client registration
16-
17-
//builder.Services.AddChatClient(
18-
// new Azure.AI.Inference.ChatCompletionsClient(
19-
// new Uri("https://models.inference.ai.azure.com"),
20-
// new AzureKeyCredential("YOUR_AZURE_OPENAI_CREDENTIAL")
21-
// ).AsChatClient("Phi-3.5-MoE-instruct"));
22-
23-
#endregion Azure AI Inference Client registration
24-
25-
#region OpenAI Client registration
26-
27-
//builder.Services.AddSingleton(new OpenAIClient("YOUR_API_KEY"));
28-
29-
//builder.Services.AddChatClient(services => services.GetRequiredService<OpenAIClient>().AsChatClient("YOUR_MODEL_NAME"));
30-
31-
#endregion OpenAI Client registration
32-
33-
#region Azure OpenAI Client registration
34-
35-
//builder.Services.AddSingleton(new AzureOpenAIClient(
36-
// new Uri("YOUR_AZURE_OPENAI_ENDPOINT"),
37-
// new AzureKeyCredential("YOUR_AZURE_OPENAI_CREDENTIAL")));
38-
39-
//builder.Services.AddChatClient(services => services.GetRequiredService<AzureOpenAIClient>().AsChatClient("gpt-4o-mini"));
40-
41-
#endregion Azure OpenAI Client registration
42-
43-
#region Ollama Chat Client registration
44-
45-
//builder.Services.AddChatClient(new OllamaChatClient(new Uri("THE_URI_OF_YOUR_CLIENT"), "llama3.1"));
46-
47-
#endregion Ollama Chat Client registration
21+
// You will need to set the endpoint and key to your own values
22+
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line 💻:
23+
// cd this-project-directory
24+
// dotnet user-secrets set Endpoint https://YOUR-DEPLOYMENT-NAME.openai.azure.com
25+
// dotnet user-secrets set ApiKey super-secret-api-key
26+
27+
// 🌐 The Uri of your provider
28+
var endpoint = builder.Configuration["Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAi:Endpoint. See the README for details.");
29+
// 🔑 The API Key for your provider
30+
var apikey = builder.Configuration["ApiKey"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAi:ApiKey. See the README for details.");
31+
// 🧠 The model name or azure deployment name
32+
var model = "YOUR_MODEL_NAME";
33+
34+
// Replace the innerClient below with your preferred model provider
35+
var innerClient = new OpenAIClient(
36+
new ApiKeyCredential(apikey),
37+
new OpenAIClientOptions()
38+
{
39+
Endpoint = new Uri(endpoint)
40+
}
41+
).AsChatClient(model);
42+
43+
builder.Services.AddChatClient(innerClient) // 🤖 Add the configured chat client
44+
.UseFunctionInvocation() // 🛠️ Include tool calling
45+
.UseLogging(); //🐞 Include Logging
4846

4947
var app = builder.Build();
5048

5149
// Configure the HTTP request pipeline.
5250
if (!app.Environment.IsDevelopment())
5351
{
54-
app.UseExceptionHandler("/Error", createScopeForErrors: true);
55-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
56-
app.UseHsts();
52+
app.UseExceptionHandler("/Error", createScopeForErrors: true);
53+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
54+
app.UseHsts();
5755
}
5856

5957
app.UseHttpsRedirection();
@@ -62,6 +60,6 @@
6260
app.UseAntiforgery();
6361

6462
app.MapRazorComponents<App>()
65-
.AddInteractiveServerRenderMode();
63+
.AddInteractiveServerRenderMode();
6664

6765
app.Run();

common/microsoft-extensions-ai-integration/AIPromptIntegration/readme.md

+43
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,46 @@ To run the project successfully, you need to provide your endpoint and credentia
2626
3. Replace the placeholder values with your actual endpoint and credentials.
2727
4. Run the application and interact with the `AIPrompt` component.
2828

29+
### Client Registrations
30+
31+
**Azure AI Inference Client registration**
32+
33+
```csharp
34+
var innerClient = new Azure.AI.Inference.ChatCompletionsClient(
35+
new Uri(endpoint),
36+
new AzureKeyCredential(apikey)
37+
).AsChatClient(model);
38+
```
39+
40+
**Azure OpenAI Client registration**
41+
42+
```csharp
43+
var innerClient = new AzureOpenAIClient(
44+
new Uri(endpoint),
45+
new AzureKeyCredential(apikey)
46+
).AsChatClient(model);
47+
```
48+
49+
**OpenAI Client registration**
50+
51+
```csharp
52+
var innerClient = new OpenAIClient(apikey)
53+
.AsChatClient(model);
54+
```
55+
56+
**GitHub Models Client registration**
57+
58+
```csharp
59+
var innerClient = new OpenAIClient(
60+
new ApiKeyCredential(apikey),
61+
new OpenAIClientOptions()
62+
{
63+
Endpoint = new Uri(endpoint)
64+
}
65+
).AsChatClient(model);
66+
```
67+
68+
**Ollama Client registration**
69+
```csharp
70+
var innerClient = new OllamaChatClient(new Uri(endpoint), model);
71+
```

0 commit comments

Comments
 (0)