uncloseai.

C# Examples - Free LLM & TTS AI Service

C# Examples

This page demonstrates how to use the uncloseai. API endpoints with C#. Examples are provided for both .NET 10 (latest) and Mono (cross-platform) runtimes.

Available Endpoints:

Runtime Options

Runtime Best For Setup
.NET 10 Latest features, best performance, Windows/Linux/macOS dotnet new console
Mono Legacy systems, embedded, wide platform support mcs Program.cs

.NET 10 Examples

Using the official OpenAI .NET SDK with .NET 10. This provides the cleanest API with full async/await support.

Setup

# Create new project
dotnet new console -n UncloseAI
cd UncloseAI

# Add OpenAI package
dotnet add package OpenAI --version 2.5.0

Non-Streaming (Hermes)

using OpenAI.Chat;

var client = new ChatClient(
    model: "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
    apiKey: "choose-any-value",
    new OpenAIClientOptions
    {
        Endpoint = new Uri("https://hermes.ai.unturf.com/v1")
    }
);

ChatCompletion completion = await client.CompleteChatAsync(
    new List<ChatMessage>
    {
        new UserChatMessage("Give a Python Fizzbuzz solution in one line of code?")
    },
    new ChatCompletionOptions
    {
        Temperature = 0.5f,
        MaxTokens = 150
    }
);

Console.WriteLine(completion.Content[0].Text);

Streaming (Hermes)

using OpenAI.Chat;

var client = new ChatClient(
    model: "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
    apiKey: "choose-any-value",
    new OpenAIClientOptions
    {
        Endpoint = new Uri("https://hermes.ai.unturf.com/v1")
    }
);

var streamingUpdates = client.CompleteChatStreamingAsync(
    new List<ChatMessage>
    {
        new UserChatMessage("Write a short poem about coding")
    },
    new ChatCompletionOptions
    {
        Temperature = 0.5f,
        MaxTokens = 200
    }
);

await foreach (var update in streamingUpdates)
{
    foreach (var contentPart in update.ContentUpdate)
    {
        Console.Write(contentPart.Text);
    }
}

Text-to-Speech

using OpenAI.Audio;

var client = new AudioClient(
    model: "tts-1",
    apiKey: "choose-any-value",
    new OpenAIClientOptions
    {
        Endpoint = new Uri("https://speech.ai.unturf.com/v1")
    }
);

BinaryData speech = await client.GenerateSpeechAsync(
    "Hello! This is a test of text to speech.",
    GeneratedSpeechVoice.Alloy,
    new SpeechGenerationOptions { Speed = 1.0f }
);

await File.WriteAllBytesAsync("speech.mp3", speech.ToArray());
Console.WriteLine("Audio saved to speech.mp3");

Mono Examples

Using HttpClient directly with Mono runtime. No external packages required - works with the built-in System.Net.Http.

Setup

# Install Mono (Ubuntu/Debian)
sudo apt install mono-complete

# Compile
mcs -r:System.Net.Http.dll Program.cs

# Run
mono Program.exe

Non-Streaming (Hermes)

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static readonly HttpClient client = new HttpClient();

    static async Task Main()
    {
        var payload = @"{
            ""model"": ""adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"",
            ""messages"": [{""role"": ""user"", ""content"": ""Give a Python Fizzbuzz solution in one line of code?""}],
            ""max_tokens"": 150
        }";

        var content = new StringContent(payload, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(
            "https://hermes.ai.unturf.com/v1/chat/completions",
            content
        );

        var body = await response.Content.ReadAsStringAsync();
        Console.WriteLine(body);
    }
}

Streaming (Hermes)

using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static readonly HttpClient client = new HttpClient();

    static async Task Main()
    {
        var payload = @"{
            ""model"": ""adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"",
            ""messages"": [{""role"": ""user"", ""content"": ""Write a short poem about coding""}],
            ""max_tokens"": 200,
            ""stream"": true
        }";

        var request = new HttpRequestMessage(HttpMethod.Post, "https://hermes.ai.unturf.com/v1/chat/completions");
        request.Content = new StringContent(payload, Encoding.UTF8, "application/json");

        var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        using var stream = await response.Content.ReadAsStreamAsync();
        using var reader = new StreamReader(stream);

        while (!reader.EndOfStream)
        {
            var line = await reader.ReadLineAsync();
            if (line?.StartsWith("data: ") == true)
            {
                var json = line.Substring(6);
                if (json != "[DONE]")
                {
                    // Extract content from JSON (simplified)
                    var start = json.IndexOf("\"content\":\"") + 11;
                    if (start > 10)
                    {
                        var end = json.IndexOf("\"", start);
                        if (end > start)
                        {
                            Console.Write(json.Substring(start, end - start));
                        }
                    }
                }
            }
        }
    }
}

Text-to-Speech

using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static readonly HttpClient client = new HttpClient();

    static async Task Main()
    {
        var payload = @"{
            ""model"": ""tts-1"",
            ""input"": ""Hello! This is a test of text to speech."",
            ""voice"": ""alloy""
        }";

        var content = new StringContent(payload, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(
            "https://speech.ai.unturf.com/v1/audio/speech",
            content
        );

        var audioBytes = await response.Content.ReadAsByteArrayAsync();
        File.WriteAllBytes("speech.mp3", audioBytes);
        Console.WriteLine("Audio saved to speech.mp3");
    }
}

Available voices: alloy, echo, fable, onyx, nova, shimmer

Full SDK Implementations

For complete SDK implementations with model discovery and advanced features: