成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C# 下的LLamaSharp: 高效的本地LLM推理庫,自己寫GPT

人工智能
為了獲得高性能,LLamaSharp 與從 C++ 編譯的本地庫交互,這些庫稱為 backends。我們為 Windows、Linux 和 Mac 提供了 CPU、CUDA、Metal 和 OpenCL 的后端包。您不需要編譯任何 C++ 代碼,只需安裝后端包即可。

LLamaSharp 是一個跨平臺庫,用于在本地設備上運行 LLaMA/LLaVA 模型(以及其他模型)?;?llama.cpp,LLamaSharp 在 CPU 和 GPU 上的推理都非常高效。通過高級 API 和 RAG 支持,您可以方便地在應用程序中部署大型語言模型(LLM)。

GitHub 地址

https://github.com/SciSharp/LLamaSharp

圖片圖片

下載代碼:

git clone https://github.com/SciSharp/LLamaSharp.git

快速開始

安裝

為了獲得高性能,LLamaSharp 與從 C++ 編譯的本地庫交互,這些庫稱為 backends。我們為 Windows、Linux 和 Mac 提供了 CPU、CUDA、Metal 和 OpenCL 的后端包。您不需要編譯任何 C++ 代碼,只需安裝后端包即可。

安裝 LLamaSharp 包:

PM> Install-Package LLamaSharp

圖片圖片

安裝一個或多個后端包,或使用自編譯的后端:

  • LLamaSharp.Backend.Cpu: 適用于 Windows、Linux 和 Mac 的純 CPU 后端。支持 Mac 的 Metal (GPU)。
  • LLamaSharp.Backend.Cuda11: 適用于 Windows 和 Linux 的 CUDA 11 后端。
  • LLamaSharp.Backend.Cuda12: 適用于 Windows 和 Linux 的 CUDA 12 后端。
  • LLamaSharp.Backend.OpenCL: 適用于 Windows 和 Linux 的 OpenCL 后端。

(可選)對于 Microsoft semantic-kernel 集成,安裝 LLamaSharp.semantic-kernel 包。

(可選)要啟用 RAG 支持,安裝 LLamaSharp.kernel-memory 包(該包僅支持 net6.0 或更高版本),該包基于 Microsoft kernel-memory 集成。

模型準備

LLamaSharp 使用 GGUF 格式的模型文件,可以從 PyTorch 格式(.pth)和 Huggingface 格式(.bin)轉換而來。獲取 GGUF 文件有兩種方式:

  1. 在 Huggingface 搜索模型名稱 + 'gguf',找到已經轉換好的模型文件。
  2. 自行將 PyTorch 或 Huggingface 格式轉換為 GGUF 格式。請按照 llama.cpp readme 中的說明使用 Python 腳本進行轉換。

一般來說,我們推薦下載帶有量化的模型,因為它顯著減少了所需的內存大小,同時對生成質量的影響很小。

簡單對話

LLamaSharp 提供了一個簡單的控制臺演示,展示了如何使用該庫進行推理。以下是一個基本示例:

圖片圖片

using LLama.Common;
using LLama;


namespace appLLama
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Chart();
        }


        static async Task Chart()
        {
            string modelPath = @"E:\Models\llama-2-7b-chat.Q4_K_M.gguf"; // change it to your own model path.


            var parameters = new ModelParams(modelPath)
            {
                ContextSize = 1024, // The longest length of chat as memory.
                GpuLayerCount = 5 // 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);
            var executor = new InteractiveExecutor(context);


            // Add chat histories as prompt to tell AI how to act.
            var chatHistory = new ChatHistory();
            chatHistory.AddMessage(AuthorRole.System, "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.");
            chatHistory.AddMessage(AuthorRole.User, "Hello, Bob.");
            chatHistory.AddMessage(AuthorRole.Assistant, "Hello. How may I help you today?");


            ChatSession session = new(executor, chatHistory);


            InferenceParams inferenceParams = new InferenceParams()
            {
                MaxTokens = 256, // No more than 256 tokens should appear in answer. Remove it if antiprompt is enough for control.
                AntiPrompts = new List<string> { "User:" } // Stop generation once antiprompts appear.
            };


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


            while (userInput != "exit")
            {
                await foreach ( // Generate the response streamingly.
                    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() ?? "";
            }
        }
    }
}
  1. 模型路徑與參數設置:指定模型路徑,以及上下文的大小和 GPU 層的數量。
  2. 加載模型并創建上下文:從文件中加載模型,并使用參數初始化上下文。
  3. 執行器與對話歷史記錄:定義一個 InteractiveExecutor,并設置初始的對話歷史,包括系統消息和用戶與助手的初始對話。
  4. 會話與推理參數:建立對話會話 ChatSession,設置推理參數,包括最大 token 數和反提示語。
  5. 用戶輸入與生成回復:開始聊天會話并處理用戶輸入,使用異步方法流式地生成助手的回復,并根據反提示語停止生成。

