Update api/index.js
Browse files- api/index.js +127 -96
api/index.js
CHANGED
|
@@ -173,46 +173,66 @@ router.get(config.API_PREFIX + '/v1/models', withAuth, () =>
|
|
| 173 |
// chat 路由
|
| 174 |
router.post(config.API_PREFIX + '/v1/chat/completions', withAuth, (req) => handleCompletion(req));
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
async function GrpcToPieces(models, message, rules, stream, temperature, top_p) {
|
| 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 |
-
message: message
|
| 207 |
-
},
|
| 208 |
-
rules: rules
|
| 209 |
-
}
|
| 210 |
-
};
|
| 211 |
-
// 获取gRPC对象
|
| 212 |
-
const GRPCobjects = grpc.loadPackageDefinition(packageDefinition).runtime.aot.machine_learning.parents.vertex;
|
| 213 |
-
client = new GRPCobjects.VertexInferenceService(config.COMMON_GRPC, credentials);
|
| 214 |
}
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
}
|
| 217 |
|
| 218 |
async function messagesProcess(messages) {
|
|
@@ -239,69 +259,80 @@ async function messagesProcess(messages) {
|
|
| 239 |
return { rules, message };
|
| 240 |
}
|
| 241 |
|
| 242 |
-
async function ConvertOpenai(client,request,model,stream) {
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
if (model.includes('gpt')) {
|
| 259 |
-
response_message = response.body.message_warpper.message.message;
|
| 260 |
-
} else {
|
| 261 |
-
response_message = response.args.args.args.message;
|
| 262 |
-
}
|
| 263 |
-
// 否则,将数据块加入流中
|
| 264 |
-
controller.enqueue(encoder.encode(`data: ${JSON.stringify(ChatCompletionStreamWithModel(response_message, model))}\n\n`));
|
| 265 |
-
} else {
|
| 266 |
-
controller.error(new Error(`Error: stream chunk is not success`));
|
| 267 |
-
controller.close()
|
| 268 |
-
}
|
| 269 |
-
})
|
| 270 |
-
}
|
| 271 |
-
});
|
| 272 |
-
return new Response(ReturnStream, {
|
| 273 |
-
headers: {
|
| 274 |
-
'Content-Type': 'text/event-stream',
|
| 275 |
-
},
|
| 276 |
-
})
|
| 277 |
-
} else {
|
| 278 |
-
const call = await new Promise((resolve, reject) => {
|
| 279 |
-
client.Predict(request, (err, response) => {
|
| 280 |
-
if (err) reject(err);
|
| 281 |
-
else resolve(response);
|
| 282 |
-
});
|
| 283 |
-
});
|
| 284 |
-
let response_code = Number(call.response_code);
|
| 285 |
-
if (response_code === 200) {
|
| 286 |
-
let response_message
|
| 287 |
if (model.includes('gpt')) {
|
| 288 |
-
|
| 289 |
} else {
|
| 290 |
-
|
| 291 |
-
}
|
| 292 |
-
return new Response(JSON.stringify(ChatCompletionWithModel(response_message, model)), {
|
| 293 |
-
headers: {
|
| 294 |
-
'Content-Type': 'application/json',
|
| 295 |
-
},
|
| 296 |
-
});
|
| 297 |
}
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
}
|
| 304 |
-
|
| 305 |
}
|
| 306 |
|
| 307 |
function ChatCompletionWithModel(message, model) {
|
|
|
|
| 173 |
// chat 路由
|
| 174 |
router.post(config.API_PREFIX + '/v1/chat/completions', withAuth, (req) => handleCompletion(req));
|
| 175 |
|
| 176 |
+
function getMetadata() {
|
| 177 |
+
const metadata = new grpc.Metadata();
|
| 178 |
+
metadata.set('user-agent', 'dart-grpc/2.0.0');
|
| 179 |
+
return metadata;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
async function GrpcToPieces(models, message, rules, stream, temperature, top_p) {
|
| 183 |
+
// 使用系统的根证书
|
| 184 |
+
const credentials = grpc.credentials.createSsl();
|
| 185 |
+
|
| 186 |
+
// 创建自定义metadata
|
| 187 |
+
function getMetadata() {
|
| 188 |
+
const metadata = new grpc.Metadata();
|
| 189 |
+
metadata.set('user-agent', 'dart-grpc/2.0.0');
|
| 190 |
+
return metadata;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
const metadata = getMetadata();
|
| 194 |
+
const options = {
|
| 195 |
+
'grpc.primary_user_agent': 'dart-grpc/2.0.0'
|
| 196 |
+
};
|
| 197 |
+
|
| 198 |
+
let client, request;
|
| 199 |
+
|
| 200 |
+
if (models.includes('gpt')) {
|
| 201 |
+
// 加载proto文件
|
| 202 |
+
const packageDefinition = new GRPCHandler(config.GPT_PROTO).packageDefinition;
|
| 203 |
+
// 构建请求消息
|
| 204 |
+
request = {
|
| 205 |
+
models: models,
|
| 206 |
+
messages: [
|
| 207 |
+
{role: 0, message: rules}, // system
|
| 208 |
+
{role: 1, message: message} // user
|
| 209 |
+
],
|
| 210 |
+
temperature: temperature || 0.1,
|
| 211 |
+
top_p: top_p ?? 1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
}
|
| 213 |
+
// 获取gRPC对象
|
| 214 |
+
const GRPCobjects = grpc.loadPackageDefinition(packageDefinition).runtime.aot.machine_learning.parents.gpt;
|
| 215 |
+
client = new GRPCobjects.GPTInferenceService(config.GPT_GRPC, credentials, options);
|
| 216 |
+
} else {
|
| 217 |
+
// 加载proto文件
|
| 218 |
+
const packageDefinition = new GRPCHandler(config.COMMON_PROTO).packageDefinition;
|
| 219 |
+
// 构建请求消息
|
| 220 |
+
request = {
|
| 221 |
+
models: models,
|
| 222 |
+
args: {
|
| 223 |
+
messages: {
|
| 224 |
+
unknown: 1,
|
| 225 |
+
message: message
|
| 226 |
+
},
|
| 227 |
+
rules: rules
|
| 228 |
+
}
|
| 229 |
+
};
|
| 230 |
+
// 获取gRPC对象
|
| 231 |
+
const GRPCobjects = grpc.loadPackageDefinition(packageDefinition).runtime.aot.machine_learning.parents.vertex;
|
| 232 |
+
client = new GRPCobjects.VertexInferenceService(config.COMMON_GRPC, credentials, options);
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
return await ConvertOpenai(client, request, models, stream, metadata);
|
| 236 |
}
|
| 237 |
|
| 238 |
async function messagesProcess(messages) {
|
|
|
|
| 259 |
return { rules, message };
|
| 260 |
}
|
| 261 |
|
| 262 |
+
async function ConvertOpenai(client, request, model, stream, metadata) {
|
| 263 |
+
for (let i = 0; i < config.MAX_RETRY_COUNT; i++) {
|
| 264 |
+
try {
|
| 265 |
+
if (stream) {
|
| 266 |
+
const call = client.PredictWithStream(request, metadata);
|
| 267 |
+
const encoder = new TextEncoder();
|
| 268 |
+
const ReturnStream = new ReadableStream({
|
| 269 |
+
start(controller) {
|
| 270 |
+
call.on('data', (response) => {
|
| 271 |
+
let response_code = Number(response.response_code);
|
| 272 |
+
if (response_code === 204) {
|
| 273 |
+
// 如果 response_code 是 204,关闭流
|
| 274 |
+
controller.close();
|
| 275 |
+
call.destroy();
|
| 276 |
+
} else if (response_code === 200) {
|
| 277 |
+
let response_message;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
if (model.includes('gpt')) {
|
| 279 |
+
response_message = response.body.message_warpper.message.message;
|
| 280 |
} else {
|
| 281 |
+
response_message = response.args.args.args.message;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
}
|
| 283 |
+
// 否则,将数据块加入流中
|
| 284 |
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(ChatCompletionStreamWithModel(response_message, model))}\n\n`));
|
| 285 |
+
} else {
|
| 286 |
+
controller.error(new Error(`Error: stream chunk is not success`));
|
| 287 |
+
controller.close();
|
| 288 |
+
}
|
| 289 |
+
});
|
| 290 |
+
call.on('error', (error) => {
|
| 291 |
+
controller.error(error);
|
| 292 |
+
controller.close();
|
| 293 |
+
});
|
| 294 |
+
call.on('end', () => {
|
| 295 |
+
controller.close();
|
| 296 |
+
});
|
| 297 |
+
}
|
| 298 |
+
});
|
| 299 |
+
return new Response(ReturnStream, {
|
| 300 |
+
headers: {
|
| 301 |
+
'Content-Type': 'text/event-stream',
|
| 302 |
+
},
|
| 303 |
+
});
|
| 304 |
+
} else {
|
| 305 |
+
const call = await new Promise((resolve, reject) => {
|
| 306 |
+
client.Predict(request, metadata, (err, response) => {
|
| 307 |
+
if (err) reject(err);
|
| 308 |
+
else resolve(response);
|
| 309 |
+
});
|
| 310 |
+
});
|
| 311 |
+
let response_code = Number(call.response_code);
|
| 312 |
+
if (response_code === 200) {
|
| 313 |
+
let response_message;
|
| 314 |
+
if (model.includes('gpt')) {
|
| 315 |
+
response_message = call.body.message_warpper.message.message;
|
| 316 |
+
} else {
|
| 317 |
+
response_message = call.args.args.args.message;
|
| 318 |
+
}
|
| 319 |
+
return new Response(JSON.stringify(ChatCompletionWithModel(response_message, model)), {
|
| 320 |
+
headers: {
|
| 321 |
+
'Content-Type': 'application/json',
|
| 322 |
+
},
|
| 323 |
+
});
|
| 324 |
+
} else {
|
| 325 |
+
throw new Error(`Error: response code ${response_code}`);
|
| 326 |
}
|
| 327 |
+
}
|
| 328 |
+
} catch (err) {
|
| 329 |
+
console.error(err);
|
| 330 |
+
if (i === config.MAX_RETRY_COUNT - 1) {
|
| 331 |
+
return error(500, err.message);
|
| 332 |
+
}
|
| 333 |
+
await new Promise((resolve) => setTimeout(resolve, config.RETRY_DELAY));
|
| 334 |
}
|
| 335 |
+
}
|
| 336 |
}
|
| 337 |
|
| 338 |
function ChatCompletionWithModel(message, model) {
|