CoreTemplate.AI 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CoreTemplate.AI --version 1.1.0
                    
NuGet\Install-Package CoreTemplate.AI -Version 1.1.0
                    
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="CoreTemplate.AI" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CoreTemplate.AI" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CoreTemplate.AI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CoreTemplate.AI --version 1.1.0
                    
#r "nuget: CoreTemplate.AI, 1.1.0"
                    
#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.
#:package CoreTemplate.AI@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CoreTemplate.AI&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CoreTemplate.AI&version=1.1.0
                    
Install as a Cake Tool

๐Ÿง  CoreTemplate.AI

Modular & Configurable AI Service Layer for .NET (OpenRouter + Ollama)

CoreTemplate.AI is a pluggable, strategy-based AI integration library for .NET Core projects.
It lets you easily switch between cloud-based and local LLM providers โ€” like OpenRouter and Ollama โ€” at runtime via configuration.

Designed with Clean Architecture, Strategy Pattern, MediatR, and strong extensibility in mind.


โœจ Features

  • โœ… Plug-and-play AI integration
  • โš™๏ธ Provider switch via appsettings.json (OpenRouter, Ollama)
  • ๐Ÿ”„ Dynamic model override (per-request)
  • โœ… Model support validation (checks if model exists via real API)
  • ๐Ÿงฑ Fully testable & decoupled structure
  • ๐Ÿ’ก Built-in CQRS Command: PromptTextCommand (MediatR)

๐Ÿ“ฆ Installation


dotnet add package CoreTemplate.AI

Or add manually in .csproj:

<PackageReference Include="CoreTemplate.AI" Version="1.1.0" />

๐Ÿ› ๏ธ Program.cs Setup

// --- AI Services ---
builder.Services.AddOptions<AISettings>()
    .Bind(builder.Configuration.GetSection("AiSettings"))
    .Validate(settings => Enum.IsDefined(typeof(AIProvider), settings.Provider),
        "Invalid AI provider configured in AiSettings.Provider");

builder.Services.AddSingleton(sp =>
    sp.GetRequiredService<IOptions<AISettings>>().Value);

// Providers
builder.Services.AddScoped<OpenRouterAiService>();
builder.Services.AddScoped<OllamaAiService>();
builder.Services.AddScoped<IAIService, AIServiceResolver>();

// Model Providers
builder.Services.AddScoped<OpenRouterModelProvider>();
builder.Services.AddScoped<OllamaModelProvider>();
builder.Services.AddScoped<AIModelProviderResolver>();

// MediatR
builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssembly(typeof(PromptTextCommandHandler).Assembly);
});

โš™๏ธ Configuration

appsettings.json

"AiSettings": {
  "Provider": "OpenRouter", // or "Ollama"
  "Model": "anthropic/claude-3-haiku" // any model that supported
},
"OpenAI": {
  "ApiKey": "sk-your-openrouter-api-key" // need for OpenRouter
}

๐Ÿš€ Example Usage

๐Ÿงฉ 1. Using MediatR (Preferred) Send prompt through built-in PromptTextCommand

var result = await _mediator.Send(new PromptTextCommand
{
    Prompt = "Write a poem about code.",
    Options = new AIRequestOptions
    {
        Context = "You are a helpful assistant",
        Temperature = 0.7,
        Model = "mistralai/mistral-7b-instruct" // overrides config default
    }
});

๐Ÿงฉ 2. Using IAIService or with API Controller Directly

public class MyService
{
    private readonly IAIService _ai;

    public MyService(IAIService ai) => _ai = ai;

    public async Task<string> AskAsync(string prompt)
    {
        return await _ai.PromptAsync(prompt, new AIRequestOptions
        {
            Model = "llama2",
            Temperature = 0.8
        });
    }
}

or

[Route("api/ai")]
[ApiController]
public class AiController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly IAIService _aiService;

    public AiController(IMediator mediator, IAIService aiService)
    {
        _mediator = mediator;
        _aiService = aiService;
    }

    // POST: /api/ai/prompt
    [HttpPost("prompt")]
    public async Task<IActionResult> Prompt([FromBody] PromptTextCommand command)
    {
        var result = await _mediator.Send(command);

        return Ok(new { result });
    }

    // GET: /api/ai/model-supported?model=anthropic/claude-3-haiku
    [HttpGet("model-supported")]
    public async Task<IActionResult> IsModelSupported([FromQuery] string model)
    {
        var isSupported = await _aiService.IsModelSupportedAsync(model);

        return Ok(new { model, isSupported });
    }
}

๐Ÿ“˜ Supported Providers

Provider Description Notes
OpenRouter Cloud-based OpenAI-compatible proxy Requires API key
Ollama Local models via Ollama CLI Requires ollama run

๐Ÿ›ก๏ธ Model Validation

Before sending the prompt, the system checks if the model exists using:

https://openrouter.ai/api/v1/models for OpenRouter

http://localhost:11434/tags for Ollama

๐Ÿงช Example cURL Test

curl -X POST https://localhost:5001/api/ai/prompt
-H "Content-Type: application/json"
-d '{ "prompt": "Explain quantum computing", "options": { "model": "anthropic/claude-3-haiku" } }'

๐Ÿงฐ Technologies

  • .NET 9
  • Clean Architecture
  • Strategy Pattern
  • MediatR (CQRS support)
  • FluentValidation
  • JSON-based configuration
  • OpenAI-compatible APIs

๐Ÿ‘ค Author

Developed by @CerenSusuz GitHub: [https://github.com/CerenSusuz/CoreTemplateApp/tree/master/Core.AI]

๐Ÿ“„ License

This project is licensed under the MIT License.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.4.0 135 8/20/2025
1.3.0 137 7/9/2025
1.2.0 134 7/8/2025
1.1.1 140 7/3/2025
1.1.0 226 7/3/2025 1.1.0 is deprecated because it is no longer maintained.