Merge pull request #37 from Yanyutin753/transfer

feat the context transfer files
This commit is contained in:
Vinlic科技 2024-04-01 22:48:00 +08:00 committed by GitHub
commit 1e55571b2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -328,9 +328,15 @@ async function fakeRequest(refreshToken: string) {
* @param messages gpt系列消息格式 * @param messages gpt系列消息格式
*/ */
function extractRefFileUrls(messages: any[]) { function extractRefFileUrls(messages: any[]) {
return messages.reduce((urls, message) => { const urls = [];
if (_.isArray(message.content)) { // 如果没有消息,则返回[]
message.content.forEach(v => { if (!messages.length) {
return urls;
}
// 只获取最新的消息
const lastMessage = messages[messages.length - 1];
if (_.isArray(lastMessage.content)) {
lastMessage.content.forEach(v => {
if (!_.isObject(v) || !['file', 'image_url'].includes(v['type'])) if (!_.isObject(v) || !['file', 'image_url'].includes(v['type']))
return; return;
// kimi-free-api支持格式 // kimi-free-api支持格式
@ -342,7 +348,6 @@ function extractRefFileUrls(messages: any[]) {
}); });
} }
return urls; return urls;
}, []);
} }
/** /**
@ -356,8 +361,16 @@ function extractRefFileUrls(messages: any[]) {
* @param messages gpt系列消息格式 * @param messages gpt系列消息格式
*/ */
function messagesPrepare(messages: any[]) { function messagesPrepare(messages: any[]) {
const content = messages.reduce((content, message) => { // 只保留最新消息以及不包含"type": "image_url"或"type": "file"的消息
if (_.isArray(message.content)) { let validMessages = messages.filter((message, index) => {
if (index === messages.length - 1) return true;
if (!Array.isArray(message.content)) return true;
// 不含"type": "image_url"或"type": "file"的消息保留
return !message.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])));
});
const content = validMessages.reduce((content, message) => {
if (Array.isArray(message.content)) {
return message.content.reduce((_content, v) => { return message.content.reduce((_content, v) => {
if (!_.isObject(v) || v['type'] != 'text') if (!_.isObject(v) || v['type'] != 'text')
return _content; return _content;
@ -366,7 +379,6 @@ function messagesPrepare(messages: any[]) {
} }
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`; return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
}, ''); }, '');
return [ return [
{ role: 'user', content } { role: 'user', content }
] ]