mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2025-10-14 22:34:40 +00:00
Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
38d59e20fc | ||
![]() |
bc37f56d1f | ||
![]() |
b2ae0554ee | ||
![]() |
399de41b61 | ||
![]() |
44316372a5 | ||
![]() |
fcc2c9f795 | ||
![]() |
a626a78553 | ||
![]() |
5f2eb803aa | ||
![]() |
684aedae1c | ||
![]() |
984d724367 | ||
![]() |
889c874264 | ||
![]() |
6105410dd2 | ||
![]() |
01ff5c250a | ||
![]() |
82a8359634 | ||
![]() |
7275ab7e11 | ||
![]() |
d862808226 | ||
![]() |
7cc6033201 | ||
![]() |
8f72c5de78 | ||
![]() |
72df4e1fc1 | ||
![]() |
9b00be5883 | ||
![]() |
61cc3a4655 | ||
![]() |
1aa45264f1 | ||
![]() |
56caa486c8 | ||
![]() |
2aa6465a36 | ||
![]() |
09250f208a | ||
![]() |
a2d5ab9390 | ||
![]() |
fe584180b1 | ||
![]() |
c1c601b498 | ||
![]() |
b9caca3289 | ||
![]() |
2b32fc66f4 | ||
![]() |
bffd5a24a3 | ||
![]() |
95f8c4e3e3 | ||
![]() |
0632d8111e | ||
![]() |
f1aa2e822c | ||
![]() |
53436b5f21 |
48
.github/workflows/sync.yml
vendored
Normal file
48
.github/workflows/sync.yml
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
name: Upstream Sync
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
issues: write
|
||||
actions: write
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 * * * *' # every hour
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
sync_latest_from_upstream:
|
||||
name: Sync latest commits from upstream repo
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ github.event.repository.fork }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Clean issue notice
|
||||
uses: actions-cool/issues-helper@v3
|
||||
with:
|
||||
actions: 'close-issues'
|
||||
labels: '🚨 Sync Fail'
|
||||
|
||||
- name: Sync upstream changes
|
||||
id: sync
|
||||
uses: aormsby/Fork-Sync-With-Upstream-action@v3.4
|
||||
with:
|
||||
upstream_sync_repo: LLM-Red-Team/kimi-free-api
|
||||
upstream_sync_branch: master
|
||||
target_sync_branch: master
|
||||
target_repo_token: ${{ secrets.GITHUB_TOKEN }} # automatically generated, no need to set
|
||||
test_mode: false
|
||||
|
||||
- name: Sync check
|
||||
if: failure()
|
||||
uses: actions-cool/issues-helper@v3
|
||||
with:
|
||||
actions: 'create-issue'
|
||||
title: '🚨 同步失败 | Sync Fail'
|
||||
labels: '🚨 Sync Fail'
|
||||
body: |
|
||||
Due to a change in the workflow file of the LLM-Red-Team/kimi-free-api upstream repository, GitHub has automatically suspended the scheduled automatic update. You need to manually sync your fork. Please refer to the detailed [Tutorial][tutorial-en-US] for instructions.
|
||||
|
||||
由于 LLM-Red-Team/kimi-free-api 上游仓库的 workflow 文件变更,导致 GitHub 自动暂停了本次自动更新,你需要手动 Sync Fork 一次,
|
41
README.md
41
README.md
@@ -11,19 +11,25 @@
|
||||

|
||||

