您的位置:首页 > 房产 > 建筑 > 中国市场营销培训网_一个产品的宣传和推广方案_河北seo网络优化师_百度快照收录入口

中国市场营销培训网_一个产品的宣传和推广方案_河北seo网络优化师_百度快照收录入口

2025/1/10 21:32:03 来源:https://blog.csdn.net/u011234288/article/details/144989914  浏览:    关键词:中国市场营销培训网_一个产品的宣传和推广方案_河北seo网络优化师_百度快照收录入口
中国市场营销培训网_一个产品的宣传和推广方案_河北seo网络优化师_百度快照收录入口

资源及介绍接上篇

nuget引用以下组件

效果展示:

内存和cpu占有:

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

 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 closepublic 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}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com