mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2025-10-15 06:40:39 +00:00
Compare commits
47 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6105410dd2 | ||
![]() |
01ff5c250a | ||
![]() |
82a8359634 | ||
![]() |
7275ab7e11 | ||
![]() |
d862808226 | ||
![]() |
7cc6033201 | ||
![]() |
8f72c5de78 | ||
![]() |
72df4e1fc1 | ||
![]() |
9b00be5883 | ||
![]() |
61cc3a4655 | ||
![]() |
1aa45264f1 | ||
![]() |
56caa486c8 | ||
![]() |
2aa6465a36 | ||
![]() |
09250f208a | ||
![]() |
a2d5ab9390 | ||
![]() |
fe584180b1 | ||
![]() |
c1c601b498 | ||
![]() |
b9caca3289 | ||
![]() |
2b32fc66f4 | ||
![]() |
bffd5a24a3 | ||
![]() |
95f8c4e3e3 | ||
![]() |
0632d8111e | ||
![]() |
f1aa2e822c | ||
![]() |
53436b5f21 | ||
![]() |
e8284288c9 | ||
![]() |
04db70bec5 | ||
![]() |
f7c1fa7be3 | ||
![]() |
b9d479b9f6 | ||
![]() |
c9c26fdd31 | ||
![]() |
43e14b6e3e | ||
![]() |
65a3fed83b | ||
![]() |
4a225853af | ||
![]() |
6b343f4094 | ||
![]() |
e8c6622e83 | ||
![]() |
ae6dc4a79f | ||
![]() |
bdb8ced5ce | ||
![]() |
a0c1bba3c9 | ||
![]() |
c6da81a53e | ||
![]() |
77d42d9484 | ||
![]() |
d73a9bc95d | ||
![]() |
65f45697e8 | ||
![]() |
875bb55f21 | ||
![]() |
cbf215d8a8 | ||
![]() |
7c3bc3c0d8 | ||
![]() |
ae8e8316e4 | ||
![]() |
e1b7e55e70 | ||
![]() |
e1710ee95a |
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 一次,
|
38
README.md
38
README.md
@@ -6,7 +6,7 @@
|
|||||||
<span>[ 中文 | <a href="README_EN.md">English</a> ]</span>
|
<span>[ 中文 | <a href="README_EN.md">English</a> ]</span>
|
||||||
|
|
||||||
|
|
||||||

