.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
 }
相关推荐
KG_LLM图谱增强大模型5 分钟前
Palantir 本体论与知识图谱深度分析及实现路径
人工智能·知识图谱
yzx9910136 分钟前
项目名称:灵犀——基于大模型与知识图谱的全栈智慧创作与协同平台
人工智能·知识图谱
RAG专家7 分钟前
【KG²RAG】结合知识图谱解决RAG 文本块孤立问题
人工智能·知识图谱·rag·检索增强生成
小袁进化之路8 分钟前
黎跃春讲AI智能体运营工程师核心知识图谱(2026完整版)
人工智能·知识图谱
CyberwayTech9 分钟前
赛博威线上营销费用管理:咨询+系统,双轮驱动ROI增长
大数据·人工智能
一粒黑子9 分钟前
【实测】GitNexus实测:拖入GitHub链接秒出代码知识图谱,今天涨了857星
人工智能·gpt·安全·ai·大模型·ai编程
chaofan9809 分钟前
GPT-5.5 领衔 Image 2.0:像素级控制时代,AI 绘图告别开盲盒
开发语言·人工智能·python·gpt·自动化·api
秋说10 分钟前
【知识图谱】大模型驱动多模态健康智能问诊深度剖析
人工智能·大模型·知识图谱·同态加密·差分隐私·智慧医疗
User_芊芊君子11 分钟前
【OpenAI 把 AI 玩明白了】:自主推理 + 动态知识图谱,这 4 个技术突破要颠覆行业
java·人工智能·知识图谱