|
1 | | -using OpenAI.Chat; |
2 | | -using OpenAI; |
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Microsoft.Extensions.AI; |
3 | 3 | using Microsoft.SemanticKernel; |
4 | | -using Microsoft.SemanticKernel.ChatCompletion; |
| 4 | +using OpenAI; |
5 | 5 |
|
6 | 6 | var builder = WebApplication.CreateBuilder(args); |
7 | 7 |
|
8 | 8 | builder.AddServiceDefaults(); |
9 | 9 |
|
10 | | -builder.AddAzureOpenAIClient("openai"); |
| 10 | +var aiType = builder.Configuration["AI:Type"] ?? "ollama"; |
| 11 | +var chatDeploymentName = builder.Configuration["AI:ChatDeploymentName"] ?? "chat"; |
| 12 | + |
| 13 | +switch (aiType.ToLower()) |
| 14 | +{ |
| 15 | + case "ollama": |
| 16 | + builder.AddOllamaSharpChatClient(chatDeploymentName); |
| 17 | + break; |
| 18 | + case "azureopenai": |
| 19 | + builder.AddAzureOpenAIClient(chatDeploymentName); |
| 20 | + builder.Services.AddChatClient(services => services.GetRequiredService<OpenAIClient>() |
| 21 | + .AsChatClient(chatDeploymentName)); |
| 22 | + break; |
| 23 | + default: |
| 24 | + throw new InvalidOperationException($"Unsupported AI type: {aiType}"); |
| 25 | +} |
11 | 26 |
|
12 | | -var chatDeploymentName = builder.Configuration["AI_ChatDeploymentName"] ?? "chat"; |
13 | | -builder.Services.AddKernel() |
14 | | - .AddAzureOpenAIChatCompletion(chatDeploymentName); |
| 27 | +builder.Services.AddKernel(); |
15 | 28 |
|
16 | | -// Add services to the container. |
17 | 29 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle |
18 | 30 | builder.Services.AddEndpointsApiExplorer(); |
19 | 31 | builder.Services.AddSwaggerGen(); |
|
33 | 45 |
|
34 | 46 | app.UseHttpsRedirection(); |
35 | 47 |
|
36 | | -app.MapGet("/weatherforecast", (OpenAIClient client) => |
| 48 | +app.MapGet("/weatherforecast", async (IChatClient client) => |
37 | 49 | { |
38 | | - var forecast = Enumerable.Range(1, 5).Select(index => |
| 50 | + async IAsyncEnumerable<WeatherForecast> GetForecasts() |
39 | 51 | { |
40 | | - var temperature = Random.Shared.Next(-20, 55); |
41 | | - var summary = GetWeatherSummary(client, temperature); |
42 | | - return new WeatherForecast |
43 | | - ( |
44 | | - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), |
45 | | - temperature, |
46 | | - summary |
47 | | - ); |
48 | | - }) |
49 | | - .ToArray(); |
50 | | - return forecast; |
51 | | - |
52 | | - static string GetWeatherSummary(OpenAIClient client, int temp) |
| 52 | + for (int index = 1; index <= 5; index++) |
| 53 | + { |
| 54 | + var temperature = Random.Shared.Next(-20, 55); |
| 55 | + var summary = await GetWeatherSummary(client, temperature); |
| 56 | + yield return new WeatherForecast |
| 57 | + ( |
| 58 | + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), |
| 59 | + temperature, |
| 60 | + summary |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return GetForecasts(); |
| 66 | + |
| 67 | + static async Task<string> GetWeatherSummary(IChatClient client, int temp) |
53 | 68 | { |
54 | | - var chatClient = client.GetChatClient("chat"); |
55 | | - ChatCompletion completion = chatClient.CompleteChat( |
56 | | - [ |
57 | | - // System messages represent instructions or other guidance about how the assistant should behave |
58 | | - new SystemChatMessage("You are a helpful assistant that provides a description of the weather in one word based on the temperature."), |
59 | | - // User messages represent user input, whether historical or the most recen tinput |
60 | | - new UserChatMessage($"How would you describe the weather at temp {temp} in celcius? Provide the response in 1 word with no punctuation."), |
61 | | - // Assistant messages in a request represent conversation history for responses |
62 | | - ] |
63 | | - ); |
64 | | - |
65 | | - return $"{completion.Content[0].Text}"; |
| 69 | + List<ChatMessage> conversation = new() |
| 70 | + { |
| 71 | + // System messages represent instructions or other guidance about how the assistant should behave |
| 72 | + new(ChatRole.System, "You are a helpful assistant that provides a description of the weather in one word based on the temperature."), |
| 73 | + // User messages represent user input, whether historical or the most recent input |
| 74 | + new(ChatRole.User, $"How would you describe the weather at temp {temp} in celcius? Provide the response in 1 word with no punctuation.") |
| 75 | + }; |
| 76 | + var completion = await client.CompleteAsync(conversation); |
| 77 | + |
| 78 | + return $"{completion.Message.Text}"; |
66 | 79 | } |
67 | 80 | }) |
68 | 81 | .WithName("GetWeatherForecast") |
|
0 commit comments