Customers who sign-up prior to 30/06/2024 get unlimited access to free features, newer features (with some restrictions), but for free for at least 1 year.Sign up now! https://webveta.alightservices.com/
Categories
Database MariaDB

MariaDB Galera

I am NOT DBA, but by reading and following the instructions provided in some blog articles, I was able to use MariaDB Galera cluster.

My SaaS Product WebVeta, was running on MariaDB and then there was a error and had been a headache for over past 3 days.

I played around with MariaDB Galera over the past 2 days and here is what I think might be useful. This blog post is for developers or other non-DBA’s who want to quickly play around with MariaDB Galera, for example, one person startup founders like me in early stages and didn’t hire yet.

I had issues when I simulated crashes and recovering from crashes using MariaDB Galera if used in a multi-master write scenarios. Instead using one of the nodes for writing and the other nodes for reads seemed easier and can be recovered using some code / scripts. Sample code / scripts part gets discussed in this blog post.

Installation can be performed using any of these blog posts:

https://www.linode.com/docs/guides/how-to-set-up-mariadb-galera-clusters-on-ubuntu-2204

Based on internet blog posts, recovering from a crash means finding the node that had the most recent write and using that node as primary, but that seemed a little messy.

This is what I did and learned over the past 2 days of playing around with MariaDB Galera (as mentioned this blog post for non-DBA’s who want to use MariaDB Galera easily, for example, one person startup founders like me in early stages and didn’t hire yet):

I was writing into node-1. This way no need of messing with or trying to find latest write and then promoting as new master node. Because the same node is always the master.

If node-1 goes down, edit

/var/lib/mysql/grastate.dat

and modify the line

safe_to_bootstrap: 1

to have value of 1. Then

sudo galera_new_cluster

on node-1. Then restart all mariadb service on all other nodes.

sudo systemctl restart mariadb

This removes all the confusion and headache. However remember whenever node-1 goes down or reboots, this becomes a mess and the above steps need to be done.

I have some code that performs background tasks written in C# as part of my SaaS product – WebVeta.

I plan to add some extra functionality for 2nd, 3rd nodes to listen for restart mariadb service commands. The 1st nodes code would parse the galera.cnf figure out the clients, update grastate.dat, start node-1, look at cluster size, call the 2nd, 3rd nodes for restart.

That’s all for now, until I get a part-time / full-time DBA into my startup’s team.

Then why MariaDB Galera if not using multi-master? Later, when I hire a DBA, it’s easy to start using multi-master.

Azure: I hope Microsoft Azure offers MariaDB Galera as part of Azure Database services.

MariaDb Galera: MariaDB Galera team should consider using some kind of protocol for easily self-promoting of secondary nodes by using some kind of internal voting and easily adding nodes i.e if one node connects to another node, the new node gets information about rest of the nodes in the cluster and communicates with every other node.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net A.I Azure C# LLM NLP

Using C# and Azure OpenAI services

If you have access to Azure OpenAI services, the following code snippet shows how to chat with ChatGPT!

Some general tips:

  1. Secure your networks, i.e use private endpoints inside Azure! Stolen keys can be used by other people if the network is not secured.

This code example is C# version of what’s discussed in https://github.com/AzureCosmosDB/Azure-OpenAI-Python-Developer-Guide/blob/main/05_Explore_OpenAI_models/README.md

The above link is for Python developers, this blog post for C# developers.

var chatClient = new OpenAIClient(new Uri(azureEndPoint), 
       new AzureKeyCredential(apiKey));

var chatCompletionOptions = new ChatCompletionsOptions();

chatCompletionOptions.DeploymentName = "gpt35";

chatCompletionOptions.Messages.Add(new
    ChatRequestSystemMessage("You are a helpful, fun and friendly sales assistant for Cosmic Works, a bicycle and bicycle accessories store."));

chatCompletionOptions.Messages.Add(new 
    ChatRequestUserMessage("Do you sell bicycles?"));

