RedPajama 0.0.0-alpha.0.3
This is a prerelease version of RedPajama.
dotnet add package RedPajama --version 0.0.0-alpha.0.3
NuGet\Install-Package RedPajama -Version 0.0.0-alpha.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RedPajama" Version="0.0.0-alpha.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RedPajama --version 0.0.0-alpha.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RedPajama, 0.0.0-alpha.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RedPajama as a Cake Addin #addin nuget:?package=RedPajama&version=0.0.0-alpha.0.3&prerelease // Install RedPajama as a Cake Tool #tool nuget:?package=RedPajama&version=0.0.0-alpha.0.3&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RedPajama
RedPajama is a C# library that generates structured JSON samples and GBNF (Generalized Backus-Naur Form) grammars from C# types. It's designed to enhance Large Language Model (LLM) interactions by providing type-safe, well-structured data generation and parsing capabilities.
Features
- Generate sample JSON templates from C# types
- Create corresponding GBNF grammars for type-safe LLM responses
- Support for complex type hierarchies and nested objects
- Custom placeholder delimiters for clear template visualization
- Configurable string length constraints (defaults to 1-512 characters)
- Property descriptions and metadata support
- String validation with AllowedValues, MinLength, and MaxLength attributes
Usage
Basic Example
class Address
{
public required string Street { get; init; }
public required string City { get; init; }
[Description("Digits only")]
public required string ZipCode { get; init; }
}
class Customer
{
public required string Name { get; init; }
public required Address ShippingAddress { get; init; }
public required Address BillingAddress { get; init; }
}
// Generate type model, GBNF grammar, and JSON sample
var typeModel = new TypeModelBuilder<Customer>().Build();
var gbnfGenerator = new GbnfGenerator();
var jsonSampleGenerator = new JsonSampleGenerator();
var gbnf = gbnfGenerator.Generate(typeModel);
var jsonSample = jsonSampleGenerator.Generate(typeModel);
// default instructions on how to build the response, including describing the delimters.
var sampleInstructions = jsonSampleGenerator.SampleInstructions();
var prompt = """
Extract the customer name and addresses from this order:
```
Customer: John Smith
Ship to: 123 Main St, Boston, MA 02108
Bill to: 456 Park Ave, New York, NY 10022
```
""";
// use the json sample
var promptWithSample = $"""
{prompt}
Return results as valid JSON in the following format:
{jsonSample}
{sampleInstructions}
""";
var executor = new StatelessExecutor(model, parameters);
var inferenceParams = new InferenceParams
{
SamplingPipeline = new DefaultSamplingPipeline
{
Grammar = new Grammar(gbnf, "root")
},
};
await foreach (var s in executor.InferAsync(promptWithSample, inferenceParams))
{
sb.Append(s);
}
var json = sb.ToString();
var customer = JsonSerializer.Deserialize<Customer>(json, JsonSerializerOptions) ?? throw new InvalidOperationException("Couldn't deserialize result");
customer.ShouldAllBe([
c => c.Name.ShouldBe("John Smith"),
c => c.ShippingAddress.Street.ShouldBe("123 Main St"),
c => c.ShippingAddress.City.ShouldBe("Boston"),
c => c.ShippingAddress.ZipCode.ShouldBe("02108"),
c => c.BillingAddress.Street.ShouldBe("456 Park Ave"),
c => c.BillingAddress.City.ShouldBe("New York"),
c => c.BillingAddress.ZipCode.ShouldBe("10022")
]);
Advanced Configuration
// Configure type model with descriptions and allowed values
var typeModel = new TypeModelBuilder<Person>()
.WithDescription(p => p.Name, "Full name of the person")
.WithAllowedValues(p => p.Name, new[] { "John", "Jane" })
.Build();
// Customize generator settings
var settings = new JsonSampleGeneratorSettings
{
OpeningDelimiter = '⟨',
ClosingDelimiter = '⟩',
PrettyPrint = true,
Indent = " "
};
var jsonSampleGenerator = new JsonSampleGenerator(settings);
Configuration Options
JsonSampleGeneratorSettings
OpeningDelimiter
: Character used to open placeholder values (default: '⟨')ClosingDelimiter
: Character used to close placeholder values (default: '⟩')PrettyPrint
: Enable/disable formatted JSON output (default: true)Indent
: Indentation string for pretty printing (default: " ")
GbnfGeneratorSettings
DefaultMinLength
: Minimum length for string values (default: 1)DefaultMaxLength
: Maximum length for string values (default: 512)OpeningDelimiter
: Character used to open placeholder valuesClosingDelimiter
: Character used to close placeholder valuesPrettyPrint
: Enable/disable formatted GBNF outputIndent
: Indentation string for pretty printing
Features in Detail
Type Support
RedPajama supports the following C# types:
- Primitive types (string, int, long, decimal, float, double)
- DateTime and DateTimeOffset
- Arrays
- Enums
- Complex objects with nested properties
- Nullable versions of supported types
Validation Attributes
[Description]
: Add descriptive comments to JSON samples[MinLength]
: Specify minimum string length[MaxLength]
: Specify maximum string length[AllowedValues]
: Define allowed string values
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
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.3 | 28 | 1/27/2025 |
0.0.0-alpha.0.2 | 29 | 1/26/2025 |
0.0.0-alpha.0.1 | 28 | 1/26/2025 |