35 Commits

Author SHA1 Message Date
Vinlic
e157e40525 增加refresh_token存活检测 2024-04-10 18:22:00 +08:00
Vinlic
d08a4b2130 Release 0.0.24 2024-04-10 17:57:16 +08:00
Vinlic
31298c9566 update Dockerfile 2024-04-10 17:56:40 +08:00
Vinlic
fe63c20198 Release 0.0.23 2024-04-09 10:47:40 +08:00
Vinlic
72e29e4168 增加日志提醒错误请求地址 2024-04-09 10:47:28 +08:00
Vinlic
9fd7ae890b 首轮不注入注意力prompt 2024-04-08 22:26:05 +08:00
Vinlic
f5bea5ea68 Release 0.0.22 2024-04-08 22:24:13 +08:00
Vinlic
0b2c8434c9 首轮不注入注意力prompt 2024-04-08 22:23:54 +08:00
Vinlic
520f26f72f Release 0.0.21 2024-04-06 00:16:18 +08:00
Vinlic科技
462c64656e Merge pull request #42 from Yanyutin753/master
optimize code in messagesPrepare
2024-04-06 00:09:24 +08:00
Yanyutin753
cda36ed4fc fix the position of "\n" 2024-04-05 19:12:47 +08:00
Yanyutin753
70ea39591b optimize code in messagesPrepare 2024-04-05 18:54:04 +08:00
Vinlic
11a145924f 加大文件上传超时时间 2024-04-05 01:16:05 +08:00
Vinlic
1b2b7927ee Release 0.0.20 2024-04-03 00:00:46 +08:00
Vinlic
66cddd522b 修改日志输出和注意力注入prompt 2024-04-02 23:27:38 +08:00
Vinlic科技
ff59201961 Merge pull request #38 from Yanyutin753/transfer
优化降低传文件上下文混淆问题
2024-04-02 23:17:13 +08:00
Yanyutin753
6853087757 优化降低传文件上下文混淆问题 2024-04-02 23:13:00 +08:00
Yanyutin753
1e09d807e6 打印上传消息日志 2024-04-02 21:15:36 +08:00
Yanyutin753
66067b4dd9 通过添加prompt改善传文件时的上下文问题 2024-04-02 20:54:46 +08:00
Vinlic
1534fbc77a Release 0.0.19 2024-04-01 22:53:18 +08:00
Vinlic科技
1e55571b2d Merge pull request #37 from Yanyutin753/transfer
feat the context transfer files
2024-04-01 22:48:00 +08:00
Yanyutin753
4380d0c05c feat the context transfer files 2024-04-01 22:33:34 +08:00
Vinlic
b7946835a8 Release 0.0.18 2024-04-01 15:46:48 +08:00
Vinlic
4a3168845e Release 0.0.17 2024-04-01 15:46:25 +08:00
Vinlic
ae541f533e 处理有几率遇到�输出的情况 2024-04-01 15:46:12 +08:00
Vinlic
980b506e94 update README 2024-03-31 04:25:02 +08:00
Vinlic
f7b6a9e64a update README 2024-03-31 03:46:36 +08:00
Vinlic
b71e8d4b24 update README 2024-03-29 12:01:24 +08:00
Vinlic
f9daf10455 update README 2024-03-29 11:29:00 +08:00
Vinlic
a387e133fb update README 2024-03-25 04:17:04 +08:00
Vinlic科技
c6e6c7e660 Merge pull request #19 from khazic/master
nb vlao
2024-03-21 19:22:18 +08:00
khazic
ff54eb3ebb nb 2024-03-21 19:16:14 +08:00
Vinlic科技
eccce82ade Merge pull request #16 from peanut996/master
添加health check api
2024-03-20 13:47:06 +08:00
peanut996
4fe9b654f5 添加health check api 2024-03-20 13:46:21 +08:00
Vinlic
7cbebf780c update README 2024-03-20 01:46:22 +08:00
11 changed files with 200 additions and 44 deletions

View File

