Reorganize project structure: move public files to public/ directory
This commit is contained in:
parent
8de043bfef
commit
38545721a0
253 changed files with 0 additions and 0 deletions
117
public/languages/javascript/openai/nodejs/uncloseai.js
Normal file
117
public/languages/javascript/openai/nodejs/uncloseai.js
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import OpenAI from 'openai';
|
||||
import fs from 'fs';
|
||||
|
||||
console.log('=== uncloseai. Node.js Client (Official OpenAI SDK) ===\n');
|
||||
|
||||
// Discover endpoints from environment variables
|
||||
const modelEndpoint1 = process.env.MODEL_ENDPOINT_1;
|
||||
const modelEndpoint2 = process.env.MODEL_ENDPOINT_2;
|
||||
const ttsEndpoint1 = process.env.TTS_ENDPOINT_1;
|
||||
|
||||
if (!modelEndpoint1 || !modelEndpoint2 || !ttsEndpoint1) {
|
||||
console.error('ERROR: No models discovered. Set environment variables:');
|
||||
console.error(' MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, TTS_ENDPOINT_1');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Discover models from endpoint 1
|
||||
console.log(`Discovering models from ${modelEndpoint1}...`);
|
||||
const client1 = new OpenAI({
|
||||
apiKey: 'dummy-key',
|
||||
baseURL: modelEndpoint1
|
||||
});
|
||||
const models1 = await client1.models.list();
|
||||
const model1Id = models1.data[0].id;
|
||||
console.log(`Model 1: ${model1Id}\n`);
|
||||
|
||||
// Discover models from endpoint 2
|
||||
console.log(`Discovering models from ${modelEndpoint2}...`);
|
||||
const client2 = new OpenAI({
|
||||
apiKey: 'dummy-key',
|
||||
baseURL: modelEndpoint2
|
||||
});
|
||||
const models2 = await client2.models.list();
|
||||
const model2Id = models2.data[0].id;
|
||||
console.log(`Model 2: ${model2Id}\n`);
|
||||
|
||||
// Non-streaming chat with Model 1
|
||||
console.log('=== Non-Streaming Chat (Model 1) ===');
|
||||
const response1 = await client1.chat.completions.create({
|
||||
model: model1Id,
|
||||
messages: [
|
||||
{ role: 'user', content: 'Give a Python Fizzbuzz solution in one line of code?' }
|
||||
],
|
||||
temperature: 0.5,
|
||||
max_tokens: 150
|
||||
});
|
||||
|
||||
console.log(`Response: ${response1.choices[0].message.content}\n`);
|
||||
|
||||
// Streaming chat with Model 1
|
||||
console.log('=== Streaming Chat (Model 1) ===');
|
||||
const stream1 = await client1.chat.completions.create({
|
||||
model: model1Id,
|
||||
messages: [
|
||||
{ role: 'user', content: 'Explain quantum entanglement in one sentence.' }
|
||||
],
|
||||
temperature: 0.5,
|
||||
max_tokens: 150,
|
||||
stream: true
|
||||
});
|
||||
|
||||
process.stdout.write('Response: ');
|
||||
for await (const chunk of stream1) {
|
||||
process.stdout.write(chunk.choices[0]?.delta?.content || '');
|
||||
}
|
||||
console.log('\n');
|
||||
|
||||
// Non-streaming chat with Model 2
|
||||
console.log('=== Non-Streaming Chat (Model 2) ===');
|
||||
const response2 = await client2.chat.completions.create({
|
||||
model: model2Id,
|
||||
messages: [
|
||||
{ role: 'user', content: 'Write a JavaScript function to check if a number is prime' }
|
||||
],
|
||||
temperature: 0.5,
|
||||
max_tokens: 150
|
||||
});
|
||||
|
||||
console.log(`Response: ${response2.choices[0].message.content}\n`);
|
||||
|
||||
// Streaming chat with Model 2
|
||||
console.log('=== Streaming Chat (Model 2) ===');
|
||||
const stream2 = await client2.chat.completions.create({
|
||||
model: model2Id,
|
||||
messages: [
|
||||
{ role: 'user', content: 'Give a Python Fizzbuzz solution in one line of code?' }
|
||||
],
|
||||
temperature: 0.5,
|
||||
max_tokens: 150,
|
||||
stream: true
|
||||
});
|
||||
|
||||
process.stdout.write('Response: ');
|
||||
for await (const chunk of stream2) {
|
||||
process.stdout.write(chunk.choices[0]?.delta?.content || '');
|
||||
}
|
||||
console.log('\n');
|
||||
|
||||
// TTS example
|
||||
console.log('=== TTS Speech Generation ===');
|
||||
const ttsClient = new OpenAI({
|
||||
apiKey: 'dummy-key',
|
||||
baseURL: ttsEndpoint1
|
||||
});
|
||||
|
||||
const mp3 = await ttsClient.audio.speech.create({
|
||||
model: 'tts-1',
|
||||
voice: 'alloy',
|
||||
input: 'I think so therefore, Today is a wonderful day to grow something people love!',
|
||||
speed: 0.9
|
||||
});
|
||||
|
||||
const buffer = Buffer.from(await mp3.arrayBuffer());
|
||||
fs.writeFileSync('speech.mp3', buffer);
|
||||
console.log(`[OK] Speech file created: speech.mp3 (${buffer.length} bytes)\n`);
|
||||
|
||||
console.log('=== Examples Complete ===');
|
||||
Loading…
Add table
Add a link
Reference in a new issue