.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
 }
相关推荐
weixin_4334176714 小时前
阿里视频大模型wan2.7‑t2v,可以生成视频
人工智能·python·音视频
宣宣猪的小花园.14 小时前
【控制算法】PID 不只是三个参数:比例、积分、微分各在解决什么问题
人工智能·嵌入式硬件·机器学习
sukioe14 小时前
一张图、两把锁:AI 物流履约平台的确定性边界设计
人工智能·python·ai·langchain
爱吃火鸡面呀14 小时前
OpenCV 实战:文档扫描与神经风格迁移完整指南
人工智能·opencv·计算机视觉
TechEdu20260614 小时前
[人工智能]Gemini(Google DeepMind):多模态模型、智能体与工程实践
人工智能·ai
java1234_小锋14 小时前
YOLO26 计算机视觉 - 训练自己的 YOLO26 模型
人工智能·yolo·机器学习·计算机视觉·yolo26
小程序设计14 小时前
机械设计之大蒜茎叶粉碎抛送装置设计
人工智能·数据挖掘
满怀冰雪15 小时前
23-PaddleClas 数据集、配置文件与训练参数详解
人工智能·python·深度学习·paddlepaddle
Akir.weiwen15 小时前
③ 约束显化:把隐含的语义假设变成显式规则
人工智能·编译·设计规范·语义
tachibana215 小时前
AI Agent 的记忆机制
人工智能·ai·大模型·llm·agent