mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2025-10-16 07:20:52 +00:00
Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9fd7ae890b | ||
![]() |
f5bea5ea68 | ||
![]() |
0b2c8434c9 | ||
![]() |
520f26f72f | ||
![]() |
462c64656e | ||
![]() |
cda36ed4fc | ||
![]() |
70ea39591b | ||
![]() |
11a145924f | ||
![]() |
1b2b7927ee | ||
![]() |
66cddd522b | ||
![]() |
ff59201961 | ||
![]() |
6853087757 | ||
![]() |
1e09d807e6 | ||
![]() |
66067b4dd9 | ||
![]() |
1534fbc77a | ||
![]() |
1e55571b2d | ||
![]() |
4380d0c05c | ||
![]() |
b7946835a8 | ||
![]() |
4a3168845e | ||
![]() |
ae541f533e | ||
![]() |
980b506e94 | ||
![]() |
f7b6a9e64a | ||
![]() |
b71e8d4b24 | ||
![]() |
f9daf10455 | ||
![]() |
a387e133fb | ||
![]() |
c6e6c7e660 | ||
![]() |
ff54eb3ebb | ||
![]() |
eccce82ade | ||
![]() |
4fe9b654f5 | ||
![]() |
7cbebf780c |
34
README.md
34
README.md
@@ -9,9 +9,19 @@
|
|||||||
|
|
||||||
与ChatGPT接口完全兼容。
|
与ChatGPT接口完全兼容。
|
||||||
|
|
||||||
|
还有以下四个free-api欢迎关注:
|
||||||
|
|
||||||
|
阶跃星辰 (跃问StepChat) 接口转API [step-free-api](https://github.com/LLM-Red-Team/step-free-api)
|
||||||
|
|
||||||
|
阿里通义 (Qwen) 接口转API [qwen-free-api](https://github.com/LLM-Red-Team/qwen-free-api)
|
||||||
|
|
||||||
|
ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
|
||||||
|
|
||||||
|
聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
* [声明](#声明)
|
* [免责声明](#免责声明)
|
||||||
* [在线体验](#在线体验)
|
* [在线体验](#在线体验)
|
||||||
* [效果示例](#效果示例)
|
* [效果示例](#效果示例)
|
||||||
* [接入准备](#接入准备)
|
* [接入准备](#接入准备)
|
||||||
@@ -26,13 +36,15 @@
|
|||||||
* [注意事项](#注意事项)
|
* [注意事项](#注意事项)
|
||||||
* [Nginx反代优化](#Nginx反代优化)
|
* [Nginx反代优化](#Nginx反代优化)
|
||||||
|
|
||||||
## 声明
|
## 免责声明
|
||||||
|
|
||||||
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
|
**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
|
||||||
|
|
||||||
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
|
|
||||||
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
|
|
||||||
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
|
|
||||||
## 在线体验
|
## 在线体验
|
||||||
|
|
||||||
@@ -66,10 +78,6 @@ https://udify.app/chat/Po0F6BMJ15q5vu2P
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
### 100线程并发测试
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## 接入准备
|
## 接入准备
|
||||||
|
|
||||||
从 [kimi.moonshot.cn](https://kimi.moonshot.cn) 获取refresh_token
|
从 [kimi.moonshot.cn](https://kimi.moonshot.cn) 获取refresh_token
|
||||||
@@ -84,7 +92,7 @@ https://udify.app/chat/Po0F6BMJ15q5vu2P
|
|||||||
|
|
||||||
### 多账号接入
|
### 多账号接入
|
||||||
|
|
||||||
目前kimi限制普通账号每3小时内只能进行30轮长文本的问答,你可以通过提供多个账号的refresh_token并使用`,`拼接提供:
|
目前kimi限制普通账号每3小时内只能进行30轮长文本的问答(短文本不限),你可以通过提供多个账号的refresh_token并使用`,`拼接提供:
|
||||||
|
|
||||||
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
|
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
|
||||||
|
|
||||||
@@ -392,4 +400,8 @@ keepalive_timeout 120;
|
|||||||
|
|
||||||
### Token统计
|
### Token统计
|
||||||
|
|
||||||
由于推理侧不再kimi-free-api,因此token不可统计,将以固定数字返回。
|
由于推理侧不在kimi-free-api,因此token不可统计,将以固定数字返回!!!!!
|
||||||
|
|
||||||
|
## Star History
|
||||||
|
|
||||||
|
[](https://star-history.com/#LLM-Red-Team/kimi-free-api&Date)
|
Binary file not shown.
Before Width: | Height: | Size: 98 KiB |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kimi-free-api",
|
"name": "kimi-free-api",
|
||||||
"version": "0.0.16",
|
"version": "0.0.22",
|
||||||
"description": "Kimi Free API Server",
|
"description": "Kimi Free API Server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@@ -299,7 +299,7 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
|||||||
*/
|
*/
|
||||||
async function fakeRequest(refreshToken: string) {
|
async function fakeRequest(refreshToken: string) {
|
||||||
const token = await acquireToken(refreshToken);
|
const token = await acquireToken(refreshToken);
|
||||||
const options = {
|
const options = {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
@@ -328,21 +328,27 @@ 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']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
logger.info("本次请求上传:" + urls.length + "个文件");
|
||||||
|
return urls;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -356,17 +362,39 @@ function extractRefFileUrls(messages: any[]) {
|
|||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
*/
|
*/
|
||||||
function messagesPrepare(messages: any[]) {
|
function messagesPrepare(messages: any[]) {
|
||||||
|
// 注入消息提升注意力
|
||||||
|
let latestMessage = messages[messages.length - 1];
|
||||||
|
let hasFileOrImage = Array.isArray(latestMessage.content)
|
||||||
|
&& latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])));
|
||||||
|
// 第二轮开始注入system prompt
|
||||||
|
if (messages.length > 2) {
|
||||||
|
if (hasFileOrImage) {
|
||||||
|
let newFileMessage = {
|
||||||
|
"content": "关注用户最新发送文件和消息",
|
||||||
|
"role": "system"
|
||||||
|
};
|
||||||
|
messages.splice(messages.length - 1, 0, newFileMessage);
|
||||||
|
logger.info("注入提升尾部文件注意力system prompt");
|
||||||
|
} else {
|
||||||
|
let newTextMessage = {
|
||||||
|
"content": "关注用户最新的消息",
|
||||||
|
"role": "system"
|
||||||
|
};
|
||||||
|
messages.splice(messages.length - 1, 0, newTextMessage);
|
||||||
|
logger.info("注入提升尾部消息注意力system prompt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const content = messages.reduce((content, message) => {
|
const content = messages.reduce((content, message) => {
|
||||||
if (_.isArray(message.content)) {
|
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;
|
return _content + `${message.role || "user"}:${v["text"] || ""}\n`;
|
||||||
return _content + (v['text'] || '');
|
|
||||||
}, content);
|
}, content);
|
||||||
}
|
}
|
||||||
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
|
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
|
||||||
}, '');
|
}, '');
|
||||||
|
logger.info("\n对话合并:\n" + content);
|
||||||
return [
|
return [
|
||||||
{ role: 'user', content }
|
{ role: 'user', content }
|
||||||
]
|
]
|
||||||
@@ -474,8 +502,8 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
|
|||||||
data: fileData,
|
data: fileData,
|
||||||
// 100M限制
|
// 100M限制
|
||||||
maxBodyLength: FILE_MAX_SIZE,
|
maxBodyLength: FILE_MAX_SIZE,
|
||||||
// 60秒超时
|
// 120秒超时
|
||||||
timeout: 60000,
|
timeout: 120000,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': mimeType,
|
'Content-Type': mimeType,
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
@@ -570,8 +598,9 @@ async function receiveStream(model: string, convId: string, stream: any) {
|
|||||||
if (_.isError(result))
|
if (_.isError(result))
|
||||||
throw new Error(`Stream response invalid: ${event.data}`);
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
// 处理消息
|
// 处理消息
|
||||||
if (result.event == 'cmpl') {
|
if (result.event == 'cmpl' && result.text) {
|
||||||
data.choices[0].message.content += result.text;
|
const exceptCharIndex = result.text.indexOf("<22>");
|
||||||
|
data.choices[0].message.content += result.text.substring(0, exceptCharIndex == -1 ? result.text.length : exceptCharIndex);
|
||||||
}
|
}
|
||||||
// 处理结束或错误
|
// 处理结束或错误
|
||||||
else if (result.event == 'all_done' || result.event == 'error') {
|
else if (result.event == 'all_done' || result.event == 'error') {
|
||||||
@@ -632,12 +661,14 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
|
|||||||
throw new Error(`Stream response invalid: ${event.data}`);
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
// 处理消息
|
// 处理消息
|
||||||
if (result.event == 'cmpl') {
|
if (result.event == 'cmpl') {
|
||||||
|
const exceptCharIndex = result.text.indexOf("<22>");
|
||||||
|
const chunk = result.text.substring(0, exceptCharIndex == -1 ? result.text.length : exceptCharIndex);
|
||||||
const data = `data: ${JSON.stringify({
|
const data = `data: ${JSON.stringify({
|
||||||
id: convId,
|
id: convId,
|
||||||
model,
|
model,
|
||||||
object: 'chat.completion.chunk',
|
object: 'chat.completion.chunk',
|
||||||
choices: [
|
choices: [
|
||||||
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + result.text }, finish_reason: null }
|
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + chunk }, finish_reason: null }
|
||||||
],
|
],
|
||||||
created
|
created
|
||||||
})}\n\n`;
|
})}\n\n`;
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
import chat from "./chat.ts";
|
import chat from "./chat.ts";
|
||||||
|
import ping from "./ping.ts";
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
chat
|
chat,
|
||||||
|
ping
|
||||||
];
|
];
|
6
src/api/routes/ping.ts
Normal file
6
src/api/routes/ping.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export default {
|
||||||
|
prefix: '/ping',
|
||||||
|
get: {
|
||||||
|
'': async () => "pong"
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user