MinjaSharp 0.0.0-alpha.0.1
dotnet add package MinjaSharp --version 0.0.0-alpha.0.1
NuGet\Install-Package MinjaSharp -Version 0.0.0-alpha.0.1
<PackageReference Include="MinjaSharp" Version="0.0.0-alpha.0.1" />
<PackageVersion Include="MinjaSharp" Version="0.0.0-alpha.0.1" />
<PackageReference Include="MinjaSharp" />
paket add MinjaSharp --version 0.0.0-alpha.0.1
#r "nuget: MinjaSharp, 0.0.0-alpha.0.1"
#:package MinjaSharp@0.0.0-alpha.0.1
#addin nuget:?package=MinjaSharp&version=0.0.0-alpha.0.1&prerelease
#tool nuget:?package=MinjaSharp&version=0.0.0-alpha.0.1&prerelease
MinjaSharp
MinjaSharp is a C# wrapper for the Minja templating engine, providing a clean and efficient interface for template rendering in .NET applications.
What is Minja and How It Applies to LLMs
Minja is a lightweight, high-performance templating engine designed for efficient text generation. It's particularly well-suited for working with Large Language Models (LLMs) because:
- It provides a flexible, Jinja-like syntax for creating prompts and templates
- It supports complex logic, loops, and conditionals within templates
- It can efficiently handle structured data for rendering
MinjaSharp brings these capabilities to the .NET ecosystem, allowing C# developers to work with Minja templates for LLM prompt engineering, text generation, and other templating needs without having to interact with the native C++ implementation directly.
Usage
Installation
dotnet add package MinjaSharp
Creating a Template
Creating a template is straightforward:
using MinjaSharp;
// Create a template with Jinja-like syntax
var template = new Template("Hello, {{ name }}! You have {{ unread }} unread message(s).");
Rendering Templates
MinjaSharp provides multiple ways to render templates:
1. Render with a typed object
The most convenient way to render templates is using strongly-typed C# objects:
// Using anonymous type
var result = template.Render(new {
name = "World",
unread = 7
});
// Or with a defined class
public class User {
public string Name { get; set; }
public int Unread { get; set; }
}
var user = new User { Name = "Alice", Unread = 3 };
var result = template.Render(user);
2. Render with a JSON string
You can also provide context as a JSON string:
var result = template.RenderJson("{\"name\":\"World\",\"unread\":7}");
3. Render with direct value construction
For more control, you can build the template context manually:
// Create values manually
var root = Value.Object();
root.Set("name", Value.String("World"));
root.Set("unread", Value.Int(7));
// Create a context
using var ctx = new Context(root);
// Render with the context
var result = template.Render(ctx);
Working with Complex Structures
MinjaSharp handles nested objects, arrays, and complex structures seamlessly:
var template = new Template(@"
{% for user in users %}
{{ user.name }} is {{ user.age }} years old.
{% endfor %}
");
var result = template.Render(new {
users = new[] {
new { name = "Alice", age = 25 },
new { name = "Bob", age = 30 },
new { name = "Charlie", age = 35 }
}
});
LLM Chat Templates
MinjaSharp is particularly useful for working with LLM chat templates. Here's an example using a chat template for Qwen:
// Create the template
var template = new Template(QwenChatTemplate);
// Create a chat request object
var request = new ChatRequest {
Messages = [
new ChatMessage { Role = "system", Content = "You are a helpful AI assistant." },
new ChatMessage { Role = "user", Content = "Hello, how are you?" }
]
};
// Render the template to get the properly formatted prompt
var formattedPrompt = template.Render(request);
Performance Considerations
When working with MinjaSharp, keep these performance tips in mind:
Reuse Template Instances: Creating a template involves parsing and compiling the template string, which is relatively expensive. For better performance, create templates once and reuse them for multiple renders.
// Good - Create once, reuse many times var template = new Template("Hello, {{ name }}!"); // Render multiple times with different data for (int i = 0; i < 1000; i++) { template.Render(new { name = $"User{i}" }); }Dispose Templates When Done: Templates implement
IDisposable, so useusingstatements or explicitly dispose them when they're no longer needed.using (var template = new Template("Hello, {{ name }}!")) { var result = template.Render(new { name = "World" }); } // Automatically disposed hereUse Value Builder for Repeated Structures: When repeatedly rendering with similar structures, consider building the value tree manually once and updating specific values for better performance.
Building Locally
To build MinjaSharp locally:
Prerequisites:
- .NET 9.0 SDK or later
- C++ compiler (MSVC on Windows, GCC on Linux, or Clang on macOS)
- CMake 3.14 or later
Clone the Repository:
git clone https://github.com/phil-scott-78/minjasharp cd minjasharpBuild the Native Library and .NET Wrapper:
# On Windows .\build.ps1 # On Linux/macOS ./build.shRun Tests:
dotnet test
License
MinjaSharp is released under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.0-alpha.0.1 | 292 | 5/15/2025 |
| 0.0.0-alpha.0 | 158 | 5/9/2025 |