365 lines
14 KiB
HTML
365 lines
14 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>Rust 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="/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" class="active">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="/inference.html">Inference Setup</a></li>
|
|
<li><a href="https://git.unturf.com/engineering/unturf/uncloseai.com/-/tree/master/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>Rust 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="rust-intro">Rust Examples</h2>
|
|
<p>This page demonstrates how to use the <strong>uncloseai.</strong> API endpoints with Rust using the async-openai community library. All examples use the same 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="rust-client">Rust Client Installation</h3>
|
|
<p>Add the async-openai crate to your <code>Cargo.toml</code>:</p>
|
|
<pre><code class="toml">[dependencies]
|
|
async-openai = "0.24"
|
|
tokio = { version = "1", features = ["full"] }</code></pre>
|
|
|
|
<h2 id="rust-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="rust-hermes-non-stream">Using Hermes (General Purpose)</h3>
|
|
<pre><code class="rust">use async_openai::{
|
|
config::OpenAIConfig,
|
|
types::{ChatCompletionRequestMessage, CreateChatCompletionRequestArgs},
|
|
Client,
|
|
};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let config = OpenAIConfig::new()
|
|
.with_api_key("choose-any-value")
|
|
.with_api_base("https://hermes.ai.unturf.com/v1");
|
|
|
|
let client = Client::with_config(config);
|
|
|
|
let request = CreateChatCompletionRequestArgs::default()
|
|
.model("adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic")
|
|
.messages(vec![ChatCompletionRequestMessage::User(
|
|
"Give a Python Fizzbuzz solution in one line of code?".into()
|
|
)])
|
|
.temperature(0.5)
|
|
.max_tokens(150u32)
|
|
.build()?;
|
|
|
|
let response = client.chat().create(request).await?;
|
|
|
|
println!("{}", response.choices[0].message.content.as_ref().unwrap());
|
|
|
|
Ok(())
|
|
}
|
|
</code></pre>
|
|
|
|
<h3 id="rust-qwen-non-stream">Using Qwen 3 Coder (Specialized for Coding)</h3>
|
|
<pre><code class="rust">use async_openai::{
|
|
config::OpenAIConfig,
|
|
types::{ChatCompletionRequestMessage, CreateChatCompletionRequestArgs},
|
|
Client,
|
|
};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let config = OpenAIConfig::new()
|
|
.with_api_key("choose-any-value")
|
|
.with_api_base("https://qwen.ai.unturf.com/v1");
|
|
|
|
let client = Client::with_config(config);
|
|
|
|
let request = CreateChatCompletionRequestArgs::default()
|
|
.model("hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M")
|
|
.messages(vec![ChatCompletionRequestMessage::User(
|
|
"Give a Python Fizzbuzz solution in one line of code?".into()
|
|
)])
|
|
.temperature(0.5)
|
|
.max_tokens(150u32)
|
|
.build()?;
|
|
|
|
let response = client.chat().create(request).await?;
|
|
|
|
println!("{}", response.choices[0].message.content.as_ref().unwrap());
|
|
|
|
Ok(())
|
|
}
|
|
</code></pre>
|
|
|
|
<h2 id="rust-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="rust-hermes-stream">Using Hermes (General Purpose)</h3>
|
|
<pre><code class="rust">use async_openai::{
|
|
config::OpenAIConfig,
|
|
types::{ChatCompletionRequestMessage, CreateChatCompletionRequestArgs},
|
|
Client,
|
|
};
|
|
use futures::StreamExt;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let config = OpenAIConfig::new()
|
|
.with_api_key("choose-any-value")
|
|
.with_api_base("https://hermes.ai.unturf.com/v1");
|
|
|
|
let client = Client::with_config(config);
|
|
|
|
let request = CreateChatCompletionRequestArgs::default()
|
|
.model("adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic")
|
|
.messages(vec![ChatCompletionRequestMessage::User(
|
|
"Give a Python Fizzbuzz solution in one line of code?".into()
|
|
)])
|
|
.temperature(0.5)
|
|
.max_tokens(150u32)
|
|
.build()?;
|
|
|
|
let mut stream = client.chat().create_stream(request).await?;
|
|
|
|
while let Some(result) = stream.next().await {
|
|
match result {
|
|
Ok(response) => {
|
|
for choice in response.choices {
|
|
if let Some(content) = &choice.delta.content {
|
|
print!("{}", content);
|
|
}
|
|
}
|
|
}
|
|
Err(err) => eprintln!("Error: {}", err),
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
</code></pre>
|
|
|
|
<h3 id="rust-qwen-stream">Using Qwen 3 Coder (Specialized for Coding)</h3>
|
|
<pre><code class="rust">use async_openai::{
|
|
config::OpenAIConfig,
|
|
types::{ChatCompletionRequestMessage, CreateChatCompletionRequestArgs},
|
|
Client,
|
|
};
|
|
use futures::StreamExt;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let config = OpenAIConfig::new()
|
|
.with_api_key("choose-any-value")
|
|
.with_api_base("https://qwen.ai.unturf.com/v1");
|
|
|
|
let client = Client::with_config(config);
|
|
|
|
let request = CreateChatCompletionRequestArgs::default()
|
|
.model("hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M")
|
|
.messages(vec![ChatCompletionRequestMessage::User(
|
|
"Give a Python Fizzbuzz solution in one line of code?".into()
|
|
)])
|
|
.temperature(0.5)
|
|
.max_tokens(150u32)
|
|
.build()?;
|
|
|
|
let mut stream = client.chat().create_stream(request).await?;
|
|
|
|
while let Some(result) = stream.next().await {
|
|
match result {
|
|
Ok(response) => {
|
|
for choice in response.choices {
|
|
if let Some(content) = &choice.delta.content {
|
|
print!("{}", content);
|
|
}
|
|
}
|
|
}
|
|
Err(err) => eprintln!("Error: {}", err),
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
</code></pre>
|
|
|
|
<h2 id="rust-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>
|
|
|
|
<pre><code class="rust">use async_openai::{
|
|
config::OpenAIConfig,
|
|
types::{CreateSpeechRequestArgs, SpeechModel, Voice},
|
|
Client,
|
|
};
|
|
use std::fs::File;
|
|
use std::io::Write;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let config = OpenAIConfig::new()
|
|
.with_api_key("YOLO")
|
|
.with_api_base("https://speech.ai.unturf.com/v1");
|
|
|
|
let client = Client::with_config(config);
|
|
|
|
let request = CreateSpeechRequestArgs::default()
|
|
.model(SpeechModel::Tts1)
|
|
.voice(Voice::Alloy)
|
|
.input("I think so therefore, Today is a wonderful day to grow something people love!")
|
|
.speed(0.9)
|
|
.build()?;
|
|
|
|
let response = client.audio().speech(request).await?;
|
|
|
|
let mut file = File::create("speech.mp3")?;
|
|
file.write_all(&response.bytes)?;
|
|
|
|
Ok(())
|
|
}
|
|
</code></pre>
|
|
|
|
<script>hljs.highlightAll();</script>
|
|
|
|
<footer>
|
|
<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>
|
|
</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;
|
|
const sectionHeight = section.clientHeight;
|
|
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="#rust-intro" class="active">Overview</a></li>
|
|
<li><a href="#rust-client">Installation</a></li>
|
|
<li><a href="#rust-non-streaming">Non-Streaming</a>
|
|
<ul>
|
|
<li><a href="#rust-hermes-non-stream">Hermes</a></li>
|
|
<li><a href="#rust-qwen-non-stream">Qwen Coder</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#rust-streaming">Streaming</a>
|
|
<ul>
|
|
<li><a href="#rust-hermes-stream">Hermes</a></li>
|
|
<li><a href="#rust-qwen-stream">Qwen Coder</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#rust-tts">Text-to-Speech</a></li>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</aside>
|
|
</body>
|
|
|
|
</html>
|