-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_compatible.js
More file actions
104 lines (89 loc) · 3.59 KB
/
Copy pathopenai_compatible.js
File metadata and controls
104 lines (89 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Example demonstrating OpenAI-compatible API usage.
*
* This script shows how to use the OpenAI-compatible interface for both
* chat completions and image generation.
*/
import { Pollinations } from '@gpt4free/g4f.dev';
async function main() {
console.log('='.repeat(70));
console.log('OpenAI-Compatible API Examples');
console.log('='.repeat(70));
// Initialize client (with or without API key)
// Without API key (uses free public API)
const client = new Pollinations();
// With API key (uses gen.pollinations.ai)
// const client = new Pollinations({ apiKey: 'your-api-key-here' });
// ========================================================================
// Chat Completions API (OpenAI-compatible)
// ========================================================================
console.log('\n1. Simple Chat Completion:');
console.log('-'.repeat(70));
const response1 = await client.chat.completions.create({
messages: [
{ role: 'user', content: 'What is JavaScript?' }
]
});
console.log(`Response: ${response1.choices[0].message.content}`);
console.log('\n2. Chat with System Message:');
console.log('-'.repeat(70));
const response2 = await client.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a helpful coding assistant.' },
{ role: 'user', content: 'Explain async/await in JavaScript' }
],
model: 'openai'
});
console.log(`Response: ${response2.choices[0].message.content}`);
console.log('\n3. Chat with Temperature Control:');
console.log('-'.repeat(70));
const response3 = await client.chat.completions.create({
messages: [
{ role: 'user', content: 'Write a creative story about a robot' }
],
temperature: 0.9,
max_tokens: 200
});
console.log(`Response: ${response3.choices[0].message.content}`);
console.log('\n4. Accessing Response Fields:');
console.log('-'.repeat(70));
const response4 = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Hello!' }],
model: 'openai'
});
console.log(`Object Type: ${response4.object}`);
console.log(`Model: ${response4.model}`);
console.log(`Finish Reason: ${response4.choices[0].finish_reason}`);
console.log(`Message Role: ${response4.choices[0].message.role}`);
console.log(`Message Content: ${response4.choices[0].message.content}`);
// ========================================================================
// Images API (OpenAI-compatible)
// ========================================================================
console.log('\n5. Simple Image Generation:');
console.log('-'.repeat(70));
const imgResponse1 = await client.images.generate({
prompt: 'A serene mountain landscape at sunset'
});
console.log(`Image URL: ${imgResponse1.data[0].url}`);
console.log('\n6. Image with Size:');
console.log('-'.repeat(70));
const imgResponse2 = await client.images.generate({
prompt: 'A futuristic city skyline',
size: '1024x768'
});
console.log(`Image URL: ${imgResponse2.data[0].url.substring(0, 80)}...`);
console.log('\n7. Image with Model:');
console.log('-'.repeat(70));
const imgResponse3 = await client.images.generate({
prompt: 'Abstract art with vibrant colors',
model: 'flux',
size: '512x512'
});
console.log(`Image URL: ${imgResponse3.data[0].url.substring(0, 80)}...`);
console.log(`Revised Prompt: ${imgResponse3.data[0].revised_prompt}`);
console.log('\n' + '='.repeat(70));
console.log('OpenAI-compatible API works perfectly!');
console.log('='.repeat(70));
}
// Run the examples
main().catch(console.error);