-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs_example.js
More file actions
116 lines (104 loc) · 2.69 KB
/
Copy pathnodejs_example.js
File metadata and controls
116 lines (104 loc) · 2.69 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
105
106
107
108
109
110
111
112
113
114
115
116
/**
* 蓝鹰AI网关 - Node.js 调用示例
* BlueEagle AI Gateway - Node.js Usage Example
*
* 官方文档: https://ahg.codes
*
* 安装依赖: npm install openai
*/
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://ahg.codes/v1',
apiKey: 'YOUR_API_KEY', // 从控制台获取 | Get from dashboard
});
/**
* 使用 GPT-4o 进行对话
* Chat with GPT-4o
*/
async function chatWithGPT4o() {
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello, BlueEagle!' },
],
temperature: 0.7,
max_tokens: 2048,
});
console.log('GPT-4o Response:');
console.log(response.choices[0].message.content);
}
/**
* 使用 Claude 3.5 Sonnet 进行对话
* Chat with Claude 3.5 Sonnet
*/
async function chatWithClaude() {
const response = await client.chat.completions.create({
model: 'claude-3-5-sonnet-20241022',
messages: [
{ role: 'user', content: 'Explain quantum computing in simple terms.' },
],
temperature: 0.5,
max_tokens: 4096,
});
console.log('Claude 3.5 Sonnet Response:');
console.log(response.choices[0].message.content);
}
/**
* 使用 Gemini 1.5 Pro 进行对话
* Chat with Gemini 1.5 Pro
*/
async function chatWithGemini() {
const response = await client.chat.completions.create({
model: 'gemini-1.5-pro-latest',
messages: [
{ role: 'user', content: 'Write a JavaScript function to calculate Fibonacci numbers.' },
],
temperature: 0.3,
max_tokens: 2048,
});
console.log('Gemini 1.5 Pro Response:');
console.log(response.choices[0].message.content);
}
/**
* 流式输出示例
* Streaming response example
*/
async function streamChat() {
const response = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Tell me a short story.' }],
stream: true,
max_tokens: 1024,
});
console.log('Streaming Response:');
for await (const chunk of response) {
if (chunk.choices[0]?.delta?.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
}
console.log();
}
/**
* 列出可用模型
* List available models
*/
async function listModels() {
const models = await client.models.list();
console.log('Available Models:');
for (const model of models.data) {
console.log(` - ${model.id}`);
}
}
// 运行示例 | Run example
(async () => {
try {
await chatWithGPT4o();
// await chatWithClaude();
// await chatWithGemini();
// await streamChat();
// await listModels();
} catch (error) {
console.error('Error:', error.message);
}
})();