Spaces:
Sleeping
Sleeping
yuanjiajun commited on
Commit ·
1aad435
1
Parent(s): 26a8d61
feat: 配置模型参数
Browse files- src/api/controllers/chat.ts +11 -18
- src/api/routes/chat.ts +0 -3
src/api/controllers/chat.ts
CHANGED
|
@@ -86,22 +86,18 @@ async function createCompletion(
|
|
| 86 |
) {
|
| 87 |
let session: http2.ClientHttp2Session;
|
| 88 |
return (async () => {
|
| 89 |
-
console.log(333333333);
|
| 90 |
logger.info(messages);
|
| 91 |
|
| 92 |
// 提取引用文件URL并上传qwen获得引用的文件ID列表
|
| 93 |
const refFileUrls = extractRefFileUrls(messages);
|
| 94 |
-
console.log(44444444);
|
| 95 |
const refs = refFileUrls.length
|
| 96 |
? await Promise.all(
|
| 97 |
refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
|
| 98 |
)
|
| 99 |
: [];
|
| 100 |
-
console.log(55555555);
|
| 101 |
// 如果引用对话ID不正确则重置引用
|
| 102 |
if (!/[0-9a-z]{32}/.test(refConvId))
|
| 103 |
refConvId = '';
|
| 104 |
-
console.log(666666666);
|
| 105 |
// 请求流
|
| 106 |
const session: http2.ClientHttp2Session = await new Promise(
|
| 107 |
(resolve, reject) => {
|
|
@@ -110,7 +106,6 @@ async function createCompletion(
|
|
| 110 |
session.on("error", reject);
|
| 111 |
}
|
| 112 |
);
|
| 113 |
-
console.log(7777777777);
|
| 114 |
const [sessionId, parentMsgId = ''] = refConvId.split('-');
|
| 115 |
const req = session.request({
|
| 116 |
":method": "POST",
|
|
@@ -120,12 +115,11 @@ async function createCompletion(
|
|
| 120 |
...FAKE_HEADERS,
|
| 121 |
Accept: "text/event-stream",
|
| 122 |
});
|
| 123 |
-
console.log(888888888888);
|
| 124 |
req.setTimeout(120000);
|
| 125 |
req.write(
|
| 126 |
JSON.stringify({
|
| 127 |
mode: "chat",
|
| 128 |
-
model
|
| 129 |
action: "next",
|
| 130 |
userAction: "chat",
|
| 131 |
requestId: util.uuid(false),
|
|
@@ -141,8 +135,7 @@ async function createCompletion(
|
|
| 141 |
req.setEncoding("utf8");
|
| 142 |
const streamStartTime = util.timestamp();
|
| 143 |
// 接收流为输出文本
|
| 144 |
-
|
| 145 |
-
const answer = await receiveStream(req);
|
| 146 |
session.close();
|
| 147 |
logger.success(
|
| 148 |
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
|
@@ -217,7 +210,7 @@ async function createCompletionStream(
|
|
| 217 |
req.write(
|
| 218 |
JSON.stringify({
|
| 219 |
mode: "chat",
|
| 220 |
-
model
|
| 221 |
action: "next",
|
| 222 |
userAction: "chat",
|
| 223 |
requestId: util.uuid(false),
|
|
@@ -233,7 +226,7 @@ async function createCompletionStream(
|
|
| 233 |
req.setEncoding("utf8");
|
| 234 |
const streamStartTime = util.timestamp();
|
| 235 |
// 创建转换流将消息格式转换为gpt兼容格式
|
| 236 |
-
return createTransStream(req, (convId: string) => {
|
| 237 |
// 关闭请求会话
|
| 238 |
session.close();
|
| 239 |
logger.success(
|
|
@@ -287,7 +280,7 @@ async function generateImages(
|
|
| 287 |
req.write(
|
| 288 |
JSON.stringify({
|
| 289 |
mode: "chat",
|
| 290 |
-
model
|
| 291 |
action: "next",
|
| 292 |
userAction: "chat",
|
| 293 |
requestId: util.uuid(false),
|
|
@@ -435,12 +428,12 @@ function checkResult(result: AxiosResponse) {
|
|
| 435 |
*
|
| 436 |
* @param stream 消息流
|
| 437 |
*/
|
| 438 |
-
async function receiveStream(stream: any): Promise<any> {
|
| 439 |
return new Promise((resolve, reject) => {
|
| 440 |
// 消息初始化
|
| 441 |
const data = {
|
| 442 |
id: "",
|
| 443 |
-
model
|
| 444 |
object: "chat.completion",
|
| 445 |
choices: [
|
| 446 |
{
|
|
@@ -518,7 +511,7 @@ async function receiveStream(stream: any): Promise<any> {
|
|
| 518 |
* @param stream 消息流
|
| 519 |
* @param endCallback 传输结束回调
|
| 520 |
*/
|
| 521 |
-
function createTransStream(stream: any, endCallback?: Function) {
|
| 522 |
// 消息创建时间
|
| 523 |
const created = util.unixTimestamp();
|
| 524 |
// 创建转换流
|
|
@@ -528,7 +521,7 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|
| 528 |
transStream.write(
|
| 529 |
`data: ${JSON.stringify({
|
| 530 |
id: "",
|
| 531 |
-
model
|
| 532 |
object: "chat.completion.chunk",
|
| 533 |
choices: [
|
| 534 |
{
|
|
@@ -576,7 +569,7 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|
| 576 |
content += chunk;
|
| 577 |
const data = `data: ${JSON.stringify({
|
| 578 |
id: `${result.sessionId}-${result.msgId}`,
|
| 579 |
-
model
|
| 580 |
object: "chat.completion.chunk",
|
| 581 |
choices: [
|
| 582 |
{ index: 0, delta: { content: chunk }, finish_reason: null },
|
|
@@ -593,7 +586,7 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|
| 593 |
delta.content += `服务暂时不可用,第三方响应错误:${result.errorCode}`;
|
| 594 |
const data = `data: ${JSON.stringify({
|
| 595 |
id: `${result.sessionId}-${result.msgId}`,
|
| 596 |
-
model
|
| 597 |
object: "chat.completion.chunk",
|
| 598 |
choices: [
|
| 599 |
{
|
|
|
|
| 86 |
) {
|
| 87 |
let session: http2.ClientHttp2Session;
|
| 88 |
return (async () => {
|
|
|
|
| 89 |
logger.info(messages);
|
| 90 |
|
| 91 |
// 提取引用文件URL并上传qwen获得引用的文件ID列表
|
| 92 |
const refFileUrls = extractRefFileUrls(messages);
|
|
|
|
| 93 |
const refs = refFileUrls.length
|
| 94 |
? await Promise.all(
|
| 95 |
refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
|
| 96 |
)
|
| 97 |
: [];
|
|
|
|
| 98 |
// 如果引用对话ID不正确则重置引用
|
| 99 |
if (!/[0-9a-z]{32}/.test(refConvId))
|
| 100 |
refConvId = '';
|
|
|
|
| 101 |
// 请求流
|
| 102 |
const session: http2.ClientHttp2Session = await new Promise(
|
| 103 |
(resolve, reject) => {
|
|
|
|
| 106 |
session.on("error", reject);
|
| 107 |
}
|
| 108 |
);
|
|
|
|
| 109 |
const [sessionId, parentMsgId = ''] = refConvId.split('-');
|
| 110 |
const req = session.request({
|
| 111 |
":method": "POST",
|
|
|
|
| 115 |
...FAKE_HEADERS,
|
| 116 |
Accept: "text/event-stream",
|
| 117 |
});
|
|
|
|
| 118 |
req.setTimeout(120000);
|
| 119 |
req.write(
|
| 120 |
JSON.stringify({
|
| 121 |
mode: "chat",
|
| 122 |
+
model,
|
| 123 |
action: "next",
|
| 124 |
userAction: "chat",
|
| 125 |
requestId: util.uuid(false),
|
|
|
|
| 135 |
req.setEncoding("utf8");
|
| 136 |
const streamStartTime = util.timestamp();
|
| 137 |
// 接收流为输出文本
|
| 138 |
+
const answer = await receiveStream(req, model);
|
|
|
|
| 139 |
session.close();
|
| 140 |
logger.success(
|
| 141 |
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
|
|
|
| 210 |
req.write(
|
| 211 |
JSON.stringify({
|
| 212 |
mode: "chat",
|
| 213 |
+
model,
|
| 214 |
action: "next",
|
| 215 |
userAction: "chat",
|
| 216 |
requestId: util.uuid(false),
|
|
|
|
| 226 |
req.setEncoding("utf8");
|
| 227 |
const streamStartTime = util.timestamp();
|
| 228 |
// 创建转换流将消息格式转换为gpt兼容格式
|
| 229 |
+
return createTransStream(req, model, (convId: string) => {
|
| 230 |
// 关闭请求会话
|
| 231 |
session.close();
|
| 232 |
logger.success(
|
|
|
|
| 280 |
req.write(
|
| 281 |
JSON.stringify({
|
| 282 |
mode: "chat",
|
| 283 |
+
model,
|
| 284 |
action: "next",
|
| 285 |
userAction: "chat",
|
| 286 |
requestId: util.uuid(false),
|
|
|
|
| 428 |
*
|
| 429 |
* @param stream 消息流
|
| 430 |
*/
|
| 431 |
+
async function receiveStream(stream: any, model): Promise<any> {
|
| 432 |
return new Promise((resolve, reject) => {
|
| 433 |
// 消息初始化
|
| 434 |
const data = {
|
| 435 |
id: "",
|
| 436 |
+
model,
|
| 437 |
object: "chat.completion",
|
| 438 |
choices: [
|
| 439 |
{
|
|
|
|
| 511 |
* @param stream 消息流
|
| 512 |
* @param endCallback 传输结束回调
|
| 513 |
*/
|
| 514 |
+
function createTransStream(stream: any, model, endCallback?: Function) {
|
| 515 |
// 消息创建时间
|
| 516 |
const created = util.unixTimestamp();
|
| 517 |
// 创建转换流
|
|
|
|
| 521 |
transStream.write(
|
| 522 |
`data: ${JSON.stringify({
|
| 523 |
id: "",
|
| 524 |
+
model,
|
| 525 |
object: "chat.completion.chunk",
|
| 526 |
choices: [
|
| 527 |
{
|
|
|
|
| 569 |
content += chunk;
|
| 570 |
const data = `data: ${JSON.stringify({
|
| 571 |
id: `${result.sessionId}-${result.msgId}`,
|
| 572 |
+
model,
|
| 573 |
object: "chat.completion.chunk",
|
| 574 |
choices: [
|
| 575 |
{ index: 0, delta: { content: chunk }, finish_reason: null },
|
|
|
|
| 586 |
delta.content += `服务暂时不可用,第三方响应错误:${result.errorCode}`;
|
| 587 |
const data = `data: ${JSON.stringify({
|
| 588 |
id: `${result.sessionId}-${result.msgId}`,
|
| 589 |
+
model,
|
| 590 |
object: "chat.completion.chunk",
|
| 591 |
choices: [
|
| 592 |
{
|
src/api/routes/chat.ts
CHANGED
|
@@ -9,7 +9,6 @@ export default {
|
|
| 9 |
|
| 10 |
post: {
|
| 11 |
"/completions": async (request: Request) => {
|
| 12 |
-
console.log('111111');
|
| 13 |
request
|
| 14 |
.validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
|
| 15 |
.validate("body.messages", _.isArray)
|
|
@@ -20,7 +19,6 @@ export default {
|
|
| 20 |
const token = _.sample(tokens);
|
| 21 |
const { model, conversation_id: convId, messages, stream } = request.body;
|
| 22 |
|
| 23 |
-
console.log('222222');
|
| 24 |
if (stream) {
|
| 25 |
const stream = await chat.createCompletionStream(
|
| 26 |
model,
|
|
@@ -32,7 +30,6 @@ export default {
|
|
| 32 |
type: "text/event-stream",
|
| 33 |
});
|
| 34 |
} else
|
| 35 |
-
console.log('3333333');
|
| 36 |
|
| 37 |
return await chat.createCompletion(
|
| 38 |
model,
|
|
|
|
| 9 |
|
| 10 |
post: {
|
| 11 |
"/completions": async (request: Request) => {
|
|
|
|
| 12 |
request
|
| 13 |
.validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
|
| 14 |
.validate("body.messages", _.isArray)
|
|
|
|
| 19 |
const token = _.sample(tokens);
|
| 20 |
const { model, conversation_id: convId, messages, stream } = request.body;
|
| 21 |
|
|
|
|
| 22 |
if (stream) {
|
| 23 |
const stream = await chat.createCompletionStream(
|
| 24 |
model,
|
|
|
|
| 30 |
type: "text/event-stream",
|
| 31 |
});
|
| 32 |
} else
|
|
|
|
| 33 |
|
| 34 |
return await chat.createCompletion(
|
| 35 |
model,
|