.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
 }
相关推荐
神仙别闹1 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
nju_spy1 分钟前
周志华《机器学习导论》第8章 集成学习 Ensemble Learning
人工智能·随机森林·机器学习·集成学习·boosting·bagging·南京大学
静心问道25 分钟前
TrOCR: 基于Transformer的光学字符识别方法,使用预训练模型
人工智能·深度学习·transformer·多模态
说私域27 分钟前
基于开源AI大模型、AI智能名片与S2B2C商城小程序源码的用户价值引导与核心用户沉淀策略研究
人工智能·开源
亲持红叶28 分钟前
GLU 变种:ReGLU 、 GEGLU 、 SwiGLU
人工智能·深度学习·神经网络·激活函数
说私域28 分钟前
线上协同办公时代:以开源AI大模型等工具培养网感,拥抱职业变革
人工智能·开源
群联云防护小杜30 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
摘星编程35 分钟前
构建智能客服Agent:从需求分析到生产部署
人工智能·需求分析·智能客服·agent开发·生产部署
不爱学习的YY酱38 分钟前
信息检索革命:Perplexica+cpolar打造你的专属智能搜索中枢
人工智能
向宇it1 小时前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质