mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-12-23 03:09:19 +08:00
Merge pull request #37 from Yanyutin753/transfer
feat the context transfer files
This commit is contained in:
commit
1e55571b2d
@ -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 }
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user