Scroll till the end of the page if you just want code
Ok, so you want to explore Large Language Models(LLM's) like ChatGPT with help of greatest programming language in the world(c#)?
Scroll till the end of the page if you just want code
Let's do this!
You, probably, heard that ChatGPT API costs some money, but don't worry. In this example we will use so called local models. You can think of it as a smaller version of ChatGPT that can run on your computer. Even without graphics card!!!! And completly free!
Installation
Create new console application and use nuget to install these packages(dont't forget to check Include prerelease):
LangChain
- meta package for development, it includes the Core package, OpenAI, HuggingFace and LLamaSharp providers
You also need to install ONE of these packages for the backend if you are using the LLamaSharp provider
1 2 3 |
|
Preparing the model
Lets download our model first. For this example we will be using model called Thespis with 13 billions parameters and quantization Q2_K.
You can check it here: Thespis-13B-v0.5-GGUF
You can look for other models here: TheBloke. We can only run models in GGUF format.
What is quantization? - you may ask
In simple words in is precision of the model. Models with small quantization are easily getting confused with complex prompts, but are working much faster.
So, finally, let's write some code!
1 2 3 4 5 6 7 8 9 10 |
|
This line will download the model and save it locally for future usage. After model is downloaded it will return path to the .gguf file. _You can manually download any model you want and insert path to it directly. Without using HuggingFaceModelDownloader.*_
Now it's time to load our model into memory:
1 2 |
|
Now let's build a chain!
Building a chain
This is minimal chain to make LLM work:
1 2 3 4 5 6 7 8 9 10 11 |
|
We can see here 2 chains(or links) working together: Set and LLM.
- Set - setting value for the chain context variable prompt
- LLM - getting value from chain context variable prompt and passing it to specified model
Links are connected together with '|' symbol.
Finally we run the entire chain. After some seconds you will see entire dialog in console window:
1 2 3 |
|
Complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|