|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.SemanticKernel; |
| 8 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 9 | +using Microsoft.SemanticKernel.TextGeneration; |
| 10 | + |
| 11 | +// List of available models |
| 12 | +Dictionary<int, ModelDefinition> bedrockModels = GetBedrockModels(); |
| 13 | + |
| 14 | +// Get user choice |
| 15 | +int choice = GetUserChoice(); |
| 16 | + |
| 17 | +switch (choice) |
| 18 | +{ |
| 19 | + case 1: |
| 20 | + await PerformChatCompletion().ConfigureAwait(false); |
| 21 | + break; |
| 22 | + case 2: |
| 23 | + await PerformTextGeneration().ConfigureAwait(false); |
| 24 | + break; |
| 25 | + case 3: |
| 26 | + await PerformStreamChatCompletion().ConfigureAwait(false); |
| 27 | + break; |
| 28 | + case 4: |
| 29 | + await PerformStreamTextGeneration().ConfigureAwait(false); |
| 30 | + break; |
| 31 | +} |
| 32 | + |
| 33 | +async Task PerformChatCompletion() |
| 34 | +{ |
| 35 | + string userInput; |
| 36 | + ChatHistory chatHistory = new(); |
| 37 | + |
| 38 | + // Get available chat completion models |
| 39 | + var availableChatModels = bedrockModels.Values |
| 40 | + .Where(m => m.Modalities.Contains(ModelDefinition.SupportedModality.ChatCompletion)) |
| 41 | + .ToDictionary(m => bedrockModels.Single(kvp => kvp.Value.Name == m.Name).Key, m => m.Name); |
| 42 | + |
| 43 | + // Show user what models are available and let them choose |
| 44 | + int chosenModel = GetModelNumber(availableChatModels, "chat completion"); |
| 45 | + |
| 46 | + var kernel = Kernel.CreateBuilder().AddBedrockChatCompletionService(availableChatModels[chosenModel]).Build(); |
| 47 | + var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>(); |
| 48 | + |
| 49 | + do |
| 50 | + { |
| 51 | + Console.Write("Enter a prompt (or leave empty to quit): "); |
| 52 | + userInput = Console.ReadLine() ?? string.Empty; |
| 53 | + |
| 54 | + if (!string.IsNullOrEmpty(userInput)) |
| 55 | + { |
| 56 | + chatHistory.AddMessage(AuthorRole.User, userInput); |
| 57 | + var result = await chatCompletionService.GetChatMessageContentsAsync(chatHistory).ConfigureAwait(false); |
| 58 | + string output = ""; |
| 59 | + foreach (var message in result) |
| 60 | + { |
| 61 | + output += message.Content; |
| 62 | + Console.WriteLine($"Chat Completion Answer: {message.Content}"); |
| 63 | + Console.WriteLine(); |
| 64 | + } |
| 65 | + |
| 66 | + chatHistory.AddMessage(AuthorRole.Assistant, output); |
| 67 | + } |
| 68 | + } while (!string.IsNullOrEmpty(userInput)); |
| 69 | +} |
| 70 | + |
| 71 | +async Task PerformTextGeneration() |
| 72 | +{ |
| 73 | + // Get available text generation models |
| 74 | + var availableTextGenerationModels = bedrockModels.Values |
| 75 | + .Where(m => m.Modalities.Contains(ModelDefinition.SupportedModality.TextCompletion)) |
| 76 | + .ToDictionary(m => bedrockModels.Single(kvp => kvp.Value.Name == m.Name).Key, m => m.Name); |
| 77 | + |
| 78 | + // Show user what models are available and let them choose |
| 79 | + int chosenTextGenerationModel = GetModelNumber(availableTextGenerationModels, "text generation"); |
| 80 | + |
| 81 | + Console.Write("Text Generation Prompt: "); |
| 82 | + string userTextPrompt = Console.ReadLine() ?? ""; |
| 83 | + |
| 84 | + var kernel = Kernel.CreateBuilder().AddBedrockTextGenerationService(availableTextGenerationModels[chosenTextGenerationModel]).Build(); |
| 85 | + |
| 86 | + var textGenerationService = kernel.GetRequiredService<ITextGenerationService>(); |
| 87 | + var textGeneration = await textGenerationService.GetTextContentsAsync(userTextPrompt).ConfigureAwait(false); |
| 88 | + if (textGeneration.Count > 0) |
| 89 | + { |
| 90 | + var firstTextContent = textGeneration[0]; |
| 91 | + if (firstTextContent != null) |
| 92 | + { |
| 93 | + Console.WriteLine("Text Generation Answer: " + firstTextContent.Text); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + Console.WriteLine("Text Generation Answer: (none)"); |
| 98 | + } |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + Console.WriteLine("Text Generation Answer: (No output text)"); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +async Task PerformStreamChatCompletion() |
| 107 | +{ |
| 108 | + string userInput; |
| 109 | + ChatHistory streamChatHistory = new(); |
| 110 | + |
| 111 | + // Get available streaming chat completion models |
| 112 | + var availableStreamingChatModels = bedrockModels.Values |
| 113 | + .Where(m => m.Modalities.Contains(ModelDefinition.SupportedModality.ChatCompletion) && m.CanStream) |
| 114 | + .ToDictionary(m => bedrockModels.Single(kvp => kvp.Value.Name == m.Name).Key, m => m.Name); |
| 115 | + |
| 116 | + // Show user what models are available and let them choose |
| 117 | + int chosenStreamChatCompletionModel = GetModelNumber(availableStreamingChatModels, "stream chat completion"); |
| 118 | + |
| 119 | + var kernel = Kernel.CreateBuilder().AddBedrockChatCompletionService(availableStreamingChatModels[chosenStreamChatCompletionModel]).Build(); |
| 120 | + var chatStreamCompletionService = kernel.GetRequiredService<IChatCompletionService>(); |
| 121 | + |
| 122 | + do |
| 123 | + { |
| 124 | + Console.Write("Enter a prompt (or leave empty to quit): "); |
| 125 | + userInput = Console.ReadLine() ?? string.Empty; |
| 126 | + |
| 127 | + if (!string.IsNullOrEmpty(userInput)) |
| 128 | + { |
| 129 | + streamChatHistory.AddMessage(AuthorRole.User, userInput); |
| 130 | + var result = chatStreamCompletionService.GetStreamingChatMessageContentsAsync(streamChatHistory).ConfigureAwait(false); |
| 131 | + string output = ""; |
| 132 | + await foreach (var message in result) |
| 133 | + { |
| 134 | + Console.Write($"{message.Content}"); |
| 135 | + output += message.Content; |
| 136 | + } |
| 137 | + |
| 138 | + Console.WriteLine(); |
| 139 | + streamChatHistory.AddMessage(AuthorRole.Assistant, output); |
| 140 | + } |
| 141 | + } while (!string.IsNullOrEmpty(userInput)); |
| 142 | +} |
| 143 | + |
| 144 | +async Task PerformStreamTextGeneration() |
| 145 | +{ |
| 146 | + // Get available streaming text generation models |
| 147 | + var availableStreamingTextGenerationModels = bedrockModels.Values |
| 148 | + .Where(m => m.Modalities.Contains(ModelDefinition.SupportedModality.TextCompletion) && m.CanStream) |
| 149 | + .ToDictionary(m => bedrockModels.Single(kvp => kvp.Value.Name == m.Name).Key, m => m.Name); |
| 150 | + |
| 151 | + // Show user what models are available and let them choose |
| 152 | + int chosenStreamTextGenerationModel = GetModelNumber(availableStreamingTextGenerationModels, "stream text generation"); |
| 153 | + |
| 154 | + Console.Write("Stream Text Generation Prompt: "); |
| 155 | + string userStreamTextPrompt = Console.ReadLine() ?? ""; |
| 156 | + |
| 157 | + var kernel = Kernel.CreateBuilder().AddBedrockTextGenerationService(availableStreamingTextGenerationModels[chosenStreamTextGenerationModel]).Build(); |
| 158 | + |
| 159 | + var streamTextGenerationService = kernel.GetRequiredService<ITextGenerationService>(); |
| 160 | + var streamTextGeneration = streamTextGenerationService.GetStreamingTextContentsAsync(userStreamTextPrompt).ConfigureAwait(true); |
| 161 | + await foreach (var textContent in streamTextGeneration) |
| 162 | + { |
| 163 | + Console.Write(textContent.Text); |
| 164 | + } |
| 165 | + |
| 166 | + Console.WriteLine(); |
| 167 | +} |
| 168 | + |
| 169 | +// Get the user's model choice |
| 170 | +int GetUserChoice() |
| 171 | +{ |
| 172 | + int pick; |
| 173 | + |
| 174 | + // Display the available options |
| 175 | + Console.WriteLine("Choose an option:"); |
| 176 | + Console.WriteLine("1. Chat Completion"); |
| 177 | + Console.WriteLine("2. Text Generation"); |
| 178 | + Console.WriteLine("3. Stream Chat Completion"); |
| 179 | + Console.WriteLine("4. Stream Text Generation"); |
| 180 | + |
| 181 | + Console.Write("Enter your choice (1-4): "); |
| 182 | + while (!int.TryParse(Console.ReadLine(), out pick) || pick < 1 || pick > 4) |
| 183 | + { |
| 184 | + Console.WriteLine("Invalid input. Please enter a valid number from the list."); |
| 185 | + Console.Write("Enter your choice (1-4): "); |
| 186 | + } |
| 187 | + |
| 188 | + return pick; |
| 189 | +} |
| 190 | + |
| 191 | +int GetModelNumber(Dictionary<int, string> availableModels, string serviceType) |
| 192 | +{ |
| 193 | + int chosenModel; |
| 194 | + |
| 195 | + // Display the model options |
| 196 | + Console.WriteLine($"Available {serviceType} models:"); |
| 197 | + foreach (var option in availableModels) |
| 198 | + { |
| 199 | + Console.WriteLine($"{option.Key}. {option.Value}"); |
| 200 | + } |
| 201 | + |
| 202 | + Console.Write($"Enter the number of the model you want to use for {serviceType}: "); |
| 203 | + while (!int.TryParse(Console.ReadLine(), out chosenModel) || !availableModels.ContainsKey(chosenModel)) |
| 204 | + { |
| 205 | + Console.WriteLine("Invalid input. Please enter a valid number from the list."); |
| 206 | + Console.Write($"Enter the number of the model you want to use for {serviceType}: "); |
| 207 | + } |
| 208 | + |
| 209 | + return chosenModel; |
| 210 | +} |
| 211 | + |
| 212 | +Dictionary<int, ModelDefinition> GetBedrockModels() |
| 213 | +{ |
| 214 | + return new Dictionary<int, ModelDefinition> |
| 215 | + { |
| 216 | + { 1, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "anthropic.claude-v2", CanStream = true } }, |
| 217 | + { 2, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "anthropic.claude-v2:1", CanStream = true } }, |
| 218 | + { 3, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "anthropic.claude-instant-v1", CanStream = false } }, |
| 219 | + { 4, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "anthropic.claude-3-sonnet-20240229-v1:0", CanStream = false } }, |
| 220 | + { 5, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "anthropic.claude-3-haiku-20240307-v1:0", CanStream = false } }, |
| 221 | + { 6, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.TextCompletion], Name = "cohere.command-light-text-v14", CanStream = false } }, |
| 222 | + { 7, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.TextCompletion], Name = "cohere.command-text-v14", CanStream = false } }, |
| 223 | + { 8, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "cohere.command-r-v1:0", CanStream = true } }, |
| 224 | + { 9, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "cohere.command-r-plus-v1:0", CanStream = true } }, |
| 225 | + { 10, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "ai21.jamba-instruct-v1:0", CanStream = true } }, |
| 226 | + { 11, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.TextCompletion], Name = "ai21.j2-mid-v1", CanStream = false } }, |
| 227 | + { 12, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.TextCompletion], Name = "ai21.j2-ultra-v1", CanStream = false } }, |
| 228 | + { 13, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "meta.llama3-8b-instruct-v1:0", CanStream = true } }, |
| 229 | + { 14, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "meta.llama3-70b-instruct-v1:0", CanStream = true } }, |
| 230 | + { 15, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "mistral.mistral-7b-instruct-v0:2", CanStream = true } }, |
| 231 | + { 16, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "mistral.mixtral-8x7b-instruct-v0:1", CanStream = true } }, |
| 232 | + { 17, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "mistral.mistral-large-2402-v1:0", CanStream = true } }, |
| 233 | + { 18, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "mistral.mistral-small-2402-v1:0", CanStream = true } }, |
| 234 | + { 19, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "amazon.titan-text-lite-v1", CanStream = true } }, |
| 235 | + { 20, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "amazon.titan-text-express-v1", CanStream = true } }, |
| 236 | + { 21, new ModelDefinition { Modalities = [ModelDefinition.SupportedModality.ChatCompletion, ModelDefinition.SupportedModality.TextCompletion], Name = "amazon.titan-text-premier-v1:0", CanStream = true } } |
| 237 | + }; |
| 238 | +} |
| 239 | + |
| 240 | +/// <summary> |
| 241 | +/// ModelDefinition. |
| 242 | +/// </summary> |
| 243 | +internal struct ModelDefinition |
| 244 | +{ |
| 245 | + /// <summary> |
| 246 | + /// List of services that the model supports. |
| 247 | + /// </summary> |
| 248 | + internal List<SupportedModality> Modalities { get; set; } |
| 249 | + /// <summary> |
| 250 | + /// Model ID. |
| 251 | + /// </summary> |
| 252 | + internal string Name { get; set; } |
| 253 | + /// <summary> |
| 254 | + /// If the model supports streaming. |
| 255 | + /// </summary> |
| 256 | + internal bool CanStream { get; set; } |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// The services the model supports. |
| 260 | + /// </summary> |
| 261 | + internal enum SupportedModality |
| 262 | + { |
| 263 | + /// <summary> |
| 264 | + /// Text completion service. |
| 265 | + /// </summary> |
| 266 | + TextCompletion, |
| 267 | + /// <summary> |
| 268 | + /// Chat completion service. |
| 269 | + /// </summary> |
| 270 | + ChatCompletion |
| 271 | + } |
| 272 | +} |
0 commit comments