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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.4/GetStarted/)
  14. - [Tricks for FAQ](https://scisharp.github.io/LLamaSharp/0.4/Tricks/)
  15. - [Full documentation](https://scisharp.github.io/LLamaSharp/0.4/)
  16. - [API reference](https://scisharp.github.io/LLamaSharp/0.4/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
  26. LLamaSharp.Backend.Cuda11
  27. LLamaSharp.Backend.Cuda12
  28. ```
  29. 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.
  30. | LLamaSharp.Backend | LLamaSharp | Verified Model Resources | llama.cpp commit id |
  31. | - | - | -- | - |
  32. | - | v0.2.0 | This version is not recommended to use. | - |
  33. | - | 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) | - |
  34. | 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 |
  35. | 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 |
  36. | 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 |
  37. | v0.4.2-preview (cpu,cuda11) |v0.4.2-preview | [Llama2 7b](https://huggingface.co/TheBloke/llama-2-7B-Guanaco-QLoRA-GGML)| 332311234a0aa2974b2450710e22e09d90dd6b0b |
  38. 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! 😊
  39. 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)
  40. from source and put the `libllama` under your project's output path ([guide](https://scisharp.github.io/LLamaSharp/0.4/ContributingGuide/)).
  41. ## FAQ
  42. 1. GPU out of memory: Please try setting `n_gpu_layers` to a smaller number.
  43. 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).
  44. ## Usages
  45. #### Model Inference and Chat Session
  46. 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.
  47. ```cs
  48. using LLama.Common;
  49. using LLama;
  50. string modelPath = "<Your model path>"; // change it to your own model path
  51. 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.
  52. // Initialize a chat session
  53. var ex = new InteractiveExecutor(new LLamaModel(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
  54. ChatSession session = new ChatSession(ex);
  55. // show the prompt
  56. Console.WriteLine();
  57. Console.Write(prompt);
  58. // run the inference in a loop to chat with LLM
  59. while (prompt != "stop")
  60. {
  61. foreach (var text in session.Chat(prompt, new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "User:" } }))
  62. {
  63. Console.Write(text);
  64. }
  65. prompt = Console.ReadLine();
  66. }
  67. // save the session
  68. session.SaveSession("SavedSessionPath");
  69. ```
  70. #### Quantization
  71. 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#.
  72. ```cs
  73. string srcFilename = "<Your source path>";
  74. string dstFilename = "<Your destination path>";
  75. string ftype = "q4_0";
  76. if(Quantizer.Quantize(srcFileName, dstFilename, ftype))
  77. {
  78. Console.WriteLine("Quantization succeed!");
  79. }
  80. else
  81. {
  82. Console.WriteLine("Quantization failed!");
  83. }
  84. ```
  85. For more usages, please refer to [Examples](./LLama.Examples).
  86. #### Web API
  87. 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.
  88. 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.
  89. ## Demo
  90. ![demo-console](Assets/console_demo.gif)
  91. ## Roadmap
  92. ---
  93. ✅: completed. ⚠️: outdated for latest release but will be updated. 🔳: not completed
  94. ---
  95. ✅ LLaMa model inference
  96. ✅ Embeddings generation, tokenization and detokenization
  97. ✅ Chat session
  98. ✅ Quantization
  99. ✅ State saving and loading
  100. ⚠️ BotSharp Integration
  101. ✅ ASP.NET core Integration
  102. ⚠️ Semantic-kernel Integration
  103. 🔳 Fine-tune
  104. 🔳 Local document search
  105. 🔳 MAUI Integration
  106. 🔳 Follow up llama.cpp and improve performance
  107. ## Assets
  108. Some extra model resources could be found below:
  109. - [Qunatized models provided by LLamaSharp Authors](https://huggingface.co/AsakusaRinne/LLamaSharpSamples)
  110. - [eachadea/ggml-vicuna-13b-1.1](https://huggingface.co/eachadea/ggml-vicuna-13b-1.1/tree/main)
  111. - [TheBloke/wizardLM-7B-GGML](https://huggingface.co/TheBloke/wizardLM-7B-GGML)
  112. - Magnet: [magnet:?xt=urn:btih:b8287ebfa04f879b048d4d4404108cf3e8014352&dn=LLaMA](magnet:?xt=urn:btih:b8287ebfa04f879b048d4d4404108cf3e8014352&dn=LLaMA)
  113. The weights included in the magnet is exactly the weights from [Facebook LLaMa](https://github.com/facebookresearch/llama).
  114. The prompts could be found below:
  115. - [llama.cpp prompts](https://github.com/ggerganov/llama.cpp/tree/master/prompts)
  116. - [ChatGPT_DAN](https://github.com/0xk1h0/ChatGPT_DAN)
  117. - [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts)
  118. - [awesome-chatgpt-prompts-zh](https://github.com/PlexPt/awesome-chatgpt-prompts-zh) (Chinese)
  119. ## Contributing
  120. 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:
  121. - Append a model link that is available for a version. (This is very important!)
  122. - Star and share `LLamaSharp` to let others know it.
  123. - Add a feature or fix a BUG.
  124. - Help to develop Web API and UI integration.
  125. - Just start an issue about the problem you met!
  126. ## Contact us
  127. Join our chat on [Discord](https://discord.gg/7wNVU65ZDY).
  128. Join [QQ group](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726)
  129. ## License
  130. This project is licensed under the terms of the MIT license.