uncloseai.com/public/c-examples.html
russell@unturf.com c6e47df7c2 add browser toys portal page and sidebar links
new browser-toys.html with install instructions, storage/privacy
docs, and per-browser guides. sidebar link added to all 26 pages.
browser toys section added to index.html and uncloseai-js.html.
2026-02-22 10:51:53 -05:00

387 lines
15 KiB
HTML

<!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" class="active">C Examples</a></li>
<li><a href="/csharp-examples.html">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="/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="/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="c-intro">C Examples</h2>
<p>This page demonstrates how to use the <strong>uncloseai.</strong> API endpoints with C using libcurl. All examples use the OpenAI-compatible API interface, making it easy to switch between different models and endpoints.</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>
<h3 id="c-setup">Setup & Dependencies</h3>
<p>Install libcurl development libraries:</p>
<pre><code class="bash"># Ubuntu/Debian
sudo apt-get install libcurl4-openssl-dev
# macOS
brew install curl
# Alpine
apk add curl-dev</code></pre>
<p>Compile with:</p>
<pre><code class="bash">gcc -o chat chat.c -lcurl</code></pre>
<h2 id="c-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="c-hermes-non-stream">Using Hermes (General Purpose)</h3>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;curl/curl.h&gt;
struct Memory {
char *response;
size_t size;
};
static size_t write_callback(void *data, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct Memory *mem = (struct Memory *)userp;
char *ptr = realloc(mem->response, mem->size + realsize + 1);
if (!ptr) return 0;
mem->response = ptr;
memcpy(&amp;(mem->response[mem->size]), data, realsize);
mem->size += realsize;
mem->response[mem->size] = 0;
return realsize;
}
int main(void) {
CURL *curl = curl_easy_init();
if (!curl) return 1;
struct Memory chunk = {.response = malloc(1), .size = 0};
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
const char *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"
"}";
curl_easy_setopt(curl, CURLOPT_URL, "https://hermes.ai.unturf.com/v1/chat/completions");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&amp;chunk);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK) {
printf("Response:\n%s\n", chunk.response);
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
free(chunk.response);
return 0;
}</code></pre>
<h3 id="c-qwen-non-stream">Using Qwen 3 Coder (Specialized for Coding)</h3>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;curl/curl.h&gt;
// ... same write_callback and Memory struct as above ...
int main(void) {
CURL *curl = curl_easy_init();
if (!curl) return 1;
struct Memory chunk = {.response = malloc(1), .size = 0};
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
const char *payload =
"{"
"\"model\": \"hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M\","
"\"messages\": [{\"role\": \"user\", \"content\": \"Explain quicksort in C with code\"}],"
"\"max_tokens\": 500"
"}";
curl_easy_setopt(curl, CURLOPT_URL, "https://qwen.ai.unturf.com/v1/chat/completions");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&amp;chunk);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK) {
printf("Response:\n%s\n", chunk.response);
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
free(chunk.response);
return 0;
}</code></pre>
<h2 id="c-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="c-hermes-stream">Streaming with Hermes</h3>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;curl/curl.h&gt;
static size_t stream_callback(void *data, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
char *chunk = (char *)data;
// Process SSE data lines
char *line = strtok(chunk, "\n");
while (line) {
if (strncmp(line, "data: ", 6) == 0) {
const char *json = line + 6;
if (strcmp(json, "[DONE]") != 0) {
// Extract content from: {"choices":[{"delta":{"content":"..."}}]}
const char *content_start = strstr(json, "\"content\":\"");
if (content_start) {
content_start += 11;
const char *content_end = strchr(content_start, '"');
if (content_end) {
fwrite(content_start, 1, content_end - content_start, stdout);
fflush(stdout);
}
}
}
}
line = strtok(NULL, "\n");
}
return realsize;
}
int main(void) {
CURL *curl = curl_easy_init();
if (!curl) return 1;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
const char *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"
"}";
curl_easy_setopt(curl, CURLOPT_URL, "https://hermes.ai.unturf.com/v1/chat/completions");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stream_callback);
printf("Streaming response:\n");
curl_easy_perform(curl);
printf("\n");
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return 0;
}</code></pre>
<h2 id="c-tts">Text-to-Speech Examples</h2>
<p>Generate audio from text using the TTS endpoint.</p>
<h3 id="c-tts-basic">Basic TTS</h3>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;curl/curl.h&gt;
static size_t write_file(void *data, size_t size, size_t nmemb, void *userp) {
return fwrite(data, size, nmemb, (FILE *)userp);
}
int main(void) {
CURL *curl = curl_easy_init();
if (!curl) return 1;
FILE *fp = fopen("output.mp3", "wb");
if (!fp) {
curl_easy_cleanup(curl);
return 1;
}
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
const char *payload =
"{"
"\"model\": \"tts-1\","
"\"input\": \"Hello! This is a test of text to speech.\","
"\"voice\": \"alloy\""
"}";
curl_easy_setopt(curl, CURLOPT_URL, "https://speech.ai.unturf.com/v1/audio/speech");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK) {
printf("Audio saved to output.mp3\n");
}
fclose(fp);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return 0;
}</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="c-source">Full SDK Implementation</h2>
<p>For a complete SDK with model discovery and advanced features, see the full implementations in the source repository:</p>
<ul>
<li><a href="/languages/c/curl/" target="_blank">C with libcurl</a></li>
<li><a href="/languages/c/libsoup/" target="_blank">C with libsoup (GNOME)</a></li>
<li><a href="/languages/c/nghttp2/" target="_blank">C with nghttp2 (HTTP/2)</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');
});
});
</script>
</main>
<!-- Right sidebar - Page TOC -->
<aside class="sidebar-right">
<div class="table-of-contents">
<nav>
<ul>
<li><a href="#c-intro" class="active">Introduction</a></li>
<li><a href="#c-setup">Setup</a></li>
<li><a href="#c-non-streaming">Non-Streaming</a>
<ul>
<li><a href="#c-hermes-non-stream">Hermes</a></li>
<li><a href="#c-qwen-non-stream">Qwen Coder</a></li>
</ul>
</li>
<li><a href="#c-streaming">Streaming</a>
<ul>
<li><a href="#c-hermes-stream">Hermes</a></li>
</ul>
</li>
<li><a href="#c-tts">Text-to-Speech</a></li>
<li><a href="#c-source">Full SDK</a></li>
</ul>
</nav>
</div>
</aside>
</body>
</html>