mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2025-10-14 14:20:36 +00:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e157e40525 | ||
![]() |
d08a4b2130 | ||
![]() |
31298c9566 | ||
![]() |
fe63c20198 | ||
![]() |
72e29e4168 | ||
![]() |
9fd7ae890b | ||
![]() |
f5bea5ea68 | ||
![]() |
0b2c8434c9 | ||
![]() |
520f26f72f | ||
![]() |
462c64656e | ||
![]() |
cda36ed4fc | ||
![]() |
70ea39591b | ||
![]() |
11a145924f | ||
![]() |
1b2b7927ee | ||
![]() |
66cddd522b | ||
![]() |
ff59201961 | ||
![]() |
6853087757 | ||
![]() |
1e09d807e6 | ||
![]() |
66067b4dd9 | ||
![]() |
1534fbc77a | ||
![]() |
1e55571b2d | ||
![]() |
4380d0c05c |
@@ -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
|
||||
|
||||
|
21
README.md
21
README.md
@@ -33,6 +33,7 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
|
||||
* [对话补全](#对话补全)
|
||||
* [文档解读](#文档解读)
|
||||
* [图像解析](#图像解析)
|
||||
* [refresh_token存活检测](#refresh_token存活检测)
|
||||
* [注意事项](#注意事项)
|
||||
* [Nginx反代优化](#Nginx反代优化)
|
||||
|
||||
@@ -379,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反代优化
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kimi-free-api",
|
||||
"version": "0.0.18",
|
||||
"version": "0.0.24",
|
||||
"description": "Kimi Free API Server",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
10
public/welcome.html
Normal file
10
public/welcome.html
Normal 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>
|
@@ -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}`,
|
||||
@@ -711,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
|
||||
};
|
||||
|
@@ -1,7 +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 [
|
||||
{
|
||||
get: {
|
||||
'/': async () => {
|
||||
const content = await fs.readFile('public/welcome.html');
|
||||
return new Response(content, {
|
||||
type: 'html',
|
||||
headers: {
|
||||
Expires: '-1'
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
chat,
|
||||
ping
|
||||
ping,
|
||||
token
|
||||
];
|
25
src/api/routes/token.ts
Normal file
25
src/api/routes/token.ts
Normal 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -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',
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user