.NET AI 开发人员库 --AI Dev Gallery简单示例--问答机器人

资源及介绍接上篇

nuget引用以下组件

效果展示:

内存和cpu占有:

代码如下:路径换成自己的模型路径 模型请从上篇文尾下载

cs 复制代码
 internal class Program
 {
     private static CancellationTokenSource? cts;
     private static IChatClient? model;
     private static List<Message> Messages { get; } = [];
     static async Task Main(string[] args)
     {
         bool bRet = SetConsoleCtrlHandler(cancelHandler, true);
         GenAIModel.InitializeGenAI();

         model = await GenAIModel.CreateAsync(@"D:\microsoft--Phi-3.5-mini-instruct-onnx\main\cpu_and_mobile\cpu-int4-awq-block-128-acc-level-4", new LlmPromptTemplate
         {
             System = "<|system|>\n{
  
  {CONTENT}}<|end|>\n",
             User = "<|user|>\n{
  
  {CONTENT}}<|end|>\n",
             Assistant = "<|assistant|>\n{
  
  {CONTENT}}<|end|>\n",
             Stop = ["<|system|>", "<|user|>", "<|assistant|>", "<|end|>"]
         });
         Console.WriteLine("Enter your prompt (Press Shift + Enter to insert a newline)");
         while (true)
         {
             var txt = "";
             txt = Console.ReadLine();
             if (!string.IsNullOrEmpty(txt))
                 AddMessage(txt);
         }
     }

     private static void AddMessage(string text)
     {
         if (model == null)
         {
             return;
         }

         Messages.Add(new Message(text.Trim(), DateTime.Now, ChatRole.User));

         Task.Run(async () =>
         {
             var history = Messages.Select(m => new ChatMessage(m.Role, m.Content)).ToList();

             var responseMessage = new Message(string.Empty, DateTime.Now, ChatRole.Assistant);

             Messages.Add(responseMessage);

             cts = new CancellationTokenSource();

             history.Insert(0, new ChatMessage(ChatRole.System, "You are a helpful assistant"));
             Console.WriteLine(responseMessage);
             await foreach (var messagePart in model.CompleteStreamingAsync(history, null, cts.Token))
             {
                 var part = messagePart;
                 responseMessage.Content += part;
                 Console.Write(part);
             }
             Console.WriteLine("***************************END***************************");
             cts?.Dispose();
             cts = null;
         });
     }

     #region close
     public delegate bool ControlCtrlDelegate(int CtrlType);
     [DllImport("kernel32.dll")]
     private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerAppClose, bool Add);
     private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerAppClose);

     /// <summary>
     /// 关闭窗口时的事件
     /// </summary>
     /// <param name="CtrlType"></param>
     /// <returns></returns>
     static bool HandlerAppClose(int CtrlType)
     {
         cts?.Cancel();
         cts?.Dispose();
         cts = null;
         model?.Dispose();
         return false;
     }

     #endregion
 }
相关推荐
l1t7 分钟前
张泽鹏先生手搓的纯ANSI处理UTF-8与美团龙猫调用expat库读取Excel xml对比测试
xml·人工智能·excel·utf8·expat
THMAIL10 分钟前
量化基金从小白到大师 - 金融数据获取大全:从免费API到Tick级数据实战指南
人工智能·python·深度学习·算法·机器学习·金融·kafka
zzywxc78710 分钟前
AI在金融、医疗、教育、制造业等领域的落地案例(含代码、流程图、Prompt示例与图表)
人工智能·spring·机器学习·金融·数据挖掘·prompt·流程图
周末程序猿1 小时前
谈谈Vibe编程(氛围编程)
人工智能
水印云2 小时前
AI配音工具哪个好用?7款热门配音软件推荐指南!
人工智能·语音识别
Luke Ewin2 小时前
FunASR的Java实现Paraformer实时语音识别 | 一款无需联网的本地实时字幕软件
java·人工智能·语音识别·asr·funasr·paraformer·sensevoice
先做个垃圾出来………2 小时前
PyTorch 模型文件介绍
人工智能·pytorch·python
浅醉樱花雨2 小时前
vosk语音识别实战
人工智能·python·语音识别·asr·vosk
IoT砖家涂拉拉2 小时前
从“找新家”到“走向全球”,布尔云携手涂鸦智能开启机器人新冒险
人工智能·机器人·ai助手·ai智能体·ai机器人
xz2024102****2 小时前
最大似然估计:损失函数的底层数学原理
人工智能·算法·机器学习·概率论