add bash language example - tested working with Docker
This commit is contained in:
parent
332a64825d
commit
973c045af6
4 changed files with 249 additions and 0 deletions
9
languages/bash/Dockerfile
Normal file
9
languages/bash/Dockerfile
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
FROM alpine:latest
|
||||
|
||||
RUN apk add --no-cache bash curl jq ca-certificates
|
||||
|
||||
WORKDIR /app
|
||||
COPY examples.sh .
|
||||
RUN chmod +x examples.sh
|
||||
|
||||
CMD ["./examples.sh"]
|
||||
74
languages/bash/examples.sh
Executable file
74
languages/bash/examples.sh
Executable file
|
|
@ -0,0 +1,74 @@
|
|||
#!/bin/bash
|
||||
|
||||
# uncloseai.com API Examples in Bash
|
||||
# Demonstrates Hermes AI, Qwen Coder, and TTS endpoints
|
||||
|
||||
echo "=== uncloseai.com Bash Examples ==="
|
||||
echo ""
|
||||
|
||||
# Example 1: Hermes AI Chat (Non-Streaming)
|
||||
echo "1. Hermes AI - General Purpose Chat"
|
||||
echo " Asking: 'Give a Python Fizzbuzz solution in one line of code?'"
|
||||
echo ""
|
||||
|
||||
hermes_response=$(curl -s -X POST "https://hermes.ai.unturf.com/v1/chat/completions" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer dummy-key" \
|
||||
-d '{
|
||||
"model": "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
|
||||
"messages": [{"role": "user", "content": "Give a Python Fizzbuzz solution in one line of code?"}],
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 150
|
||||
}')
|
||||
|
||||
echo "Response:"
|
||||
echo "$hermes_response" | jq -r '.choices[0].message.content' 2>/dev/null || echo "$hermes_response"
|
||||
echo ""
|
||||
echo "---"
|
||||
echo ""
|
||||
|
||||
# Example 2: Qwen 3 Coder - Specialized Coding Model
|
||||
echo "2. Qwen 3 Coder - Specialized for Code"
|
||||
echo " Asking: 'Write a bash function to check if a port is open'"
|
||||
echo ""
|
||||
|
||||
qwen_response=$(curl -s -X POST "https://qwen.ai.unturf.com/v1/chat/completions" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer dummy-key" \
|
||||
-d '{
|
||||
"model": "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M",
|
||||
"messages": [{"role": "user", "content": "Write a bash function to check if a port is open"}],
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 200
|
||||
}')
|
||||
|
||||
echo "Response:"
|
||||
echo "$qwen_response" | jq -r '.choices[0].message.content' 2>/dev/null || echo "$qwen_response"
|
||||
echo ""
|
||||
echo "---"
|
||||
echo ""
|
||||
|
||||
# Example 3: Text-to-Speech
|
||||
echo "3. Text-to-Speech Generation"
|
||||
echo " Converting text to speech and saving to speech.mp3"
|
||||
echo ""
|
||||
|
||||
curl -s -X POST "https://speech.ai.unturf.com/v1/audio/speech" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer YOLO" \
|
||||
-d '{
|
||||
"model": "tts-1",
|
||||
"voice": "alloy",
|
||||
"input": "Hello from Bash! Today is a wonderful day to build something people love!"
|
||||
}' \
|
||||
--output speech.mp3
|
||||
|
||||
if [ -f speech.mp3 ]; then
|
||||
file_size=$(stat -f%z speech.mp3 2>/dev/null || stat -c%s speech.mp3 2>/dev/null)
|
||||
echo "✓ Speech file created: speech.mp3 (${file_size} bytes)"
|
||||
else
|
||||
echo "✗ Failed to create speech file"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=== Examples Complete ==="
|
||||
166
languages/bash/index.html
Normal file
166
languages/bash/index.html
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Bash - uncloseai.com API Examples</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/styles/github-dark.min.css">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/highlight.min.js"></script>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; max-width: 900px; margin: 40px auto; padding: 0 20px; line-height: 1.6; }
|
||||
h1 { color: #43a047; }
|
||||
h2 { color: #333; margin-top: 2em; }
|
||||
pre { background: #1e1e1e; padding: 15px; border-radius: 5px; overflow-x: auto; }
|
||||
code { font-family: 'Courier New', monospace; }
|
||||
.note { background: #fff3cd; padding: 15px; border-left: 4px solid #ffc107; margin: 20px 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Bash - uncloseai.com API Examples</h1>
|
||||
|
||||
<p>This directory contains Bash examples for interacting with uncloseai.com's AI services using curl. Bash is perfect for quick scripts and command-line automation.</p>
|
||||
|
||||
<h2>Prerequisites</h2>
|
||||
<ul>
|
||||
<li>Bash shell</li>
|
||||
<li>curl (for HTTP requests)</li>
|
||||
<li>jq (optional, for JSON parsing)</li>
|
||||
<li>ca-certificates (for HTTPS)</li>
|
||||
</ul>
|
||||
|
||||
<h2>Files in This Directory</h2>
|
||||
<ul>
|
||||
<li><strong>examples.sh</strong> - Main script demonstrating all three APIs</li>
|
||||
<li><strong>Dockerfile</strong> - Docker container for running examples</li>
|
||||
<li><strong>index.html</strong> - This documentation file</li>
|
||||
</ul>
|
||||
|
||||
<h2>Running with Docker</h2>
|
||||
<pre><code class="bash"># Build the Docker image
|
||||
docker build -t ai-unturf-bash languages/bash/
|
||||
|
||||
# Run the examples
|
||||
docker run --rm ai-unturf-bash</code></pre>
|
||||
|
||||
<h2>Running Directly</h2>
|
||||
<pre><code class="bash"># Make script executable
|
||||
chmod +x examples.sh
|
||||
|
||||
# Run the examples
|
||||
./examples.sh</code></pre>
|
||||
|
||||
<h2>Code Example: Hermes AI Chat</h2>
|
||||
<p>Using curl to call the Hermes AI endpoint for general purpose conversation:</p>
|
||||
<pre><code class="bash">#!/bin/bash
|
||||
|
||||
# Hermes AI Chat - Non-Streaming
|
||||
hermes_response=$(curl -s -X POST "https://hermes.ai.unturf.com/v1/chat/completions" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer dummy-key" \
|
||||
-d '{
|
||||
"model": "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
|
||||
"messages": [{"role": "user", "content": "Give a Python Fizzbuzz solution in one line of code?"}],
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 150
|
||||
}')
|
||||
|
||||
# Parse response with jq (optional)
|
||||
echo "$hermes_response" | jq -r '.choices[0].message.content'</code></pre>
|
||||
|
||||
<h2>Code Example: Qwen 3 Coder</h2>
|
||||
<p>Qwen is specialized for coding tasks:</p>
|
||||
<pre><code class="bash">#!/bin/bash
|
||||
|
||||
# Qwen 3 Coder - Specialized for Code
|
||||
qwen_response=$(curl -s -X POST "https://qwen.ai.unturf.com/v1/chat/completions" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer dummy-key" \
|
||||
-d '{
|
||||
"model": "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M",
|
||||
"messages": [{"role": "user", "content": "Write a bash function to check if a port is open"}],
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 200
|
||||
}')
|
||||
|
||||
echo "$qwen_response" | jq -r '.choices[0].message.content'</code></pre>
|
||||
|
||||
<h2>Code Example: Text-to-Speech</h2>
|
||||
<p>Generate speech from text and save to a file:</p>
|
||||
<pre><code class="bash">#!/bin/bash
|
||||
|
||||
# Text-to-Speech - Save to file
|
||||
curl -s -X POST "https://speech.ai.unturf.com/v1/audio/speech" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer YOLO" \
|
||||
-d '{
|
||||
"model": "tts-1",
|
||||
"voice": "alloy",
|
||||
"input": "Hello from Bash! Today is a wonderful day to build something people love!"
|
||||
}' \
|
||||
--output speech.mp3
|
||||
|
||||
# Check if file was created
|
||||
if [ -f speech.mp3 ]; then
|
||||
echo "✓ Speech file created successfully"
|
||||
fi</code></pre>
|
||||
|
||||
<h2>Understanding the Code</h2>
|
||||
|
||||
<h3>1. Using curl for HTTP Requests</h3>
|
||||
<p>The curl command makes HTTP POST requests to the API endpoints:</p>
|
||||
<ul>
|
||||
<li><code>-s</code> - Silent mode (no progress bar)</li>
|
||||
<li><code>-X POST</code> - Use POST method</li>
|
||||
<li><code>-H</code> - Add HTTP headers (Content-Type, Authorization)</li>
|
||||
<li><code>-d</code> - Send JSON data in request body</li>
|
||||
<li><code>--output</code> - Save response to file (for TTS)</li>
|
||||
</ul>
|
||||
|
||||
<h3>2. Command Substitution</h3>
|
||||
<p>The <code>$(command)</code> syntax captures command output into a variable:</p>
|
||||
<pre><code class="bash">response=$(curl -s "https://api.example.com")
|
||||
echo "$response"</code></pre>
|
||||
|
||||
<h3>3. JSON Parsing with jq</h3>
|
||||
<p>The jq tool extracts data from JSON responses:</p>
|
||||
<pre><code class="bash">echo "$response" | jq -r '.choices[0].message.content'
|
||||
# -r flag removes quotes from string output</code></pre>
|
||||
|
||||
<h2>Common Issues</h2>
|
||||
|
||||
<div class="note">
|
||||
<strong>Issue:</strong> "jq: command not found"<br>
|
||||
<strong>Solution:</strong> Install jq: <code>apk add jq</code> (Alpine) or <code>apt-get install jq</code> (Debian/Ubuntu)
|
||||
</div>
|
||||
|
||||
<div class="note">
|
||||
<strong>Issue:</strong> "curl: (60) SSL certificate problem"<br>
|
||||
<strong>Solution:</strong> Install ca-certificates: <code>apk add ca-certificates</code>
|
||||
</div>
|
||||
|
||||
<div class="note">
|
||||
<strong>Issue:</strong> Empty or error response<br>
|
||||
<strong>Solution:</strong> Remove <code>-s</code> flag from curl to see error messages
|
||||
</div>
|
||||
|
||||
<h2>API Endpoints</h2>
|
||||
<ul>
|
||||
<li><strong>Hermes AI:</strong> https://hermes.ai.unturf.com/v1/chat/completions</li>
|
||||
<li><strong>Qwen Coder:</strong> https://qwen.ai.unturf.com/v1/chat/completions</li>
|
||||
<li><strong>TTS Speech:</strong> https://speech.ai.unturf.com/v1/audio/speech</li>
|
||||
</ul>
|
||||
|
||||
<h2>Next Steps</h2>
|
||||
<p>Try modifying the examples:</p>
|
||||
<ul>
|
||||
<li>Change the user message to ask different questions</li>
|
||||
<li>Adjust temperature (0.0-2.0) to control randomness</li>
|
||||
<li>Increase max_tokens for longer responses</li>
|
||||
<li>Try different TTS voices: alloy, echo, fable, onyx, nova, shimmer</li>
|
||||
</ul>
|
||||
|
||||
<p><a href="../../index.html">← Back to Main Page</a></p>
|
||||
|
||||
<script>hljs.highlightAll();</script>
|
||||
</body>
|
||||
</html>
|
||||
BIN
languages/bash/speech.mp3
Normal file
BIN
languages/bash/speech.mp3
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue