Update C# examples with .NET 10 and Mono sections
This commit is contained in:
parent
6c0dfcb4ca
commit
0ac9ba7430
1 changed files with 193 additions and 102 deletions
|
|
@ -45,12 +45,10 @@
|
|||
<!-- 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">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>
|
||||
|
|
@ -91,7 +89,7 @@
|
|||
</header>
|
||||
|
||||
<h2 id="csharp-intro">C# Examples</h2>
|
||||
<p>This page demonstrates how to use the <strong>uncloseai.</strong> API endpoints with C# using the official OpenAI .NET library. All examples use the same OpenAI-compatible API interface, making it easy to switch between different models and endpoints.</p>
|
||||
<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>
|
||||
|
|
@ -100,17 +98,42 @@
|
|||
<li><strong>TTS:</strong> <code>https://speech.ai.unturf.com/v1</code> - Text-to-speech generation</li>
|
||||
</ul>
|
||||
|
||||
<h3 id="csharp-client">C# Client Installation</h3>
|
||||
<p>Install the OpenAI NuGet package using the .NET CLI:</p>
|
||||
<pre><code class="bash">dotnet add package OpenAI --version 2.5.0</code></pre>
|
||||
<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>
|
||||
|
||||
<p>Or via Package Manager Console:</p>
|
||||
<pre><code class="bash">Install-Package OpenAI</code></pre>
|
||||
<!-- .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>
|
||||
|
||||
<h2 id="csharp-non-streaming">Non-Streaming Examples</h2>
|
||||
<p>Non-streaming mode waits for the complete response before returning. This is simpler to use but provides no intermediate feedback during generation.</p>
|
||||
<h3 id="dotnet10-setup">Setup</h3>
|
||||
<pre><code class="bash"># Create new project
|
||||
dotnet new console -n UncloseAI
|
||||
cd UncloseAI
|
||||
|
||||
<h3 id="csharp-hermes-non-stream">Using Hermes (General Purpose)</h3>
|
||||
# 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(
|
||||
|
|
@ -134,40 +157,9 @@ ChatCompletion completion = await client.CompleteChatAsync(
|
|||
}
|
||||
);
|
||||
|
||||
Console.WriteLine(completion.Content[0].Text);
|
||||
</code></pre>
|
||||
Console.WriteLine(completion.Content[0].Text);</code></pre>
|
||||
|
||||
<h3 id="csharp-qwen-non-stream">Using Qwen 3 Coder (Specialized for Coding)</h3>
|
||||
<pre><code class="csharp">using OpenAI.Chat;
|
||||
|
||||
var client = new ChatClient(
|
||||
model: "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M",
|
||||
apiKey: "choose-any-value",
|
||||
new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri("https://qwen.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);
|
||||
</code></pre>
|
||||
|
||||
<h2 id="csharp-streaming">Streaming Examples</h2>
|
||||
<p>Streaming mode returns chunks of the response as they are generated, providing real-time feedback. This is ideal for interactive applications and long responses.</p>
|
||||
|
||||
<h3 id="csharp-hermes-stream">Using Hermes (General Purpose)</h3>
|
||||
<h3 id="dotnet10-stream">Streaming (Hermes)</h3>
|
||||
<pre><code class="csharp">using OpenAI.Chat;
|
||||
|
||||
var client = new ChatClient(
|
||||
|
|
@ -182,12 +174,12 @@ var client = new ChatClient(
|
|||
var streamingUpdates = client.CompleteChatStreamingAsync(
|
||||
new List<ChatMessage>
|
||||
{
|
||||
new UserChatMessage("Give a Python Fizzbuzz solution in one line of code?")
|
||||
new UserChatMessage("Write a short poem about coding")
|
||||
},
|
||||
new ChatCompletionOptions
|
||||
{
|
||||
Temperature = 0.5f,
|
||||
MaxTokens = 150
|
||||
MaxTokens = 200
|
||||
}
|
||||
);
|
||||
|
||||
|
|
@ -197,50 +189,14 @@ await foreach (var update in streamingUpdates)
|
|||
{
|
||||
Console.Write(contentPart.Text);
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
|
||||
<h3 id="csharp-qwen-stream">Using Qwen 3 Coder (Specialized for Coding)</h3>
|
||||
<pre><code class="csharp">using OpenAI.Chat;
|
||||
|
||||
var client = new ChatClient(
|
||||
model: "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M",
|
||||
apiKey: "choose-any-value",
|
||||
new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri("https://qwen.ai.unturf.com/v1")
|
||||
}
|
||||
);
|
||||
|
||||
var streamingUpdates = client.CompleteChatStreamingAsync(
|
||||
new List<ChatMessage>
|
||||
{
|
||||
new UserChatMessage("Give a Python Fizzbuzz solution in one line of code?")
|
||||
},
|
||||
new ChatCompletionOptions
|
||||
{
|
||||
Temperature = 0.5f,
|
||||
MaxTokens = 150
|
||||
}
|
||||
);
|
||||
|
||||
await foreach (var update in streamingUpdates)
|
||||
{
|
||||
foreach (var contentPart in update.ContentUpdate)
|
||||
{
|
||||
Console.Write(contentPart.Text);
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
|
||||
<h2 id="csharp-tts">Text-to-Speech Example</h2>
|
||||
<p>Generate audio speech from text using the TTS endpoint. The audio is saved as an MP3 file.</p>
|
||||
}</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: "YOLO",
|
||||
apiKey: "choose-any-value",
|
||||
new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri("https://speech.ai.unturf.com/v1")
|
||||
|
|
@ -248,21 +204,154 @@ var client = new AudioClient(
|
|||
);
|
||||
|
||||
BinaryData speech = await client.GenerateSpeechAsync(
|
||||
"I think so therefore, Today is a wonderful day to grow something people love!",
|
||||
"Hello! This is a test of text to speech.",
|
||||
GeneratedSpeechVoice.Alloy,
|
||||
new SpeechGenerationOptions
|
||||
{
|
||||
Speed = 0.9f
|
||||
}
|
||||
new SpeechGenerationOptions { Speed = 1.0f }
|
||||
);
|
||||
|
||||
await File.WriteAllBytesAsync("speech.mp3", speech.ToArray());
|
||||
</code></pre>
|
||||
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>© uncloseai. 2025</small>
|
||||
<small>© 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>
|
||||
|
|
@ -287,7 +376,6 @@ await File.WriteAllBytesAsync("speech.mp3", speech.ToArray());
|
|||
let current = '';
|
||||
sections.forEach(section => {
|
||||
const sectionTop = section.offsetTop;
|
||||
const sectionHeight = section.clientHeight;
|
||||
if (window.scrollY >= sectionTop - 100) {
|
||||
current = section.getAttribute('id');
|
||||
}
|
||||
|
|
@ -306,24 +394,27 @@ await File.WriteAllBytesAsync("speech.mp3", speech.ToArray());
|
|||
<!-- 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="#csharp-client">Installation</a></li>
|
||||
<li><a href="#csharp-non-streaming">Non-Streaming</a>
|
||||
<li><a href="#runtimes">Runtime Options</a></li>
|
||||
<li><a href="#dotnet10">.NET 10</a>
|
||||
<ul>
|
||||
<li><a href="#csharp-hermes-non-stream">Hermes</a></li>
|
||||
<li><a href="#csharp-qwen-non-stream">Qwen Coder</a></li>
|
||||
<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="#csharp-streaming">Streaming</a>
|
||||
<li><a href="#mono">Mono</a>
|
||||
<ul>
|
||||
<li><a href="#csharp-hermes-stream">Hermes</a></li>
|
||||
<li><a href="#csharp-qwen-stream">Qwen Coder</a></li>
|
||||
<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="#csharp-tts">Text-to-Speech</a></li>
|
||||
<li><a href="#source">Full SDK</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue