Table of Contents

πŸš€ Getting Started with LM-Kit.NET

This guide will help you integrate generative AI capabilities into your .NET applications using LM-Kit.NET. Follow the steps below to get up and running quickly.


πŸ“‹ Prerequisites

LM-Kit.NET can be used in any application targeting the .NET framework from version 4.6.2 to 9.0, with just four clicks. Ensure the following prerequisites are met before starting:

  • .NET Framework: .NET 4.6.2 or later.
  • Development Environment: Rider, Visual Studio 2019 or later, or Visual Studio Code.
    • Operating Systems: Windows or macOS.
  • NuGet Package Manager: Ensure the NuGet Package Manager is installed for easy installation of the LM-Kit.NET package.

πŸ“₯ Installation

To use LM-Kit.NET, install the SDK via NuGet Package Manager as follows:

Using NuGet Package Manager Console

  1. Open the project in Visual Studio (or preferred .NET IDE).
  2. Open the NuGet Package Manager Console from the Tools menu.
  3. Execute the following command:
    Install-Package LM-Kit.NET
    

Using NuGet Package Manager UI

  1. Right-click on the project in the Solution Explorer.
  2. Select Manage NuGet Packages...
  3. Search for LM-Kit.NET in the Browse tab.
  4. Click Install to add the package to the project.

Note: It is recommended to install the optional CUDA Backend to benefit from Nvidia GPU acceleration:

Install-Package LM-Kit.NET.Backend.Cuda12.Windows
Install-Package LM-Kit.NET.Backend.Cuda12.Linux

πŸ›  Basic Usage

Initialize LM-Kit.NET

After installing the SDK, initialize LM-Kit.NET in the application. Below is an example demonstrating basic initialization with logging and CUDA acceleration:

using LMKit;

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            // Optionally set a license key if available
            LMKit.Licensing.LicenseManager.SetLicenseKey("YOUR_LICENSE_KEY");
			
            // Set log level to Debug
            LMKit.Global.Runtime.LogLevel = LMKit.Global.Runtime.LMKitLogLevel.Debug;
            
            // Enable CUDA acceleration
            LMKit.Global.Runtime.EnableCuda = true;

            // Initialize LM-Kit.NET runtime
            LMKit.Global.Runtime.Initialize();

            // Your code goes here
        }
    }
}

πŸ’¬ Example: Text Generation

The following example demonstrates generating text using LM-Kit.NET:

using LMKit;
using LMKit.TextGeneration;

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the model
            var model = new LMKit.Model.LM("https://huggingface.co/lm-kit/phi-3.5-mini-3.8b-instruct-gguf/resolve/main/Phi-3.5-mini-Instruct-Q4_K_M.gguf?download=true");

            // Create a multi-turn conversation instance
            var chat = new LMKit.TextGeneration.MultiTurnConversation(model);

            // Submit a prompt and get the response. The Submit method returns an object of type [LMKit.TextGeneration.TextGenerationResult](https://docs.lm-kit.com/lm-kit-net/api/LMKit.TextGeneration.TextGenerationResult.html)
            var response = chat.Submit("Why are cats so cute?");

            // Output the generated text
            Console.WriteLine("Response: " + response.Completion);
        }
    }
}

πŸ—‚οΈ Example: Structured Text Extraction

Extract structured data from unstructured text using LM-Kit.NET..

using LMKit.Extraction;
using LMKit.Model;
using System;
using System.Collections.Generic;

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the language model (LLM)
            LLM languageModel = new LM("https://huggingface.co/lm-kit/phi-3.5-mini-3.8b-instruct-gguf/resolve/main/Phi-3.5-mini-Instruct-Q4_K_M.gguf?download=true");

            // Create an instance of TextExtraction
            TextExtraction textExtraction = new TextExtraction(languageModel);

            // Define the elements to extract
            textExtraction.Elements = new List<TextExtractionElement>
            {
                new TextExtractionElement("Product", ElementType.String, "Name of the product"),
                new TextExtractionElement("Price", ElementType.Float, "Price of the product"),
                new TextExtractionElement("Availability Date", ElementType.Date, "When the product will be available")
            };

            // Set the content to extract data from
            textExtraction.SetContent("The new SuperWidget will cost $49.99 and will be available on December 1, 2023.");

            // Perform the extraction
            TextExtractionResult result = textExtraction.Parse();

            // Display the extracted data
            foreach (var item in result.Elements)
            {
                Console.WriteLine($"{item.TextExtractionElement.Name}: {item.Value}");
            }
        }
    }
}

