You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 10 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # LLamaSharp - .NET Binding for llama.cpp
  2. ![logo](Assets/LLamaSharpLogo.png)
  3. [![Discord](https://img.shields.io/discord/1106946823282761851?label=Discord)](https://discord.gg/7wNVU65ZDY)
  4. [![QQ Group](https://img.shields.io/static/v1?label=QQ&message=加入QQ群&color=brightgreen)](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726)
  5. [![LLamaSharp Badge](https://img.shields.io/nuget/v/LLamaSharp?label=LLamaSharp)](https://www.nuget.org/packages/LLamaSharp)
  6. [![LLamaSharp Badge](https://img.shields.io/nuget/v/LLamaSharp.Backend.Cpu?label=LLamaSharp.Backend.Cpu)](https://www.nuget.org/packages/LLamaSharp.Backend.Cpu)
  7. [![LLamaSharp Badge](https://img.shields.io/nuget/v/LLamaSharp.Backend.Cuda11?label=LLamaSharp.Backend.Cuda11)](https://www.nuget.org/packages/LLamaSharp.Backend.Cuda11)
  8. [![LLamaSharp Badge](https://img.shields.io/nuget/v/LLamaSharp.Backend.Cuda12?label=LLamaSharp.Backend.Cuda12)](https://www.nuget.org/packages/LLamaSharp.Backend.Cuda12)
  9. [![LLamaSharp Badge](https://img.shields.io/nuget/v/LLamaSharp.Backend.MacMetal?label=LLamaSharp.Backend.MacMetal)](https://www.nuget.org/packages/LLamaSharp.Backend.MacMetal)
  10. [![LLamaSharp Badge](https://img.shields.io/nuget/v/LLamaSharp.semantic-kernel?label=LLamaSharp.semantic-kernel)](https://www.nuget.org/packages/LLamaSharp.semantic-kernel)
  11. **The C#/.NET binding of [llama.cpp](https://github.com/ggerganov/llama.cpp). It provides higher-level APIs to inference the LLaMA Models and deploy it on local device with C#/.NET. It works on
  12. both Windows, Linux and MAC without requirment for compiling llama.cpp yourself. Even without GPU or not enought GPU memory, you can still apply LLaMA models well with this repo. 🤗**
  13. **Furthermore, it provides integrations with other projects such as [semantic-kernel](https://github.com/microsoft/semantic-kernel), [kernel-memory](https://github.com/microsoft/kernel-memory) and [BotSharp](https://github.com/SciSharp/BotSharp) to provide higher-level applications.**
  14. ## Documentation
  15. - [Quick start](https://scisharp.github.io/LLamaSharp/0.5/GetStarted/)
  16. - [Tricks for FAQ](https://scisharp.github.io/LLamaSharp/0.5/Tricks/)
  17. - [Full documentation](https://scisharp.github.io/LLamaSharp/0.5/)
  18. - [API reference](https://scisharp.github.io/LLamaSharp/0.5/xmldocs/)
  19. - [Examples](./LLama.Examples/NewVersion/)
  20. ## Installation
  21. Firstly, search `LLamaSharp` in nuget package manager and install it.
  22. ```
  23. PM> Install-Package LLamaSharp
  24. ```
  25. Then, search and install one of the following backends:
  26. ```
  27. LLamaSharp.Backend.Cpu # cpu for windows, linux and mac (mac metal is also supported)
  28. LLamaSharp.Backend.Cuda11 # cuda11 for windows and linux
  29. LLamaSharp.Backend.Cuda12 # cuda12 for windows and linux
  30. LLamaSharp.Backend.MacMetal # special for using mac metal
  31. ```
  32. If you would like to use it with [microsoft semantic-kernel](https://github.com/microsoft/semantic-kernel), please search and install the following package:
  33. ```
  34. LLamaSharp.semantic-kernel
  35. ```
  36. ### Tips for choosing a version
  37. In general, there may be some break changes between two minor releases, for example 0.5.1 and 0.6.0. On the contrary, we don't introduce API break changes in patch release. Therefore it's recommended to keep the highest patch version of a minor release. For example, keep 0.5.6 instead of 0.5.3.
  38. ### Mapping from LLamaSharp to llama.cpp
  39. Here's the mapping of them and corresponding model samples provided by `LLamaSharp`. If you're not sure which model is available for a version, please try our sample model.
  40. The llama.cpp commit id will help if you want to compile a DLL yourself.
  41. | LLamaSharp.Backend | LLamaSharp | Verified Model Resources | llama.cpp commit id |
  42. | - | - | -- | - |
  43. | - | v0.2.0 | This version is not recommended to use. | - |
  44. | - | v0.2.1 | [WizardLM](https://huggingface.co/TheBloke/wizardLM-7B-GGML/tree/previous_llama), [Vicuna (filenames with "old")](https://huggingface.co/eachadea/ggml-vicuna-13b-1.1/tree/main) | - |
  45. | v0.2.2 | v0.2.2, v0.2.3 | [WizardLM](https://huggingface.co/TheBloke/wizardLM-7B-GGML/tree/previous_llama_ggmlv2), [Vicuna (filenames without "old")](https://huggingface.co/eachadea/ggml-vicuna-13b-1.1/tree/main) | 63d2046 |
  46. | v0.3.0, v0.3.1 | v0.3.0, v0.4.0 | [LLamaSharpSamples v0.3.0](https://huggingface.co/AsakusaRinne/LLamaSharpSamples/tree/v0.3.0), [WizardLM](https://huggingface.co/TheBloke/wizardLM-7B-GGML/tree/main) | 7e4ea5b |
  47. | v0.4.1-preview (cpu only) | v0.4.1-preview | [Open llama 3b](https://huggingface.co/SlyEcho/open_llama_3b_ggml), [Open Buddy](https://huggingface.co/OpenBuddy/openbuddy-llama-ggml)| aacdbd4 |
  48. | v0.4.2-preview (cpu,cuda11) |v0.4.2-preview | [Llama2 7b GGML](https://huggingface.co/TheBloke/llama-2-7B-Guanaco-QLoRA-GGML)| 3323112 |
  49. | v0.5.1 | v0.5.1 | [Llama2 7b GGUF](https://huggingface.co/TheBloke/llama-2-7B-Guanaco-QLoRA-GGUF)| 6b73ef1 |
  50. | v0.6.0 | v0.6.0 | | [cb33f43](https://github.com/ggerganov/llama.cpp/commit/cb33f43a2a9f5a5a5f8d290dd97c625d9ba97a2f) |
  51. | v0.7.0 | v0.7.0 | | [207b519](https://github.com/ggerganov/llama.cpp/commit/207b51900e15cc7f89763a3bb1c565fe11cbb45d) |
  52. Many hands make light work. If you have found any other model resource that could work for a version, we'll appreciate it for opening an PR about it! 😊
  53. We publish the backend with cpu, cuda11 and cuda12 because they are the most popular ones. If none of them matches, please compile the [llama.cpp](https://github.com/ggerganov/llama.cpp)
  54. from source and put the `libllama` under your project's output path ([guide](https://scisharp.github.io/LLamaSharp/0.5/ContributingGuide/)).
  55. ## FAQ
  56. 1. GPU out of memory: Please try setting `n_gpu_layers` to a smaller number.
  57. 2. Unsupported model: `llama.cpp` is under quick development and often has break changes. Please check the release date of the model and find a suitable version of LLamaSharp to install, or use the model we provide [on huggingface](https://huggingface.co/AsakusaRinne/LLamaSharpSamples).
  58. ## Quick Start
  59. #### Model Inference and Chat Session
  60. LLamaSharp provides two ways to run inference: `LLamaExecutor` and `ChatSession`. The chat session is a higher-level wrapping of the executor and the model. Here's a simple example to use chat session.
  61. ```cs
  62. using LLama.Common;
  63. using LLama;
  64. string modelPath = "<Your model path>"; // change it to your own model path
  65. var prompt = "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.\r\n\r\nUser: Hello, Bob.\r\nBob: Hello. How may I help you today?\r\nUser: Please tell me the largest city in Europe.\r\nBob: Sure. The largest city in Europe is Moscow, the capital of Russia.\r\nUser:"; // use the "chat-with-bob" prompt here.
  66. // Load a model
  67. var parameters = new ModelParams(modelPath)
  68. {
  69. ContextSize = 1024,
  70. Seed = 1337,
  71. GpuLayerCount = 5
  72. };
  73. using var model = LLamaWeights.LoadFromFile(parameters);
  74. // Initialize a chat session
  75. using var context = model.CreateContext(parameters);
  76. var ex = new InteractiveExecutor(context);
  77. ChatSession session = new ChatSession(ex);
  78. // show the prompt
  79. Console.WriteLine();
  80. Console.Write(prompt);
  81. // run the inference in a loop to chat with LLM
  82. while (prompt != "stop")
  83. {
  84. foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
  85. {
  86. Console.Write(text);
  87. }
  88. prompt = Console.ReadLine();
  89. }
  90. // save the session
  91. session.SaveSession("SavedSessionPath");
  92. ```
  93. #### Quantization
  94. The following example shows how to quantize the model. With LLamaSharp you needn't to compile c++ project and run scripts to quantize the model, instead, just run it in C#.
  95. ```cs
  96. string srcFilename = "<Your source path>";
  97. string dstFilename = "<Your destination path>";
  98. string ftype = "q4_0";
  99. if(Quantizer.Quantize(srcFileName, dstFilename, ftype))
  100. {
  101. Console.WriteLine("Quantization succeed!");
  102. }
  103. else
  104. {
  105. Console.WriteLine("Quantization failed!");
  106. }
  107. ```
  108. For more usages, please refer to [Examples](./LLama.Examples).
  109. #### Web API
  110. We provide [the integration of ASP.NET core](./LLama.WebAPI) and a [web app demo](./LLama.Web). Please clone the repo to have a try.
  111. Since we are in short of hands, if you're familiar with ASP.NET core, we'll appreciate it if you would like to help upgrading the Web API integration.
  112. ## Console Demo
  113. ![demo-console](Assets/console_demo.gif)
  114. ## How to Get a Model
  115. Model in format `gguf` is valid for LLamaSharp (and `ggml` before v0.5.1). One option is to search `LLama` and `gguf` in [huggingface](https://huggingface.co/) to find a model.
  116. Another choice is generate gguf format file yourself with a pytorch weight (or any other), pleae refer to [convert.py](https://github.com/ggerganov/llama.cpp/blob/master/convert.py) and [convert-llama-ggml-to-gguf.py](https://github.com/ggerganov/llama.cpp/blob/master/convert-llama-ggml-to-gguf.py) to get gguf file though a ggml transform.
  117. ## Roadmap
  118. ---
  119. ✅: completed. ⚠️: outdated for latest release but will be updated. 🔳: not completed
  120. ---
  121. ✅ LLaMa model inference
  122. ✅ Embeddings generation, tokenization and detokenization
  123. ✅ Chat session
  124. ✅ Quantization
  125. ✅ Grammar
  126. ✅ State saving and loading
  127. ⚠️ BotSharp Integration
  128. ✅ ASP.NET core Integration
  129. ✅ Semantic-kernel Integration
  130. 🔳 Fine-tune
  131. ⚠️ Local document search (enabled by kernel-memory now)
  132. 🔳 MAUI Integration
  133. ## Contributing
  134. Any contribution is welcomed! Please read the [contributing guide](https://scisharp.github.io/LLamaSharp/0.4/ContributingGuide/). You can do one of the followings to help us make `LLamaSharp` better:
  135. - Append a model link that is available for a version. (This is very important!)
  136. - Star and share `LLamaSharp` to let others know it.
  137. - Add a feature or fix a BUG.
  138. - Help to develop Web API and UI integration.
  139. - Just start an issue about the problem you met!
  140. ## Contact us
  141. Join our chat on [Discord](https://discord.gg/7wNVU65ZDY).
  142. Join [QQ group](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726)
  143. ## License
  144. This project is licensed under the terms of the MIT license.