支持与gpt4v兼容的图像解析和文件解读,增加usage字段兼容,完善文件处理流程,补充注释

This commit is contained in:
Vinlic 2024-03-11 10:25:56 +08:00
parent 99e1a4812a
commit 438830bc01
2 changed files with 255 additions and 39 deletions

View File

@ -3,5 +3,6 @@ export default {
API_REQUEST_PARAMS_INVALID: [-2000, '请求参数非法'],
API_REQUEST_FAILED: [-2001, '请求失败'],
API_TOKEN_EXPIRES: [-2002, 'Token已失效'],
API_FILE_URL_INVALID: [-2003, '远程文件URL非法']
API_FILE_URL_INVALID: [-2003, '远程文件URL非法'],
API_FILE_EXECEEDS_SIZE: [-2004, '远程文件超出大小']
}

View File

@ -10,15 +10,29 @@ import { createParser } from 'eventsource-parser'
import logger from '@/lib/logger.ts';
import util from '@/lib/util.ts';
// 模型名称
const MODEL_NAME = 'kimi';
// access_token有效期
const ACCESS_TOKEN_EXPIRES = 300;
// 伪装headers
const FAKE_HEADERS = {
'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
};
// 文件最大大小
const FILE_MAX_SIZE = 100 * 1024 * 1024;
// access_token映射
const accessTokenMap = new Map();
/**
* access_token
*
* 使refresh_token去刷新获得access_token
*
* @param refreshToken access_token的refresh_token
*/
async function requestToken(refreshToken: string) {
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
headers: {
@ -26,6 +40,7 @@ async function requestToken(refreshToken: string) {
Referer: 'https://kimi.moonshot.cn',
...FAKE_HEADERS
},
timeout: 15000,
validateStatus: () => true
});
const {
@ -39,6 +54,13 @@ async function requestToken(refreshToken: string) {
}
}
/**
* access_token
*
* token
*
* @param refreshToken access_token的refresh_token
*/
async function acquireToken(refreshToken: string): Promise<string> {
let result = accessTokenMap.get(refreshToken);
if (!result) {
@ -50,6 +72,13 @@ async function acquireToken(refreshToken: string): Promise<string> {
return result.accessToken;
}
/**
*
*
*
*
* @param refreshToken access_token的refresh_token
*/
async function createConversation(name: string, refreshToken: string) {
const token = await acquireToken(refreshToken);
const result = await axios.post('https://kimi.moonshot.cn/api/chat', {
@ -61,6 +90,7 @@ async function createConversation(name: string, refreshToken: string) {
Referer: 'https://kimi.moonshot.cn',
...FAKE_HEADERS
},
timeout: 15000,
validateStatus: () => true
});
const {
@ -69,6 +99,13 @@ async function createConversation(name: string, refreshToken: string) {
return convId;
}
/**
*
*
*
*
* @param refreshToken access_token的refresh_token
*/
async function removeConversation(convId: string, refreshToken: string) {
const token = await acquireToken(refreshToken);
const result = await axios.delete(`https://kimi.moonshot.cn/api/chat/${convId}`, {
@ -77,17 +114,36 @@ async function removeConversation(convId: string, refreshToken: string) {
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
...FAKE_HEADERS
},
timeout: 15000,
validateStatus: () => true
});
checkResult(result, refreshToken);
}
/**
*
*
* @param messages gpt系列消息格式
* @param refreshToken access_token的refresh_token
* @param useSearch
*/
async function createCompletion(messages: any[], refreshToken: string, useSearch = true) {
logger.info(messages);
// 提取引用文件URL并上传kimi获得引用的文件ID列表
const refFileUrls = extractRefFileUrls(messages);
const refs = refFileUrls.length ? await Promise.all(refFileUrls.map(fileUrl => uploadFile(fileUrl, refreshToken))) : [];
// 创建会话
const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken);
// 请求流
const token = await acquireToken(refreshToken);
console.log({
messages: messagesPrepare(messages),
refs,
use_search: useSearch
})
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
messages: messagesPrepare(messages),
refs,
@ -98,26 +154,48 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
...FAKE_HEADERS
},
// 120秒超时
timeout: 120000,
validateStatus: () => true,
responseType: 'stream'
});
// 接收流为输出文本
const answer = await receiveStream(convId, result.data);
// 异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
removeConversation(convId, refreshToken)
.catch(err => console.error(err));
.catch(err => console.error(err));
return answer;
}
/**
*
*
* @param messages gpt系列消息格式
* @param refreshToken access_token的refresh_token
* @param useSearch
*/
async function createCompletionStream(messages: any[], refreshToken: string, useSearch = true) {
logger.info(messages);
// 提取引用文件URL并上传kimi获得引用的文件ID列表
const refFileUrls = extractRefFileUrls(messages);
const refs = refFileUrls.length ? await Promise.all(refFileUrls.map(fileUrl => uploadFile(fileUrl, refreshToken))) : [];
// 创建会话
const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken);
// 请求流
const token = await acquireToken(refreshToken);
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
messages: messagesPrepare(messages),
refs,
use_search: useSearch
}, {
// 120秒超时
timeout: 120000,
headers: {
Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
@ -126,25 +204,57 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
validateStatus: () => true,
responseType: 'stream'
});
// 创建转换流将消息格式转换为gpt兼容格式
return createTransStream(convId, result.data, () => {
// 流传输结束后异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
removeConversation(convId, refreshToken)
.catch(err => console.error(err));
});
}
/**
* URL
*
* @param messages gpt系列消息格式
*/
function extractRefFileUrls(messages: any[]) {
return messages.reduce((urls, message) => {
if(message.type != 'file' || !message.url)
return urls;
urls.push(message.url);
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']);
});
}
return urls;
}, []);
}
/**
*
*
*
* user:旧消息1
* assistant:旧消息2
* user:新消息
*
* @param messages gpt系列消息格式
*/
function messagesPrepare(messages: any[]) {
const content = messages.reduce((content, message) => {
if(message.type == 'file')
return content;
if (_.isArray(message.content)) {
return message.content.reduce((_content, v) => {
if(!_.isObject(v) || v['type'] != 'text')
return _content;
return _content + (v['text'] || '');
}, content);
}
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
}, '');
@ -153,16 +263,30 @@ function messagesPrepare(messages: any[]) {
]
}
/**
* URL包装为HTML标签
*
* kimi网页版中会自动将url包装为url标签用于处理状态仿
*
* @param content
*/
function wrapUrlsToTags(content: string) {
return content.replace(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gi, url => `<url id="" type="url" status="" title="" wc="">${url}</url>`);
}
async function preSignUrl(fileName: string, refreshToken: string) {
/**
* URL
*
* @param filename
* @param refreshToken access_token的refresh_token
*/
async function preSignUrl(filename: string, refreshToken: string) {
const token = await acquireToken(refreshToken);
const result = await axios.post('https://kimi.moonshot.cn/api/pre-sign-url', {
action: 'file',
name: fileName
action: 'file',
name: filename
}, {
timeout: 15000,
headers: {
Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn`,
@ -173,30 +297,64 @@ async function preSignUrl(fileName: string, refreshToken: string) {
return checkResult(result, refreshToken);
}
/**
* URL有效性
*
* @param fileUrl URL
*/
async function checkFileUrl(fileUrl: string) {
const result = await axios.head(fileUrl, {
timeout: 15000,
validateStatus: () => true
});
return result.status == 200 ? true : false;
if(result.status >= 400)
throw new APIException(EX.API_FILE_URL_INVALID, `File ${fileUrl} is not valid: [${result.status}] ${result.statusText}`);
// 检查文件大小
if (result.headers && result.headers['content-length']) {
const fileSize = parseInt(result.headers['content-length'], 10);
if(fileSize > FILE_MAX_SIZE)
throw new APIException(EX.API_FILE_EXECEEDS_SIZE, `File ${fileUrl} is not valid`);
}
}
/**
*
*
* @param fileUrl URL
* @param refreshToken access_token的refresh_token
*/
async function uploadFile(fileUrl: string, refreshToken: string) {
if(!await checkFileUrl(fileUrl))
throw new APIException(EX.API_FILE_URL_INVALID, `File ${fileUrl} is not valid`);
const fileName = path.basename(fileUrl);
// 预检查远程文件URL可用性
await checkFileUrl(fileUrl);
// 下载文件到内存,如果您的服务器内存很小,建议考虑改造为流直传到下一个接口上,避免停留占用内存
const filename = path.basename(fileUrl);
const { data: fileData } = await axios.get(fileUrl, {
responseType: 'arraybuffer'
responseType: 'arraybuffer',
// 100M限制
maxContentLength: FILE_MAX_SIZE,
// 60秒超时
timeout: 60000
});
// 获取预签名文件URL
const {
url: uploadUrl,
object_name: objectName
} = await preSignUrl(fileName, refreshToken);
const mimeType = mime.getType(fileName);
} = await preSignUrl(filename, refreshToken);
// 获取文件的MIME类型
const mimeType = mime.getType(filename);
// 上传文件到目标OSS
const token = await acquireToken(refreshToken);
let result = await axios.request({
method: 'PUT',
url: uploadUrl,
data: fileData,
// 100M限制
maxBodyLength: FILE_MAX_SIZE,
// 60秒超时
timeout: 60000,
headers: {
'Content-Type': mimeType,
Authorization: `Bearer ${token}`,
@ -206,10 +364,13 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
validateStatus: () => true
});
checkResult(result, refreshToken);
// 获取文件上传结果
result = await axios.post('https://kimi.moonshot.cn/api/file', {
type: 'file',
name: fileName,
object_name: objectName
name: filename,
object_name: objectName,
timeout: 15000
}, {
headers: {
Authorization: `Bearer ${token}`,
@ -218,11 +379,31 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
}
});
const { id: fileId } = checkResult(result, refreshToken);
// 处理文件转换
result = await axios.post('https://kimi.moonshot.cn/api/file/parse_process', {
ids: [fileId],
timeout: 120000
}, {
headers: {
Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn`,
...FAKE_HEADERS
}
});
checkResult(result, refreshToken);
return fileId;
}
/**
*
*
* @param result
* @param refreshToken access_token的refresh_token
*/
function checkResult(result: AxiosResponse, refreshToken: string) {
if(result.status == 401) {
if (result.status == 401) {
accessTokenMap.delete(refreshToken);
throw new APIException(EX.API_REQUEST_FAILED);
}
@ -236,33 +417,45 @@ function checkResult(result: AxiosResponse, refreshToken: string) {
throw new APIException(EX.API_REQUEST_FAILED, `[请求kimi失败]: ${message}`);
}
/**
*
*
* @param convId ID
* @param stream
*/
async function receiveStream(convId: string, stream: any) {
return new Promise((resolve, reject) => {
// 第一条消息初始化
const data = {
id: convId,
model: 'kimi',
model: MODEL_NAME,
object: 'chat.completion',
choices: [
{ index: 0, message: { role: 'assistant', content: '' }, finish_reason: 'stop' }
],
created: parseInt(performance.now() as any)
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
created: util.unixTimestamp()
};
let refContent = '';
const parser = createParser(event => {
try {
if (event.type !== "event") return;
// 解析JSON
const result = _.attempt(() => JSON.parse(event.data));
if (_.isError(result))
throw new Error(`stream response invalid: ${event.data}`);
throw new Error(`Stream response invalid: ${event.data}`);
// 处理消息
if (result.event == 'cmpl') {
data.choices[0].message.content += result.text;
}
// 处理结束或错误
else if (result.event == 'all_done' || result.event == 'error') {
data.choices[0].message.content += (result.event == 'error' ? '\n[内容由于不合规被停止生成,我们换个话题吧]' : '') + (refContent ? `\n\n搜索结果来自\n${refContent}` : '');
refContent = '';
resolve(data);
}
else if(result.event == 'search_plus' && result.msg && result.msg.type == 'get_res')
// 处理联网搜索
else if (result.event == 'search_plus' && result.msg && result.msg.type == 'get_res')
refContent += `${result.msg.title}(${result.msg.url})\n`;
// else
// logger.warn(result.event, result);
@ -272,19 +465,31 @@ async function receiveStream(convId: string, stream: any) {
reject(err);
}
});
// 将流数据喂给SSE转换器
stream.on("data", buffer => parser.feed(buffer.toString()));
stream.once("error", err => reject(err));
stream.once("close", () => resolve(data));
});
}
/**
*
*
* gpt兼容流格式
*
* @param convId ID
* @param stream
* @param endCallback
*/
function createTransStream(convId: string, stream: any, endCallback?: Function) {
const created = parseInt(performance.now() as any);
// 消息创建时间
const created = util.unixTimestamp();
// 创建转换流
const transStream = new PassThrough();
let searchFlag = false;
!transStream.closed && transStream.write(`data: ${JSON.stringify({
id: convId,
model: 'kimi',
model: MODEL_NAME,
object: 'chat.completion.chunk',
choices: [
{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }
@ -294,50 +499,59 @@ function createTransStream(convId: string, stream: any, endCallback?: Function)
const parser = createParser(event => {
try {
if (event.type !== "event") return;
// 解析JSON
const result = _.attempt(() => JSON.parse(event.data));
if (_.isError(result))
throw new Error(`stream response invalid: ${event.data}`);
throw new Error(`Stream response invalid: ${event.data}`);
// 处理消息
if (result.event == 'cmpl') {
const data = `data: ${JSON.stringify({
id: convId,
model: 'kimi',
model: MODEL_NAME,
object: 'chat.completion.chunk',
choices: [
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + result.text }, finish_reason: null }
],
created
})}\n\n`;
if(searchFlag)
if (searchFlag)
searchFlag = false;
!transStream.closed && transStream.write(data);
}
// 处理结束或错误
else if (result.event == 'all_done' || result.event == 'error') {
const data = `data: ${JSON.stringify({
id: convId,
model: 'kimi',
model: MODEL_NAME,
object: 'chat.completion.chunk',
choices: [
{ index: 0, delta: result.event == 'error' ? {
content: '\n[内容由于不合规被停止生成,我们换个话题吧]'
} : {}, finish_reason: 'stop' }
{
index: 0, delta: result.event == 'error' ? {
content: '\n[内容由于不合规被停止生成,我们换个话题吧]'
} : {}, finish_reason: 'stop'
}
],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
created
})}\n\n`;
!transStream.closed && transStream.write(data);
!transStream.closed && transStream.end('data: [DONE]\n\n');
endCallback && endCallback();
}
else if(result.event == 'search_plus' && result.msg && result.msg.type == 'get_res') {
if(!searchFlag)
// 处理联网搜索
else if (result.event == 'search_plus' && result.msg && result.msg.type == 'get_res') {
if (!searchFlag)
searchFlag = true;
const data = `data: ${JSON.stringify({
id: convId,
model: 'kimi',
model: MODEL_NAME,
object: 'chat.completion.chunk',
choices: [
{ index: 0, delta: {
content: `检索 ${result.msg.title}(${result.msg.url}) ...\n`
}, finish_reason: null }
{
index: 0, delta: {
content: `检索 ${result.msg.title}(${result.msg.url}) ...\n`
}, finish_reason: null
}
],
created
})}\n\n`;
@ -351,6 +565,7 @@ function createTransStream(convId: string, stream: any, endCallback?: Function)
!transStream.closed && transStream.end('\n\n');
}
});
// 将流数据喂给SSE转换器
stream.on("data", buffer => parser.feed(buffer.toString()));
stream.once("error", () => !transStream.closed && transStream.end('data: [DONE]\n\n'));
stream.once("close", () => !transStream.closed && transStream.end('data: [DONE]\n\n'));