mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2025-10-15 14:50:37 +00:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
84353391cd | ||
![]() |
e193e905f3 | ||
![]() |
0783ba52be | ||
![]() |
438830bc01 | ||
![]() |
99e1a4812a | ||
![]() |
04c399eeef | ||
![]() |
1f806a6b16 | ||
![]() |
4993d043ab | ||
![]() |
dc7a953e4f | ||
![]() |
3dbabb0d47 | ||
![]() |
cc0ea384a7 | ||
![]() |
6284628d99 | ||
![]() |
399242b09e | ||
![]() |
d13ba31d0e | ||
![]() |
c8c215448f | ||
![]() |
f3dae64a4c | ||
![]() |
376c48e8e5 | ||
![]() |
8c4c6408ba |
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"]
|
224
README.md
224
README.md
@@ -1,4 +1,6 @@
|
||||
# KIMI Free 服务
|
||||
# KIMI AI Free 服务
|
||||
|
||||
支持流式输出、支持联网搜索、支持文件解读、支持图像解析。
|
||||
|
||||
## 声明
|
||||
|
||||
@@ -10,15 +12,192 @@
|
||||
|
||||
## 在线体验
|
||||
|
||||
此链接仅临时测试功能,不可长期使用,长期使用请自行部署并
|
||||
此链接仅临时测试功能,不可长期使用,长期使用请自行部署。
|
||||
|
||||
https://udify.app/chat/Po0F6BMJ15q5vu2P
|
||||
|
||||

|
||||