|
||||
|
||||
支持高速流式输出、支持多轮对话、支持联网搜索、支持长文档解读、支持图像解析,零配置部署,多路token支持,自动清理会话痕迹。
|
||||
支持高速流式输出、支持多轮对话、支持联网搜索、支持智能体对话、支持长文档解读、支持图像OCR,零配置部署,多路token支持,自动清理会话痕迹。
|
||||
|
||||
与ChatGPT接口完全兼容。
|
||||
|
||||
还有以下五个free-api欢迎关注:
|
||||
还有以下八个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)
|
||||
智谱AI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
|
||||
|
||||
秘塔AI (metaso) 接口转API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-free-api)
|
||||
秘塔AI (Metaso) 接口转API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-free-api)
|
||||
|
||||
讯飞星火(Spark)接口转API [spark-free-api](https://github.com/LLM-Red-Team/spark-free-api)
|
||||
|
||||
MiniMax(海螺AI)接口转API [hailuo-free-api](https://github.com/LLM-Red-Team/hailuo-free-api)
|
||||
|
||||
深度求索(DeepSeek)接口转API [deepseek-free-api](https://github.com/LLM-Red-Team/deepseek-free-api)
|
||||
|
||||
聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
||||
|
||||
@@ -40,6 +46,7 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
|
||||
* [Vercel部署](#Vercel部署)
|
||||
* [Zeabur部署](#Zeabur部署)
|
||||
* [原生部署](#原生部署)
|
||||
* [推荐使用客户端](#推荐使用客户端)
|
||||
* [接口列表](#接口列表)
|
||||
* [对话补全](#对话补全)
|
||||
* [文档解读](#文档解读)
|
||||
@@ -48,6 +55,7 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
|
||||
* [注意事项](#注意事项)
|
||||
* [Nginx反代优化](#Nginx反代优化)
|
||||
* [Token统计](#Token统计)
|
||||
* [Star History](#star-history)
|
||||
|
||||
## 免责声明
|
||||
|
||||
@@ -81,11 +89,17 @@ https://udify.app/chat/Po0F6BMJ15q5vu2P
|
||||
|
||||

|
||||
|
||||
### 智能体对话Demo
|
||||
|
||||
此处使用 [翻译通](https://kimi.moonshot.cn/chat/coo6l3pkqq4ri39f36bg) 智能体。
|
||||
|
||||

|
||||
|
||||
### 长文档解读Demo
|
||||
|
||||

|
||||
|
||||
### 图像解析Demo
|
||||
### 图像OCR Demo
|
||||
|
||||

|
||||
|
||||
@@ -238,6 +252,14 @@ pm2 reload kimi-free-api
|
||||
pm2 stop kimi-free-api
|
||||
```
|
||||
|
||||
## 推荐使用客户端
|
||||
|
||||
使用以下二次开发客户端接入free-api系列项目更快更简单,支持文档/图像上传!
|
||||
|
||||
由 [Clivia](https://github.com/Yanyutin753/lobe-chat) 二次开发的LobeChat [https://github.com/Yanyutin753/lobe-chat](https://github.com/Yanyutin753/lobe-chat)
|
||||
|
||||
由 [时光@](https://github.com/SuYxh) 二次开发的ChatGPT Web [https://github.com/SuYxh/chatgpt-web-sea](https://github.com/SuYxh/chatgpt-web-sea)
|
||||
|
||||
## 接口列表
|
||||
|
||||
目前支持与openai兼容的 `/v1/chat/completions` 接口,可自行使用与openai或其他兼容的客户端接入接口,或者使用 [dify](https://dify.ai/) 等线上服务接入使用。
|
||||
@@ -257,8 +279,12 @@ Authorization: Bearer [refresh_token]
|
||||
请求数据:
|
||||
```json
|
||||
{
|
||||
// 模型名称随意填写,如果不希望输出检索过程模型名称请包含silent_search
|
||||
// model随意填写,如果不希望输出检索过程模型名称请包含silent_search
|
||||
// 如果使用kimi+智能体,model请填写智能体ID,就是浏览器地址栏上尾部的一串英文+数字20个字符的ID
|
||||
"model": "kimi",
|
||||
// 目前多轮对话基于消息合并实现,某些场景可能导致能力下降且受单轮最大Token数限制
|
||||
// 如果您想获得原生的多轮对话体验,可以传入首轮消息获得的id,来接续上下文,注意如果使用这个,首轮必须传none,否则第二轮会空响应!
|
||||
// "conversation_id": "cnndivilnl96vah411dg",
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
@@ -275,6 +301,7 @@ Authorization: Bearer [refresh_token]
|
||||
响应数据:
|
||||
```json
|
||||
{
|
||||
// 如果想获得原生多轮对话体验,此id,你可以传入到下一轮对话的conversation_id来接续上下文
|
||||
"id": "cnndivilnl96vah411dg",
|
||||
"model": "kimi",
|
||||
"object": "chat.completion",
|
||||
@@ -361,7 +388,7 @@ Authorization: Bearer [refresh_token]
|
||||
}
|
||||
```
|
||||
|
||||
### 图像解析
|
||||
### 图像OCR
|
||||
|
||||
提供一个可访问的图像URL或者BASE64_URL进行解析。
|
||||
|
||||
|
24
README_EN.md
24
README_EN.md
@@ -12,17 +12,19 @@ Supports high-speed streaming output, multi-turn dialogues, internet search, lon
|
||||
|
||||
Fully compatible with the ChatGPT interface.
|
||||
|
||||
Also, the following four free APIs are available for your attention:
|
||||
Also, the following six free APIs are available for your attention:
|
||||
|
||||
Step to the Stars (StepChat) API to API [step-free-api](https://github.com/LLM-Red-Team/step-free-api)
|
||||
StepFun (StepChat) API to API [step-free-api](https://github.com/LLM-Red-Team/step-free-api)
|
||||
|
||||
Ali Tongyi (Qwen) API to API [qwen-free-api](https://github.com/LLM-Red-Team/qwen-free-api)
|
||||
|
||||
ZhipuAI (Wisdom Map Clear Words) API to API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
|
||||
ZhipuAI (ChatGLM) API to API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
|
||||
|
||||
MetaAI (metaso) 接口转API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-free-api)
|
||||
Meta Sota (metaso) API to API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-free-api)
|
||||
|
||||
Listening Intelligence (Emohaa) API to API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
||||
Iflytek Spark (Spark) API to API [spark-free-api](https://github.com/LLM-Red-Team/spark-free-api)
|
||||
|
||||
Lingxin Intelligence (Emohaa) API to API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@@ -33,6 +35,7 @@ Listening Intelligence (Emohaa) API to API [emohaa-free-api](https://github.com/
|
||||
* [Multiple account access](#Multi-Account-Access)
|
||||
* [Docker Deployment](#Docker-Deployment)
|
||||
* [Docker-compose deployment](#Docker-compose-deployment)
|
||||
* [Zeabur Deployment](#Zeabur-Deployment)
|
||||
* [Native Deployment](#Native-deployment)
|
||||
* [Interface List](#Interface-List)
|
||||
* [Conversation completion](#conversation-completion)
|
||||
@@ -42,7 +45,8 @@ Listening Intelligence (Emohaa) API to API [emohaa-free-api](https://github.com/
|
||||
* [Precautions](#Precautions)
|
||||
* [Nginx anti-generation optimization](#Nginx-anti-generation-optimization)
|
||||
* [Token statistics](#Token-statistics)
|
||||
|
||||
* [Star History](#star-history)
|
||||
|
||||
## Disclaimer
|
||||
|
||||
**This organization and individuals do not accept any financial donations and transactions. This project is purely for research, communication, and learning purposes!**
|
||||
@@ -149,6 +153,10 @@ services:
|
||||
- TZ=Asia/Shanghai
|
||||
```
|
||||
|
||||
## Zeabur Deployment
|
||||
|
||||
[](https://zeabur.com/templates/GRFYBP)
|
||||
|
||||
## Native deployment
|
||||
|
||||
Please prepare a server with a public IP and open port 8000.
|
||||
@@ -197,10 +205,6 @@ Out of service
|
||||
pm2 stop kimi-free-api
|
||||
```
|
||||
|
||||
## Zeabur Deployment
|
||||
|
||||
[](https://zeabur.com/templates/GRFYBP)
|
||||
|
||||
## interface list
|
||||
|
||||
Currently, the `/v1/chat/completions` interface compatible with openai is supported. You can use the client access interface compatible with openai or other clients, or use online services such as [dify](https://dify.ai/) Access and use.
|
||||
|
BIN
doc/example-7.png
Normal file
BIN
doc/example-7.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 44 KiB |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kimi-free-api",
|
||||
"version": "0.0.28",
|
||||
"version": "0.0.34",
|
||||
"description": "Kimi Free API Server",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
@@ -137,6 +137,7 @@ async function getUserInfo(accessToken: string, refreshToken: string) {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: 'https://kimi.moonshot.cn/',
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': `7${util.generateRandomString({ length: 18, charset: 'numeric' })}`,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -153,18 +154,21 @@ async function getUserInfo(accessToken: string, refreshToken: string) {
|
||||
*
|
||||
* @param refreshToken 用于刷新access_token的refresh_token
|
||||
*/
|
||||
async function createConversation(name: string, refreshToken: string) {
|
||||
async function createConversation(model: string, name: string, refreshToken: string) {
|
||||
const {
|
||||
accessToken,
|
||||
userId
|
||||
} = await acquireToken(refreshToken);
|
||||
const result = await axios.post('https://kimi.moonshot.cn/api/chat', {
|
||||
born_from: '',
|
||||
is_example: false,
|
||||
kimiplus_id: /^[0-9a-z]{20}$/.test(model) ? model : 'kimi',
|
||||
name
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: 'https://kimi.moonshot.cn/',
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -193,6 +197,7 @@ async function removeConversation(convId: string, refreshToken: string) {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -221,6 +226,7 @@ async function promptSnippetSubmit(query: string, refreshToken: string) {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: 'https://kimi.moonshot.cn/',
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -237,9 +243,10 @@ async function promptSnippetSubmit(query: string, refreshToken: string) {
|
||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||
* @param refreshToken 用于刷新access_token的refresh_token
|
||||
* @param useSearch 是否开启联网搜索
|
||||
* @param refConvId 引用会话ID
|
||||
* @param retryCount 重试次数
|
||||
*/
|
||||
async function createCompletion(model = MODEL_NAME, messages: any[], refreshToken: string, useSearch = true, retryCount = 0) {
|
||||
async function createCompletion(model = MODEL_NAME, messages: any[], refreshToken: string, useSearch = true, refConvId?: string, retryCount = 0) {
|
||||
return (async () => {
|
||||
logger.info(messages);
|
||||
|
||||
@@ -252,22 +259,26 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
||||
.catch(err => logger.error(err));
|
||||
|
||||
// 创建会话
|
||||
const convId = await createConversation("未命名会话", refreshToken);
|
||||
const convId = /[0-9a-zA-Z]{20}/.test(refConvId) ? refConvId : await createConversation(model, "未命名会话", refreshToken);
|
||||
|
||||
// 请求流
|
||||
const {
|
||||
accessToken,
|
||||
userId
|
||||
} = await acquireToken(refreshToken);
|
||||
const sendMessages = messagesPrepare(messages);
|
||||
const sendMessages = messagesPrepare(messages, !!refConvId);
|
||||
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
||||
kimiplus_id: /^[0-9a-z]{20}$/.test(model) ? model : 'kimi',
|
||||
messages: sendMessages,
|
||||
refs,
|
||||
is_pro_search: false,
|
||||
use_search: useSearch
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||
'Priority': 'u=1, i',
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -283,7 +294,8 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
||||
logger.success(`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`);
|
||||
|
||||
// 异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
|
||||
removeConversation(convId, refreshToken)
|
||||
// 如果引用会话将不会清除,因为我们不知道什么时候你会结束会话
|
||||
!refConvId && removeConversation(convId, refreshToken)
|
||||
.catch(err => console.error(err));
|
||||
promptSnippetSubmit(sendMessages[0].content, refreshToken)
|
||||
.catch(err => console.error(err));
|
||||
@@ -296,7 +308,7 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
||||
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
||||
return (async () => {
|
||||
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
|
||||
return createCompletion(model, messages, refreshToken, useSearch, retryCount + 1);
|
||||
return createCompletion(model, messages, refreshToken, useSearch, refConvId, retryCount + 1);
|
||||
})();
|
||||
}
|
||||
throw err;
|
||||
@@ -310,9 +322,10 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||
* @param refreshToken 用于刷新access_token的refresh_token
|
||||
* @param useSearch 是否开启联网搜索
|
||||
* @param refConvId 引用会话ID
|
||||
* @param retryCount 重试次数
|
||||
*/
|
||||
async function createCompletionStream(model = MODEL_NAME, messages: any[], refreshToken: string, useSearch = true, retryCount = 0) {
|
||||
async function createCompletionStream(model = MODEL_NAME, messages: any[], refreshToken: string, useSearch = true, refConvId?: string, retryCount = 0) {
|
||||
return (async () => {
|
||||
logger.info(messages);
|
||||
|
||||
@@ -325,15 +338,16 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
||||
.catch(err => logger.error(err));
|
||||
|
||||
// 创建会话
|
||||
const convId = await createConversation("未命名会话", refreshToken);
|
||||
const convId = /[0-9a-zA-Z]{20}/.test(refConvId) ? refConvId : await createConversation(model, "未命名会话", refreshToken);
|
||||
|
||||
// 请求流
|
||||
const {
|
||||
accessToken,
|
||||
userId
|
||||
} = await acquireToken(refreshToken);
|
||||
const sendMessages = messagesPrepare(messages);
|
||||
const sendMessages = messagesPrepare(messages, !!refConvId);
|
||||
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
||||
kimiplus_id: /^[0-9a-z]{20}$/.test(model) ? model : undefined,
|
||||
messages: sendMessages,
|
||||
refs,
|
||||
use_search: useSearch
|
||||
@@ -343,6 +357,8 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||
'Priority': 'u=1, i',
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -354,7 +370,8 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
||||
return createTransStream(model, convId, result.data, () => {
|
||||
logger.success(`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`);
|
||||
// 流传输结束后异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
|
||||
removeConversation(convId, refreshToken)
|
||||
// 如果引用会话将不会清除,因为我们不知道什么时候你会结束会话
|
||||
!refConvId && removeConversation(convId, refreshToken)
|
||||
.catch(err => console.error(err));
|
||||
promptSnippetSubmit(sendMessages[0].content, refreshToken)
|
||||
.catch(err => console.error(err));
|
||||
@@ -366,7 +383,7 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
||||
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
||||
return (async () => {
|
||||
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
|
||||
return createCompletionStream(model, messages, refreshToken, useSearch, retryCount + 1);
|
||||
return createCompletionStream(model, messages, refreshToken, useSearch, refConvId, retryCount + 1);
|
||||
})();
|
||||
}
|
||||
throw err;
|
||||
@@ -389,6 +406,7 @@ async function fakeRequest(refreshToken: string) {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
@@ -447,14 +465,28 @@ function extractRefFileUrls(messages: any[]) {
|
||||
* user:新消息
|
||||
*
|
||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||
* @param isRefConv 是否为引用会话
|
||||
*/
|
||||
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) {
|
||||
function messagesPrepare(messages: any[], isRefConv = false) {
|
||||
let content;
|
||||
if (isRefConv || messages.length < 2) {
|
||||
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"] || ""}\n`;
|
||||
}, content);
|
||||
}
|
||||
return content += `${message.role == 'user' ? wrapUrlsToTags(message.content) : message.content}\n`;
|
||||
}, '')
|
||||
logger.info("\n透传内容:\n" + content);
|
||||
}
|
||||
else {
|
||||
// 注入消息提升注意力
|
||||
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 (hasFileOrImage) {
|
||||
let newFileMessage = {
|
||||
"content": "关注用户最新发送文件和消息",
|
||||
@@ -470,18 +502,18 @@ function messagesPrepare(messages: any[]) {
|
||||
messages.splice(messages.length - 1, 0, newTextMessage);
|
||||
logger.info("注入提升尾部消息注意力system prompt");
|
||||
}
|
||||
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 + `${message.role || "user"}:${v["text"] || ""}\n`;
|
||||
}, content);
|
||||
}
|
||||
return content += `${message.role || "user"}:${message.role == 'user' ? wrapUrlsToTags(message.content) : message.content}\n`;
|
||||
}, '')
|
||||
logger.info("\n对话合并:\n" + content);
|
||||
}
|
||||
|
||||
const content = messages.reduce((content, message) => {
|
||||
if (Array.isArray(message.content)) {
|
||||
return message.content.reduce((_content, v) => {
|
||||
if (!_.isObject(v) || v['type'] != 'text') return _content;
|
||||
return _content + `${message.role || "user"}:${v["text"] || ""}\n`;
|
||||
}, content);
|
||||
}
|
||||
return content += `${message.role || "user"}:${message.role == 'user' ? wrapUrlsToTags(message.content) : message.content}\n`;
|
||||
}, '');
|
||||
logger.info("\n对话合并:\n" + content);
|
||||
return [
|
||||
{ role: 'user', content }
|
||||
]
|
||||
@@ -517,6 +549,7 @@ async function preSignUrl(filename: string, refreshToken: string) {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -602,6 +635,7 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
|
||||
'Content-Type': mimeType,
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
},
|
||||
@@ -609,35 +643,51 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
|
||||
});
|
||||
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 ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
});
|
||||
const { id: fileId } = checkResult(result, refreshToken);
|
||||
let fileId, status, startTime = Date.now();
|
||||
while (status != 'initialized') {
|
||||
if (Date.now() - startTime > 30000)
|
||||
throw new Error('文件等待处理超时');
|
||||
// 获取文件上传结果
|
||||
result = await axios.post('https://kimi.moonshot.cn/api/file', {
|
||||
type: 'file',
|
||||
name: filename,
|
||||
object_name: objectName,
|
||||
timeout: 15000
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
});
|
||||
({ id: fileId, status } = checkResult(result, refreshToken));
|
||||
}
|
||||
|
||||
// 处理文件转换
|
||||
result = await axios.post('https://kimi.moonshot.cn/api/file/parse_process', {
|
||||
ids: [fileId],
|
||||
timeout: 120000
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
});
|
||||
checkResult(result, refreshToken);
|
||||
startTime = Date.now();
|
||||
let parseFinish = false;
|
||||
while (!parseFinish) {
|
||||
if (Date.now() - startTime > 30000)
|
||||
throw new Error('文件等待处理超时');
|
||||
// 处理文件转换
|
||||
parseFinish = await new Promise(resolve => {
|
||||
axios.post('https://kimi.moonshot.cn/api/file/parse_process', {
|
||||
ids: [fileId],
|
||||
timeout: 120000
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Referer: `https://kimi.moonshot.cn/`,
|
||||
'X-Msh-Platform': 'web',
|
||||
'X-Traffic-Id': userId,
|
||||
...FAKE_HEADERS
|
||||
}
|
||||
})
|
||||
.then(() => resolve(true))
|
||||
.catch(() => resolve(false));
|
||||
});
|
||||
}
|
||||
|
||||
return fileId;
|
||||
}
|
||||
@@ -673,6 +723,7 @@ function checkResult(result: AxiosResponse, refreshToken: string) {
|
||||
* @param stream 消息流
|
||||
*/
|
||||
async function receiveStream(model: string, convId: string, stream: any) {
|
||||
let webSearchCount = 0;
|
||||
return new Promise((resolve, reject) => {
|
||||
// 消息初始化
|
||||
const data = {
|
||||
@@ -706,8 +757,10 @@ async function receiveStream(model: string, convId: string, stream: any) {
|
||||
resolve(data);
|
||||
}
|
||||
// 处理联网搜索
|
||||
else if (!silentSearch && result.event == 'search_plus' && result.msg && result.msg.type == 'get_res')
|
||||
refContent += `${result.msg.title} - ${result.msg.url}\n`;
|
||||
else if (!silentSearch && result.event == 'search_plus' && result.msg && result.msg.type == 'get_res'){
|
||||
webSearchCount += 1;
|
||||
refContent += `【检索 ${webSearchCount}】 [${result.msg.title}](${result.msg.url})\n\n`;
|
||||
}
|
||||
// else
|
||||
// logger.warn(result.event, result);
|
||||
}
|
||||
@@ -738,6 +791,7 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
|
||||
const created = util.unixTimestamp();
|
||||
// 创建转换流
|
||||
const transStream = new PassThrough();
|
||||
let webSearchCount = 0;
|
||||
let searchFlag = false;
|
||||
const silentSearch = model.indexOf('silent_search') != -1;
|
||||
!transStream.closed && transStream.write(`data: ${JSON.stringify({
|
||||
@@ -797,6 +851,7 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
|
||||
else if (!silentSearch && result.event == 'search_plus' && result.msg && result.msg.type == 'get_res') {
|
||||
if (!searchFlag)
|
||||
searchFlag = true;
|
||||
webSearchCount += 1;
|
||||
const data = `data: ${JSON.stringify({
|
||||
id: convId,
|
||||
model,
|
||||
@@ -804,7 +859,7 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
|
||||
choices: [
|
||||
{
|
||||
index: 0, delta: {
|
||||
content: `检索 ${result.msg.title} - ${result.msg.url} ...\n`
|
||||
content: `【检索 ${webSearchCount}】 [${result.msg.title}](${result.msg.url})\n`
|
||||
}, finish_reason: null
|
||||
}
|
||||
],
|
||||
|
@@ -13,22 +13,22 @@ export default {
|
||||
|
||||
'/completions': async (request: Request) => {
|
||||
request
|
||||
.validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
|
||||
.validate('body.messages', _.isArray)
|
||||
.validate('headers.authorization', _.isString)
|
||||
// refresh_token切分
|
||||
const tokens = chat.tokenSplit(request.headers.authorization);
|
||||
// 随机挑选一个refresh_token
|
||||
const token = _.sample(tokens);
|
||||
const model = request.body.model;
|
||||
const messages = request.body.messages;
|
||||
if (request.body.stream) {
|
||||
const stream = await chat.createCompletionStream(model, messages, token, request.body.use_search);
|
||||
const { model, conversation_id: convId, messages, stream, use_search } = request.body;
|
||||
if (stream) {
|
||||
const stream = await chat.createCompletionStream(model, messages, token, use_search, convId);
|
||||
return new Response(stream, {
|
||||
type: "text/event-stream"
|
||||
});
|
||||
}
|
||||
else
|
||||
return await chat.createCompletion(model, messages, token, request.body.use_search);
|
||||
return await chat.createCompletion(model, messages, token, use_search, convId);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user