mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-12-23 06:49:20 +08:00
feat the context transfer files
This commit is contained in:
parent
b7946835a8
commit
4380d0c05c
@ -328,21 +328,26 @@ 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) {
|
||||||
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;
|
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系列消息格式,多轮对话请完整提供上下文
|
* @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 }
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user