mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2025-10-14 22:34:40 +00:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
99e1a4812a | ||
![]() |
04c399eeef | ||
![]() |
1f806a6b16 | ||
![]() |
4993d043ab | ||
![]() |
dc7a953e4f | ||
![]() |
3dbabb0d47 |
9
.dockerignore
Normal file
9
.dockerignore
Normal file
@@ -0,0 +1,9 @@
|
||||
logs
|
||||
dist
|
||||
doc
|
||||
node_modules
|
||||
.vscode
|
||||
.git
|
||||
.gitignore
|
||||
README.md
|
||||
*.tar.gz
|
20
Dockerfile
Normal file
20
Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
FROM node:lts AS BUILD_IMAGE
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . /app
|
||||
|
||||
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
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["npm", "start"]
|
145
README.md
145
README.md
@@ -1,5 +1,7 @@
|
||||
# KIMI AI Free 服务
|
||||
|
||||
支持流式输出、支持文件解读。
|
||||
|
||||
## 声明
|
||||
|
||||
仅限自用,禁止对外提供服务,否则风险自担!
|
||||
@@ -21,7 +23,111 @@ https://udify.app/chat/Po0F6BMJ15q5vu2P
|
||||
|
||||
从 [kimi.moonshot.cn](https://kimi.moonshot.cn) 获取refresh_token:进入kimi随便发起一个对话,然后F12打开开发者工具,从Application > Local Storage中找到refresh_token的值,这将作为API_KEY。
|
||||
|
||||
## 安装
|
||||
## 对话补全接口
|
||||
|
||||
目前支持与openai兼容的 `/v1/chat/completions` 接口,可自行使用与openai或其他兼容的客户端接入接口,或者使用dify线上服务接入使用。
|
||||
|
||||
POST /v1/chat/completions
|
||||
|
||||
header 需要设置 Authorization 头部:
|
||||
|
||||
```
|
||||
Authorization: Bearer [refresh_token]
|
||||
```
|
||||
|
||||
请求数据:
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "测试"
|
||||
}
|
||||
],
|
||||
// 是否开启联网搜索,默认false
|
||||
"use_search": true,
|
||||
// 如果使用SSE流请设置为true,默认false
|
||||
"stream": false
|
||||
}
|
||||
```
|
||||
|
||||
### 文件上传解读
|
||||
|
||||
请求数据:
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"type": "file",
|
||||
"url": "https://mj101-1317487292.cos.ap-shanghai.myqcloud.com/ai/test.pdf"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "文档里说了什么?"
|
||||
}
|
||||
],
|
||||
// 建议关闭联网搜索,防止干扰解读结果
|
||||
"use_search": false
|
||||
}
|
||||
```
|
||||
|
||||
响应数据:
|
||||
```json
|
||||
{
|
||||
"id": "cnmuo7mcp7f9hjcmihn0",
|
||||
"model": "kimi",
|
||||
"object": "chat.completion",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "文档中包含了几个古代魔法咒语的例子,这些咒语来自古希腊和罗马时期的魔法文本,被称为PGM(Papyri Graecae Magicae)。以下是文档中提到的几个咒语的内容:\n\n1. 第一个咒语(PMG 4.1390 – 1495)描述了一个仪式,要求留下一些你吃剩的面包,将其分成七块小片,然后去到英雄、角斗士和那些死于非命的人被杀的地方。对面包片念咒并扔出去,然后从仪式地点捡起一些被污染的泥土扔进你心仪的女人的家中,之后去睡觉。咒语的内容是向命运女神(Moirai)、罗马的命运女神(Fates)和自然力量(Daemons)祈求,希望他们帮助实现愿望。\n\n2. 第二个咒语(PMG 4.1342 – 57)是一个召唤咒语,通过念出一系列神秘的名字和词语来召唤一个名为Daemon的存在,以使一个名为Tereous的人(由Apia所生)受到精神和情感上的折磨,直到她来到施法者Didymos(由Taipiam所生)的身边。\n\n3. 第三个咒语(PGM 4.1265 – 74)提到了一个名为NEPHERIĒRI的神秘名字,这个名字与爱神阿佛洛狄忒(Aphrodite)有关。为了赢得一个美丽女人的心,需要保持三天的纯洁,献上乳香,并在献祭时念出这个名字。然后,在接近那位女士时,心中默念这个名字七次,连续七天这样做,以期成功。\n\n4. 第四个咒语(PGM 4.1496 – 1)描述了在燃烧没药(myrrh)时念诵的咒语。这个咒语是向没药祈祷,希望它能够像“肉食者”和“心灵点燃者”一样,吸引一个名为[名字]的女人(她的母亲名为[名字]),让她无法安坐、饮食、注视或亲吻其他人,而是让她的心中只有施法者,直到她来到施法者身边。\n\n这些咒语反映了古代人们对魔法和超自然力量的信仰,以及他们试图通过这些咒语来影响他人情感和行为的方式。"
|
||||
},
|
||||
"finish_reason": "stop"
|
||||
}
|
||||
],
|
||||
"created": 100920
|
||||
}
|
||||
```
|
||||
|
||||
## Docker部署
|
||||
|
||||
请准备一台具有公网IP的服务器并将8000端口开放。
|
||||
|
||||
拉取镜像
|
||||
|
||||
```shell
|
||||
docker pull vinlic/kimi-free-api:latest
|
||||
```
|
||||
|
||||
启动服务
|
||||
|
||||
```shell
|
||||
docker run -it -d --init --name kimi-free-api -p 8000:8000 vinlic/kimi-free-api:latest
|
||||
```
|
||||
|
||||
查看服务实时日志
|
||||
|
||||
```shell
|
||||
docker logs -f kimi-free-api
|
||||
```
|
||||
|
||||
重启服务
|
||||
|
||||
```shell
|
||||
docker restart kimi-free-api
|
||||
```
|
||||
|
||||
停止服务
|
||||
|
||||
```shell
|
||||
docker stop kimi-free-api
|
||||
```
|
||||
|
||||
## 原生部署
|
||||
|
||||
请准备一台具有公网IP的服务器并将8000端口开放。
|
||||
|
||||
请先安装好Node.js环境并且配置好环境变量,确认node命令可用。
|
||||
|
||||
@@ -65,39 +171,4 @@ pm2 reload kimi-free-api
|
||||
|
||||
```shell
|
||||
pm2 stop kimi-free-api
|
||||
```
|
||||
|
||||
## 发起请求
|
||||
|
||||
目前支持与openai兼容的 `/v1/chat/completions` 接口
|
||||
|
||||
POST /v1/chat/completions
|
||||
|
||||
header 需要设置 Authorization 头部:
|
||||
|
||||
```
|
||||
Authorization: Bearer [refresh_token]
|
||||
```
|
||||
|
||||
JSON数据:
|
||||
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "测试"
|
||||
}
|
||||
],
|
||||
// 如果使用SSE流请设置为true
|
||||
"stream": false
|
||||
}
|
||||
```
|
||||
|
||||
## 部署
|
||||
|
||||
请准备一台具有公网IP的服务器,按照[安装](#安装)步骤部署好服务并将8000端口开放。
|
||||
|
||||
自行使用与openai或其他兼容的客户端接入接口。
|
||||
|
||||
或者使用dify线上服务接入使用。
|
||||
```
|
@@ -1,8 +0,0 @@
|
||||
chatCompletion:
|
||||
provider: zhipuai
|
||||
model: glm-4
|
||||
apiKey: 99b6167d7f421fc2187785503d2ffe9f.WncREpjXE26ediTw
|
||||
contextLength: 131072
|
||||
maxToken: 8192
|
||||
concurrencyLimit: 5
|
||||
waitReponseTimeout: 30000
|
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "kimi-free-api",
|
||||
"version": "0.0.5",
|
||||
"description": "Kimi Free Server",
|
||||
"version": "0.0.6",
|
||||
"description": "Kimi Free API Server",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.mjs",
|
||||
|
@@ -2,5 +2,6 @@ export default {
|
||||
API_TEST: [-9999, 'API异常错误'],
|
||||
API_REQUEST_PARAMS_INVALID: [-2000, '请求参数非法'],
|
||||
API_REQUEST_FAILED: [-2001, '请求失败'],
|
||||
API_TOKEN_EXPIRES: [-2002, 'Token已失效']
|
||||
API_TOKEN_EXPIRES: [-2002, 'Token已失效'],
|
||||
API_FILE_URL_INVALID: [-2003, '远程文件URL非法']
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
import { PassThrough } from "stream";
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import mime from 'mime';
|
||||
import axios, { AxiosResponse } from 'axios';
|
||||
|
||||
import APIException from "@/lib/exceptions/APIException.ts";
|
||||
@@ -82,10 +84,13 @@ async function removeConversation(convId: string, refreshToken: string) {
|
||||
|
||||
async function createCompletion(messages: any[], refreshToken: string, useSearch = true) {
|
||||
logger.info(messages);
|
||||
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
|
||||
}, {
|
||||
headers: {
|
||||
@@ -104,10 +109,13 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
|
||||
|
||||
async function createCompletionStream(messages: any[], refreshToken: string, useSearch = true) {
|
||||
logger.info(messages);
|
||||
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
|
||||
}, {
|
||||
headers: {
|
||||
@@ -124,10 +132,22 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
|
||||
});
|
||||
}
|
||||
|
||||
function extractRefFileUrls(messages: any[]) {
|
||||
return messages.reduce((urls, message) => {
|
||||
if(message.type != 'file' || !message.url)
|
||||
return urls;
|
||||
urls.push(message.url);
|
||||
return urls;
|
||||
}, []);
|
||||
}
|
||||
|
||||
function messagesPrepare(messages: any[]) {
|
||||
const content = messages.reduce((content, message) => {
|
||||
if(message.type == 'file')
|
||||
return content;
|
||||
return content += `${message.role || 'user'}:${wrapUrlsToTags(message.content)}\n`;
|
||||
}, '');
|
||||
|
||||
return [
|
||||
{ role: 'user', content }
|
||||
]
|
||||
@@ -137,6 +157,70 @@ 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) {
|
||||
const token = await acquireToken(refreshToken);
|
||||
const result = await axios.post('https://kimi.moonshot.cn/api/pre-sign-url', {
|
||||
action: 'file',
|
||||
name: fileName
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Referer: `https://kimi.moonshot.cn`,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
validateStatus: () => true
|
||||
});
|
||||
return checkResult(result, refreshToken);
|
||||
}
|
||||
|
||||
async function checkFileUrl(fileUrl: string) {
|
||||
const result = await axios.head(fileUrl, {
|
||||
validateStatus: () => true
|
||||
});
|
||||
return result.status == 200 ? true : false;
|
||||
}
|
||||
|
||||
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);
|
||||
const { data: fileData } = await axios.get(fileUrl, {
|
||||
responseType: 'arraybuffer'
|
||||
});
|
||||
const {
|
||||
url: uploadUrl,
|
||||
object_name: objectName
|
||||
} = await preSignUrl(fileName, refreshToken);
|
||||
const mimeType = mime.getType(fileName);
|
||||
const token = await acquireToken(refreshToken);
|
||||
let result = await axios.request({
|
||||
method: 'PUT',
|
||||
url: uploadUrl,
|
||||
data: fileData,
|
||||
headers: {
|
||||
'Content-Type': mimeType,
|
||||
Authorization: `Bearer ${token}`,
|
||||
Referer: `https://kimi.moonshot.cn`,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
validateStatus: () => true
|
||||
});
|
||||
checkResult(result, refreshToken);
|
||||
result = await axios.post('https://kimi.moonshot.cn/api/file', {
|
||||
type: 'file',
|
||||
name: fileName,
|
||||
object_name: objectName
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Referer: `https://kimi.moonshot.cn`,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
});
|
||||
const { id: fileId } = checkResult(result, refreshToken);
|
||||
return fileId;
|
||||
}
|
||||
|
||||
function checkResult(result: AxiosResponse, refreshToken: string) {
|
||||
if(result.status == 401) {
|
||||
accessTokenMap.delete(refreshToken);
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import serviceConfig from "./configs/service-config.ts";
|
||||
import systemConfig from "./configs/system-config.ts";
|
||||
import apiConfig from "./configs/api-config.ts";
|
||||
|
||||
class Config {
|
||||
|
||||
@@ -10,9 +9,6 @@ class Config {
|
||||
/** 系统配置 */
|
||||
system = systemConfig;
|
||||
|
||||
/** API配置 */
|
||||
api = apiConfig;
|
||||
|
||||
}
|
||||
|
||||
export default new Config();
|
@@ -1,85 +0,0 @@
|
||||
import path from 'path';
|
||||
|
||||
import fs from 'fs-extra';
|
||||
import yaml from 'yaml';
|
||||
import _ from 'lodash';
|
||||
|
||||
import environment from '@/lib/environment.ts';
|
||||
|
||||
const CONFIG_PATH = path.join(path.resolve(), 'configs/', environment.env, "/api.yml");
|
||||
|
||||
export interface ProxyAgent {
|
||||
enable?: boolean;
|
||||
protocol?: string;
|
||||
host?: string;
|
||||
port?: number;
|
||||
}
|
||||
|
||||
export class ChatCompletionConfig {
|
||||
|
||||
/** 服务提供商 */
|
||||
provider: string;
|
||||
/** 调用地址 */
|
||||
url: string;
|
||||
/** API密钥 */
|
||||
apiKey: string;
|
||||
/** API版本号 */
|
||||
apiVersion: string;
|
||||
/** 模型名称 */
|
||||
model: string;
|
||||
/** 上下文长度 */
|
||||
contextLength: number;
|
||||
/** 单次最大token数 */
|
||||
maxToken: number;
|
||||
/** 并行请求数 */
|
||||
concurrencyLimit: number;
|
||||
/** 等待响应超时时间(毫秒) */
|
||||
waitReponseTimeout: number;
|
||||
/** 网络代理 */
|
||||
proxyAgent: ProxyAgent | null;
|
||||
|
||||
constructor(options?: any) {
|
||||
const { provider, url, apiKey, apiVersion, model, contextLength, concurrencyLimit, waitReponseTimeout, proxyAgent } = options || {};
|
||||
this.provider = _.defaultTo(provider, 'zhipuai');
|
||||
this.url = _.defaultTo(url, 'https://open.bigmodel.cn/api/paas/v4/chat/completions');
|
||||
this.apiKey = _.defaultTo(apiKey, '');
|
||||
this.apiVersion = _.defaultTo(apiVersion, '');
|
||||
this.model = _.defaultTo(model, 'glm-4');
|
||||
this.contextLength = _.defaultTo(contextLength, 131072);
|
||||
this.concurrencyLimit = _.defaultTo(concurrencyLimit, 100);
|
||||
this.waitReponseTimeout = _.defaultTo(waitReponseTimeout, 30000);
|
||||
this.proxyAgent = _.defaultTo(proxyAgent, null);
|
||||
}
|
||||
|
||||
static create(value) {
|
||||
return ChatCompletionConfig.isInstance(value) ? value : new ChatCompletionConfig(value);
|
||||
}
|
||||
|
||||
static isInstance(value) {
|
||||
return value instanceof ChatCompletionConfig;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* API配置
|
||||
*/
|
||||
export class APIConfig {
|
||||
|
||||
/** 聊天补全配置 */
|
||||
chatCompletion: ChatCompletionConfig;
|
||||
|
||||
constructor(options?: any) {
|
||||
const { chatCompletion } = options || {};
|
||||
this.chatCompletion = ChatCompletionConfig.create(chatCompletion);
|
||||
}
|
||||
|
||||
static load() {
|
||||
if(!fs.pathExistsSync(CONFIG_PATH)) return new APIConfig();
|
||||
const data = yaml.parse(fs.readFileSync(CONFIG_PATH).toString());
|
||||
return new APIConfig(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default APIConfig.load();
|
@@ -12,10 +12,7 @@ import CRC32 from 'crc-32';
|
||||
import randomstring from 'randomstring';
|
||||
import _ from 'lodash';
|
||||
import { CronJob } from 'cron';
|
||||
import { SocksProxyAgent } from 'socks-proxy-agent';
|
||||
import { HttpsProxyAgent } from 'https-proxy-agent';
|
||||
|
||||
import type { ProxyAgent } from './configs/api-config.ts';
|
||||
import HTTP_STATUS_CODE from './http-status-codes.ts';
|
||||
|
||||
const autoIdMap = new Map();
|
||||
@@ -67,21 +64,6 @@ const util = {
|
||||
return new CronJob(cronPatterns, () => callback(), null, false, "Asia/Shanghai");
|
||||
},
|
||||
|
||||
createProxyAgent(options: ProxyAgent) {
|
||||
const { enable, protocol, host, port } = options;
|
||||
if(enable === false)
|
||||
return null;
|
||||
switch(protocol) {
|
||||
case "socks5":
|
||||
return new SocksProxyAgent(`${protocol}://${host}:${port}`);
|
||||
case "http":
|
||||
case "https":
|
||||
return new HttpsProxyAgent(`${protocol}://${host}:${port}`);
|
||||
default:
|
||||
throw new Error(`protocol ${protocol} is not supported`);
|
||||
}
|
||||
},
|
||||
|
||||
getDateString(format = "yyyy-MM-dd", date = new Date()) {
|
||||
return dateFormat(date, format);
|
||||
},
|
||||
|
Reference in New Issue
Block a user