230 lines
7 KiB
TypeScript
230 lines
7 KiB
TypeScript
// PUBLIC DOMAIN - NO LICENSE, NO WARRANTY
|
|
// Copyright 2025 TimeHexOn & foxhop & russell@unturf
|
|
// https://www.permacomputer.com
|
|
|
|
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);
|