-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
245 lines (214 loc) · 7.85 KB
/
server.js
File metadata and controls
245 lines (214 loc) · 7.85 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// server.js - OpenAI to NVIDIA NIM API Proxy
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
// NVIDIA NIM API configuration
const NIM_API_BASE = process.env.NIM_API_BASE || 'https://integrate.api.nvidia.com/v1';
const NIM_API_KEY = process.env.NIM_API_KEY;
// 🔥 REASONING DISPLAY TOGGLE - Shows/hides reasoning in output
const SHOW_REASONING = false; // Set to true to show reasoning with <think> tags
// 🔥 THINKING MODE TOGGLE - Enables thinking for specific models that support it
const ENABLE_THINKING_MODE = false; // Set to true to enable chat_template_kwargs thinking parameter
// Model mapping (adjust based on available NIM models)
const MODEL_MAPPING = {
'gpt-3.5-turbo': 'nvidia/llama-3.1-nemotron-ultra-253b-v1',
'gpt-4': 'qwen/qwen3-coder-480b-a35b-instruct',
'gpt-4-turbo': 'moonshotai/kimi-k2-instruct-0905',
'gpt-4o': 'deepseek-ai/deepseek-v3.1',
'claude-3-opus': 'openai/gpt-oss-120b',
'claude-3-sonnet': 'openai/gpt-oss-20b',
'gemini-pro': 'qwen/qwen3-next-80b-a3b-thinking'
};
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'ok',
service: 'OpenAI to NVIDIA NIM Proxy',
reasoning_display: SHOW_REASONING,
thinking_mode: ENABLE_THINKING_MODE
});
});
// List models endpoint (OpenAI compatible)
app.get('/v1/models', (req, res) => {
const models = Object.keys(MODEL_MAPPING).map(model => ({
id: model,
object: 'model',
created: Date.now(),
owned_by: 'nvidia-nim-proxy'
}));
res.json({
object: 'list',
data: models
});
});
// Chat completions endpoint (main proxy)
app.post('/v1/chat/completions', async (req, res) => {
try {
const { model, messages, temperature, max_tokens, stream } = req.body;
// Smart model selection with fallback
let nimModel = MODEL_MAPPING[model];
if (!nimModel) {
try {
await axios.post(`${NIM_API_BASE}/chat/completions`, {
model: model,
messages: [{ role: 'user', content: 'test' }],
max_tokens: 1
}, {
headers: { 'Authorization': `Bearer ${NIM_API_KEY}`, 'Content-Type': 'application/json' },
validateStatus: (status) => status < 500
}).then(res => {
if (res.status >= 200 && res.status < 300) {
nimModel = model;
}
});
} catch (e) {}
if (!nimModel) {
const modelLower = model.toLowerCase();
if (modelLower.includes('gpt-4') || modelLower.includes('claude-opus') || modelLower.includes('405b')) {
nimModel = 'meta/llama-3.1-405b-instruct';
} else if (modelLower.includes('claude') || modelLower.includes('gemini') || modelLower.includes('70b')) {
nimModel = 'meta/llama-3.1-70b-instruct';
} else {
nimModel = 'meta/llama-3.1-8b-instruct';
}
}
}
// Transform OpenAI request to NIM format
const nimRequest = {
model: nimModel,
messages: messages,
temperature: temperature || 0.6,
max_tokens: max_tokens || 9024,
extra_body: ENABLE_THINKING_MODE ? { chat_template_kwargs: { thinking: true } } : undefined,
stream: stream || false
};
// Make request to NVIDIA NIM API
const response = await axios.post(`${NIM_API_BASE}/chat/completions`, nimRequest, {
headers: {
'Authorization': `Bearer ${NIM_API_KEY}`,
'Content-Type': 'application/json'
},
responseType: stream ? 'stream' : 'json'
});
if (stream) {
// Handle streaming response with reasoning
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
let buffer = '';
let reasoningStarted = false;
response.data.on('data', (chunk) => {
buffer += chunk.toString();
const lines = buffer.split('\\n');
buffer = lines.pop() || '';
lines.forEach(line => {
if (line.startsWith('data: ')) {
if (line.includes('[DONE]')) {
res.write(line + '\\n');
return;
}
try {
const data = JSON.parse(line.slice(6));
if (data.choices?.[0]?.delta) {
const reasoning = data.choices[0].delta.reasoning_content;
const content = data.choices[0].delta.content;
if (SHOW_REASONING) {
let combinedContent = '';
if (reasoning && !reasoningStarted) {
combinedContent = '<think>\\n' + reasoning;
reasoningStarted = true;
} else if (reasoning) {
combinedContent = reasoning;
}
if (content && reasoningStarted) {
combinedContent += '</think>\\n\\n' + content;
reasoningStarted = false;
} else if (content) {
combinedContent += content;
}
if (combinedContent) {
data.choices[0].delta.content = combinedContent;
delete data.choices[0].delta.reasoning_content;
}
} else {
if (content) {
data.choices[0].delta.content = content;
} else {
data.choices[0].delta.content = '';
}
delete data.choices[0].delta.reasoning_content;
}
}
res.write(`data: ${JSON.stringify(data)}\\n\\n`);
} catch (e) {
res.write(line + '\\n');
}
}
});
});
response.data.on('end', () => res.end());
response.data.on('error', (err) => {
console.error('Stream error:', err);
res.end();
});
} else {
// Transform NIM response to OpenAI format with reasoning
const openaiResponse = {
id: `chatcmpl-${Date.now()}`,
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: model,
choices: response.data.choices.map(choice => {
let fullContent = choice.message?.content || '';
if (SHOW_REASONING && choice.message?.reasoning_content) {
fullContent = '<think>\\n' + choice.message.reasoning_content + '\\n</think>\\n\\n' + fullContent;
}
return {
index: choice.index,
message: {
role: choice.message.role,
content: fullContent
},
finish_reason: choice.finish_reason
};
}),
usage: response.data.usage || {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
};
res.json(openaiResponse);
}
} catch (error) {
console.error('Proxy error:', error.message);
res.status(error.response?.status || 500).json({
error: {
message: error.message || 'Internal server error',
type: 'invalid_request_error',
code: error.response?.status || 500
}
});
}
});
// Catch-all for unsupported endpoints
app.all('*', (req, res) => {
res.status(404).json({
error: {
message: `Endpoint ${req.path} not found`,
type: 'invalid_request_error',
code: 404
}
});
});
app.listen(PORT, () => {
console.log(`OpenAI to NVIDIA NIM Proxy running on port ${PORT}`);
console.log(`Health check: http://localhost:${PORT}/health`);
console.log(`Reasoning display: ${SHOW_REASONING ? 'ENABLED' : 'DISABLED'}`);
console.log(`Thinking mode: ${ENABLE_THINKING_MODE ? 'ENABLED' : 'DISABLED'}`);
});