赞
踩
目录
推荐一个C#大模型推理开源项目,让你轻松驾驭私有化部署!
LLama是Meta发布的一个免费开源的大模型,是一个有着上百亿数量级参数的大语言模型,支持CPU和GPU两种方式。
而LLamaSharp就是针对llama.cpp封装的C#版本,让方便我们基于C#开发应用,让我们不需要自己编译llama.cpp。它的性能接近llama.cpp,支持Windows、Linux和MAC。
该项目提供了多种Web、WebAPI、控制台等多个Demo版本。
1、安装依赖
Install-Package LLamaSharp
2、演示源码
-
- using LLama.Common;
- using LLama;
-
- string modelPath = "..\\LLama.Unittest\\Models\\llama-2-7b-chat.Q4_0.gguf";
- var prompt = " I want a C # function to do binary search of an array , please show me the code."; //提示词
-
- // 加载模型
- var parameters = new ModelParams(modelPath)
- {
- ContextSize = 1024,
- Seed = 1337,
- GpuLayerCount = 5
- };
- using var model = LLamaWeights.LoadFromFile(parameters);
-
- // 初始化聊天会话
- using var context = model.CreateContext(parameters);
- var ex = new InteractiveExecutor(context);
- ChatSession session = new ChatSession(ex);
-
- // 显示提示
- Console.WriteLine();
- Console.Write(prompt);
-
- // 循环运行推理以与LLM聊天
- while (prompt != "stop")
- {
- foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
- {
- Console.Write(text);
- }
- prompt = Console.ReadLine();
- }
-
- // 保存会话
- session.SaveSession("SavedSessionPath");

3、效果
https://github.com/SciSharp/LLamaSharp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。