mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-11-01 18:29:19 +08:00
支持文档解读能力
This commit is contained in:
parent
04c399eeef
commit
99e1a4812a
109
README.md
109
README.md
@ -1,5 +1,7 @@
|
||||
# KIMI AI Free 服务
|
||||
|
||||
支持流式输出、支持文件解读。
|
||||
|
||||
## 声明
|
||||
|
||||
仅限自用,禁止对外提供服务,否则风险自担!
|
||||
@ -21,8 +23,78 @@ 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
|
||||
@ -55,6 +127,8 @@ docker stop kimi-free-api
|
||||
|
||||
## 原生部署
|
||||
|
||||
请准备一台具有公网IP的服务器并将8000端口开放。
|
||||
|
||||
请先安装好Node.js环境并且配置好环境变量,确认node命令可用。
|
||||
|
||||
安装依赖
|
||||
@ -98,38 +172,3 @@ 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,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);
|
||||
|
@ -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);
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user