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 9.6 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. **The C#/.NET binding of [llama.cpp](https://github.com/ggerganov/llama.cpp). It provides APIs to inference the LLaMa Models and deploy it on local environment. It works on
  10. both Windows, Linux and MAC without requirment for compiling llama.cpp yourself. Its performance is close to llama.cpp.**
  11. **Furthermore, it provides integrations with other projects such as [BotSharp](https://github.com/SciSharp/BotSharp) to provide higher-level applications and UI.**
  12. ## Documentation
  13. - [Quick start](https://scisharp.github.io/LLamaSharp/0.5/GetStarted/)
  14. - [Tricks for FAQ](https://scisharp.github.io/LLamaSharp/0.5/Tricks/)
  15. - [Full documentation](https://scisharp.github.io/LLamaSharp/0.5/)
  16. - [API reference](https://scisharp.github.io/LLamaSharp/0.5/xmldocs/)
  17. - [Examples](./LLama.Examples/NewVersion/)
  18. ## Installation
  19. Firstly, search `LLamaSharp` in nuget package manager and install it.
  20. ```
  21. PM> Install-Package LLamaSharp
  22. ```
  23. Then, search and install one of the following backends:
  24. ```
  25. LLamaSharp.Backend.Cpu # cpu for windows, linux and mac
  26. LLamaSharp.Backend.Cuda11 # cuda11 for windows and linux
  27. LLamaSharp.Backend.Cuda12 # cuda12 for windows and linux
  28. LLamaSharp.Backend.MacMetal # metal for mac
  29. ```
  30. If you would like to use it with [microsoft semantic-kernel](https://github.com/microsoft/semantic-kernel), please search and install the following package:
  31. ```
  32. LLamaSharp.semantic-kernel
  33. ```
  34. 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.
  35. | LLamaSharp.Backend | LLamaSharp | Verified Model Resources | llama.cpp commit id |
  36. | - | - | -- | - |
  37. | - | v0.2.0 | This version is not recommended to use. | - |
  38. | - | 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) | - |
  39. | 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 |
  40. | 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 |
  41. | 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 |
  42. | v0.4.2-preview (cpu,cuda11) |v0.4.2-preview | [Llama2 7b GGML](https://huggingface.co/TheBloke/llama-2-7B-Guanaco-QLoRA-GGML)| 3323112 |
  43. | v0.5.1 |v0.5.1 | [Llama2 7b GGUF](https://huggingface.co/TheBloke/llama-2-7B-Guanaco-QLoRA-GGUF)| 6b73ef1 |
  44. 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! 😊
  45. 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)
  46. from source and put the `libllama` under your project's output path ([guide](https://scisharp.github.io/LLamaSharp/0.5/ContributingGuide/)).
  47. ## FAQ
  48. 1. GPU out of memory: Please try setting `n_gpu_layers` to a smaller number.
  49. 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).
  50. ## Usages
  51. #### Model Inference and Chat Session
  52. 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.
  53. ```cs
  54. using LLama.Common;
  55. using LLama;
  56. string modelPath = "<Your model path>"; // change it to your own model path
  57. 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.
  58. // Load a model
  59. var parameters = new ModelParams(modelPath)
  60. {
  61. ContextSize = 1024,
  62. Seed = 1337,
  63. GpuLayerCount = 5
  64. };
  65. using var model = LLamaWeights.LoadFromFile(parameters);
  66. // Initialize a chat session
  67. using var context = model.CreateContext(parameters);
  68. var ex = new InteractiveExecutor(context);
  69. ChatSession session = new ChatSession(ex);
  70. // show the prompt
  71. Console.WriteLine();
  72. Console.Write(prompt);
  73. // run the inference in a loop to chat with LLM
  74. while (prompt != "stop")
  75. {
  76. foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
  77. {
  78. Console.Write(text);
  79. }
  80. prompt = Console.ReadLine();
  81. }
  82. // save the session
  83. session.SaveSession("SavedSessionPath");
  84. ```
  85. #### Quantization
  86. 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#.
  87. ```cs
  88. string srcFilename = "<Your source path>";
  89. string dstFilename = "<Your destination path>";
  90. string ftype = "q4_0";
  91. if(Quantizer.Quantize(srcFileName, dstFilename, ftype))
  92. {
  93. Console.WriteLine("Quantization succeed!");
  94. }
  95. else
  96. {
  97. Console.WriteLine("Quantization failed!");
  98. }
  99. ```
  100. For more usages, please refer to [Examples](./LLama.Examples).
  101. #### Web API
  102. We provide the integration of ASP.NET core [here](./LLama.WebAPI). Since currently the API is not stable, please clone the repo and use it. In the future we'll publish it on NuGet.
  103. 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.
  104. ## Demo
  105. ![demo-console](Assets/console_demo.gif)
  106. ## Roadmap
  107. ---
  108. ✅: completed. ⚠️: outdated for latest release but will be updated. 🔳: not completed
  109. ---
  110. ✅ LLaMa model inference
  111. ✅ Embeddings generation, tokenization and detokenization
  112. ✅ Chat session
  113. ✅ Quantization
  114. ✅ State saving and loading
  115. ⚠️ BotSharp Integration
  116. ✅ ASP.NET core Integration
  117. ✅ Semantic-kernel Integration
  118. 🔳 Fine-tune
  119. 🔳 Local document search
  120. 🔳 MAUI Integration
  121. ## Assets
  122. Some extra model resources could be found below:
  123. - [Qunatized models provided by LLamaSharp Authors](https://huggingface.co/AsakusaRinne/LLamaSharpSamples)
  124. - [eachadea/ggml-vicuna-13b-1.1](https://huggingface.co/eachadea/ggml-vicuna-13b-1.1/tree/main)
  125. - [TheBloke/wizardLM-7B-GGML](https://huggingface.co/TheBloke/wizardLM-7B-GGML)
  126. - Magnet: [magnet:?xt=urn:btih:b8287ebfa04f879b048d4d4404108cf3e8014352&dn=LLaMA](magnet:?xt=urn:btih:b8287ebfa04f879b048d4d4404108cf3e8014352&dn=LLaMA)
  127. The weights included in the magnet is exactly the weights from [Facebook LLaMa](https://github.com/facebookresearch/llama).
  128. The prompts could be found below:
  129. - [llama.cpp prompts](https://github.com/ggerganov/llama.cpp/tree/master/prompts)
  130. - [ChatGPT_DAN](https://github.com/0xk1h0/ChatGPT_DAN)
  131. - [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts)
  132. - [awesome-chatgpt-prompts-zh](https://github.com/PlexPt/awesome-chatgpt-prompts-zh) (Chinese)
  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.