|
[](LICENSE)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
@@ -15,15 +15,17 @@
|
|||||||
|
|
||||||
与ChatGPT接口完全兼容。
|
与ChatGPT接口完全兼容。
|
||||||
|
|
||||||
还有以下五个free-api欢迎关注:
|
还有以下六个free-api欢迎关注:
|
||||||
|
|
||||||
阶跃星辰 (跃问StepChat) 接口转API [step-free-api](https://github.com/LLM-Red-Team/step-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)
|
阿里通义 (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)
|
||||||
|
|
||||||
聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
||||||
|
|
||||||
@@ -38,7 +40,9 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
|
|||||||
* [Docker-compose部署](#Docker-compose部署)
|
* [Docker-compose部署](#Docker-compose部署)
|
||||||
* [Render部署](#Render部署)
|
* [Render部署](#Render部署)
|
||||||
* [Vercel部署](#Vercel部署)
|
* [Vercel部署](#Vercel部署)
|
||||||
|
* [Zeabur部署](#Zeabur部署)
|
||||||
* [原生部署](#原生部署)
|
* [原生部署](#原生部署)
|
||||||
|
* [推荐使用客户端](#推荐使用客户端)
|
||||||
* [接口列表](#接口列表)
|
* [接口列表](#接口列表)
|
||||||
* [对话补全](#对话补全)
|
* [对话补全](#对话补全)
|
||||||
* [文档解读](#文档解读)
|
* [文档解读](#文档解读)
|
||||||
@@ -46,9 +50,13 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
|
|||||||
* [refresh_token存活检测](#refresh_token存活检测)
|
* [refresh_token存活检测](#refresh_token存活检测)
|
||||||
* [注意事项](#注意事项)
|
* [注意事项](#注意事项)
|
||||||
* [Nginx反代优化](#Nginx反代优化)
|
* [Nginx反代优化](#Nginx反代优化)
|
||||||
|
* [Token统计](#Token统计)
|
||||||
|
* [Star History](#star-history)
|
||||||
|
|
||||||
## 免责声明
|
## 免责声明
|
||||||
|
|
||||||
|
**逆向API是不稳定的,建议前往MoonshotAI官方 https://platform.moonshot.cn/ 付费使用API,避免封禁的风险。**
|
||||||
|
|
||||||
**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
|
**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
|
||||||
|
|
||||||
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
@@ -63,12 +71,6 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
|
|||||||
|
|
||||||
https://udify.app/chat/Po0F6BMJ15q5vu2P
|
https://udify.app/chat/Po0F6BMJ15q5vu2P
|
||||||
|
|
||||||
## 测试接口
|
|
||||||
|
|
||||||
此接口实例部署在[Render](#Render部署)上面,遇到容器回收可能导致响应速度较慢,仅供测试,建议自行部署。
|
|
||||||
|
|
||||||
https://kimi-free-api-nut5.onrender.com
|
|
||||||
|
|
||||||
## 效果示例
|
## 效果示例
|
||||||
|
|
||||||
### 验明正身Demo
|
### 验明正身Demo
|
||||||
@@ -186,7 +188,7 @@ cd kimi-free-api
|
|||||||
vercel --prod
|
vercel --prod
|
||||||
```
|
```
|
||||||
|
|
||||||
### Zeabur 部署
|
### Zeabur部署
|
||||||
|
|
||||||
**注意:免费账户的容器实例可能无法稳定运行**
|
**注意:免费账户的容器实例可能无法稳定运行**
|
||||||
|
|
||||||
@@ -240,6 +242,14 @@ pm2 reload kimi-free-api
|
|||||||
pm2 stop 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/) 等线上服务接入使用。
|
目前支持与openai兼容的 `/v1/chat/completions` 接口,可自行使用与openai或其他兼容的客户端接入接口,或者使用 [dify](https://dify.ai/) 等线上服务接入使用。
|
||||||
@@ -261,6 +271,9 @@ Authorization: Bearer [refresh_token]
|
|||||||
{
|
{
|
||||||
// 模型名称随意填写,如果不希望输出检索过程模型名称请包含silent_search
|
// 模型名称随意填写,如果不希望输出检索过程模型名称请包含silent_search
|
||||||
"model": "kimi",
|
"model": "kimi",
|
||||||
|
// 目前多轮对话基于消息合并实现,某些场景可能导致能力下降且受单轮最大Token数限制
|
||||||
|
// 如果您想获得原生的多轮对话体验,可以传入首轮消息获得的id,来接续上下文,注意如果使用这个,首轮必须传none,否则第二轮会空响应!
|
||||||
|
// "conversation_id": "cnndivilnl96vah411dg",
|
||||||
"messages": [
|
"messages": [
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@@ -277,6 +290,7 @@ Authorization: Bearer [refresh_token]
|
|||||||
响应数据:
|
响应数据:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
// 如果想获得原生多轮对话体验,此id,你可以传入到下一轮对话的conversation_id来接续上下文
|
||||||
"id": "cnndivilnl96vah411dg",
|
"id": "cnndivilnl96vah411dg",
|
||||||
"model": "kimi",
|
"model": "kimi",
|
||||||
"object": "chat.completion",
|
"object": "chat.completion",
|
||||||
@@ -431,7 +445,7 @@ Authorization: Bearer [refresh_token]
|
|||||||
|
|
||||||
### refresh_token存活检测
|
### refresh_token存活检测
|
||||||
|
|
||||||
检测refresh_token是否存活,如果存活live未true,否则为false,请不要频繁(小于10分钟)调用此接口。
|
检测refresh_token是否存活,如果存活live为true,否则为false,请不要频繁(小于10分钟)调用此接口。
|
||||||
|
|
||||||
**POST /token/check**
|
**POST /token/check**
|
||||||
|
|
||||||
|
60
README_EN.md
60
README_EN.md
@@ -1,6 +1,9 @@
|
|||||||
# KIMI AI Free Service
|
# KIMI AI Free Service
|
||||||
|
|
||||||

|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
[](LICENSE)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
@@ -9,36 +12,41 @@ Supports high-speed streaming output, multi-turn dialogues, internet search, lon
|
|||||||
|
|
||||||
Fully compatible with the ChatGPT interface.
|
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)
|
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
|
## Table of Contents
|
||||||
|
|
||||||
* [Disclaimer](#disclaimer)
|
* [Disclaimer](#disclaimer)
|
||||||
*[Online experience](#在线experience)
|
* [Online experience](#Online-Experience)
|
||||||
* [Effect Example](#EffectExample)
|
* [Effect Examples](#Effect-Examples)
|
||||||
* [Access preparation](#access preparation)
|
* [Access preparation](#Access-Preparation)
|
||||||
* [Multiple account access](#multiple account access)
|
* [Multiple account access](#Multi-Account-Access)
|
||||||
* [Docker Deployment](#DockerDeployment)
|
* [Docker Deployment](#Docker-Deployment)
|
||||||
* [Docker-compose deployment](#Docker-compose deployment)
|
* [Docker-compose deployment](#Docker-compose-deployment)
|
||||||
* [Native Deployment](#nativedeployment)
|
* [Zeabur Deployment](#Zeabur-Deployment)
|
||||||
* [Interface List](#Interface List)
|
* [Native Deployment](#Native-deployment)
|
||||||
* [Dialogue completion](#dialogue completion)
|
* [Interface List](#Interface-List)
|
||||||
* [Document Interpretation](#document interpretation)
|
* [Conversation completion](#conversation-completion)
|
||||||
* [Image analysis](#imageanalysis)
|
* [Document Interpretation](#document-interpretation)
|
||||||
* [refresh_token survival detection](#refresh_token survival detection)
|
* [Image analysis](#image-analysis)
|
||||||
* [Note](# NOTE)
|
* [refresh_token survival detection](#refresh_token-survival-detection)
|
||||||
* [Nginx anti-generation optimization](#Nginx anti-generation optimization)
|
* [Precautions](#Precautions)
|
||||||
|
* [Nginx anti-generation optimization](#Nginx-anti-generation-optimization)
|
||||||
|
* [Token statistics](#Token-statistics)
|
||||||
|
* [Star History](#star-history)
|
||||||
|
|
||||||
## Disclaimer
|
## Disclaimer
|
||||||
|
|
||||||
**This organization and individuals do not accept any financial donations and transactions. This project is purely for research, communication, and learning purposes!**
|
**This organization and individuals do not accept any financial donations and transactions. This project is purely for research, communication, and learning purposes!**
|
||||||
@@ -145,6 +153,10 @@ services:
|
|||||||
- TZ=Asia/Shanghai
|
- TZ=Asia/Shanghai
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Zeabur Deployment
|
||||||
|
|
||||||
|
[](https://zeabur.com/templates/GRFYBP)
|
||||||
|
|
||||||
## Native deployment
|
## Native deployment
|
||||||
|
|
||||||
Please prepare a server with a public IP and open port 8000.
|
Please prepare a server with a public IP and open port 8000.
|
||||||
@@ -193,10 +205,6 @@ Out of service
|
|||||||
pm2 stop kimi-free-api
|
pm2 stop kimi-free-api
|
||||||
```
|
```
|
||||||
|
|
||||||
## Zeabur Deployment
|
|
||||||
|
|
||||||
[](https://zeabur.com/templates/GRFYBP)
|
|
||||||
|
|
||||||
## interface list
|
## 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.
|
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.
|
||||||
@@ -431,4 +439,4 @@ Since the inference side is not in kimi-free-api, the token cannot be counted an
|
|||||||
|
|
||||||
## Star History
|
## Star History
|
||||||
|
|
||||||
[](https://star-history.com/ #LLM-Red-Team/kimi-free-api&Date)
|
[](https://star-history.com/#LLM-Red-Team/kimi-free-api&Date)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kimi-free-api",
|
"name": "kimi-free-api",
|
||||||
"version": "0.0.26",
|
"version": "0.0.32",
|
||||||
"description": "Kimi Free API Server",
|
"description": "Kimi Free API Server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@@ -24,15 +24,15 @@ const FAKE_HEADERS = {
|
|||||||
'Accept-Encoding': 'gzip, deflate, br, zstd',
|
'Accept-Encoding': 'gzip, deflate, br, zstd',
|
||||||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||||
'Origin': 'https://kimi.moonshot.cn',
|
'Origin': 'https://kimi.moonshot.cn',
|
||||||
// 'Cookie': util.generateCookie(),
|
'Cookie': util.generateCookie(),
|
||||||
'R-Timezone': 'Asia/Shanghai',
|
'R-Timezone': 'Asia/Shanghai',
|
||||||
'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
|
'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
|
||||||
'Sec-Ch-Ua-Mobile': '?0',
|
'Sec-Ch-Ua-Mobile': '?0',
|
||||||
'Sec-Ch-Ua-Platform': '"Windows"',
|
'Sec-Ch-Ua-Platform': '"Windows"',
|
||||||
'Sec-Fetch-Dest': 'empty',
|
'Sec-Fetch-Dest': 'empty',
|
||||||
'Sec-Fetch-Mode': 'cors',
|
'Sec-Fetch-Mode': 'cors',
|
||||||
'Sec-Fetch-Site': 'same-origin',
|
'Sec-Fetch-Site': 'same-origin',
|
||||||
'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'
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
|
||||||
};
|
};
|
||||||
// 文件最大大小
|
// 文件最大大小
|
||||||
const FILE_MAX_SIZE = 100 * 1024 * 1024;
|
const FILE_MAX_SIZE = 100 * 1024 * 1024;
|
||||||
@@ -56,9 +56,21 @@ async function requestToken(refreshToken: string) {
|
|||||||
const result = await (async () => {
|
const result = await (async () => {
|
||||||
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
|
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
|
||||||
headers: {
|
headers: {
|
||||||
|
Accept: '*/*',
|
||||||
|
'Accept-Encoding': 'gzip, deflate, br, zstd',
|
||||||
|
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||||
Authorization: `Bearer ${refreshToken}`,
|
Authorization: `Bearer ${refreshToken}`,
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
'Cookie': util.generateCookie(),
|
||||||
|
Pragma: 'no-cache',
|
||||||
Referer: 'https://kimi.moonshot.cn/',
|
Referer: 'https://kimi.moonshot.cn/',
|
||||||
...FAKE_HEADERS
|
'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
|
||||||
|
'Sec-Ch-Ua-Mobile': '?0',
|
||||||
|
'Sec-Ch-Ua-Platform': '"Windows"',
|
||||||
|
'Sec-Fetch-Dest': 'empty',
|
||||||
|
'Sec-Fetch-Mode': 'cors',
|
||||||
|
'Sec-Fetch-Site': 'same-origin',
|
||||||
|
'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'
|
||||||
},
|
},
|
||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
validateStatus: () => true
|
validateStatus: () => true
|
||||||
@@ -67,7 +79,9 @@ async function requestToken(refreshToken: string) {
|
|||||||
access_token,
|
access_token,
|
||||||
refresh_token
|
refresh_token
|
||||||
} = checkResult(result, refreshToken);
|
} = checkResult(result, refreshToken);
|
||||||
|
const { id: userId } = await getUserInfo(access_token, refreshToken);
|
||||||
return {
|
return {
|
||||||
|
userId,
|
||||||
accessToken: access_token,
|
accessToken: access_token,
|
||||||
refreshToken: refresh_token,
|
refreshToken: refresh_token,
|
||||||
refreshTime: util.unixTimestamp() + ACCESS_TOKEN_EXPIRES
|
refreshTime: util.unixTimestamp() + ACCESS_TOKEN_EXPIRES
|
||||||
@@ -100,7 +114,7 @@ async function requestToken(refreshToken: string) {
|
|||||||
*
|
*
|
||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
*/
|
*/
|
||||||
async function acquireToken(refreshToken: string): Promise<string> {
|
async function acquireToken(refreshToken: string): Promise<any> {
|
||||||
let result = accessTokenMap.get(refreshToken);
|
let result = accessTokenMap.get(refreshToken);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
result = await requestToken(refreshToken);
|
result = await requestToken(refreshToken);
|
||||||
@@ -110,7 +124,26 @@ async function acquireToken(refreshToken: string): Promise<string> {
|
|||||||
result = await requestToken(refreshToken);
|
result = await requestToken(refreshToken);
|
||||||
accessTokenMap.set(refreshToken, result);
|
accessTokenMap.set(refreshToken, result);
|
||||||
}
|
}
|
||||||
return result.accessToken;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
*
|
||||||
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
|
*/
|
||||||
|
async function getUserInfo(accessToken: string, refreshToken: string) {
|
||||||
|
const result = await axios.get('https://kimi.moonshot.cn/api/user', {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
Referer: 'https://kimi.moonshot.cn/',
|
||||||
|
'X-Traffic-Id': `7${util.generateRandomString({ length: 18, charset: 'numeric' })}`,
|
||||||
|
...FAKE_HEADERS
|
||||||
|
},
|
||||||
|
timeout: 15000,
|
||||||
|
validateStatus: () => true
|
||||||
|
});
|
||||||
|
return checkResult(result, refreshToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,14 +154,18 @@ async function acquireToken(refreshToken: string): Promise<string> {
|
|||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
*/
|
*/
|
||||||
async function createConversation(name: string, refreshToken: string) {
|
async function createConversation(name: string, refreshToken: string) {
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
const result = await axios.post('https://kimi.moonshot.cn/api/chat', {
|
const result = await axios.post('https://kimi.moonshot.cn/api/chat', {
|
||||||
name,
|
is_example: false,
|
||||||
is_example: false
|
name
|
||||||
}, {
|
}, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: 'https://kimi.moonshot.cn/',
|
Referer: 'https://kimi.moonshot.cn/',
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
},
|
},
|
||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
@@ -148,11 +185,43 @@ async function createConversation(name: string, refreshToken: string) {
|
|||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
*/
|
*/
|
||||||
async function removeConversation(convId: string, refreshToken: string) {
|
async function removeConversation(convId: string, refreshToken: string) {
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
const result = await axios.delete(`https://kimi.moonshot.cn/api/chat/${convId}`, {
|
const result = await axios.delete(`https://kimi.moonshot.cn/api/chat/${convId}`, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
|
...FAKE_HEADERS
|
||||||
|
},
|
||||||
|
timeout: 15000,
|
||||||
|
validateStatus: () => true
|
||||||
|
});
|
||||||
|
checkResult(result, refreshToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prompt片段提交
|
||||||
|
*
|
||||||
|
* @param query prompt
|
||||||
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
|
*/
|
||||||
|
async function promptSnippetSubmit(query: string, refreshToken: string) {
|
||||||
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
|
const result = await axios.post('https://kimi.moonshot.cn/api/prompt-snippet/instance', {
|
||||||
|
"offset": 0,
|
||||||
|
"size": 10,
|
||||||
|
"query": query.replace('user:', '').replace('assistant:', '')
|
||||||
|
}, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
Referer: 'https://kimi.moonshot.cn/',
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
},
|
},
|
||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
@@ -168,9 +237,10 @@ async function removeConversation(convId: string, refreshToken: string) {
|
|||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
* @param useSearch 是否开启联网搜索
|
* @param useSearch 是否开启联网搜索
|
||||||
|
* @param refConvId 引用会话ID
|
||||||
* @param retryCount 重试次数
|
* @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 () => {
|
return (async () => {
|
||||||
logger.info(messages);
|
logger.info(messages);
|
||||||
|
|
||||||
@@ -183,18 +253,24 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
|||||||
.catch(err => logger.error(err));
|
.catch(err => logger.error(err));
|
||||||
|
|
||||||
// 创建会话
|
// 创建会话
|
||||||
const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken);
|
const convId = /[0-9a-zA-Z]{20}/.test(refConvId) ? refConvId : await createConversation("未命名会话", refreshToken);
|
||||||
|
|
||||||
// 请求流
|
// 请求流
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
|
const sendMessages = messagesPrepare(messages, !!refConvId);
|
||||||
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
||||||
messages: messagesPrepare(messages),
|
messages: sendMessages,
|
||||||
refs,
|
refs,
|
||||||
use_search: useSearch
|
use_search: useSearch
|
||||||
}, {
|
}, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||||
|
'Priority': 'u=1, i',
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
},
|
},
|
||||||
// 120秒超时
|
// 120秒超时
|
||||||
@@ -209,7 +285,10 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
|||||||
logger.success(`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`);
|
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));
|
.catch(err => console.error(err));
|
||||||
|
|
||||||
return answer;
|
return answer;
|
||||||
@@ -220,7 +299,7 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
|||||||
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
||||||
return (async () => {
|
return (async () => {
|
||||||
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
|
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;
|
throw err;
|
||||||
@@ -234,9 +313,10 @@ async function createCompletion(model = MODEL_NAME, messages: any[], refreshToke
|
|||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
* @param useSearch 是否开启联网搜索
|
* @param useSearch 是否开启联网搜索
|
||||||
|
* @param refConvId 引用会话ID
|
||||||
* @param retryCount 重试次数
|
* @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 () => {
|
return (async () => {
|
||||||
logger.info(messages);
|
logger.info(messages);
|
||||||
|
|
||||||
@@ -249,20 +329,26 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
|||||||
.catch(err => logger.error(err));
|
.catch(err => logger.error(err));
|
||||||
|
|
||||||
// 创建会话
|
// 创建会话
|
||||||
const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken);
|
const convId = /[0-9a-zA-Z]{20}/.test(refConvId) ? refConvId : await createConversation("未命名会话", refreshToken);
|
||||||
|
|
||||||
// 请求流
|
// 请求流
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
|
const sendMessages = messagesPrepare(messages, !!refConvId);
|
||||||
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
const result = await axios.post(`https://kimi.moonshot.cn/api/chat/${convId}/completion/stream`, {
|
||||||
messages: messagesPrepare(messages),
|
messages: sendMessages,
|
||||||
refs,
|
refs,
|
||||||
use_search: useSearch
|
use_search: useSearch
|
||||||
}, {
|
}, {
|
||||||
// 120秒超时
|
// 120秒超时
|
||||||
timeout: 120000,
|
timeout: 120000,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
|
||||||
|
'Priority': 'u=1, i',
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
},
|
},
|
||||||
validateStatus: () => true,
|
validateStatus: () => true,
|
||||||
@@ -273,7 +359,10 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
|||||||
return createTransStream(model, convId, result.data, () => {
|
return createTransStream(model, convId, result.data, () => {
|
||||||
logger.success(`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`);
|
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));
|
.catch(err => console.error(err));
|
||||||
});
|
});
|
||||||
})()
|
})()
|
||||||
@@ -283,7 +372,7 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
|||||||
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
||||||
return (async () => {
|
return (async () => {
|
||||||
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
|
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;
|
throw err;
|
||||||
@@ -298,11 +387,15 @@ async function createCompletionStream(model = MODEL_NAME, messages: any[], refre
|
|||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
*/
|
*/
|
||||||
async function fakeRequest(refreshToken: string) {
|
async function fakeRequest(refreshToken: string) {
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
const options = {
|
const options = {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -360,14 +453,28 @@ function extractRefFileUrls(messages: any[]) {
|
|||||||
* user:新消息
|
* user:新消息
|
||||||
*
|
*
|
||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
|
* @param isRefConv 是否为引用会话
|
||||||
*/
|
*/
|
||||||
function messagesPrepare(messages: any[]) {
|
function messagesPrepare(messages: any[], isRefConv = false) {
|
||||||
// 注入消息提升注意力
|
let content;
|
||||||
let latestMessage = messages[messages.length - 1];
|
if (isRefConv || messages.length < 2) {
|
||||||
let hasFileOrImage = Array.isArray(latestMessage.content)
|
content = messages.reduce((content, message) => {
|
||||||
&& latestMessage.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type'])));
|
if (_.isArray(message.content)) {
|
||||||
// 第二轮开始注入system prompt
|
return message.content.reduce((_content, v) => {
|
||||||
if (messages.length > 2) {
|
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) {
|
if (hasFileOrImage) {
|
||||||
let newFileMessage = {
|
let newFileMessage = {
|
||||||
"content": "关注用户最新发送文件和消息",
|
"content": "关注用户最新发送文件和消息",
|
||||||
@@ -383,18 +490,18 @@ function messagesPrepare(messages: any[]) {
|
|||||||
messages.splice(messages.length - 1, 0, newTextMessage);
|
messages.splice(messages.length - 1, 0, newTextMessage);
|
||||||
logger.info("注入提升尾部消息注意力system prompt");
|
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 [
|
return [
|
||||||
{ role: 'user', content }
|
{ role: 'user', content }
|
||||||
]
|
]
|
||||||
@@ -418,15 +525,19 @@ function wrapUrlsToTags(content: string) {
|
|||||||
* @param refreshToken 用于刷新access_token的refresh_token
|
* @param refreshToken 用于刷新access_token的refresh_token
|
||||||
*/
|
*/
|
||||||
async function preSignUrl(filename: string, refreshToken: string) {
|
async function preSignUrl(filename: string, refreshToken: string) {
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
const result = await axios.post('https://kimi.moonshot.cn/api/pre-sign-url', {
|
const result = await axios.post('https://kimi.moonshot.cn/api/pre-sign-url', {
|
||||||
action: 'file',
|
action: 'file',
|
||||||
name: filename
|
name: filename
|
||||||
}, {
|
}, {
|
||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
},
|
},
|
||||||
validateStatus: () => true
|
validateStatus: () => true
|
||||||
@@ -495,7 +606,10 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
|
|||||||
// 获取文件的MIME类型
|
// 获取文件的MIME类型
|
||||||
mimeType = mimeType || mime.getType(filename);
|
mimeType = mimeType || mime.getType(filename);
|
||||||
// 上传文件到目标OSS
|
// 上传文件到目标OSS
|
||||||
const token = await acquireToken(refreshToken);
|
const {
|
||||||
|
accessToken,
|
||||||
|
userId
|
||||||
|
} = await acquireToken(refreshToken);
|
||||||
let result = await axios.request({
|
let result = await axios.request({
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
url: uploadUrl,
|
url: uploadUrl,
|
||||||
@@ -506,41 +620,58 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
|
|||||||
timeout: 120000,
|
timeout: 120000,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': mimeType,
|
'Content-Type': mimeType,
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
...FAKE_HEADERS
|
...FAKE_HEADERS
|
||||||
},
|
},
|
||||||
validateStatus: () => true
|
validateStatus: () => true
|
||||||
});
|
});
|
||||||
checkResult(result, refreshToken);
|
checkResult(result, refreshToken);
|
||||||
|
|
||||||
// 获取文件上传结果
|
let fileId, status, startTime = Date.now();
|
||||||
result = await axios.post('https://kimi.moonshot.cn/api/file', {
|
while (status != 'initialized') {
|
||||||
type: 'file',
|
if (Date.now() - startTime > 30000)
|
||||||
name: filename,
|
throw new Error('文件等待处理超时');
|
||||||
object_name: objectName,
|
// 获取文件上传结果
|
||||||
timeout: 15000
|
result = await axios.post('https://kimi.moonshot.cn/api/file', {
|
||||||
}, {
|
type: 'file',
|
||||||
headers: {
|
name: filename,
|
||||||
Authorization: `Bearer ${token}`,
|
object_name: objectName,
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
timeout: 15000
|
||||||
...FAKE_HEADERS
|
}, {
|
||||||
}
|
headers: {
|
||||||
});
|
Authorization: `Bearer ${accessToken}`,
|
||||||
const { id: fileId } = checkResult(result, refreshToken);
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
|
...FAKE_HEADERS
|
||||||
|
}
|
||||||
|
});
|
||||||
|
({ id: fileId, status } = checkResult(result, refreshToken));
|
||||||
|
}
|
||||||
|
|
||||||
// 处理文件转换
|
startTime = Date.now();
|
||||||
result = await axios.post('https://kimi.moonshot.cn/api/file/parse_process', {
|
let parseFinish = false;
|
||||||
ids: [fileId],
|
while (!parseFinish) {
|
||||||
timeout: 120000
|
if (Date.now() - startTime > 30000)
|
||||||
}, {
|
throw new Error('文件等待处理超时');
|
||||||
headers: {
|
// 处理文件转换
|
||||||
Authorization: `Bearer ${token}`,
|
parseFinish = await new Promise(resolve => {
|
||||||
Referer: `https://kimi.moonshot.cn/`,
|
axios.post('https://kimi.moonshot.cn/api/file/parse_process', {
|
||||||
...FAKE_HEADERS
|
ids: [fileId],
|
||||||
}
|
timeout: 120000
|
||||||
});
|
}, {
|
||||||
checkResult(result, refreshToken);
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
Referer: `https://kimi.moonshot.cn/`,
|
||||||
|
'X-Traffic-Id': userId,
|
||||||
|
...FAKE_HEADERS
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(() => resolve(true))
|
||||||
|
.catch(() => resolve(false));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return fileId;
|
return fileId;
|
||||||
}
|
}
|
||||||
@@ -610,7 +741,7 @@ async function receiveStream(model: string, convId: string, stream: any) {
|
|||||||
}
|
}
|
||||||
// 处理联网搜索
|
// 处理联网搜索
|
||||||
else if (!silentSearch && result.event == 'search_plus' && result.msg && result.msg.type == 'get_res')
|
else if (!silentSearch && result.event == 'search_plus' && result.msg && result.msg.type == 'get_res')
|
||||||
refContent += `${result.msg.title}(${result.msg.url})\n`;
|
refContent += `${result.msg.title} - ${result.msg.url}\n`;
|
||||||
// else
|
// else
|
||||||
// logger.warn(result.event, result);
|
// logger.warn(result.event, result);
|
||||||
}
|
}
|
||||||
@@ -707,7 +838,7 @@ function createTransStream(model: string, convId: string, stream: any, endCallba
|
|||||||
choices: [
|
choices: [
|
||||||
{
|
{
|
||||||
index: 0, delta: {
|
index: 0, delta: {
|
||||||
content: `检索 ${result.msg.title}(${result.msg.url}) ...\n`
|
content: `检索 ${result.msg.title} - ${result.msg.url} ...\n`
|
||||||
}, finish_reason: null
|
}, finish_reason: null
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -759,7 +890,7 @@ async function getTokenLiveStatus(refreshToken: string) {
|
|||||||
} = checkResult(result, refreshToken);
|
} = checkResult(result, refreshToken);
|
||||||
return !!(access_token && refresh_token)
|
return !!(access_token && refresh_token)
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch (err) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,22 +13,22 @@ export default {
|
|||||||
|
|
||||||
'/completions': async (request: Request) => {
|
'/completions': async (request: Request) => {
|
||||||
request
|
request
|
||||||
|
.validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
|
||||||
.validate('body.messages', _.isArray)
|
.validate('body.messages', _.isArray)
|
||||||
.validate('headers.authorization', _.isString)
|
.validate('headers.authorization', _.isString)
|
||||||
// refresh_token切分
|
// refresh_token切分
|
||||||
const tokens = chat.tokenSplit(request.headers.authorization);
|
const tokens = chat.tokenSplit(request.headers.authorization);
|
||||||
// 随机挑选一个refresh_token
|
// 随机挑选一个refresh_token
|
||||||
const token = _.sample(tokens);
|
const token = _.sample(tokens);
|
||||||
const model = request.body.model;
|
const { model, conversation_id: convId, messages, stream, use_search } = request.body;
|
||||||
const messages = request.body.messages;
|
if (stream) {
|
||||||
if (request.body.stream) {
|
const stream = await chat.createCompletionStream(model, messages, token, use_search, convId);
|
||||||
const stream = await chat.createCompletionStream(model, messages, token, request.body.use_search);
|
|
||||||
return new Response(stream, {
|
return new Response(stream, {
|
||||||
type: "text/event-stream"
|
type: "text/event-stream"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return await chat.createCompletion(model, messages, token, request.body.use_search);
|
return await chat.createCompletion(model, messages, token, use_search, convId);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,7 @@ import Response from '@/lib/response/Response.ts';
|
|||||||
import chat from "./chat.ts";
|
import chat from "./chat.ts";
|
||||||
import ping from "./ping.ts";
|
import ping from "./ping.ts";
|
||||||
import token from './token.ts';
|
import token from './token.ts';
|
||||||
|
import models from './models.ts';
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
{
|
{
|
||||||
@@ -21,5 +22,6 @@ export default [
|
|||||||
},
|
},
|
||||||
chat,
|
chat,
|
||||||
ping,
|
ping,
|
||||||
token
|
token,
|
||||||
|
models
|
||||||
];
|
];
|
41
src/api/routes/models.ts
Normal file
41
src/api/routes/models.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
|
||||||
|
prefix: '/v1',
|
||||||
|
|
||||||
|
get: {
|
||||||
|
'/models': async () => {
|
||||||
|
return {
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"id": "moonshot-v1",
|
||||||
|
"object": "model",
|
||||||
|
"owned_by": "kimi-free-api"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "moonshot-v1-8k",
|
||||||
|
"object": "model",
|
||||||
|
"owned_by": "kimi-free-api"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "moonshot-v1-32k",
|
||||||
|
"object": "model",
|
||||||
|
"owned_by": "kimi-free-api"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "moonshot-v1-128k",
|
||||||
|
"object": "model",
|
||||||
|
"owned_by": "kimi-free-api"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "moonshot-v1-vision",
|
||||||
|
"object": "model",
|
||||||
|
"owned_by": "kimi-free-api"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -50,13 +50,10 @@ const util = {
|
|||||||
generateCookie() {
|
generateCookie() {
|
||||||
const timestamp = util.unixTimestamp();
|
const timestamp = util.unixTimestamp();
|
||||||
const items = [
|
const items = [
|
||||||
`Hm_lvt_4532beacc312859e0aa3e4a80566b706=${timestamp - Math.round(Math.random() * 2592000)}`,
|
|
||||||
`Hm_lvt_358cae4815e85d48f7e8ab7f3680a74b=${timestamp - Math.round(Math.random() * 2592000)}`,
|
`Hm_lvt_358cae4815e85d48f7e8ab7f3680a74b=${timestamp - Math.round(Math.random() * 2592000)}`,
|
||||||
`_ga=GA1.1.${util.generateRandomString({ length: 10, charset: 'numeric' })}.${timestamp - Math.round(Math.random() * 2592000)}`,
|
`_ga=GA1.1.${util.generateRandomString({ length: 10, charset: 'numeric' })}.${timestamp - Math.round(Math.random() * 2592000)}`,
|
||||||
`_ga_31QPQG2YYD=GS1.1.${timestamp - Math.round(Math.random() * 2592000)}.17.0.${timestamp - Math.round(Math.random() * 2592000)}.0.0.0`,
|
`_ga_YXD8W70SZP=GS1.1.${timestamp - Math.round(Math.random() * 2592000)}.1.1.${timestamp - Math.round(Math.random() * 2592000)}.0.0.0`,
|
||||||
`Hm_lpvt_4532beacc312859e0aa3e4a80566b706=${timestamp - Math.round(Math.random() * 2592000)}`,
|
`Hm_lpvt_358cae4815e85d48f7e8ab7f3680a74b=${timestamp - Math.round(Math.random() * 2592000)}`
|
||||||
`Hm_lpvt_358cae4815e85d48f7e8ab7f3680a74b=${timestamp - Math.round(Math.random() * 2592000)}`,
|
|
||||||
`_ga_YXD8W70SZP=GS1.1.${timestamp - Math.round(Math.random() * 2592000)}.35.1.${timestamp - Math.round(Math.random() * 2592000)}.0.0.0`
|
|
||||||
];
|
];
|
||||||
return items.join('; ');
|
return items.join('; ');
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user