uncloseai.com/public/csharp-examples.html

446 lines
17 KiB
HTML

<!--
This is free software for the public good of a permacomputer hosted at
permacomputer.com, an always-on computer by the people, for the people.
One which is durable, easy to repair, & distributed like tap water
for machine learning intelligence.
The permacomputer is community-owned infrastructure optimized around
four values:
TRUTH First principles, math & science, open source code freely distributed
FREEDOM Voluntary partnerships, freedom from tyranny & corporate control
HARMONY Minimal waste, self-renewing systems with diverse thriving connections
LOVE Be yourself without hurting others, cooperation through natural law
This software contributes to that vision by making machine learning
accessible to everyone through a free, open, embeddable chat interface.
Code is seeds to sprout on any abandoned technology.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#43a047">
<meta name="color-scheme" content="light dark">
<title>C# Examples | uncloseai.com</title>
<!-- PicoCSS -->
<link rel="stylesheet" href="/css/pico.classless.min.css">
<!-- ChunkFive Font -->
<link rel="stylesheet" href="/css/chunkfive/stylesheet.css" type="text/css" charset="utf-8" />
<!-- Sidebar Theme -->
<link rel="stylesheet" href="/css/sidebar-theme.css">
<!-- Highlight.js for syntax highlighting -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/styles/a11y-dark.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<!-- Theme Switcher Script -->
<script>
function switchTheme(theme) {
if (theme === "auto") {
document.documentElement.removeAttribute('data-theme');
} else {
document.documentElement.setAttribute('data-theme', theme);
}
}
// Disable custom styling for uncloseai.js - use PicoCSS instead
window.UNCLOSEAI_CUSTOM_STYLING = false;
</script>
<script src="https://uncloseai.com/uncloseai.js" type="module"></script>
</head>
<body>
<!-- Mobile menu toggle -->
<button class="sidebar-toggle" onclick="document.querySelector('.sidebar').classList.toggle('open')">
</button>
<!-- Left sidebar - Main Navigation -->
<aside class="sidebar">
<div class="table-of-contents">
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/c-examples.html">C Examples</a></li>
<li><a href="/csharp-examples.html" class="active">C# Examples</a></li>
<li><a href="/dart-examples.html">Dart Examples</a></li>
<li><a href="/elixir-examples.html">Elixir Examples</a></li>
<li><a href="/go-examples.html">Go Examples</a></li>
<li><a href="/java-examples.html">Java Examples</a></li>
<li><a href="/kotlin-examples.html">Kotlin Examples</a></li>
<li><a href="/nodejs-examples.html">Node.js Examples</a></li>
<li><a href="/php-examples.html">PHP Examples</a></li>
<li><a href="/python-examples.html">Python Examples</a></li>
<li><a href="/ruby-examples.html">Ruby Examples</a></li>
<li><a href="/rust-examples.html">Rust Examples</a></li>
<li><a href="/swift-examples.html">Swift Examples</a></li>
<li><a href="/uncloseai-js.html">uncloseai.js Docs</a></li>
<li><a href="/uncloseai-js-styleguide.html">Styleguide</a></li>
<li><a href="/cli.html">uncloseai-cli</a></li>
<li><a href="/browser-toys.html">Browser Toys</a></li>
<li><a href="/inference.html">Inference Setup</a></li>
<li><a href="/text-to-speech.html">Text-to-Speech</a></li>
<li><a href="/crawler.html">Our Crawler</a></li>
<li><a href="/reverse-retrieval-augmented-generations-rag.html">Reverse RAG</a></li>
<li><a href="/languages" target="_blank">🔗 All Languages</a></li>
<li><a href="https://shop.unturf.com/p/8486f492-a93e-11f0-b477-02dfe05770ee/uncloseai-machine-learning-reference-guide-to-inference-clients" target="_blank">📚 Book</a></li>
</ul>
</nav>
</div>
</aside>
<!-- Main content -->
<main>
<header>
<hgroup>
<a href="https://uncloseai.com"><h1 class="unturf" style="font-family: 'ChunkFiveRegular';">uncloseai.</h1></a>
<p>C# Examples - Free LLM & TTS AI Service</p>
</hgroup>
<nav>
<ul>
<li><a href="#" onclick="switchTheme('auto')">Auto</a></li>
<li><a href="#" onclick="switchTheme('light')">Light</a></li>
<li><a href="#" onclick="switchTheme('dark')">Dark</a></li>
</ul>
</nav>
</header>
<h2 id="csharp-intro">C# Examples</h2>
<p>This page demonstrates how to use the <strong>uncloseai.</strong> API endpoints with C#. Examples are provided for both <strong>.NET 10</strong> (latest) and <strong>Mono</strong> (cross-platform) runtimes.</p>
<p><strong>Available Endpoints:</strong></p>
<ul>
<li><strong>Hermes:</strong> <code>https://hermes.ai.unturf.com/v1</code> - General purpose conversational AI</li>
<li><strong>Qwen 3 Coder:</strong> <code>https://qwen.ai.unturf.com/v1</code> - Specialized coding model</li>
<li><strong>TTS:</strong> <code>https://speech.ai.unturf.com/v1</code> - Text-to-speech generation</li>
</ul>
<h2 id="runtimes">Runtime Options</h2>
<table>
<thead>
<tr>
<th>Runtime</th>
<th>Best For</th>
<th>Setup</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>.NET 10</strong></td>
<td>Latest features, best performance, Windows/Linux/macOS</td>
<td><code>dotnet new console</code></td>
</tr>
<tr>
<td><strong>Mono</strong></td>
<td>Legacy systems, embedded, wide platform support</td>
<td><code>mcs Program.cs</code></td>
</tr>
</tbody>
</table>
<!-- .NET 10 SECTION -->
<h2 id="dotnet10">.NET 10 Examples</h2>
<p>Using the official OpenAI .NET SDK with .NET 10. This provides the cleanest API with full async/await support.</p>
<h3 id="dotnet10-setup">Setup</h3>
<pre><code class="bash"># Create new project
dotnet new console -n UncloseAI
cd UncloseAI
# Add OpenAI package
dotnet add package OpenAI --version 2.5.0</code></pre>
<h3 id="dotnet10-non-stream">Non-Streaming (Hermes)</h3>
<pre><code class="csharp">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&lt;ChatMessage&gt;
{
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);</code></pre>
<h3 id="dotnet10-stream">Streaming (Hermes)</h3>
<pre><code class="csharp">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&lt;ChatMessage&gt;
{
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);
}
}</code></pre>
<h3 id="dotnet10-tts">Text-to-Speech</h3>
<pre><code class="csharp">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");</code></pre>
<!-- MONO SECTION -->
<h2 id="mono">Mono Examples</h2>
<p>Using HttpClient directly with Mono runtime. No external packages required - works with the built-in System.Net.Http.</p>
<h3 id="mono-setup">Setup</h3>
<pre><code class="bash"># Install Mono (Ubuntu/Debian)
sudo apt install mono-complete
# Compile
mcs -r:System.Net.Http.dll Program.cs
# Run
mono Program.exe</code></pre>
<h3 id="mono-non-stream">Non-Streaming (Hermes)</h3>
<pre><code class="csharp">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);
}
}</code></pre>
<h3 id="mono-stream">Streaming (Hermes)</h3>
<pre><code class="csharp">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));
}
}
}
}
}
}
}</code></pre>
<h3 id="mono-tts">Text-to-Speech</h3>
<pre><code class="csharp">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");
}
}</code></pre>
<p><strong>Available voices:</strong> <code>alloy</code>, <code>echo</code>, <code>fable</code>, <code>onyx</code>, <code>nova</code>, <code>shimmer</code></p>
<h2 id="source">Full SDK Implementations</h2>
<p>For complete SDK implementations with model discovery and advanced features:</p>
<ul>
<li><a href="/languages/csharp/dotnet10/" target="_blank">.NET 10 SDK</a></li>
<li><a href="/languages/csharp/mono/" target="_blank">Mono SDK</a></li>
<li><a href="/languages/csharp/http/" target="_blank">HttpClient (generic .NET)</a></li>
<li><a href="/languages/csharp/openai/" target="_blank">OpenAI SDK wrapper</a></li>
</ul>
<script>hljs.highlightAll();</script>
<footer>
<small>&copy; uncloseai. 2025</small>
<br>
<small>Stylesheets by <a href="https://picocss.com" target="_blank">PicoCSS</a></small>
<small>& <a href="https://highlightjs.org/" target="_blank">highlight.js</a></small>
<br>
<small><a href="/privacy-policy.html">Privacy Policy</a> | <a href="/terms-of-use.html">Terms of Use</a></small>
</footer>
<script>
// Simple active link highlighting
document.querySelectorAll('.table-of-contents a').forEach(link => {
link.addEventListener('click', function() {
document.querySelectorAll('.table-of-contents a').forEach(a => a.classList.remove('active'));
this.classList.add('active');
});
});
// Highlight current section on scroll
const sections = document.querySelectorAll('h2[id], h3[id]');
const navLinks = document.querySelectorAll('.table-of-contents a');
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
if (window.scrollY >= sectionTop - 100) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === '#' + current) {
link.classList.add('active');
}
});
});
</script>
</main>
<!-- Right sidebar - Page TOC -->
<aside class="sidebar-right">
<div class="table-of-contents">
<nav>
<ul>
<li><a href="#csharp-intro" class="active">Overview</a></li>
<li><a href="#runtimes">Runtime Options</a></li>
<li><a href="#dotnet10">.NET 10</a>
<ul>
<li><a href="#dotnet10-setup">Setup</a></li>
<li><a href="#dotnet10-non-stream">Non-Streaming</a></li>
<li><a href="#dotnet10-stream">Streaming</a></li>
<li><a href="#dotnet10-tts">TTS</a></li>
</ul>
</li>
<li><a href="#mono">Mono</a>
<ul>
<li><a href="#mono-setup">Setup</a></li>
<li><a href="#mono-non-stream">Non-Streaming</a></li>
<li><a href="#mono-stream">Streaming</a></li>
<li><a href="#mono-tts">TTS</a></li>
</ul>
</li>
<li><a href="#source">Full SDK</a></li>
</ul>
</nav>
</div>
</aside>
</body>
</html>