Spaces:
Sleeping
Sleeping
Upload index.js
Browse files
index.js
CHANGED
@@ -416,15 +416,15 @@ class MessageProcessor {
|
|
416 |
};
|
417 |
}
|
418 |
|
|
|
|
|
|
|
419 |
return {
|
420 |
...baseResponse,
|
421 |
object: 'chat.completion',
|
422 |
choices: [{
|
423 |
index: 0,
|
424 |
-
message: {
|
425 |
-
role: 'assistant',
|
426 |
-
content: message
|
427 |
-
},
|
428 |
finish_reason: 'stop'
|
429 |
}],
|
430 |
usage: null
|
@@ -509,6 +509,7 @@ async function handleNormalResponse(response, model, res) {
|
|
509 |
|
510 |
try {
|
511 |
const responseText = await response.text();
|
|
|
512 |
const lines = responseText.split('\n');
|
513 |
|
514 |
for (const line of lines) {
|
@@ -517,33 +518,42 @@ async function handleNormalResponse(response, model, res) {
|
|
517 |
const data = line.slice(6);
|
518 |
if (data === '[DONE]') continue;
|
519 |
let linejosn = JSON.parse(data);
|
|
|
|
|
520 |
if (linejosn.response === "token") {
|
521 |
const token = linejosn.token;
|
522 |
if (token && token.length > 0) {
|
523 |
fullResponse += token;
|
524 |
}
|
525 |
} else if (linejosn.response === "modelResponse" && model === 'grok-latest-image') {
|
|
|
526 |
if (linejosn?.modelResponse?.generatedImageUrls) {
|
527 |
imageUrl = linejosn.modelResponse.generatedImageUrls;
|
|
|
528 |
}
|
529 |
}
|
530 |
}
|
531 |
|
532 |
if (imageUrl) {
|
|
|
533 |
const dataImage = await handleImageResponse(imageUrl);
|
|
|
534 |
fullResponse = [{
|
535 |
type: "image_url",
|
536 |
image_url: {
|
537 |
url: dataImage
|
538 |
}
|
539 |
-
}]
|
|
|
540 |
const responseData = MessageProcessor.createChatResponse(fullResponse, model);
|
541 |
res.json(responseData);
|
542 |
} else {
|
|
|
543 |
const responseData = MessageProcessor.createChatResponse(fullResponse, model);
|
544 |
res.json(responseData);
|
545 |
}
|
546 |
} catch (error) {
|
|
|
547 |
Utils.handleError(error, res);
|
548 |
}
|
549 |
}
|
|
|
416 |
};
|
417 |
}
|
418 |
|
419 |
+
// 检查message是否为数组(图片响应的情况)
|
420 |
+
const messageContent = Array.isArray(message) ? message : { role: 'assistant', content: message };
|
421 |
+
|
422 |
return {
|
423 |
...baseResponse,
|
424 |
object: 'chat.completion',
|
425 |
choices: [{
|
426 |
index: 0,
|
427 |
+
message: Array.isArray(message) ? { role: 'assistant', content: message } : messageContent,
|
|
|
|
|
|
|
428 |
finish_reason: 'stop'
|
429 |
}],
|
430 |
usage: null
|
|
|
509 |
|
510 |
try {
|
511 |
const responseText = await response.text();
|
512 |
+
console.log('原始响应文本:', responseText);
|
513 |
const lines = responseText.split('\n');
|
514 |
|
515 |
for (const line of lines) {
|
|
|
518 |
const data = line.slice(6);
|
519 |
if (data === '[DONE]') continue;
|
520 |
let linejosn = JSON.parse(data);
|
521 |
+
console.log('解析的JSON数据:', linejosn);
|
522 |
+
|
523 |
if (linejosn.response === "token") {
|
524 |
const token = linejosn.token;
|
525 |
if (token && token.length > 0) {
|
526 |
fullResponse += token;
|
527 |
}
|
528 |
} else if (linejosn.response === "modelResponse" && model === 'grok-latest-image') {
|
529 |
+
console.log('检测到图片响应:', linejosn.modelResponse);
|
530 |
if (linejosn?.modelResponse?.generatedImageUrls) {
|
531 |
imageUrl = linejosn.modelResponse.generatedImageUrls;
|
532 |
+
console.log('获取到图片URL:', imageUrl);
|
533 |
}
|
534 |
}
|
535 |
}
|
536 |
|
537 |
if (imageUrl) {
|
538 |
+
console.log('开始处理图片URL:', imageUrl);
|
539 |
const dataImage = await handleImageResponse(imageUrl);
|
540 |
+
console.log('处理后的图片数据:', dataImage.substring(0, 100) + '...');
|
541 |
fullResponse = [{
|
542 |
type: "image_url",
|
543 |
image_url: {
|
544 |
url: dataImage
|
545 |
}
|
546 |
+
}];
|
547 |
+
console.log('最终的响应对象:', JSON.stringify(fullResponse));
|
548 |
const responseData = MessageProcessor.createChatResponse(fullResponse, model);
|
549 |
res.json(responseData);
|
550 |
} else {
|
551 |
+
console.log('没有图片URL,返回文本响应:', fullResponse);
|
552 |
const responseData = MessageProcessor.createChatResponse(fullResponse, model);
|
553 |
res.json(responseData);
|
554 |
}
|
555 |
} catch (error) {
|
556 |
+
console.error('处理响应时发生错误:', error);
|
557 |
Utils.handleError(error, res);
|
558 |
}
|
559 |
}
|