圖片圖片

你會發現中文支持不太好,即使用了千問的量化庫。

中文處理官方例子

我這換成了千問的庫:

using LLama.Common;
using LLama;
using System.Text;


namespace appLLama
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Register provider for GB2312 encoding
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            Run();
        }


        private static string ConvertEncoding(string input, Encoding original, Encoding target)
        {
            byte[] bytes = original.GetBytes(input);
            var convertedBytes = Encoding.Convert(original, target, bytes);
            return target.GetString(convertedBytes);
        }


        public static async Task Run()
        {
            // Register provider for GB2312 encoding
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);


            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("This example shows how to use Chinese with gb2312 encoding, which is common in windows. It's recommended" +
                " to use https://huggingface.co/hfl/chinese-alpaca-2-7b-gguf/blob/main/ggml-model-q5_0.gguf, which has been verified by LLamaSharp developers.");
            Console.ForegroundColor = ConsoleColor.White;


            string modelPath = @"E:\LMModels\ay\Repository\qwen1_5-7b-chat-q8_0.gguf";// @"E:\Models\llama-2-7b-chat.Q4_K_M.gguf";


            var parameters = new ModelParams(modelPath)
            {
                ContextSize = 1024,
                Seed = 1337,
                GpuLayerCount = 5,
                Encoding = Encoding.UTF8
            };
            using var model = LLamaWeights.LoadFromFile(parameters);
            using var context = model.CreateContext(parameters);
            var executor = new InteractiveExecutor(context);


            ChatSession session;
            ChatHistory chatHistory = new ChatHistory();


            session = new ChatSession(executor, chatHistory);


            session
                .WithHistoryTransform(new LLamaTransforms.DefaultHistoryTransform());


            InferenceParams inferenceParams = new InferenceParams()
            {
                Temperature = 0.9f,
                AntiPrompts = new List<string> { "用戶:" }
            };


            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("The chat session has started.");


            // show the prompt
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("用戶:");
            Console.ForegroundColor = ConsoleColor.Green;
            string userInput = Console.ReadLine() ?? "";


            while (userInput != "exit")
            {
                // Convert the encoding from gb2312 to utf8 for the language model
                // and later saving to the history json file.
                userInput = ConvertEncoding(userInput, Encoding.GetEncoding("gb2312"), Encoding.UTF8);


                if (userInput == "save")
                {
                    session.SaveSession("chat-with-kunkun-chinese");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Session saved.");
                }
                else if (userInput == "regenerate")
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Regenerating last response ...");


                    await foreach (
                        var text
                        in session.RegenerateAssistantMessageAsync(
                            inferenceParams))
                    {
                        Console.ForegroundColor = ConsoleColor.White;


                        // Convert the encoding from utf8 to gb2312 for the console output.
                        Console.Write(ConvertEncoding(text, Encoding.UTF8, Encoding.GetEncoding("gb2312")));
                    }
                }
                else
                {
                    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() ?? "";


                Console.ForegroundColor = ConsoleColor.White;
            }
        }
    }
}

圖片圖片

Winform寫 一個簡單例子

圖片圖片

Chat類:

public class Chat
{
    ChatSession session;




    InferenceParams inferenceParams = new InferenceParams()
        {
            Temperature = 0.9f,
            AntiPrompts = new List<string> { "用戶:" }
        };


    private  string ConvertEncoding(string input, Encoding original, Encoding target)
        {
            byte[] bytes = original.GetBytes(input);
            var convertedBytes = Encoding.Convert(original, target, bytes);
            return target.GetString(convertedBytes);
        }


    public void Init()
        {
            // Register provider for GB2312 encoding
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);


            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("This example shows how to use Chinese with gb2312 encoding, which is common in windows. It's recommended" +
                " to use https://huggingface.co/hfl/chinese-alpaca-2-7b-gguf/blob/main/ggml-model-q5_0.gguf, which has been verified by LLamaSharp developers.");
            Console.ForegroundColor = ConsoleColor.White;


            string modelPath = @"E:\LMModels\ay\Repository\qwen1_5-7b-chat-q8_0.gguf";// @"E:\Models\llama-2-7b-chat.Q4_K_M.gguf";


