-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs_example.js
More file actions
164 lines (148 loc) · 4.1 KB
/
Copy pathnodejs_example.js
File metadata and controls
164 lines (148 loc) · 4.1 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* BlueEagle AI Gateway - Node.js 调用示例
* 官方文档: https://ahg.codes
*/
import OpenAI from 'openai';
// 初始化客户端 | Initialize client
// 仅需修改 baseUrl,其余与官方 OpenAI SDK 完全一致
const client = new OpenAI({
apiKey: 'YOUR_API_KEY', // 替换为您的蓝鹰API密钥
baseURL: 'https://ahg.codes/v1'
});
/**
* 非流式对话 | Non-streaming chat
*/
async function chatNonStreaming() {
try {
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello, BlueEagle AI Gateway!' }
],
temperature: 0.7,
max_tokens: 500
});
console.log('='.repeat(50));
console.log('非流式响应 | Non-streaming Response:');
console.log('='.repeat(50));
console.log(response.choices[0].message.content);
console.log(`\n消耗tokens: ${response.usage.total_tokens}`);
} catch (error) {
console.error('Error:', error.message);
}
}
/**
* 流式对话 | Streaming chat
*/
async function chatStreaming() {
try {
const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: '请用三句话介绍蓝鹰AI网关的优势' }
],
stream: true,
temperature: 0.7
});
console.log('='.repeat(50));
console.log('流式响应 | Streaming Response:');
console.log('='.repeat(50));
for await (const chunk of stream) {
if (chunk.choices[0]?.delta?.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
}
console.log('\n');
} catch (error) {
console.error('Error:', error.message);
}
}
/**
* 调用 Claude 模型 | Claude model
*/
async function chatWithClaude() {
try {
const response = await client.chat.completions.create({
model: 'claude-3-5-sonnet-20241022',
messages: [
{ role: 'user', content: 'Explain quantum computing in simple terms.' }
],
max_tokens: 1000
});
console.log('='.repeat(50));
console.log('Claude 响应 | Claude Response:');
console.log('='.repeat(50));
console.log(response.choices[0].message.content);
} catch (error) {
console.error('Error:', error.message);
}
}
/**
* 调用 Gemini 模型 | Gemini model
*/
async function chatWithGemini() {
try {
const response = await client.chat.completions.create({
model: 'gemini-1.5-pro-latest',
messages: [
{ role: 'user', content: 'What are the benefits of AI gateways?' }
],
max_tokens: 1000
});
console.log('='.repeat(50));
console.log('Gemini 响应 | Gemini Response:');
console.log('='.repeat(50));
console.log(response.choices[0].message.content);
} catch (error) {
console.error('Error:', error.message);
}
}
/**
* 函数调用 | Function calling
*/
async function functionCalling() {
try {
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name, e.g. Beijing'
}
},
required: ['location']
}
}
}
];
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: "What's the weather like in Shanghai?" }
],
tools: tools,
tool_choice: 'auto'
});
console.log('='.repeat(50));
console.log('函数调用 | Function Calling:');
console.log('='.repeat(50));
console.log(JSON.stringify(response.choices[0].message, null, 2));
} catch (error) {
console.error('Error:', error.message);
}
}
// 运行示例 | Run examples
console.log('🦅 BlueEagle AI Gateway - Node.js Examples\n');
// 取消注释以运行对应示例
chatNonStreaming();
// chatStreaming();
// chatWithClaude();
// chatWithGemini();
// functionCalling();