LLamaSharp Documentation

repository·master·Indexed 25 days ago

https://github.com/scisharp/llamasharp

A C# wrapper for llama.cpp that enables running large language models (LLMs) locally within the .NET ecosystem. It supports GPU acceleration via CUDA, Metal, and Vulkan, and provides integrations for Semantic Kernel for text completion and embedding generation. The library includes a core architecture consisting of LLamaWeights, LLamaContext, and various LLamaExecutors (Interactive, Instruct, Stateless, and Batched), as well as a ChatSession wrapper for interactive tasks.

Tokens
65.9K
Snippets
168
Records
456
Agent score
85%

What's inside LLamaSharp

  1. Overview of LLamaSharp

    master
    LLamaSharp is a cross-platform library designed to run LLaMA, LLaVA, and other large language models (LLM) locally on your device. It is based on llama.cpp and provides efficient inference on both CPU and GPU. The library offers high-level APIs and support for Retrieval Augmented Generation (RAG), making it suitable for deploying LLMs within your own applications.
  2. Overview of LLamaSharp Executors

    master

    LLamaSharp executors define how a model behaves when called. There are four primary types of executors available:

    • InteractiveExecutor: Best for continuous question-and-answer sessions where the model acts as an assistant.
    • InstructExecutor: Designed for following specific instructions (e.g., "continue writing").
    • StatelessExecutor: Ideal for one-time tasks. It has no memory of previous inferences, ensuring a clean context for every call.
    • BatchedExecutor: High-throughput executor that accepts multiple inputs from different sessions to generate multiple outputs simultaneously.
  3. Understand LLamaContext and its role

    master

    LLamaContext is the central component linking native APIs to higher-level APIs in LLamaSharp. It serves two primary purposes:

    1. Inference Settings: It holds the configuration for model inference.
    2. KV-Cache Management: It holds the key-value (KV) cache, which is critical for accelerating model inference.

    Key Architectural Behaviors:

    • Decoupling: LLamaContext is not coupled with LLamaWeights. This allows you to create multiple contexts (different sessions/states) from a single set of model weights.
    • Executor Relationship: Each ILLamaExecutor holds a LLamaContext instance, though an executor can be switched to use different contexts.
    • Session Management: If your application manages multiple user sessions, you must manually manage the lifecycle and state of your LLamaContext instances.
  4. Understand the LLamaSharp core architecture

    master

    LLamaSharp is structured into several layers that manage model weights, native interactions, and execution logic:

    • LLamaWeights: Holds the model weights.
    • LLamaContext: Directly interacts with the native library to provide basic APIs like tokenization and embedding. It requires LLamaWeights to function.
    • LLamaExecutors: Defines the execution mode for the model. They provide text-to-text and image-to-text APIs. Supported executors include:
      • InteractiveExecutor
      • InstructExecutor
      • StatelessExecutor
      • BatchedExecutor
    • ChatSession: A high-level wrapper for InteractiveExecutor and LLamaContext. It is designed for interactive tasks and supports saving/re-loading sessions. It allows text processing customization via IHistoryTransform, ITextTransform, and ITextStreamTransform.
    • Integrations: Extensions for specific use cases, such as kernel-memory for Retrieval Augmented Generation (RAG).
  5. Choose between InteractiveExecutor and InstructExecutor

    master

    Both executors aim to "complete the prompt," but they differ in how they handle conversation roles and prompt formatting:

    • Interactive Mode: The user acts as a 'User' and the LLM acts as an 'Assistant'. This is suitable for models like chat-with-bob. Use a prompt format that establishes a dialogue (e.g., User: ... Bob: ...).
    • Instruct Mode: The user provides instructions for the LLM to follow. This is suitable for models like alpaca. Use a prompt format that explicitly describes a task (e.g., Below is an instruction...).

    Always modify your prompt template when switching between these two modes to ensure optimal performance.

  6. Handle image inputs in multimodal prompts

    master

    When using InteractiveExecutor with multimodal weights, you can include images in prompts by wrapping their file paths in braces (e.g., {c:/path/to/image.jpg}).

    Processing Logic

    • Loading Media: Referenced files are loaded via SafeMtmdWeights.LoadMedia, which produces SafeMtmdEmbed instances.
    • Prompt Replacement: The executor identifies brace-wrapped paths, clears the KV cache using MemorySequenceRemove, and replaces the paths with the designated media marker.
    • Embedding Submission: The embeds for the current turn are collected into ex.Embeds. The executor then submits both the text prompt and the pending media embeds to the helper for generation.
  7. Integrate LLamaSharp with Semantic Kernel for Text Completion

    master

    You can use LLamaSharpTextCompletion to add local LLaMA queries as a text completion service within a Semantic Kernel KernelBuilder. This implementation requires an ILLamaExecutor, such as a StatelessExecutor.

    using var model = LLamaWeights.LoadFromFile(parameters);
    // LLamaSharpTextCompletion can accept ILLamaExecutor. 
    var ex = new StatelessExecutor(model, parameters);
    var builder = new KernelBuilder();
    builder.WithAIService<ITextCompletion>("local-llama", new LLamaSharpTextCompletion(ex), true);
  8. Manage prompt length and context size

    master

    To avoid errors or truncated responses, ensure your total token count stays within the model's context limit. The following inequality must hold:

    len(prompt) + len(response) < len(context)

    Where len(response) is the number of tokens you expect the LLM to generate.

  9. Use Multi-modal Models (LLaVA)

    master

    LLamaSharp supports multi-modal capabilities, allowing models to process both text and image inputs. To use multi-modal models like LLaVA, you must provide two separate model files:

    1. The main model file.
    2. The mm-proj (multi-modal projector) model file.
  10. Integrate LLamaSharp with Semantic Kernel for Text Embedding Generation

    master

    You can use LLamaSharpEmbeddingGeneration to provide local embedding capabilities to Semantic Kernel. This is typically used when building a kernel with memory storage (e.g., VolatileMemoryStore). This implementation requires a LLamaEmbedder instance.

    using var model = LLamaWeights.LoadFromFile(parameters);
    var embedding = new LLamaEmbedder(model, parameters);
    var kernelWithCustomDb = Kernel.Builder
        .WithLoggerFactory(ConsoleLogger.LoggerFactory)
        .WithAIService<ITextEmbeddingGeneration>("local-llama-embed", new LLamaSharpEmbeddingGeneration(embedding), true)
        .WithMemoryStorage(new VolatileMemoryStore())
        .Build();
  11. Integrate LLamaSharp with Semantic Kernel for Chat Completion

    master

    To use LLamaSharp for chat-based interactions in Semantic Kernel, use the LLamaSharpChatCompletion class. This implementation requires an InteractiveExecutor to best fit the chat command pattern.

    using var model = LLamaWeights.LoadFromFile(parameters);
    using var context = model.CreateContext(parameters);
    // LLamaSharpChatCompletion requires InteractiveExecutor, as it's the best fit for the given command.
    var ex = new InteractiveExecutor(context);
    var chatGPT = new LLamaSharpChatCompletion(ex);