@@ -8,10 +8,11 @@ RUN npm i --registry http://registry.npmmirror.com && npm run build
FROM node:lts-alpine
COPY --from=BUILD_IMAGE /app/configs ./configs
COPY --from=BUILD_IMAGE /app/package.json ./package.json
COPY --from=BUILD_IMAGE /app/dist ./dist
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
COPY --from=BUILD_IMAGE /app/public /app/public
COPY --from=BUILD_IMAGE /app/configs /app/configs
COPY --from=BUILD_IMAGE /app/package.json /app/package.json
COPY --from=BUILD_IMAGE /app/dist /app/dist
COPY --from=BUILD_IMAGE /app/node_modules /app/node_modules
WORKDIR /app

View File

@@ -9,9 +9,19 @@
与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)
## 目录
* [声明](#声明)
* [免责声明](#免责声明)
* [在线体验](#在线体验)
* [效果示例](#效果示例)
* [接入准备](#接入准备)
@@ -23,16 +33,19 @@
* [对话补全](#对话补全)
* [文档解读](#文档解读)
* [图像解析](#图像解析)
* [refresh_token存活检测](#refresh_token存活检测)
* [注意事项](#注意事项)
* [Nginx反代优化](#Nginx反代优化)
## 声明
## 免责声明
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
## 在线体验
@@ -66,10 +79,6 @@ https://udify.app/chat/Po0F6BMJ15q5vu2P
![响应流畅度一致](https://github.com/LLM-Red-Team/kimi-free-api/assets/20235341/48c7ec00-2b03-46c4-95d0-452d3075219b)
### 100线程并发测试
![100线程并发测试](./doc/example-7.jpg)
## 接入准备
从 [kimi.moonshot.cn](https://kimi.moonshot.cn) 获取refresh_token
@@ -84,7 +93,7 @@ https://udify.app/chat/Po0F6BMJ15q5vu2P
### 多账号接入
目前kimi限制普通账号每3小时内只能进行30轮长文本的问答你可以通过提供多个账号的refresh_token并使用`,`拼接提供:
目前kimi限制普通账号每3小时内只能进行30轮长文本的问答(短文本不限)你可以通过提供多个账号的refresh_token并使用`,`拼接提供:
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
@@ -371,6 +380,26 @@ Authorization: Bearer [refresh_token]
}
```
### refresh_token存活检测
检测refresh_token是否存活如果存活live未true否则为false请不要频繁小于10分钟调用此接口。
**POST /token/check**
请求数据:
```json
{
"token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9..."
}
```
响应数据:
```json
{
"live": true
}
```
## 注意事项
### Nginx反代优化
@@ -392,4 +421,8 @@ keepalive_timeout 120;
### Token统计
由于推理侧不kimi-free-api因此token不可统计将以固定数字返回
由于推理侧不kimi-free-api因此token不可统计将以固定数字返回!!!!!
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=LLM-Red-Team/kimi-free-api&type=Date)](https://star-history.com/#LLM-Red-Team/kimi-free-api&Date)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 98 KiB

View File

@@ -1,6 +1,6 @@
{
"name": "kimi-free-api",
"version": "0.0.16",
"version": "0.0.24",
"description": "Kimi Free API Server",
"type": "module",
"main": "dist/index.js",

10
public/welcome.html Normal file
View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>🚀 服务已启动</title>
</head>
<body>
<p>kimi-free-api已启动<br>请通过LobeChat / NextChat / Dify等客户端或OpenAI SDK接入</p>
</body>
</html>

View File

@@ -299,7 +299,7 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
*/
async function fakeRequest(refreshToken: string) {
const token = await acquireToken(refreshToken);
const options = {
const options = {
headers: {
Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn/`,
@@ -328,21 +328,27 @@ 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']);
});
}
logger.info("本次请求上传:" + urls.length + "个文件");
return urls;
}
/**
@@ -356,17 +362,39 @@ function extractRefFileUrls(messages: any[]) {
* @param messages 参考gpt系列消息格式多轮对话请完整提供上下文
*/
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) => {
if (_.isArray(message.content)) {
if (Array.isArray(message.content)) {
return message.content.reduce((_content, v) => {
if (!_.isObject(v) || v['type'] != 'text')
return _content;
return _content + (v['text'] || '');
if (!_.isObject(v) || v['type'] != 'text') return _content;
return _content + `${message.role || "user"}:${v["text"] || ""}\n`;
}, content);
}
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
}, '');
logger.info("\n对话合并\n" + content);
return [
{ role: 'user', content }
]
@@ -474,8 +502,8 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
data: fileData,
// 100M限制
maxBodyLength: FILE_MAX_SIZE,
// 60秒超时
timeout: 60000,
// 120秒超时
timeout: 120000,
headers: {
'Content-Type': mimeType,
Authorization: `Bearer ${token}`,
@@ -570,8 +598,9 @@ async function receiveStream(model: string, convId: string, stream: any) {
if (_.isError(result))
throw new Error(`Stream response invalid: ${event.data}`);
// 处理消息
if (result.event == 'cmpl') {
data.choices[0].message.content += result.text;
if (result.event == 'cmpl' && 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') {
@@ -632,12 +661,14 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
throw new Error(`Stream response invalid: ${event.data}`);
// 处理消息
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({
id: convId,
model,
object: 'chat.completion.chunk',
choices: [
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + result.text }, finish_reason: null }
{ index: 0, delta: { content: (searchFlag ? '\n' : '') + chunk }, finish_reason: null }
],
created
})}\n\n`;
@@ -708,9 +739,35 @@ function tokenSplit(authorization: string) {
return authorization.replace('Bearer ', '').split(',');
}
/**
* 获取Token存活状态
*/
async function getTokenLiveStatus(refreshToken: string) {
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
headers: {
Authorization: `Bearer ${refreshToken}`,
Referer: 'https://kimi.moonshot.cn/',
...FAKE_HEADERS
},
timeout: 15000,
validateStatus: () => true
});
try {
const {
access_token,
refresh_token
} = checkResult(result, refreshToken);
return !!(access_token && refresh_token)
}
catch(err) {
return false;
}
}
export default {
createConversation,
createCompletion,
createCompletionStream,
getTokenLiveStatus,
tokenSplit
};

View File

@@ -1,5 +1,25 @@
import fs from 'fs-extra';
import Response from '@/lib/response/Response.ts';
import chat from "./chat.ts";
import ping from "./ping.ts";
import token from './token.ts';
export default [
chat
{
get: {
'/': async () => {
const content = await fs.readFile('public/welcome.html');
return new Response(content, {
type: 'html',
headers: {
Expires: '-1'
}
});
}
}
},
chat,
ping,
token
];

6
src/api/routes/ping.ts Normal file
View File

@@ -0,0 +1,6 @@
export default {
prefix: '/ping',
get: {
'': async () => "pong"
}
}

25
src/api/routes/token.ts Normal file
View File

@@ -0,0 +1,25 @@
import _ from 'lodash';
import Request from '@/lib/request/Request.ts';
import Response from '@/lib/response/Response.ts';
import chat from '@/api/controllers/chat.ts';
import logger from '@/lib/logger.ts';
export default {
prefix: '/token',
post: {
'/check': async (request: Request) => {
request
.validate('body.token', _.isString)
const live = await chat.getTokenLiveStatus(request.body.token);
return {
live
}
}
}
}

View File

@@ -15,7 +15,7 @@ export default class FailureBody extends Body {
else if(error instanceof APIException || error instanceof Exception)
({ errcode, errmsg, data, httpStatusCode } = error);
else if(_.isError(error))
error = new Exception(EX.SYSTEM_ERROR, error.message);
({ errcode, errmsg, data, httpStatusCode } = new Exception(EX.SYSTEM_ERROR, error.message));
super({
code: errcode || -1,
message: errmsg || 'Internal error',

View File

@@ -73,7 +73,11 @@ class Server {
this.app.use((ctx: any) => {
const request = new Request(ctx);
logger.debug(`-> ${ctx.request.method} ${ctx.request.url} request is not supported - ${request.remoteIP || "unknown"}`);
const failureBody = new FailureBody(new Exception(EX.SYSTEM_NOT_ROUTE_MATCHING, "Request is not supported"));
// const failureBody = new FailureBody(new Exception(EX.SYSTEM_NOT_ROUTE_MATCHING, "Request is not supported"));
// const response = new Response(failureBody);
const message = `[请求有误]: 正确请求为 POST -> /v1/chat/completions当前请求为 ${ctx.request.method} -> ${ctx.request.url} 请纠正`;
logger.warn(message);
const failureBody = new FailureBody(new Error(message));
const response = new Response(failureBody);
response.injectTo(ctx);
if(config.system.requestLog)