|
||||
|
||||
## 接入准备
|
||||
|
||||
从kimi获取refresh_token:进入kimi随便发起一个对话,然后F12打开开发者工具,从Application > Local Storage中找到refresh_token的值,这将作为API_KEY。
|
||||
从 [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
|
||||
}
|
||||
```
|
||||
|
||||
### 文件上传解读
|
||||
|
||||
提供一个可访问的文件URL或者BASE64_URL进行解析。
|
||||
|
||||
请求数据:
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "file",
|
||||
"file_url": {
|
||||
"url": "https://mj101-1317487292.cos.ap-shanghai.myqcloud.com/ai/test.pdf"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "文档里说了什么?"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
// 建议关闭联网搜索,防止干扰解读结果
|
||||
"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"
|
||||
}
|
||||
],
|
||||
"usage": {
|
||||
"prompt_tokens": 0,
|
||||
"completion_tokens": 0,
|
||||
"total_tokens": 0
|
||||
},
|
||||
"created": 100920
|
||||
}
|
||||
```
|
||||
|
||||
### 图像解析
|
||||
|
||||
提供一个可访问的图像URL或者BASE64_URL进行解析。
|
||||
|
||||
此格式兼容 [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API格式,您也可以用这个格式传送文档进行解析。
|
||||
|
||||
请求数据:
|
||||
```json
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {
|
||||
"url": "https://www.moonshot.cn/assets/logo/normal-dark.png"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "图像描述了什么?"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
// 建议关闭联网搜索,防止干扰解读结果
|
||||
"use_search": false
|
||||
}
|
||||
```
|
||||
|
||||
响应数据:
|
||||
```json
|
||||
{
|
||||
"id": "cnn6l8ilnl92l36tu8ag",
|
||||
"model": "kimi",
|
||||
"object": "chat.completion",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "图像中展示了“Moonshot AI”的字样,这可能是月之暗面科技有限公司(Moonshot AI)的标志或者品牌标识。通常这样的图像用于代表公司或产品,传达品牌信息。由于图像是PNG格式,它可能是一个透明背景的logo,用于网站、应用程序或其他视觉材料中。"
|
||||
},
|
||||
"finish_reason": "stop"
|
||||
}
|
||||
],
|
||||
"usage": {
|
||||
"prompt_tokens": 0,
|
||||
"completion_tokens": 0,
|
||||
"total_tokens": 0
|
||||
},
|
||||
"created": 1710123627
|
||||
}
|
||||
```
|
||||
|
||||
## 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命令可用。
|
||||
|
||||
@@ -62,39 +241,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
|
BIN
doc/example-1.png
Normal file
BIN
doc/example-1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 100 KiB |
BIN
doc/example-2.png
Normal file
BIN
doc/example-2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 101 KiB |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "kimi-free-api",
|
||||
"version": "0.0.2",
|
||||
"description": "Kimi Free Server",
|
||||
"version": "0.0.8",
|
||||
"description": "Kimi Free API Server",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.mjs",
|
||||
@@ -27,7 +27,6 @@
|
||||
"date-fns": "^3.3.1",
|
||||
"eventsource-parser": "^1.1.2",
|
||||
"fs-extra": "^11.2.0",
|
||||
"https-proxy-agent": "^7.0.4",
|
||||
"koa": "^2.15.0",
|
||||
"koa-body": "^5.0.0",
|
||||
"koa-bodyparser": "^4.4.1",
|
||||
@@ -38,7 +37,6 @@
|
||||
"mime": "^4.0.1",
|
||||
"minimist": "^1.2.8",
|
||||
"randomstring": "^1.3.0",
|
||||
"socks-proxy-agent": "^8.0.2",
|
||||
"uuid": "^9.0.1",
|
||||
"yaml": "^2.3.4"
|
||||
},
|
||||
|
@@ -2,5 +2,7 @@ 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非法'],
|
||||
API_FILE_EXECEEDS_SIZE: [-2004, '远程文件超出大小']
|
||||
}
|
@@ -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";
|
||||
@@ -8,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: {
|
||||
@@ -24,6 +40,7 @@ async function requestToken(refreshToken: string) {
|
||||
Referer: 'https://kimi.moonshot.cn',
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
timeout: 15000,
|
||||
validateStatus: () => true
|
||||
});
|
||||
const {
|
||||
@@ -37,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) {
|
||||
@@ -48,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', {
|
||||
@@ -59,6 +90,7 @@ async function createConversation(name: string, refreshToken: string) {
|
||||
Referer: 'https://kimi.moonshot.cn',
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
timeout: 15000,
|
||||
validateStatus: () => true
|
||||
});
|
||||
const {
|
||||
@@ -67,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}`, {
|
||||
@@ -75,17 +114,34 @@ 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);
|
||||
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
||||
messages,
|
||||
messages: messagesPrepare(messages),
|
||||
refs,
|
||||
use_search: useSearch
|
||||
}, {
|
||||
headers: {
|
||||
@@ -93,23 +149,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,
|
||||
messages: messagesPrepare(messages),
|
||||
refs,
|
||||
use_search: useSearch
|
||||
}, {
|
||||
// 120秒超时
|
||||
timeout: 120000,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||
@@ -118,14 +199,218 @@ 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(_.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 (_.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`;
|
||||
}, '');
|
||||
|
||||
return [
|
||||
{ role: 'user', content }
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* 将消息中的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>`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预签名的文件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
|
||||
}, {
|
||||
timeout: 15000,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Referer: `https://kimi.moonshot.cn`,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
validateStatus: () => true
|
||||
});
|
||||
return checkResult(result, refreshToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* 预检查文件URL有效性
|
||||
*
|
||||
* @param fileUrl 文件URL
|
||||
*/
|
||||
async function checkFileUrl(fileUrl: string) {
|
||||
if(util.isBASE64Data(fileUrl))
|
||||
return;
|
||||
const result = await axios.head(fileUrl, {
|
||||
timeout: 15000,
|
||||
validateStatus: () => true
|
||||
});
|
||||
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) {
|
||||
// 预检查远程文件URL可用性
|
||||
await checkFileUrl(fileUrl);
|
||||
|
||||
let filename, fileData, mimeType;
|
||||
// 如果是BASE64数据则直接转换为Buffer
|
||||
if(util.isBASE64Data(fileUrl)) {
|
||||
mimeType = util.extractBASE64DataFormat(fileUrl);
|
||||
const ext = mime.getExtension(mimeType);
|
||||
filename = `${util.uuid()}.${ext}`;
|
||||
fileData = Buffer.from(util.removeBASE64DataHeader(fileUrl), 'base64');
|
||||
}
|
||||
// 下载文件到内存,如果您的服务器内存很小,建议考虑改造为流直传到下一个接口上,避免停留占用内存
|
||||
else {
|
||||
filename = path.basename(fileUrl);
|
||||
({ data: fileData } = await axios.get(fileUrl, {
|
||||
responseType: 'arraybuffer',
|
||||
// 100M限制
|
||||
maxContentLength: FILE_MAX_SIZE,
|
||||
// 60秒超时
|
||||
timeout: 60000
|
||||
}));
|
||||
}
|
||||
|
||||
// 获取预签名文件URL
|
||||
const {
|
||||
url: uploadUrl,
|
||||
object_name: objectName
|
||||
} = await preSignUrl(filename, refreshToken);
|
||||
|
||||
// 获取文件的MIME类型
|
||||
mimeType = 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}`,
|
||||
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,
|
||||
timeout: 15000
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Referer: `https://kimi.moonshot.cn`,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
});
|
||||
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);
|
||||
}
|
||||
@@ -139,33 +424,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 += '\n[内容由于不合规被停止生成,我们换个话题吧]' + (refContent ? `\n\n搜索结果来自:\n${refContent}` : '');
|
||||
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);
|
||||
@@ -175,19 +472,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 }
|
||||
@@ -197,50 +506,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`;
|
||||
@@ -254,6 +572,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'));
|
||||
|
@@ -23,7 +23,7 @@ const startupTime = performance.now();
|
||||
await server.listen();
|
||||
|
||||
config.service.bindAddress &&
|
||||
logger.success("service bind address:", config.service.bindAddress);
|
||||
logger.success("Service bind address:", config.service.bindAddress);
|
||||
})()
|
||||
.then(() =>
|
||||
logger.success(
|
||||
|
@@ -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);
|
||||
},
|
||||
@@ -154,18 +136,18 @@ const util = {
|
||||
return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
|
||||
},
|
||||
|
||||
isBASE64Image(value) {
|
||||
return /^data:image/.test(value);
|
||||
isBASE64Data(value) {
|
||||
return /^data:/.test(value);
|
||||
},
|
||||
|
||||
extractBASE64ImageFormat(value): string | null {
|
||||
const match = value.trim().match(/^data:image\/(.+);base64,/);
|
||||
extractBASE64DataFormat(value): string | null {
|
||||
const match = value.trim().match(/^data:(.+);base64,/);
|
||||
if(!match) return null;
|
||||
return match[1];
|
||||
},
|
||||
|
||||
removeBASE64ImageHeader(value): string {
|
||||
return value.replace(/^data:image\/(.+);base64,/, "");
|
||||
removeBASE64DataHeader(value): string {
|
||||
return value.replace(/^data:(.+);base64,/, "");
|
||||
},
|
||||
|
||||
isDataString(value): boolean {
|
||||
|
Reference in New Issue
Block a user