            var parameters = new ModelParams(modelPath)
            {
                ContextSize = 1024,
                Seed = 1337,
                GpuLayerCount = 5,
                Encoding = Encoding.UTF8
            };


            var model = LLamaWeights.LoadFromFile(parameters);
            var context = model.CreateContext(parameters);
            var executor = new InteractiveExecutor(context);
            var chatHistory = new ChatHistory();
            session = new ChatSession(executor, chatHistory);
            session
                .WithHistoryTransform(new LLamaTransforms.DefaultHistoryTransform());
        }


    public async Task Run(string userInput,Action<string> callback)
        {
            while (userInput != "exit")
            {
                userInput = ConvertEncoding(userInput, Encoding.GetEncoding("gb2312"), Encoding.UTF8);


                if (userInput == "save")
                {
                    session.SaveSession("chat-with-kunkun-chinese");


                }
                else if (userInput == "regenerate")
                {
                    await foreach (
                        var text
                        in session.RegenerateAssistantMessageAsync(
                            inferenceParams))
                    {
                        callback(ConvertEncoding(text, Encoding.UTF8, Encoding.GetEncoding("gb2312")));
                    }
                }
                else
                {
                    await foreach (
                        var text
                        in session.ChatAsync(
                            new ChatHistory.Message(AuthorRole.User, userInput),
                            inferenceParams))
                    {
                        callback(text);
                    }
                }


                userInput = "";
            }
        }
}

Form1界面事件:

public partial class Form1 : Form
{
    Chat chat = new Chat();
    public Form1()
    {
        InitializeComponent();
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        chat.Init();
    }


    private  void btnSend_Click(object sender, EventArgs e)
    {
        var call = new Action<string>(x =>
        {
            this.Invoke(() =>
            {
                txtLog.AppendText(x);
            });
        });
        //chat.Run(txtMsg.Text, call);


        Task.Run(() =>
        {
            chat.Run(txtMsg.Text, call);
        });


    }
}

更新例子可以去官網上看,寫的比較專業。

https://scisharp.github.io/LLamaSharp/0.13.0/

責任編輯:武曉燕 來源: 技術老小子
相關推薦

2024-08-13 08:23:43

LLamaSharpLLM推理庫

2024-03-25 14:22:07

大型語言模型GaLore

2024-12-27 09:08:25

2024-02-26 07:43:10

大語言模型LLM推理框架

2011-11-21 14:10:53

C#

2011-07-06 09:46:56

C#

2009-08-07 16:19:00

C#下數據庫編程

2024-03-12 10:05:47

大型語言模型

2009-08-07 16:19:00

C#下數據庫編程

2009-07-31 16:45:23

ASP.NET數據庫操

2023-05-09 06:54:34

2025-02-25 10:21:15

2023-11-30 15:56:54

大型語言模型人工智能

2023-11-03 13:07:00

AI模型

2011-02-23 08:50:22

C#.NETdynamic

2023-09-01 15:22:49

人工智能數據

2021-06-26 16:24:21

Linux命令系統

2009-08-12 17:27:11

C#讀取文件

2025-04-24 10:26:40

2025-06-23 08:30:05

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产1区 | 国产激情偷乱视频一区二区三区 | 亚洲精品综合一区二区 | 日韩成人在线视频 | 亚洲国产精品视频一区 | 四虎影视一区二区 | 日韩一区二区在线免费观看 | 日本久久网| 亚洲激情av| 天天干天天干 | a视频在线观看 | 国产偷录视频叫床高潮对白 | 欧美黑人一级爽快片淫片高清 | 日本韩国电影免费观看 | 欧美日韩91 | 天天干天天操天天爽 | 浮生影院免费观看中文版 | 国产精品久久影院 | 91久久国产综合久久 | 国产精品久久久 | 中文字幕成人 | 亚洲午夜精品视频 | 精品国产一区二区三区性色av | 久久精品一 | 成人亚洲精品 | av色站| 亚洲精品免费观看 | 色偷偷888欧美精品久久久 | 黄色毛片在线看 | 青青草原精品99久久精品66 | 中文字幕福利 | 色婷婷国产精品综合在线观看 | 日韩一区精品 | 一区二区视频在线 | 国产精品夜夜春夜夜爽久久电影 | 亚洲成人免费视频在线 | 日韩影音 | 久久精品国产亚洲一区二区三区 | 精品国模一区二区三区欧美 | 久久久成 | 国产精品成人一区 |