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,21 +328,26 @@ async function fakeRequest(refreshToken: string) {
* @param messages gpt系列消息格式
*/
function extractRefFileUrls(messages: any[]) {
return messages.reduce((urls, message) => {
if (_.isArray(message.content)) {
message.content.forEach(v => {
if (!_.isObject(v) || !['file', 'image_url'].includes(v['type']))
return;
// kimi-free-api支持格式
if (v['type'] == 'file' && _.isObject(v['file_url']) && _.isString(v['file_url']['url']))
urls.push(v['file_url']['url']);
// 兼容gpt-4-vision-preview API格式
else if (v['type'] == 'image_url' && _.isObject(v['image_url']) && _.isString(v['image_url']['url']))
urls.push(v['image_url']['url']);
});
}
const urls = [];
// 如果没有消息,则返回[]
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']))
return;
// kimi-free-api支持格式
if (v['type'] == 'file' && _.isObject(v['file_url']) && _.isString(v['file_url']['url']))
urls.push(v['file_url']['url']);
// 兼容gpt-4-vision-preview API格式
else if (v['type'] == 'image_url' && _.isObject(v['image_url']) && _.isString(v['image_url']['url']))
urls.push(v['image_url']['url']);
});
}
return urls;
}
/**
@ -356,8 +361,16 @@ function extractRefFileUrls(messages: any[]) {
* @param messages gpt系列消息格式
*/
function messagesPrepare(messages: any[]) {
const content = messages.reduce((content, message) => {
if (_.isArray(message.content)) {
// 只保留最新消息以及不包含"type": "image_url"或"type": "file"的消息
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) => {
if (!_.isObject(v) || v['type'] != 'text')
return _content;
@ -366,7 +379,6 @@ function messagesPrepare(messages: any[]) {
}
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
}, '');
return [
{ role: 'user', content }
]