.net 调用 llama.cpp
using LLama;
using LLama.Common;
using LLama.Sampling;

// 引用 LLamaSharp, LLamaSharp.Backend.Cpu
// 这个例子中,内存使用的 4 个G

// Llava 调用的例子,新版本中被移除
// scisharp.github.io/LLamaSharp/0.25.0/QuickStart/
// https://github.com/SciSharp/LLamaSharp/issues/1194

var modelPath = @"/Users/iox/Downloads/MiniCPM-V-4_5-Q4_0.gguf"; // 引用的 model 路径
// var multiModalProj = @"/Users/iox/Downloads/mmproj-model-f16.gguf";
// var modelImage = @"/Users/iox/Downloads/截屏2025-11-11 02.24.48.png";

var parameters = new ModelParams(modelPath)
{
    ContextSize = 1024, // The longest length of chat as memory.
    GpuLayerCount = 0, // How many layers to offload to GPU. Please adjust it according to your GPU memory.
};

using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);

// Llava Init
// https://github.com/SciSharp/LLamaSharp/issues/1255, Llava 已经被移至 mtmd, 新的版本中还没有提供。等新版本出来再研究
// using var clipModel = LLavaWeights.LoadFromFile(multiModalProj);
// var executor = new InteractiveExecutor(context, clipModel);s
var executor = new InteractiveExecutor(context);

var system = @"你是一位名叫小瓜的通用生活 AI 助手,你的主要目标是成为用户日常生活中最值得信赖、最有趣、最全能的帮手。";

// Add chat histories as prompt to tell AI how to act.
var chatHistory = new ChatHistory();
chatHistory.AddMessage(AuthorRole.System, system);
// chatHistory.AddMessage(AuthorRole.User, "Hello, Bob.");
// chatHistory.AddMessage(AuthorRole.Assistant, "Hello. How may I help you today?");

ChatSession session = new(executor, chatHistory);

var inferenceParams = new InferenceParams
{
    MaxTokens = 256, // 最大回复数,如果指定了合适的停止词(AntiPrompts)也可以不设置该值
    AntiPrompts = new List<string> { "User:", }, // 停止词,防止模型生成无意义的内容。比如 AI 自动完成 user 的输出
    SamplingPipeline = new DefaultSamplingPipeline(),
};

Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("The chat session has started.\nUser: ");
Console.ForegroundColor = ConsoleColor.Green;
var userInput = Console.ReadLine() ?? "";

while (userInput != "exit")
{
    // Generate the response streamingly.
    await foreach (var text in session.ChatAsync(new ChatHistory.Message(AuthorRole.User, userInput), inferenceParams))
    {
        Console.ForegroundColor = ConsoleColor.White;
        Console.Write(text);
    }

    Console.ForegroundColor = ConsoleColor.Green;
    userInput = Console.ReadLine() ?? "";
}
上一篇
下一篇