chatCompletionOptions.Messages.Add(new
    ChatRequestAssistantMessage("Yes, we do sell bicycles. What kind of bicycle are you looking for?"));

chatCompletionOptions.Messages.Add(new
    ChatRequestUserMessage("I'm not sure what I'm looking for. Could you help me decide?"));

var response = await 
    chatClient.GetChatCompletionsAsync(chatCompletionOptions);

if (response != null && response.Value != null && 
    response.Value.Choices != null && 
    response.Value.Choices.Count > 0)
{

    System.Console.WriteLine(
        response.Value.Choices.ElementAt(0).Message.Content);
}

The above code has 3 configuration variables:

  1. azureEndPoint – This is the endpoint from Azure Portal.
  2. apiKey – One of the API keys from Azure Portal.
  3. The deployment name of the model that has been added through Azure Portal.

If the code was run successful, output looks like this:

If there is network connectivity issues, there might be exceptions, if denied due to network security issues errors might be like the following:

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
A.I Artificial Intelligence Llama LLM

Have you tried Ollama – ChatGPT on your local machine, great software!

Most of you know, from around 2016, I had interest in DataScience/Machine Learning/Artificial Intelligence and even did some courses as a hobby! I am primarily .Net full-stack web developer, but A.I has been fascinating and I have been hobbyist!

In 2021 I started my own startup, 2023 prototyped a concept for a SaaS product known as WebVeta! 2024 – launched an mvp and now is the time to dive into A.I. Over the past 2 weeks, I was experimenting with several different things in A.I both from a development perspective, features perspective!

Over the past 2 days I am playing around with a nice software that allows working with several LLMs from local machine! I would say at least 16GB of RAM, possibly slightly higher.

https://ollama.com

https://github.com/ollama/ollama

The setup instructions are straightforward!

On the Github page, under “Community Integrations” -> “Web & Desktop” there are several web and desktop clients for UI, choose one of those based on your operating system and you can play around with a large set of A.I models. The list of models can be found at: https://ollama.com/library

Try llama3, phi3 if you have enough CPU and RAM! Or try the smaller models – tinydolphin, tinyllama! There are several coding related LLMs i.e GitHub co-pilot’ish and there are some Visual Studio Code extensions that can communicate with locally version of Ollama and help with code!

Remember the LLMs need to be downloaded, the exact syntax is provided on the LLMs pages, but the general syntax is:

ollama pull <LLM_NAME>

I have used https://github.com/ollama-ui/ollama-ui on Linux, https://github.com/tgraupmann/WinForm_Ollama_Copilot for the client UI!

The client UI’s query and get the available local LLMs and allow specifying / selecting which particular LLM to interact with.

If anyone interested let me know via any of my social media profiles, I might consider doing a small demo for any enthusiasts!

Ollama is a great tool and great effort by the team of developers who developed Ollama! Thank you!

WebVeta – Advanced, unified, consistent search for your website(s), from content of your website(s), blogs(s). First 50 customers, who sign-up prior to 15/05/2024 get unlimited access to existing features, newer features for at least 1 year. Sign up now! https://webveta.alightservices.com/

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net A.I Artificial Intelligence C# Llama LLM NLP

How to get text embeddings from Meta Llama using C# .Net

This post is about getting text embeddings i.e vector representation of text using C# .Net and using Meta’s Llama 2!

Meta’s Llama

Meta (Facebook) has released few different LLM’s, the latest Llama3, but this blog post about Llama2. Using Llama3 might be similar, but I have not tried yet! There are few more things that can be tried, but those are out of scope and this is an end to end blog post for using Llama2 using C#.

https://llama.meta.com/

From the above link provide click “Download Models”, provide information. Then links to some github, some keys are provided. Make note of the keys. The keys are valid for 24 hours and each model can be downloaded 5 times.

llama.cpp

We use llama.cpp for certain activities:

https://github.com/ggerganov/llama.cpp

LLamaSharp

This is the wrapper for interacting from C# .Net with Llama models.

I have introduced the tools and software that are going to be used. Now, let’s look at the different steps:

  1. Download Llama model (Meta’s Llama has Llama 2 and Llama 3, each has smaller and larger models, this discusses the smallest model from Llama 2)
  2. Prepare and convert Llama model into gguf format.
  3. Use in C# code

Download Llama model:

Once you submit your information and receive the keys from Meta Facebook, clone the repo:

https://github.com/meta-llama/llama for Llama2,

https://github.com/meta-llama/llama3 for Llama3

git clone https://github.com/meta-llama/llama

Navigate into llama folder, then run download.sh

cd llama
sudo ./download.sh

You would be prompted for the download key, enter the key.

Now 12.5 GB file gets downloaded into a folder “llama-2-7b”

Prepare and convert Llama model into gguf format:

We are going to convert the Llama model into gguf format. For this we need Python3 and Python3-Pip, if these are not installed, install using the following command

sudo apt install python3 python3-pip

Clone the llama.cpp repo into a different directory.

git clone https://github.com/ggerganov/llama.cpp

Navigate into llama.cpp and compile

cd llama.cpp
make -j

Install the requirement for python:

python3 -m pip install -r requirements.txt

Now copy the entire “llama-2-7b” into llama.cpp/models.

Listing models directory should show “llama–2-7b”

ls ./models
python3 convert.py models/llama-2-7b/

This generates a 2.17 GB file ggml-model-f32.gguf

Now run the following command:

./quantize ./models/llama-2-7b/ggml-model-f32.gguf ./models/llama-2-7b/ggml-model-Q4_K_M.gguf Q4_K_M

This should generate a 3.79 GB file.

Optional (I have NOT tried this yet)

The following extra params can be passed for the python3 convert.py models/llama-2-7b/

python convert.py models/llama-2-7b/ --vocab-type bpe

C# code

Create a new or in an existing project add the following Nuget packages:

LLamaSharp

LLamaSharp.Backend.Cpu or LLamaSharp.Backend.Cuda11 or 
LLamaSharp.Backend.Cuda12 or LLamaSharp.Backend.OpenCL

// I used LLamaSharp.Backend.Cpu

Use the following using statements:

using LLama;
using LLama.Common;

The following code is adapted from the samples of LlamaSharp – https://github.com/SciSharp/LLamaSharp/blob/master/LLama.Examples/Examples/GetEmbeddings.cs

string modelPath = PATH_TO_GGUF_FILE

var @params = new ModelParams(modelPath) {EmbeddingMode = true };
using var weights = LLamaWeights.LoadFromFile(@params);
var embedder = new LLamaEmbedder(weights, @params);

Use the path for your .gguf from quantize step file’s path.

Here is code for getting embeddings:

float[] embeddings = embedder.GetEmbeddings("Hello, this is sample text for embeddings").Result;

Hope this helps some people, I am .Net developer (primarily C#), A.I enthusiast.

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.

Categories
.Net C# NLP

C# .Net, Python and NLP – Natural Language Processing

The available NLP libraries for C# are very less. Some of the best NLP libraries are in Python. There are few C# implementations that either are a re-write of Python’s implementations or wrappers.

If you are serious about any NLP related work in C#, use well-maintained Python implementations and integrate with C#. The wrappers / re-write’s might not have much support.

Let’s look at 2 popular libraries:

NLTK – Natural Language Toolkit

spaCy – Industrial-Strength Natural Language Processing

Both the libraries are well documented and easy to use. i.e even C# developer like me can understand the python code examples provided. And very easy to experiment.

Now here are some ways of integrating with C#:

  1. Have a separate set of micro-services or message based applications for NLP.
  2. Use some of the following libraries that attempt to integrate Python with .Net
  3. Use System.Diagnostics.Process class i.e run the Python program as a separate process.

The integration libraries are:

Python.NET

IronPython

Python.Included

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.