You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SemanticKernelChat.cs 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Reflection.Metadata;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using LLama.Abstractions;
  5. using LLama.Common;
  6. using Microsoft.SemanticKernel;
  7. using Microsoft.SemanticKernel.AI.ChatCompletion;
  8. using Microsoft.SemanticKernel.AI.TextCompletion;
  9. using Microsoft.SemanticKernel.Connectors.AI.LLama.ChatCompletion;
  10. using Microsoft.SemanticKernel.Connectors.AI.LLama.TextCompletion;
  11. namespace LLama.Examples.NewVersion
  12. {
  13. public class SemanticKernelChat
  14. {
  15. public static async Task Run()
  16. {
  17. Console.WriteLine("Example from: https://github.com/microsoft/semantic-kernel/blob/main/dotnet/README.md");
  18. Console.Write("Please input your model path: ");
  19. var modelPath = Console.ReadLine();
  20. // Load weights into memory
  21. var parameters = new ModelParams(modelPath)
  22. {
  23. Seed = RandomNumberGenerator.GetInt32(int.MaxValue),
  24. };
  25. using var model = LLamaWeights.LoadFromFile(parameters);
  26. using var context = model.CreateContext(parameters);
  27. var ex = new InteractiveExecutor(context);
  28. //var builder = new KernelBuilder();
  29. //builder.WithAIService<IChatCompletion>("local-llama", new LLamaSharpChatCompletion(ex), true);
  30. //var kernel = builder.Build();
  31. var chatGPT = new LLamaSharpChatCompletion(ex);
  32. var chatHistory = chatGPT.CreateNewChat("You are a librarian, expert about books");
  33. Console.WriteLine("Chat content:");
  34. Console.WriteLine("------------------------");
  35. chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");
  36. await MessageOutputAsync(chatHistory);
  37. // First bot assistant message
  38. string reply = await chatGPT.GenerateMessageAsync(chatHistory);
  39. chatHistory.AddAssistantMessage(reply);
  40. await MessageOutputAsync(chatHistory);
  41. // Second user message
  42. chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion");
  43. await MessageOutputAsync(chatHistory);
  44. // Second bot assistant message
  45. reply = await chatGPT.GenerateMessageAsync(chatHistory);
  46. chatHistory.AddAssistantMessage(reply);
  47. await MessageOutputAsync(chatHistory);
  48. }
  49. /// <summary>
  50. /// Outputs the last message of the chat history
  51. /// </summary>
  52. private static Task MessageOutputAsync(Microsoft.SemanticKernel.AI.ChatCompletion.ChatHistory chatHistory)
  53. {
  54. var message = chatHistory.Messages.Last();
  55. Console.WriteLine($"{message.Role}: {message.Content}");
  56. Console.WriteLine("------------------------");
  57. return Task.CompletedTask;
  58. }
  59. }
  60. }