File size: 14,611 Bytes
408b814 68c10a7 408b814 9104f63 68c10a7 408b814 9104f63 408b814 9104f63 a2240e3 68c10a7 a2240e3 68c10a7 a2240e3 9104f63 a2240e3 9104f63 408b814 68c10a7 408b814 9104f63 a2240e3 9104f63 408b814 8d7241f 408b814 68c10a7 408b814 68c10a7 408b814 8d7241f 408b814 |
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';
import {AutoRouter, cors, error, json} from 'itty-router';
import dotenv from 'dotenv';
import path,{ dirname } from 'path';
import { fileURLToPath } from 'url';
import {createServerAdapter} from '@whatwg-node/server';
import {createServer} from 'http';
// 加载环境变量
dotenv.config();
// 获取当前文件的目录路径(ESM 方式)
const __dirname = dirname(fileURLToPath(import.meta.url));
// 初始化配置
class Config {
constructor() {
this.API_PREFIX = process.env.API_PREFIX || '/';
this.API_KEY = process.env.API_KEY || '';
this.MAX_RETRY_COUNT = process.env.MAX_RETRY_COUNT || 3;
this.RETRY_DELAY = process.env.RETRY_DELAY || 5000;
this.COMMON_GRPC = 'runtime-native-io-vertex-inference-grpc-service-lmuw6mcn3q-ul.a.run.app';
this.COMMON_PROTO = path.join(__dirname,'..', 'protos', 'VertexInferenceService.proto')
this.GPT_GRPC = 'runtime-native-io-gpt-inference-grpc-service-lmuw6mcn3q-ul.a.run.app';
this.GPT_PROTO = path.join(__dirname,'..', 'protos', 'GPTInferenceService.proto')
this.PORT = process.env.PORT || 8787;
}
}
class GRPCHandler {
constructor(protoFilePath) {
// 动态加载传入的 .proto 文件路径
this.packageDefinition = protoLoader.loadSync(protoFilePath, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
}
}
const config = new Config();
// 中间件
// 添加运行回源
const { preflight, corsify } = cors({
origin: '*',
allowMethods: '*',
exposeHeaders: '*',
});
// 添加认证
const withAuth = (request) => {
if (config.API_KEY) {
const authHeader = request.headers.get('Authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return error(401, 'Unauthorized: Missing or invalid Authorization header');
}
const token = authHeader.substring(7);
if (token !== config.API_KEY) {
return error(403, 'Forbidden: Invalid API key');
}
}
};
// 返回运行信息
const logger = (res, req) => {
console.log(req.method, res.status, req.url, Date.now() - req.start, 'ms');
};
// 定义模型映射信息
const MODEL_INFO = {
"claude-3-sonnet-20240229": {
"provider": "anthropic",
"mapping": "claude-3-sonnet@20240229"
},
"claude-3-opus-20240229": {
"provider": "anthropic",
"mapping": "claude-3-opus@20240229"
},
"claude-3-haiku-20240307": {
"provider": "anthropic",
"mapping": "claude-3-haiku@20240307"
},
"claude-3-5-sonnet-20240620": {
"provider": "anthropic",
"mapping": "claude-3-5-sonnet@20240620"
},
"gpt-4o-mini": {
"provider": "openai",
"mapping": "gpt-4o-mini"
},
"gpt-4o": {
"provider": "openai",
"mapping": "gpt-4o"
},
"gpt-4-turbo": {
"provider": "openai",
"mapping": "gpt-4-turbo"
},
"gpt-4": {
"provider": "openai",
"mapping": "gpt-4"
},
"gpt-3.5-turbo": {
"provider": "openai",
"mapping": "gpt-3.5-turbo"
},
"gemini-1.5-pro": {
"provider": "google",
"mapping": "gemini-1.5-pro"
},
"gemini-1.5-flash": {
"provider": "google",
"mapping": "gemini-1.5-flash"
},
"chat-bison": {
"provider": "pieces-os",
"mapping": "chat-bison"
},
"codechat-bison": {
"provider": "pieces-os",
"mapping": "codechat-bison"
}
};
// 定义路由
const router = AutoRouter({
before: [preflight], // 只保留 CORS preflight 检查
missing: () => error(404, '404 not found.'),
finally: [corsify, logger],
});
// 根路由
router.get('/', () => json({
service: "AI Chat Completion Proxy",
usage: {
endpoint: "/v1/chat/completions",
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
},
body: {
model: "One of: " + Object.keys(MODEL_INFO).join(", "),
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello, who are you?" }
],
stream: false,
temperature: 0.7,
top_p: 1
}
},
note: "Replace YOUR_API_KEY with your actual API key."
}));
// models 路由
router.get(config.API_PREFIX + '/v1/models', withAuth, () =>
json({
object: "list",
data: Object.entries(MODEL_INFO).map(([modelId, info]) => ({
id: modelId,
object: "model",
created: Date.now(),
owned_by: "pieces-os",
permission: [],
root: modelId,
parent: null,
mapping: info.mapping,
provider: info.provider
}))
})
);
// chat 路由
router.post(config.API_PREFIX + '/v1/chat/completions', withAuth, (req) => handleCompletion(req));
async function GrpcToPieces(models, message, rules, stream, temperature, top_p) {
// 在非GPT类型的模型中,temperature和top_p是无效的
// 使用系统的根证书
const credentials = grpc.credentials.createSsl();
let client,request;
if (models.includes('gpt')){
// 加载proto文件
const packageDefinition = new GRPCHandler(config.GPT_PROTO).packageDefinition;
// 构建请求消息
request = {
models: models,
messages: [
{role: 0, message: rules}, // system
{role: 1, message: message} // user
],
temperature:temperature || 0.1,
top_p:top_p ?? 1,
}
// 获取gRPC对象
const GRPCobjects = grpc.loadPackageDefinition(packageDefinition).runtime.aot.machine_learning.parents.gpt;
client = new GRPCobjects.GPTInferenceService(config.GPT_GRPC, credentials);
} else {
// 加载proto文件
const packageDefinition = new GRPCHandler(config.COMMON_PROTO).packageDefinition;
// 构建请求消息
request = {
models: models,
args: {
messages: {
unknown: 1,
message: message
},
rules: rules
}
};
// 获取gRPC对象
const GRPCobjects = grpc.loadPackageDefinition(packageDefinition).runtime.aot.machine_learning.parents.vertex;
client = new GRPCobjects.VertexInferenceService(config.COMMON_GRPC, credentials);
}
return await ConvertOpenai(client,request,models,stream);
}
async function messagesProcess(messages) {
let rules = '';
let message = '';
for (const msg of messages) {
let role = msg.role;
// 格式化为字符串
const contentStr = Array.isArray(msg.content)
? msg.content
.filter((item) => item.text)
.map((item) => item.text)
.join('') || ''
: msg.content;
// 判断身份
if (role === 'system') {
rules += `system:${contentStr};\r\n`;
} else if (['user', 'assistant'].includes(role)) {
message += `${role}:${contentStr};\r\n`;
}
}
return { rules, message };
}
async function ConvertOpenai(client,request,model,stream) {
for (let i = 0; i < config.MAX_RETRY_COUNT; i++) {
try {
if (stream) {
const call = client.PredictWithStream(request);
const encoder = new TextEncoder();
const ReturnStream = new ReadableStream({
start(controller) {
call.on('data', (response) => {
let response_code = Number(response.response_code);
if (response_code === 204) {
// 如果 response_code 是 204,关闭流
controller.close()
call.destroy()
} else if (response_code === 200) {
let response_message
if (model.includes('gpt')) {
response_message = response.body.message_warpper.message.message;
} else {
response_message = response.args.args.args.message;
}
// 否则,将数据块加入流中
controller.enqueue(encoder.encode(`data: ${JSON.stringify(ChatCompletionStreamWithModel(response_message, model))}\n\n`));
} else {
controller.error(new Error(`Error: stream chunk is not success`));
controller.close()
}
})
}
});
return new Response(ReturnStream, {
headers: {
'Content-Type': 'text/event-stream',
},
})
} else {
const call = await new Promise((resolve, reject) => {
client.Predict(request, (err, response) => {
if (err) reject(err);
else resolve(response);
});
});
let response_code = Number(call.response_code);
if (response_code === 200) {
let response_message
if (model.includes('gpt')) {
response_message = call.body.message_warpper.message.message;
} else {
response_message = call.args.args.args.message;
}
return new Response(JSON.stringify(ChatCompletionWithModel(response_message, model)), {
headers: {
'Content-Type': 'application/json',
},
});
}
}
} catch (err) {
console.error(err);
await new Promise((resolve) => setTimeout(resolve, config.RETRY_DELAY));
}
}
return error(500, err.message);
}
function ChatCompletionWithModel(message, model) {
return {
id: 'Chat-Nekohy',
object: 'chat.completion',
created: Date.now(),
model,
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
},
choices: [
{
message: {
content: message,
role: 'assistant',
},
index: 0,
},
],
};
}
function ChatCompletionStreamWithModel(text, model) {
return {
id: 'chatcmpl-Nekohy',
object: 'chat.completion.chunk',
created: 0,
model,
choices: [
{
index: 0,
delta: {
content: text,
},
finish_reason: null,
},
],
};
}
async function handleCompletion(request) {
try {
const { model: inputModel, messages, stream, temperature, top_p } = await request.json();
// 获取模型映射
const modelInfo = MODEL_INFO[inputModel];
if (!modelInfo) {
return error(400, `Unsupported model: ${inputModel}`);
}
const mappedModel = modelInfo.mapping;
// 解析 system 和 user/assistant 消息
const { rules, message: content } = await messagesProcess(messages);
// 使用映射后的模型名称
return await GrpcToPieces(mappedModel, content, rules, stream, temperature, top_p);
} catch (err) {
return error(500, err.message);
}
}
(async () => {
//For Cloudflare Workers
if (typeof addEventListener === 'function') return;
// For Nodejs
const ittyServer = createServerAdapter(router.fetch);
console.log(`Listening on http://localhost:${config.PORT}`);
const httpServer = createServer(ittyServer);
httpServer.listen(config.PORT);
})(); |