Getting Started with MiaDotNet SDK

MiaDotNet SDK is a modern .NET library that provides seamless access to Heroku's Managed Inference and Agent (MIA) add-on capabilities, enabling .NET developers to leverage powerful AI tools with minimal code.

Installation & Setup

1. Add NuGet Package

Add the MiaDotNet SDK to your project using NuGet:

dotnet add package MiaDotNet.SDK
2. Register Services

Register the MiaDotNet services in your Program.cs file:

builder.Services.AddMiaDotNetSdk();

This single line registers all required services for working with Heroku AI models.

3. Configure Environment Variables

Each service requires specific environment variables that are provided by Heroku when you provision the add-on:

Service Required Environment Variables
Inference (Chat) INFERENCE_URL, INFERENCE_KEY, INFERENCE_MODEL_ID
Embedding EMBEDDING_URL, EMBEDDING_KEY, EMBEDDING_MODEL_ID
Diffusion (Image) DIFFUSION_URL, DIFFUSION_KEY, DIFFUSION_MODEL_ID
Supported AI Models
Text Generation
  • claude-3-5-sonnet-latest
  • claude-3-5-haiku
  • claude-3-7-sonnet
Embedding
  • cohere-embed-multilingual
Image Generation
  • stable-image-ultra

Core Services

Inference Service

Enables interaction with Large Language Models (LLMs) for chat, completion, and tool usage scenarios.

Key Features:
  • Sync & async response options
  • Streaming chat completions
  • Tool calling support
  • Automatic validation
Use Cases:
  • Chatbots
  • Content generation
  • Tool-using AI agents

Embedding Service

Creates vector representations (embeddings) of text for semantic search and similarity matching.

Key Features:
  • Multi-lingual support
  • Generic type interface
  • Optimized for RAG patterns
Use Cases:
  • Semantic search
  • Document clustering
  • Recommendations

Diffusion Service

Generates images from text prompts using state-of-the-art diffusion models.

Key Features:
  • High-quality image generation
  • Configurable parameters
  • Base64 image encoding
Use Cases:
  • Content creation
  • Design mockups
  • Visual storytelling

Basic Usage Examples


using MiaDotNetSdk.DTOs;
using MiaDotNetSdk.Services;

public class ChatService
{
    private readonly IMiaInferenceService _inferenceService;

    public ChatService(IMiaInferenceService inferenceService)
    {
        _inferenceService = inferenceService;
    }

    public async Task<string> GetChatCompletionAsync(string userMessage)
    {
        var request = new InferenceRequest
        {
            Messages = 
            [
                new()
                {
                    Role = "user",
                    Content = userMessage
                }
            ]
        };

        var response = await _inferenceService.GetResponseAsync(request);
        
        return response?.FirstOrDefault()?.Choices?.FirstOrDefault()?.Message?.Content ?? string.Empty;
    }
    
    // Example with streaming
    public async Task StreamChatCompletionAsync(string userMessage, Action<string> onChunkReceived)
    {
        var request = new InferenceRequest
        {
            Stream = true,
            Messages = 
            [
                new()
                {
                    Role = "user",
                    Content = userMessage
                }
            ]
        };

        await foreach (var chunk in _inferenceService.GetResponseChunksAsync(request))
        {
            var content = chunk.Choices?.FirstOrDefault()?.Delta?.Content;
            if (!string.IsNullOrEmpty(content))
            {
                onChunkReceived(content);
            }
        }
    }
}
                            


using MiaDotNetSdk.DTOs;
using MiaDotNetSdk.Services;

public class SearchService
{
    private readonly IMiaEmbeddingService _embeddingService;

    public SearchService(IMiaEmbeddingService embeddingService)
    {
        _embeddingService = embeddingService;
    }

    public async Task<float[]> GetEmbeddingAsync(string text)
    {
        var request = new EmbeddingRequest
        {
            Input = text
        };

        var response = await _embeddingService.GetResponseAsync<float[]>(request, CancellationToken.None);
        
        return response?.Embedding ?? Array.Empty<float>();
    }
}
                            


using MiaDotNetSdk.DTOs;
using MiaDotNetSdk.Services;

public class ImageService
{
    private readonly IMiaDiffusionService _diffusionService;

    public ImageService(IMiaDiffusionService diffusionService)
    {
        _diffusionService = diffusionService;
    }

    public async Task<string> GenerateImageAsync(string prompt)
    {
        var request = new DiffusionRequest
        {
            Prompt = prompt,
            Size = "1024x1024",
            Quality = "standard",
            Style = "natural"
        };

        var response = await _diffusionService.GenerateResponseAsync(request);
        
        return response.Success ? response.Results?.FirstOrDefault()?.B64String ?? string.Empty : string.Empty;
    }
}
                            


using MiaDotNetSdk.Constants;
using MiaDotNetSdk.DTOs;
using MiaDotNetSdk.Services;

public class AgentService
{
    private readonly IMiaInferenceService _inferenceService;

    public AgentService(IMiaInferenceService inferenceService)
    {
        _inferenceService = inferenceService;
    }

    public async Task<string> SearchWebAsync(string query)
    {
        var request = new InferenceRequest
        {
            Messages = 
            [
                new()
                {
                    Role = "user",
                    Content = query
                }
            ],
            Tools = 
            [
                new()
                {
                    Type = "heroku_tool",
                    Function = new Function
                    {
                        Name = HerokuToolConsts.SearchWeb
                    }
                }
            ]
        };

        var response = await _inferenceService.GetResponseAsync(request);
        
        return response?.FirstOrDefault()?.Choices?.FirstOrDefault()?.Message?.Content ?? string.Empty;
    }
}
                            

Heroku Tool Integration

MiaDotNet SDK provides seamless access to Heroku's tool ecosystem, allowing you to create powerful AI agents capable of:

Web Browsing

Enable your AI to search the web, browse multi-page content, and extract information from web pages.

Document Analysis

Read and analyze PDF documents from URLs with automatic content extraction.

Database Operations

Query database schemas and execute SQL statements against Heroku Postgres databases.

Code Execution

Run dynamically generated code in Python, Ruby, Node.js, and Go with automatic error correction.

Dyno Management

Execute commands on Heroku dynos to integrate with your existing application logic.

Custom Tools

Extend functionality by creating your own custom tools that integrate with your application.

Security Note

Environment variables containing API keys should be properly secured and never exposed in client-side code. Follow the ASP.NET Core Secret Management guidelines for development and proper configuration management for production.