modified: .gitignore
modified: CLAUDE.md modified: Makefile new file: languages/awk/Dockerfile new file: languages/awk/uncloseai.awk modified: languages/bash/Dockerfile deleted: languages/bash/examples.sh new file: languages/bash/uncloseai.sh new file: languages/c/curl/Dockerfile new file: languages/c/curl/Makefile new file: languages/c/curl/index.html new file: languages/c/curl/uncloseai.c new file: languages/c/libh2o/Dockerfile new file: languages/c/libh2o/Makefile new file: languages/c/libh2o/uncloseai.c new file: languages/c/nghttp2/Dockerfile new file: languages/c/nghttp2/Makefile new file: languages/c/nghttp2/uncloseai.c new file: languages/clojure/Dockerfile new file: languages/clojure/deps.edn new file: languages/clojure/uncloseai.clj new file: languages/cobol/Dockerfile new file: languages/cobol/discover.sh new file: languages/cobol/hermes.sh new file: languages/cobol/qwen.sh new file: languages/cobol/tts.sh new file: languages/cobol/uncloseai.cob new file: languages/cpp/boost-beast/Dockerfile new file: languages/cpp/boost-beast/Makefile new file: languages/cpp/boost-beast/uncloseai.cpp new file: languages/cpp/cpp-httplib/Dockerfile new file: languages/cpp/cpp-httplib/Makefile new file: languages/cpp/cpp-httplib/uncloseai.cpp new file: languages/cpp/libcurl/Dockerfile new file: languages/cpp/libcurl/Makefile new file: languages/cpp/libcurl/uncloseai.cpp new file: languages/crystal/Dockerfile new file: languages/crystal/uncloseai.cr new file: languages/csharp/Dockerfile new file: languages/csharp/Uncloseai.cs new file: languages/csharp/csharp.csproj new file: languages/dart/Dockerfile new file: languages/dart/bin/uncloseai.dart new file: languages/dart/pubspec.yaml new file: languages/deno/Dockerfile new file: languages/deno/uncloseai.ts new file: languages/elixir/Dockerfile new file: languages/elixir/lib/uncloseai.ex new file: languages/elixir/mix.exs new file: languages/elixir/run.exs new file: languages/erlang/Dockerfile new file: languages/erlang/rebar.config new file: languages/erlang/src/uncloseai.app.src new file: languages/erlang/src/uncloseai.erl new file: languages/fortran/Dockerfile new file: languages/fortran/uncloseai.f90 new file: languages/fsharp/Dockerfile new file: languages/fsharp/Uncloseai.fs new file: languages/fsharp/fsharp.fsproj new file: languages/go/Dockerfile new file: languages/go/README.md new file: languages/go/examples/basic.go new file: languages/go/go.mod new file: languages/go/uncloseai.go new file: languages/go/uncloseai/uncloseai.go new file: languages/haskell/Dockerfile new file: languages/haskell/UncloseAI.hs new file: languages/haskell/uncloseai.cabal new file: languages/java/Dockerfile new file: languages/java/UncloseAI.java new file: languages/javascript/bun/Dockerfile new file: languages/javascript/bun/uncloseai.ts new file: languages/javascript/nodejs/Dockerfile new file: languages/javascript/nodejs/README.md new file: languages/javascript/nodejs/package.json new file: languages/javascript/nodejs/uncloseai.js new file: languages/javascript/typescript/Dockerfile new file: languages/javascript/typescript/package.json new file: languages/javascript/typescript/tsconfig.json new file: languages/javascript/typescript/uncloseai.ts new file: languages/javascript/vanilla/Dockerfile new file: languages/javascript/vanilla/uncloseai.html new file: languages/julia/Dockerfile new file: languages/julia/Project.toml new file: languages/julia/src/uncloseai.jl new file: languages/kotlin/Dockerfile new file: languages/kotlin/build.gradle.kts new file: languages/kotlin/src/main/kotlin/UncloseAI.kt new file: languages/kotlin/uncloseai.kt new file: languages/lua/Dockerfile new file: languages/lua/uncloseai.lua new file: languages/nim/Dockerfile new file: languages/nim/uncloseai.nim new file: languages/ocaml/Dockerfile new file: languages/ocaml/dune new file: languages/ocaml/dune-project new file: languages/ocaml/uncloseai.ml new file: languages/odin/Dockerfile new file: languages/odin/uncloseai.odin new file: languages/perl/Dockerfile new file: languages/perl/uncloseai.pl new file: languages/php/Dockerfile new file: languages/php/uncloseai.php new file: languages/powershell/Dockerfile new file: languages/powershell/uncloseai.ps1 new file: languages/prolog/Dockerfile new file: languages/prolog/uncloseai.pl new file: languages/python/aiohttp/Dockerfile new file: languages/python/aiohttp/requirements.txt new file: languages/python/aiohttp/uncloseai.py modified: languages/python/httpx-async/Dockerfile deleted: languages/python/httpx-async/examples.py new file: languages/python/httpx-async/uncloseai.py modified: languages/python/openai-client/Dockerfile deleted: languages/python/openai-client/examples.py new file: languages/python/openai-client/uncloseai.py modified: languages/python/requests/Dockerfile deleted: languages/python/requests/examples.py new file: languages/python/requests/uncloseai.py new file: languages/r/Dockerfile new file: languages/r/uncloseai.R new file: languages/ruby/Dockerfile new file: languages/ruby/README.md new file: languages/ruby/uncloseai.rb new file: languages/rust/Cargo.toml new file: languages/rust/Dockerfile new file: languages/rust/README.md new file: languages/rust/examples/basic.rs new file: languages/rust/src/lib.rs new file: languages/rust/src/uncloseai.rs new file: languages/scala/Dockerfile new file: languages/scala/build.sbt new file: languages/scala/project/build.properties new file: languages/scala/project/plugins.sbt new file: languages/scala/src/main/scala/UncloseAI.scala new file: languages/tcl/Dockerfile new file: languages/tcl/uncloseai.tcl new file: languages/v/Dockerfile new file: languages/v/uncloseai.v new file: languages/v/v.mod new file: languages/vbnet/Dockerfile new file: languages/vbnet/UncloseAI.vb new file: languages/vbnet/UncloseAI.vbproj modified: languages/zig/Dockerfile modified: languages/zig/build.zig deleted: languages/zig/src/main.zig new file: languages/zig/src/uncloseai.zig
This commit is contained in:
parent
bf3f8cfac6
commit
9e3eccb1b9
147 changed files with 20225 additions and 566 deletions
8
languages/javascript/bun/Dockerfile
Normal file
8
languages/javascript/bun/Dockerfile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Bun 1.x (checked 2025-10-13: oven/bun:1 tracks latest 1.x)
|
||||
FROM oven/bun:1
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY uncloseai.ts .
|
||||
|
||||
CMD ["bun", "run", "uncloseai.ts"]
|
||||
226
languages/javascript/bun/uncloseai.ts
Normal file
226
languages/javascript/bun/uncloseai.ts
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
console.log('=== Bun AI API Examples (Dynamic Model Discovery) ===\n');
|
||||
|
||||
interface ChatMessage {
|
||||
role: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
interface ChatRequest {
|
||||
model: string;
|
||||
messages: ChatMessage[];
|
||||
max_tokens: number;
|
||||
}
|
||||
|
||||
interface TTSRequest {
|
||||
model: string;
|
||||
voice: string;
|
||||
input: string;
|
||||
}
|
||||
|
||||
interface ModelInfo {
|
||||
id: string;
|
||||
endpoint: string;
|
||||
max_tokens: number;
|
||||
}
|
||||
|
||||
interface ModelsResponse {
|
||||
data: Array<{ id: string; max_model_len?: number }>;
|
||||
}
|
||||
|
||||
async function discoverModels(): Promise<{ models: ModelInfo[]; ttsEndpoints: string[] }> {
|
||||
console.log('Discovering models from environment variables...');
|
||||
|
||||
const models: ModelInfo[] = [];
|
||||
const ttsEndpoints: string[] = [];
|
||||
|
||||
// Discover chat/code models from MODEL_ENDPOINT_1..9999
|
||||
for (let i = 1; i < 10000; i++) {
|
||||
const endpoint = process.env[`MODEL_ENDPOINT_${i}`];
|
||||
if (!endpoint) break;
|
||||
|
||||
console.log(`Discovering from: ${endpoint}`);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${endpoint}/models`, { signal: AbortSignal.timeout(10000) });
|
||||
if (response.ok) {
|
||||
const data: ModelsResponse = await response.json();
|
||||
for (const model of data.data || []) {
|
||||
models.push({
|
||||
id: model.id,
|
||||
endpoint: endpoint,
|
||||
max_tokens: model.max_model_len || 8192
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(` Error: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Discover TTS endpoints from TTS_ENDPOINT_1..9999
|
||||
for (let i = 1; i < 10000; i++) {
|
||||
const endpoint = process.env[`TTS_ENDPOINT_${i}`];
|
||||
if (!endpoint) break;
|
||||
|
||||
console.log(`Discovering TTS from: ${endpoint}`);
|
||||
ttsEndpoints.push(endpoint);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log(`Discovered ${models.length} model(s) and ${ttsEndpoints.length} TTS endpoint(s)`);
|
||||
console.log('');
|
||||
|
||||
return { models, ttsEndpoints };
|
||||
}
|
||||
|
||||
async function chatExample(model: ModelInfo, systemMsg: string, userMsg: string, maxTokens: number = 100): Promise<void> {
|
||||
console.log('\n=== Non-Streaming Chat ===');
|
||||
console.log(`Model: ${model.id}`);
|
||||
console.log(`Endpoint: ${model.endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request: ChatRequest = {
|
||||
model: model.id,
|
||||
messages: [
|
||||
{ role: 'system', content: systemMsg },
|
||||
{ role: 'user', content: userMsg }
|
||||
],
|
||||
max_tokens: maxTokens
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${model.endpoint}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request)
|
||||
});
|
||||
const data = await response.json();
|
||||
console.log('Response:');
|
||||
console.log(data.choices[0].message.content);
|
||||
} catch (error) {
|
||||
console.log('Error:', (error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function chatStreamExample(model: ModelInfo, systemMsg: string, userMsg: string, maxTokens: number = 500): Promise<void> {
|
||||
console.log('\n=== Streaming Chat ===');
|
||||
console.log(`Model: ${model.id}`);
|
||||
console.log(`Endpoint: ${model.endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request = {
|
||||
model: model.id,
|
||||
messages: [
|
||||
{ role: 'system', content: systemMsg },
|
||||
{ role: 'user', content: userMsg }
|
||||
],
|
||||
max_tokens: maxTokens,
|
||||
stream: true
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${model.endpoint}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request)
|
||||
});
|
||||
|
||||
if (!response.body) {
|
||||
throw new Error('No response body');
|
||||
}
|
||||
|
||||
process.stdout.write('Response: ');
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
|
||||
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]') {
|
||||
process.stdout.write('\n');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
if (parsed.choices?.[0]?.delta?.content) {
|
||||
process.stdout.write(parsed.choices[0].delta.content);
|
||||
}
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.stdout.write('\n');
|
||||
} catch (error) {
|
||||
console.log('\nError:', (error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function ttsExample(endpoint: string): Promise<void> {
|
||||
console.log('');
|
||||
console.log('---');
|
||||
console.log('');
|
||||
console.log('=== TTS Speech Generation Example ===');
|
||||
console.log(`Endpoint: ${endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request: TTSRequest = {
|
||||
model: 'tts-1',
|
||||
voice: 'alloy',
|
||||
input: 'Hello from Bun! This is a text to speech example with dynamic model discovery.'
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${endpoint}/audio/speech`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request)
|
||||
});
|
||||
const audioData = await response.arrayBuffer();
|
||||
await Bun.write('speech.mp3', audioData);
|
||||
console.log(`✓ Speech file created: speech.mp3 (${audioData.byteLength} bytes)`);
|
||||
} catch (error) {
|
||||
console.log('✗ Error:', (error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const { models, ttsEndpoints } = await discoverModels();
|
||||
|
||||
if (models.length === 0) {
|
||||
console.log('ERROR: No models discovered. Set environment variables:');
|
||||
console.log(' MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, etc.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await chatExample(models[0], 'You are a helpful AI assistant.', 'Explain quantum computing in one sentence.');
|
||||
|
||||
const modelIdx = models.length > 1 ? 1 : 0;
|
||||
await chatStreamExample(models[modelIdx], 'You are a coding assistant.', 'Write a Bun function to check if a number is prime', 200);
|
||||
|
||||
if (ttsEndpoints.length > 0) {
|
||||
await ttsExample(ttsEndpoints[0]);
|
||||
} else {
|
||||
console.log('\n=== TTS Speech Generation Example ===');
|
||||
console.log('ERROR: No TTS endpoints available. Set TTS_ENDPOINT_1');
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log('=== Examples Complete ===');
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
8
languages/javascript/nodejs/Dockerfile
Normal file
8
languages/javascript/nodejs/Dockerfile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Node.js 23 (checked 2025-10-13: node:23-alpine is latest stable)
|
||||
FROM node:23-alpine
|
||||
|
||||
WORKDIR /app
|
||||
COPY uncloseai.js .
|
||||
COPY package.json .
|
||||
|
||||
CMD ["node", "uncloseai.js"]
|
||||
359
languages/javascript/nodejs/README.md
Normal file
359
languages/javascript/nodejs/README.md
Normal file
|
|
@ -0,0 +1,359 @@
|
|||
# UncloseAI Node.js Client
|
||||
|
||||
A Node.js client library for interacting with vLLM, Ollama, and OpenAI-compatible APIs.
|
||||
|
||||
## Features
|
||||
|
||||
- 🔍 **Automatic Model Discovery** - Discovers available models from configured endpoints
|
||||
- 💬 **Chat Completions** - Both streaming and non-streaming modes
|
||||
- 🎙️ **Text-to-Speech** - Generate audio from text with multiple voice options
|
||||
- 🔄 **Multiple Endpoints** - Support for multiple model and TTS endpoints
|
||||
- 🛡️ **Error Handling** - Comprehensive error handling with custom exceptions
|
||||
- 📦 **Zero Dependencies** - Uses only Node.js built-in modules (https, http, fs)
|
||||
|
||||
## Installation
|
||||
|
||||
No external dependencies required! Just copy `uncloseai_lib.js` to your project:
|
||||
|
||||
```bash
|
||||
# Copy the library file
|
||||
cp uncloseai_lib.js your-project/
|
||||
|
||||
# Or use it directly
|
||||
node examples.js
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
```javascript
|
||||
const { UncloseAI } = require('./uncloseai_lib');
|
||||
|
||||
// Initialize client (auto-discovers from environment variables)
|
||||
const client = new UncloseAI();
|
||||
|
||||
// Non-streaming chat
|
||||
const response = await client.chat({
|
||||
model: 'auto',
|
||||
messages: [{ role: 'user', content: 'Hello!' }]
|
||||
});
|
||||
console.log(response.choices[0].message.content);
|
||||
|
||||
// Streaming chat
|
||||
for await (const chunk of client.chatStream({
|
||||
model: 'auto',
|
||||
messages: [{ role: 'user', content: 'Write a story' }]
|
||||
})) {
|
||||
const content = chunk.choices?.[0]?.delta?.content || '';
|
||||
process.stdout.write(content);
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
### Environment Variables
|
||||
|
||||
```bash
|
||||
# Model endpoints (numbered 1-9999)
|
||||
export MODEL_ENDPOINT_1="https://hermes.ai.unturf.com/v1"
|
||||
export MODEL_ENDPOINT_2="https://qwen.ai.unturf.com/v1"
|
||||
|
||||
# TTS endpoints (numbered 1-9999)
|
||||
export TTS_ENDPOINT_1="https://speech.ai.unturf.com/v1"
|
||||
```
|
||||
|
||||
### Programmatic Configuration
|
||||
|
||||
```javascript
|
||||
const client = new UncloseAI({
|
||||
endpoints: ['https://api.example.com/v1'],
|
||||
ttsEndpoints: ['https://tts.example.com/v1'],
|
||||
apiKey: 'your-api-key', // Optional
|
||||
timeout: 30000, // Request timeout in milliseconds
|
||||
debug: true // Enable debug logging
|
||||
});
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### UncloseAI
|
||||
|
||||
Main client class for interacting with AI APIs.
|
||||
|
||||
#### `constructor(options)`
|
||||
|
||||
Initialize the client.
|
||||
|
||||
**Parameters:**
|
||||
- `endpoints` (Array, optional): Model endpoints (auto-discovers from env if not provided)
|
||||
- `ttsEndpoints` (Array, optional): TTS endpoints (auto-discovers from env if not provided)
|
||||
- `apiKey` (String, optional): API key for authentication
|
||||
- `timeout` (Number): Request timeout in milliseconds (default: 30000)
|
||||
- `debug` (Boolean): Enable debug logging (default: false)
|
||||
|
||||
#### `async listModels()`
|
||||
|
||||
List all discovered models with their metadata.
|
||||
|
||||
**Returns:**
|
||||
- Array of objects with `id`, `endpoint`, and `max_tokens`
|
||||
|
||||
**Example:**
|
||||
```javascript
|
||||
const models = await client.listModels();
|
||||
console.log(models);
|
||||
// [{ id: 'model-name', endpoint: 'https://...', max_tokens: 8192 }, ...]
|
||||
```
|
||||
|
||||
#### `async chat(options)`
|
||||
|
||||
Send a non-streaming chat completion request.
|
||||
|
||||
**Parameters:**
|
||||
- `messages` (Array): Array of message objects with 'role' and 'content'
|
||||
- `model` (String): Model ID or 'auto' for first available (default: 'auto')
|
||||
- `maxTokens` (Number, optional): Maximum tokens to generate
|
||||
- `temperature` (Number): Sampling temperature 0-2 (default: 0.7)
|
||||
- `topP` (Number): Nucleus sampling parameter (default: 1.0)
|
||||
- Additional parameters passed to API
|
||||
|
||||
**Returns:**
|
||||
- Chat completion response object
|
||||
|
||||
**Throws:**
|
||||
- `ModelNotFoundError`: If model not found
|
||||
- `ConnectionError`: If request fails
|
||||
|
||||
**Example:**
|
||||
```javascript
|
||||
const response = await client.chat({
|
||||
model: 'auto',
|
||||
messages: [
|
||||
{ role: 'system', content: 'You are a helpful assistant.' },
|
||||
{ role: 'user', content: 'What is AI?' }
|
||||
],
|
||||
maxTokens: 100
|
||||
});
|
||||
console.log(response.choices[0].message.content);
|
||||
```
|
||||
|
||||
#### `async *chatStream(options)`
|
||||
|
||||
Send a streaming chat completion request.
|
||||
|
||||
**Parameters:**
|
||||
- Same as `chat()`
|
||||
|
||||
**Yields:**
|
||||
- Chat completion chunk objects
|
||||
|
||||
**Throws:**
|
||||
- `ModelNotFoundError`: If model not found
|
||||
- `StreamingError`: If streaming fails
|
||||
|
||||
**Example:**
|
||||
```javascript
|
||||
for await (const chunk of client.chatStream({
|
||||
model: 'auto',
|
||||
messages: [{ role: 'user', content: 'Write a haiku' }]
|
||||
})) {
|
||||
const content = chunk.choices?.[0]?.delta?.content || '';
|
||||
if (content) {
|
||||
process.stdout.write(content);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### `async tts(options)`
|
||||
|
||||
Generate speech from text.
|
||||
|
||||
**Parameters:**
|
||||
- `text` (String): Text to convert to speech
|
||||
- `voice` (String): Voice to use - alloy, echo, fable, onyx, nova, shimmer (default: 'alloy')
|
||||
- `model` (String): TTS model - tts-1 or tts-1-hd (default: 'tts-1')
|
||||
- Additional parameters passed to API
|
||||
|
||||
**Returns:**
|
||||
- Buffer containing audio data (MP3 format)
|
||||
|
||||
**Throws:**
|
||||
- `ConnectionError`: If request fails
|
||||
- `UncloseAIError`: If no TTS endpoints available
|
||||
|
||||
**Example:**
|
||||
```javascript
|
||||
const audioData = await client.tts({
|
||||
text: 'Hello from UncloseAI!',
|
||||
voice: 'alloy',
|
||||
model: 'tts-1'
|
||||
});
|
||||
|
||||
fs.writeFileSync('output.mp3', audioData);
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Chat
|
||||
|
||||
```javascript
|
||||
const { UncloseAI } = require('./uncloseai_lib');
|
||||
|
||||
const client = new UncloseAI();
|
||||
|
||||
const response = await client.chat({
|
||||
model: 'auto',
|
||||
messages: [
|
||||
{ role: 'system', content: 'You are a helpful assistant.' },
|
||||
{ role: 'user', content: 'What is JavaScript?' }
|
||||
],
|
||||
maxTokens: 100
|
||||
});
|
||||
|
||||
console.log(response.choices[0].message.content);
|
||||
```
|
||||
|
||||
### Streaming Chat
|
||||
|
||||
```javascript
|
||||
for await (const chunk of client.chatStream({
|
||||
model: 'auto',
|
||||
messages: [{ role: 'user', content: 'Write a haiku about code' }],
|
||||
maxTokens: 100
|
||||
})) {
|
||||
const content = chunk.choices?.[0]?.delta?.content || '';
|
||||
if (content) {
|
||||
process.stdout.write(content);
|
||||
}
|
||||
}
|
||||
console.log(); // newline
|
||||
```
|
||||
|
||||
### Multi-Turn Conversation
|
||||
|
||||
```javascript
|
||||
const messages = [
|
||||
{ role: 'system', content: 'You are a helpful assistant.' },
|
||||
{ role: 'user', content: 'What is AI?' }
|
||||
];
|
||||
|
||||
// First response
|
||||
const response1 = await client.chat({ model: 'auto', messages });
|
||||
const assistantMsg = response1.choices[0].message.content;
|
||||
messages.push({ role: 'assistant', content: assistantMsg });
|
||||
|
||||
// Follow-up question
|
||||
messages.push({ role: 'user', content: 'Can you explain more?' });
|
||||
const response2 = await client.chat({ model: 'auto', messages });
|
||||
```
|
||||
|
||||
### Text-to-Speech
|
||||
|
||||
```javascript
|
||||
const fs = require('fs');
|
||||
|
||||
const audioData = await client.tts({
|
||||
text: 'Hello from UncloseAI!',
|
||||
voice: 'alloy',
|
||||
model: 'tts-1'
|
||||
});
|
||||
|
||||
fs.writeFileSync('output.mp3', audioData);
|
||||
```
|
||||
|
||||
### Using Specific Models
|
||||
|
||||
```javascript
|
||||
// List available models
|
||||
const models = await client.listModels();
|
||||
for (const model of models) {
|
||||
console.log(`${model.id} - Max tokens: ${model.max_tokens}`);
|
||||
}
|
||||
|
||||
// Use specific model
|
||||
const response = await client.chat({
|
||||
model: models[0].id,
|
||||
messages: [{ role: 'user', content: 'Hello' }]
|
||||
});
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
```javascript
|
||||
const { UncloseAI, UncloseAIError, ModelNotFoundError } = require('./uncloseai_lib');
|
||||
|
||||
const client = new UncloseAI();
|
||||
|
||||
try {
|
||||
const response = await client.chat({
|
||||
model: 'non-existent-model',
|
||||
messages: [{ role: 'user', content: 'Hello' }]
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof ModelNotFoundError) {
|
||||
console.log(`Model error: ${error.message}`);
|
||||
} else if (error instanceof UncloseAIError) {
|
||||
console.log(`API error: ${error.message}`);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Running Examples
|
||||
|
||||
```bash
|
||||
# Set environment variables
|
||||
export MODEL_ENDPOINT_1="https://hermes.ai.unturf.com/v1"
|
||||
export MODEL_ENDPOINT_2="https://qwen.ai.unturf.com/v1"
|
||||
export TTS_ENDPOINT_1="https://speech.ai.unturf.com/v1"
|
||||
|
||||
# Run example script
|
||||
node examples.js
|
||||
```
|
||||
|
||||
## Docker Usage
|
||||
|
||||
```bash
|
||||
# Build
|
||||
docker build -t uncloseai-nodejs .
|
||||
|
||||
# Run examples
|
||||
docker run -e MODEL_ENDPOINT_1="https://..." uncloseai-nodejs node examples.js
|
||||
```
|
||||
|
||||
## Compatibility
|
||||
|
||||
Tested with:
|
||||
- ✅ vLLM (v0.5.0+)
|
||||
- ✅ Ollama (v0.1.0+)
|
||||
- ✅ OpenAI API (compatible endpoints)
|
||||
|
||||
## Error Types
|
||||
|
||||
- `UncloseAIError` - Base error class for all library errors
|
||||
- `ConnectionError` - Network connection errors
|
||||
- `ModelNotFoundError` - Requested model not available
|
||||
- `StreamingError` - Errors during streaming requests
|
||||
|
||||
## License
|
||||
|
||||
MIT License - See LICENSE file for details
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions welcome! Please submit pull requests or open issues.
|
||||
|
||||
## Support
|
||||
|
||||
For issues, questions, or contributions, please visit:
|
||||
https://github.com/yourusername/uncloseai
|
||||
|
||||
## Changelog
|
||||
|
||||
### v1.0.0 (2025-10-13)
|
||||
- Initial release
|
||||
- Streaming and non-streaming chat support
|
||||
- Text-to-speech generation
|
||||
- Automatic model discovery
|
||||
- Zero external dependencies
|
||||
- Comprehensive error handling
|
||||
27
languages/javascript/nodejs/package.json
Normal file
27
languages/javascript/nodejs/package.json
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"name": "uncloseai-nodejs",
|
||||
"version": "1.0.0",
|
||||
"description": "Node.js client library for interacting with vLLM, Ollama, and OpenAI-compatible APIs",
|
||||
"main": "uncloseai.js",
|
||||
"scripts": {
|
||||
"start": "node uncloseai.js"
|
||||
},
|
||||
"keywords": [
|
||||
"ai",
|
||||
"openai",
|
||||
"vllm",
|
||||
"ollama",
|
||||
"llm",
|
||||
"chat",
|
||||
"tts",
|
||||
"text-to-speech",
|
||||
"streaming"
|
||||
],
|
||||
"author": "UncloseAI",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {}
|
||||
}
|
||||
314
languages/javascript/nodejs/uncloseai.js
Normal file
314
languages/javascript/nodejs/uncloseai.js
Normal file
|
|
@ -0,0 +1,314 @@
|
|||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
|
||||
console.log('=== Node.js AI API Examples (Dynamic Model Discovery) ===\n');
|
||||
|
||||
function getJSON(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const urlObj = new URL(url);
|
||||
const options = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'GET',
|
||||
timeout: 10000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
let body = '';
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode === 200) {
|
||||
resolve(JSON.parse(body));
|
||||
} else {
|
||||
reject(new Error(`HTTP ${res.statusCode}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function postJSON(url, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonData = JSON.stringify(data);
|
||||
const urlObj = new URL(url);
|
||||
|
||||
const options = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': jsonData.length
|
||||
},
|
||||
timeout: 30000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
let body = '';
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.headers['content-type']?.includes('application/json')) {
|
||||
resolve(JSON.parse(body));
|
||||
} else {
|
||||
resolve(body);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.write(jsonData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function postJSONBinary(url, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonData = JSON.stringify(data);
|
||||
const urlObj = new URL(url);
|
||||
|
||||
const options = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': jsonData.length
|
||||
},
|
||||
timeout: 30000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
const chunks = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => resolve(Buffer.concat(chunks)));
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.write(jsonData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function discoverModels() {
|
||||
console.log('Discovering models from environment variables...');
|
||||
|
||||
const models = [];
|
||||
const ttsEndpoints = [];
|
||||
|
||||
// Discover chat/code models from MODEL_ENDPOINT_1..9999
|
||||
for (let i = 1; i < 10000; i++) {
|
||||
const endpoint = process.env[`MODEL_ENDPOINT_${i}`];
|
||||
if (!endpoint) break;
|
||||
|
||||
console.log(`Discovering from: ${endpoint}`);
|
||||
|
||||
try {
|
||||
const response = await getJSON(`${endpoint}/models`);
|
||||
for (const model of response.data || []) {
|
||||
// Filter out modelperm-* and chatcmpl-* entries
|
||||
if (model.id.startsWith('modelperm-') || model.id.startsWith('chatcmpl-')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
models.push({
|
||||
id: model.id,
|
||||
endpoint: endpoint,
|
||||
max_tokens: model.max_model_len || 8192
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(` Error: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Discover TTS endpoints from TTS_ENDPOINT_1..9999
|
||||
for (let i = 1; i < 10000; i++) {
|
||||
const endpoint = process.env[`TTS_ENDPOINT_${i}`];
|
||||
if (!endpoint) break;
|
||||
|
||||
console.log(`Discovering TTS from: ${endpoint}`);
|
||||
ttsEndpoints.push(endpoint);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log(`Discovered ${models.length} model(s) and ${ttsEndpoints.length} TTS endpoint(s)`);
|
||||
console.log('');
|
||||
|
||||
return { models, ttsEndpoints };
|
||||
}
|
||||
|
||||
async function chatExample(model, systemMsg, userMsg, maxTokens = 100) {
|
||||
console.log('\n=== Non-Streaming Chat ===');
|
||||
console.log(`Model: ${model.id}`);
|
||||
console.log(`Endpoint: ${model.endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request = {
|
||||
model: model.id,
|
||||
messages: [
|
||||
{ role: 'system', content: systemMsg },
|
||||
{ role: 'user', content: userMsg }
|
||||
],
|
||||
max_tokens: maxTokens
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await postJSON(`${model.endpoint}/chat/completions`, request);
|
||||
console.log('Response:');
|
||||
console.log(response.choices[0].message.content);
|
||||
} catch (error) {
|
||||
console.log('Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function chatStreamExample(model, systemMsg, userMsg, maxTokens = 500) {
|
||||
console.log('\n=== Streaming Chat ===');
|
||||
console.log(`Model: ${model.id}`);
|
||||
console.log(`Endpoint: ${model.endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request = {
|
||||
model: model.id,
|
||||
messages: [
|
||||
{ role: 'system', content: systemMsg },
|
||||
{ role: 'user', content: userMsg }
|
||||
],
|
||||
max_tokens: maxTokens,
|
||||
stream: true
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonData = JSON.stringify(request);
|
||||
const urlObj = new URL(`${model.endpoint}/chat/completions`);
|
||||
|
||||
const options = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': jsonData.length
|
||||
},
|
||||
timeout: 60000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
process.stdout.write('Response: ');
|
||||
|
||||
let buffer = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
buffer += chunk.toString();
|
||||
|
||||
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]') {
|
||||
process.stdout.write('\n');
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
if (parsed.choices?.[0]?.delta?.content) {
|
||||
process.stdout.write(parsed.choices[0].delta.content);
|
||||
}
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
process.stdout.write('\n');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.write(jsonData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function ttsExample(endpoint) {
|
||||
console.log('');
|
||||
console.log('---');
|
||||
console.log('');
|
||||
console.log('=== TTS Speech Generation Example ===');
|
||||
console.log(`Endpoint: ${endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request = {
|
||||
model: 'tts-1',
|
||||
voice: 'alloy',
|
||||
input: 'Hello from Node.js! This is a text to speech example with dynamic model discovery and streaming support.'
|
||||
};
|
||||
|
||||
try {
|
||||
const audioData = await postJSONBinary(`${endpoint}/audio/speech`, request);
|
||||
fs.writeFileSync('speech.mp3', audioData);
|
||||
console.log(`✓ Speech file created: speech.mp3 (${audioData.length} bytes)`);
|
||||
} catch (error) {
|
||||
console.log('✗ Error:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const { models, ttsEndpoints } = await discoverModels();
|
||||
|
||||
if (models.length === 0) {
|
||||
console.log('ERROR: No models discovered. Set environment variables:');
|
||||
console.log(' MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, etc.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await chatExample(models[0], 'You are a helpful AI assistant.', 'Explain quantum computing in one sentence.');
|
||||
|
||||
const modelIdx = models.length > 1 ? 1 : 0;
|
||||
await chatStreamExample(models[modelIdx], 'You are a coding assistant.', 'Write a JavaScript function to check if a number is prime', 200);
|
||||
|
||||
if (ttsEndpoints.length > 0) {
|
||||
await ttsExample(ttsEndpoints[0]);
|
||||
} else {
|
||||
console.log('\n=== TTS Speech Generation Example ===');
|
||||
console.log('ERROR: No TTS endpoints available. Set TTS_ENDPOINT_1');
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log('=== Examples Complete ===');
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
10
languages/javascript/typescript/Dockerfile
Normal file
10
languages/javascript/typescript/Dockerfile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# TypeScript 5.9.3 (checked 2025-10-13: typescript@5.9.3 is latest stable)
|
||||
FROM node:23-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json tsconfig.json uncloseai.ts ./
|
||||
|
||||
RUN npm install && npm run build
|
||||
|
||||
CMD ["node", "uncloseai.js"]
|
||||
16
languages/javascript/typescript/package.json
Normal file
16
languages/javascript/typescript/package.json
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "uncloseai-typescript-examples",
|
||||
"version": "1.0.0",
|
||||
"description": "TypeScript examples for uncloseai.com API",
|
||||
"main": "examples.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node examples.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/node": "^22.10.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "5.9.3"
|
||||
}
|
||||
}
|
||||
17
languages/javascript/typescript/tsconfig.json
Normal file
17
languages/javascript/typescript/tsconfig.json
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "commonjs",
|
||||
"lib": ["ES2022"],
|
||||
"outDir": ".",
|
||||
"rootDir": ".",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": ["*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
336
languages/javascript/typescript/uncloseai.ts
Normal file
336
languages/javascript/typescript/uncloseai.ts
Normal file
|
|
@ -0,0 +1,336 @@
|
|||
import * as https from 'https';
|
||||
import * as fs from 'fs';
|
||||
|
||||
console.log('=== TypeScript AI API Examples (Dynamic Model Discovery) ===\n');
|
||||
|
||||
interface ChatMessage {
|
||||
role: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
interface ChatRequest {
|
||||
model: string;
|
||||
messages: ChatMessage[];
|
||||
max_tokens: number;
|
||||
}
|
||||
|
||||
interface TTSRequest {
|
||||
model: string;
|
||||
voice: string;
|
||||
input: string;
|
||||
}
|
||||
|
||||
interface ModelInfo {
|
||||
id: string;
|
||||
endpoint: string;
|
||||
max_tokens: number;
|
||||
}
|
||||
|
||||
interface ModelsResponse {
|
||||
data: Array<{ id: string; max_model_len?: number }>;
|
||||
}
|
||||
|
||||
function getJSON<T>(url: string): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const urlObj = new URL(url);
|
||||
const options: https.RequestOptions = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'GET',
|
||||
timeout: 10000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
let body = '';
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode === 200) {
|
||||
resolve(JSON.parse(body));
|
||||
} else {
|
||||
reject(new Error(`HTTP ${res.statusCode}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function postJSON<T>(url: string, data: any): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonData = JSON.stringify(data);
|
||||
const urlObj = new URL(url);
|
||||
|
||||
const options: https.RequestOptions = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': jsonData.length
|
||||
},
|
||||
timeout: 30000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
let body = '';
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.headers['content-type']?.includes('application/json')) {
|
||||
resolve(JSON.parse(body));
|
||||
} else {
|
||||
resolve(body as any);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.write(jsonData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function postJSONBinary(url: string, data: any): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonData = JSON.stringify(data);
|
||||
const urlObj = new URL(url);
|
||||
|
||||
const options: https.RequestOptions = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': jsonData.length
|
||||
},
|
||||
timeout: 30000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => resolve(Buffer.concat(chunks)));
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.write(jsonData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function discoverModels(): Promise<{ models: ModelInfo[]; ttsEndpoints: string[] }> {
|
||||
console.log('Discovering models from environment variables...');
|
||||
|
||||
const models: ModelInfo[] = [];
|
||||
const ttsEndpoints: string[] = [];
|
||||
|
||||
// Discover chat/code models from MODEL_ENDPOINT_1..9999
|
||||
for (let i = 1; i < 10000; i++) {
|
||||
const endpoint = process.env[`MODEL_ENDPOINT_${i}`];
|
||||
if (!endpoint) break;
|
||||
|
||||
console.log(`Discovering from: ${endpoint}`);
|
||||
|
||||
try {
|
||||
const response = await getJSON<ModelsResponse>(`${endpoint}/models`);
|
||||
for (const model of response.data || []) {
|
||||
models.push({
|
||||
id: model.id,
|
||||
endpoint: endpoint,
|
||||
max_tokens: model.max_model_len || 8192
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(` Error: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Discover TTS endpoints from TTS_ENDPOINT_1..9999
|
||||
for (let i = 1; i < 10000; i++) {
|
||||
const endpoint = process.env[`TTS_ENDPOINT_${i}`];
|
||||
if (!endpoint) break;
|
||||
|
||||
console.log(`Discovering TTS from: ${endpoint}`);
|
||||
ttsEndpoints.push(endpoint);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log(`Discovered ${models.length} model(s) and ${ttsEndpoints.length} TTS endpoint(s)`);
|
||||
console.log('');
|
||||
|
||||
return { models, ttsEndpoints };
|
||||
}
|
||||
|
||||
async function chatExample(model: ModelInfo, systemMsg: string, userMsg: string, maxTokens: number = 100): Promise<void> {
|
||||
console.log('\n=== Non-Streaming Chat ===');
|
||||
console.log(`Model: ${model.id}`);
|
||||
console.log(`Endpoint: ${model.endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request: ChatRequest = {
|
||||
model: model.id,
|
||||
messages: [
|
||||
{ role: 'system', content: systemMsg },
|
||||
{ role: 'user', content: userMsg }
|
||||
],
|
||||
max_tokens: maxTokens
|
||||
};
|
||||
|
||||
try {
|
||||
const response: any = await postJSON(`${model.endpoint}/chat/completions`, request);
|
||||
console.log('Response:');
|
||||
console.log(response.choices[0].message.content);
|
||||
} catch (error) {
|
||||
console.log('Error:', (error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function chatStreamExample(model: ModelInfo, systemMsg: string, userMsg: string, maxTokens: number = 500): Promise<void> {
|
||||
console.log('\n=== Streaming Chat ===');
|
||||
console.log(`Model: ${model.id}`);
|
||||
console.log(`Endpoint: ${model.endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request = {
|
||||
model: model.id,
|
||||
messages: [
|
||||
{ role: 'system', content: systemMsg },
|
||||
{ role: 'user', content: userMsg }
|
||||
],
|
||||
max_tokens: maxTokens,
|
||||
stream: true
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonData = JSON.stringify(request);
|
||||
const urlObj = new URL(`${model.endpoint}/chat/completions`);
|
||||
|
||||
const options: https.RequestOptions = {
|
||||
hostname: urlObj.hostname,
|
||||
port: 443,
|
||||
path: urlObj.pathname,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': jsonData.length
|
||||
},
|
||||
timeout: 60000
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
process.stdout.write('Response: ');
|
||||
|
||||
let buffer = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
buffer += chunk.toString();
|
||||
|
||||
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]') {
|
||||
process.stdout.write('\n');
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
if (parsed.choices?.[0]?.delta?.content) {
|
||||
process.stdout.write(parsed.choices[0].delta.content);
|
||||
}
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
process.stdout.write('\n');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
reject(new Error('Request timeout'));
|
||||
});
|
||||
|
||||
req.write(jsonData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function ttsExample(endpoint: string): Promise<void> {
|
||||
console.log('');
|
||||
console.log('---');
|
||||
console.log('');
|
||||
console.log('=== TTS Speech Generation Example ===');
|
||||
console.log(`Endpoint: ${endpoint}`);
|
||||
console.log('');
|
||||
|
||||
const request: TTSRequest = {
|
||||
model: 'tts-1',
|
||||
voice: 'alloy',
|
||||
input: 'Hello from TypeScript! This is a text to speech example with dynamic model discovery.'
|
||||
};
|
||||
|
||||
try {
|
||||
const audioData = await postJSONBinary(`${endpoint}/audio/speech`, request);
|
||||
fs.writeFileSync('speech.mp3', audioData);
|
||||
console.log(`✓ Speech file created: speech.mp3 (${audioData.length} bytes)`);
|
||||
} catch (error) {
|
||||
console.log('✗ Error:', (error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const { models, ttsEndpoints } = await discoverModels();
|
||||
|
||||
if (models.length === 0) {
|
||||
console.log('ERROR: No models discovered. Set environment variables:');
|
||||
console.log(' MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, etc.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await chatExample(models[0], 'You are a helpful AI assistant.', 'Explain quantum computing in one sentence.');
|
||||
|
||||
const modelIdx = models.length > 1 ? 1 : 0;
|
||||
await chatStreamExample(models[modelIdx], 'You are a coding assistant.', 'Write a TypeScript function to check if a number is prime', 200);
|
||||
|
||||
if (ttsEndpoints.length > 0) {
|
||||
await ttsExample(ttsEndpoints[0]);
|
||||
} else {
|
||||
console.log('\n=== TTS Speech Generation Example ===');
|
||||
console.log('ERROR: No TTS endpoints available. Set TTS_ENDPOINT_1');
|
||||
}
|
||||
|
||||
console.log('');
|
||||
console.log('=== Examples Complete ===');
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
10
languages/javascript/vanilla/Dockerfile
Normal file
10
languages/javascript/vanilla/Dockerfile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Vanilla JavaScript (browser-based) - served with Python's simple HTTP server
|
||||
FROM python:3.13-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY uncloseai.html index.html
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["python3", "-m", "http.server", "8000"]
|
||||
248
languages/javascript/vanilla/uncloseai.html
Normal file
248
languages/javascript/vanilla/uncloseai.html
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
<!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">✓ Model: ${model.id}</span>\n<span class="success">✓ Endpoint: ${model.endpoint}</span>\n\n${data.choices[0].message.content}`;
|
||||
} catch (error) {
|
||||
output.innerHTML = `<span class="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">✓ Model: ${model.id}</span>\n<span class="success">✓ 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.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">✓ Endpoint: ${endpoint}</span>\n<span class="success">✓ 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.message}</span>`;
|
||||
}
|
||||
}
|
||||
|
||||
// Discover models on page load
|
||||
discoverModels();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue