.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
 }
相关推荐
ai小鬼头1 小时前
AIStarter新版重磅来袭!永久订阅限时福利抢先看
人工智能·开源·github
说私域1 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
飞哥数智坊2 小时前
新版定价不够用,Cursor如何退回旧版定价
人工智能·cursor
12点一刻2 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务2 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
JNU freshman3 小时前
计算机视觉速成 之 概述
人工智能·计算机视觉
说私域4 小时前
基于开源AI大模型AI智能名片S2B2C商城小程序源码的私域流量新生态构建
人工智能·开源
HollowKnightZ4 小时前
目标姿态估计综述:Deep Learning-Based Object Pose Estimation: A Comprehensive Survey
人工智能·深度学习
算家计算5 小时前
“28项评测23项SOTA——GLM-4.1V-9B-Thinking本地部署教程:10B级视觉语言模型的性能天花板!
人工智能·开源