266 lines
11 KiB
HTML
266 lines
11 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">
|
|
<title>uncloseai.com - Vanilla JavaScript Examples</title>
|
|
<style>
|
|
body {
|
|
font-family: system-ui, -apple-system, sans-serif;
|
|
max-width: 900px;
|
|
margin: 40px auto;
|
|
padding: 20px;
|
|
background: #1a1a1a;
|
|
color: #e0e0e0;
|
|
}
|
|
h1 { color: #4fc3f7; }
|
|
h2 { color: #81c784; margin-top: 30px; }
|
|
button {
|
|
background: #4fc3f7;
|
|
color: black;
|
|
border: none;
|
|
padding: 10px 20px;
|
|
margin: 10px 5px;
|
|
cursor: pointer;
|
|
border-radius: 4px;
|
|
font-weight: bold;
|
|
}
|
|
button:hover { background: #29b6f6; }
|
|
.output {
|
|
background: #2a2a2a;
|
|
border: 1px solid #444;
|
|
border-radius: 4px;
|
|
padding: 15px;
|
|
margin: 10px 0;
|
|
white-space: pre-wrap;
|
|
font-family: 'Courier New', monospace;
|
|
font-size: 14px;
|
|
}
|
|
.loading { color: #ffa726; }
|
|
.success { color: #81c784; }
|
|
.error { color: #e57373; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>uncloseai.com - Vanilla JavaScript Examples</h1>
|
|
<p>These examples use the browser's native fetch API - no dependencies needed!</p>
|
|
|
|
<h2>Hermes AI Chat</h2>
|
|
<button onclick="runHermesExample()">Run Hermes Example</button>
|
|
<div id="hermes-output" class="output">Click the button to run the example...</div>
|
|
|
|
<h2>Qwen 3 Coder (Streaming)</h2>
|
|
<button onclick="runQwenStreamingExample()">Run Qwen Streaming Example</button>
|
|
<div id="qwen-output" class="output">Click the button to run the example...</div>
|
|
|
|
<h2>Text-to-Speech</h2>
|
|
<button onclick="runTTSExample()">Run TTS Example</button>
|
|
<div id="tts-output" class="output">Click the button to run the example...</div>
|
|
|
|
<script>
|
|
// NOTE: Browser-based JavaScript cannot access environment variables
|
|
// Configuration must be done via global variables or query parameters
|
|
const CONFIG = {
|
|
// Override these in production by setting window.MODEL_ENDPOINTS and window.TTS_ENDPOINTS
|
|
MODEL_ENDPOINTS: window.MODEL_ENDPOINTS || [
|
|
'https://hermes.ai.unturf.com/v1',
|
|
'https://qwen.ai.unturf.com/v1'
|
|
],
|
|
TTS_ENDPOINTS: window.TTS_ENDPOINTS || [
|
|
'https://speech.ai.unturf.com/v1'
|
|
]
|
|
};
|
|
|
|
let discoveredModels = [];
|
|
|
|
async function discoverModels() {
|
|
console.log('Discovering models from configured endpoints...');
|
|
discoveredModels = [];
|
|
|
|
for (const endpoint of CONFIG.MODEL_ENDPOINTS) {
|
|
try {
|
|
const response = await fetch(`${endpoint}/models`);
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
for (const model of data.data || []) {
|
|
discoveredModels.push({
|
|
id: model.id,
|
|
endpoint: endpoint.replace('/v1', ''),
|
|
max_tokens: model.max_model_len || 8192
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`Failed to discover from ${endpoint}:`, error);
|
|
}
|
|
}
|
|
|
|
console.log(`Discovered ${discoveredModels.length} models`);
|
|
return discoveredModels;
|
|
}
|
|
|
|
async function runHermesExample() {
|
|
const output = document.getElementById('hermes-output');
|
|
output.innerHTML = '<span class="loading"> Discovering and calling AI model...</span>';
|
|
|
|
try {
|
|
if (discoveredModels.length === 0) {
|
|
await discoverModels();
|
|
}
|
|
|
|
if (discoveredModels.length === 0) {
|
|
throw new Error('No models discovered. Check CONFIG.MODEL_ENDPOINTS');
|
|
}
|
|
|
|
const model = discoveredModels[0];
|
|
const response = await fetch(`${model.endpoint}/chat/completions`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
model: model.id,
|
|
messages: [
|
|
{ role: 'system', content: 'You are a helpful AI assistant.' },
|
|
{ role: 'user', content: 'Explain quantum computing in one sentence.' }
|
|
],
|
|
max_tokens: 100
|
|
})
|
|
});
|
|
|
|
const data = await response.json();
|
|
output.innerHTML = `<span class="success">[OK] Model: ${model.id}</span>\n<span class="success">[OK] Endpoint: ${model.endpoint}</span>\n\n${data.choices[0].message.content}`;
|
|
} catch (error) {
|
|
output.innerHTML = `<span class="error">[ERROR] Error: ${error.message}</span>`;
|
|
}
|
|
}
|
|
|
|
async function runQwenStreamingExample() {
|
|
const output = document.getElementById('qwen-output');
|
|
output.innerHTML = '<span class="loading"> Discovering and calling coding model...</span>';
|
|
|
|
try {
|
|
if (discoveredModels.length === 0) {
|
|
await discoverModels();
|
|
}
|
|
|
|
if (discoveredModels.length === 0) {
|
|
throw new Error('No models discovered. Check CONFIG.MODEL_ENDPOINTS');
|
|
}
|
|
|
|
const model = discoveredModels.length > 1 ? discoveredModels[1] : discoveredModels[0];
|
|
const response = await fetch(`${model.endpoint}/chat/completions`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
model: model.id,
|
|
messages: [
|
|
{ role: 'system', content: 'You are a coding assistant.' },
|
|
{ role: 'user', content: 'Write a JavaScript function to check if a number is prime' }
|
|
],
|
|
max_tokens: 200,
|
|
stream: true
|
|
})
|
|
});
|
|
|
|
if (!response.body) {
|
|
throw new Error('No response body for streaming');
|
|
}
|
|
|
|
// Initialize output with model info
|
|
output.innerHTML = `<span class="success">[OK] Model: ${model.id}</span>\n<span class="success">[OK] Endpoint: ${model.endpoint}</span>\n\nResponse: `;
|
|
|
|
const reader = response.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let buffer = '';
|
|
let contentStarted = false;
|
|
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
const lines = buffer.split('\n');
|
|
buffer = lines.pop() || '';
|
|
|
|
for (const line of lines) {
|
|
if (line.startsWith('data: ')) {
|
|
const data = line.slice(6).trim();
|
|
if (data === '[DONE]') {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(data);
|
|
if (parsed.choices?.[0]?.delta?.content) {
|
|
const content = parsed.choices[0].delta.content;
|
|
// Append content to the output
|
|
const currentHTML = output.innerHTML;
|
|
output.innerHTML = currentHTML + content;
|
|
}
|
|
} catch {
|
|
// Ignore parse errors
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
output.innerHTML = `<span class="error">[ERROR] Error: ${error.message}</span>`;
|
|
}
|
|
}
|
|
|
|
async function runTTSExample() {
|
|
const output = document.getElementById('tts-output');
|
|
output.innerHTML = '<span class="loading"> Generating speech...</span>';
|
|
|
|
try {
|
|
if (CONFIG.TTS_ENDPOINTS.length === 0) {
|
|
throw new Error('No TTS endpoints configured. Set CONFIG.TTS_ENDPOINTS');
|
|
}
|
|
|
|
const endpoint = CONFIG.TTS_ENDPOINTS[0];
|
|
const response = await fetch(`${endpoint}/audio/speech`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
model: 'tts-1',
|
|
voice: 'alloy',
|
|
input: 'Hello from vanilla JavaScript! This is a text to speech example with dynamic model discovery running in your browser.'
|
|
})
|
|
});
|
|
|
|
const audioBlob = await response.blob();
|
|
const audioUrl = URL.createObjectURL(audioBlob);
|
|
|
|
const audio = new Audio(audioUrl);
|
|
output.innerHTML = `<span class="success">[OK] Endpoint: ${endpoint}</span>\n<span class="success">[OK] Speech generated successfully!</span>\n\n<audio controls src="${audioUrl}">Your browser does not support audio playback.</audio>\n\nAudio size: ${audioBlob.size} bytes`;
|
|
|
|
// Auto-play the audio
|
|
audio.play().catch(e => console.log('Autoplay blocked:', e));
|
|
} catch (error) {
|
|
output.innerHTML = `<span class="error">[ERROR] Error: ${error.message}</span>`;
|
|
}
|
|
}
|
|
|
|
// Discover models on page load
|
|
discoverModels();
|
|
</script>
|
|
</body>
|
|
</html>
|