🌐 Example: Language Detection

The following example demonstrates detecting the language of a given text using LM-Kit.NET:

using LMKit;
using LMKit.LanguageDetection;

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the model
            var model = new LMKit.Model.LM("https://huggingface.co/lm-kit/phi-3.5-mini-3.8b-instruct-gguf/resolve/main/Phi-3.5-mini-Instruct-Q4_K_M.gguf?download=true");

            // Create a [TextTranslation](https://docs.lm-kit.com/lm-kit-net/api/LMKit.Translation.html) instance
            var textTranslation = new LMKit.Translation.TextTranslation(model);
            string text = "Allons boire une bière après le travail !";
            Language detectedLanguage = textTranslation.DetectLanguage(text);

            // Output the detected language
            Console.WriteLine("Detected Language: " + detectedLanguage.ToString());
        }
    }
}

🌐 Example: Text Embedding Similarity

Compute similarity between texts using embeddings.

using LMKit.Embeddings;
using LMKit.Model;
using System;
using System.Threading.Tasks;

namespace YourNamespace
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Load the embedding model
            LM model = new LM("https://huggingface.co/lm-kit/nomic-embed-text-1.5/resolve/main/nomic-embed-text-1.5-F16.gguf?download=true");
            Embedder embedder = new Embedder(model);

            // Input text
            string input = "What are the benefits of meditation?";

            // Example texts
            string[] examples = new string[]
            {
                "Meditation improves mental health.",
                "How to start a meditation practice?",
                "Benefits of regular exercise.",
                "Cooking healthy meals at home.",
                "Understanding mindfulness and its advantages.",
                "Latest trends in technology."
            };

            // Get embeddings
            float[] inputEmbedding = await embedder.GetEmbeddingsAsync(input);
            float[][] exampleEmbeddings = await embedder.GetEmbeddingsAsync(examples);

            // Calculate similarities
            Console.WriteLine("Similarity Scores:");
            for (int i = 0; i < examples.Length; i++)
            {
                float similarity = Embedder.GetCosineSimilarity(inputEmbedding, exampleEmbeddings[i]);
                Console.WriteLine($"{similarity:F4} - {examples[i]}");
            }
        }
    }
}


βš™οΈ Advanced Configuration

Fine-tune LM-Kit.NET to suit your application's needs.

using LMKit;
using LMKit.Configuration;

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            // Enable KV (key-value) cache recycling 
            LMKit.Global.Configuration.EnableKVCacheRecycling = true;
            
            // Disable model caching 
            LMKit.Global.Configuration.EnableModelCache = false;
            
            // Enable token healing 
            LMKit.Global.Configuration.EnableTokenHealing = true;
            
            // Discover more parameters in the LMKit.Global.Configuration static class.

            // Code to use LM-Kit.NET goes here
        }
    }
}

πŸ” Exploring More Features

LM-Kit.NET includes various features, such as:

  • Data Processing: Streamline and transform data for analysis and integration.
  • Text Analysis: Perform sentiment analysis, emotion detection, and custom text classification.
  • AI Agents: Orchestrate intelligent agents for task automation.
  • Translation: Translate text across multiple languages with high accuracy.
  • Text Generation: Generate creative and contextually relevant text outputs.
  • Model Optimization: Enhance model performance through optimization techniques.

Refer to the API Documentation for detailed information on all available methods and classes.


πŸ“ž Support

For assistance with LM-Kit.NET:

  • Visit the Documentation for comprehensive guides and references.
  • Submit Issues: If you encounter any issues or have questions, please submit them to our demo repository's issue tracker.
  • Contact Support for professional assistance.