Compare commits

..

No commits in common. "master" and "0.0.12" have entirely different histories.

25 changed files with 245 additions and 3152 deletions

View File

@ -1,46 +0,0 @@
name: Build and Push Docker Image
on:
release:
types: [created]
workflow_dispatch:
inputs:
tag:
description: 'Tag Name'
required: true
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Set tag name
id: tag_name
run: |
if [ "${{ github.event_name }}" = "release" ]; then
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "::set-output name=tag::${{ github.event.inputs.tag }}"
fi
- name: Build and push Docker image with Release tag
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: |
vinlic/kimi-free-api:${{ steps.tag_name.outputs.tag }}
vinlic/kimi-free-api:latest
platforms: linux/amd64,linux/arm64
build-args: TARGETPLATFORM=${{ matrix.platform }}

View File

@ -1,48 +0,0 @@
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 一次,

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
dist/
node_modules/
logs/
.vercel

View File

@ -4,15 +4,14 @@ WORKDIR /app
COPY . /app
RUN yarn install --registry https://registry.npmmirror.com/ && yarn run build
RUN npm i --registry http://registry.npmmirror.com && npm run build
FROM node:lts-alpine
COPY --from=BUILD_IMAGE /app/public /app/public
COPY --from=BUILD_IMAGE /app/configs /app/configs
COPY --from=BUILD_IMAGE /app/package.json /app/package.json
COPY --from=BUILD_IMAGE /app/dist /app/dist
COPY --from=BUILD_IMAGE /app/node_modules /app/node_modules
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

205
README.md
View File

@ -1,102 +1,64 @@
# KIMI AI Free 服务
<hr>
<span>[ 中文 | <a href="README_EN.md">English</a> ]</span>
[![](https://img.shields.io/github/license/llm-red-team/kimi-free-api.svg)](LICENSE)
![](https://img.shields.io/github/license/llm-red-team/kimi-free-api.svg)
![](https://img.shields.io/github/stars/llm-red-team/kimi-free-api.svg)
![](https://img.shields.io/github/forks/llm-red-team/kimi-free-api.svg)
![](https://img.shields.io/docker/pulls/vinlic/kimi-free-api.svg)
支持高速流式输出、支持多轮对话、支持联网搜索、支持智能体对话、支持探索版、支持K1思考模型、支持长文档解读、支持图像解析零配置部署多路token支持自动清理会话痕迹。
支持高速流式输出、支持多轮对话、支持联网搜索、支持长文档解读、支持图像解析零配置部署多路token支持自动清理会话痕迹。
与ChatGPT接口完全兼容。
还有以下十个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)
智谱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)
字节跳动豆包接口转API [doubao-free-api](https://github.com/LLM-Red-Team/doubao-free-api)
字节跳动即梦AI接口转API [jimeng-free-api](https://github.com/LLM-Red-Team/jimeng-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)(当前不可用)
## 目录
* [免责声明](#免责声明)
* [声明](#声明)
* [在线体验](#在线体验)
* [效果示例](#效果示例)
* [接入准备](#接入准备)
* [多账号接入](#多账号接入)
* [Docker部署](#Docker部署)
* [Docker-compose部署](#Docker-compose部署)
* [Render部署](#Render部署)
* [Vercel部署](#Vercel部署)
* [Zeabur部署](#Zeabur部署)
* [原生部署](#原生部署)
* [推荐使用客户端](#推荐使用客户端)
* [接口列表](#接口列表)
* [对话补全](#对话补全)
* [文档解读](#文档解读)
* [图像解析](#图像解析)
* [refresh_token存活检测](#refresh_token存活检测)
* [注意事项](#注意事项)
* [Nginx反代优化](#Nginx反代优化)
* [Token统计](#Token统计)
* [Star History](#star-history)
## 免责声明
## 声明
**逆向API是不稳定的建议前往MoonshotAI官方 https://platform.moonshot.cn/ 付费使用API避免封禁的风险。**
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
## 在线体验
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
此链接仅临时测试功能,不可长期使用,长期使用请自行部署。
https://udify.app/chat/Po0F6BMJ15q5vu2P
## 效果示例
### 验明正身Demo
### 验明正身
![验明正身](./doc/example-1.png)
### 多轮对话Demo
### 多轮对话
![多轮对话](./doc/example-6.png)
### 联网搜索Demo
### 联网搜索
![联网搜索](./doc/example-2.png)
### 智能体对话Demo
此处使用 [翻译通](https://kimi.moonshot.cn/chat/coo6l3pkqq4ri39f36bg) 智能体。
![智能体对话](./doc/example-7.png)
### 长文档解读Demo
### 长文档解读
![长文档解读](./doc/example-5.png)
### 图像OCR Demo
### 图像解析
![图像解析](./doc/example-3.png)
@ -108,17 +70,13 @@ MiniMax海螺AI接口转API [hailuo-free-api](https://github.com/LLM-Red-T
从 [kimi.moonshot.cn](https://kimi.moonshot.cn) 获取refresh_token
进入kimi随便发起一个对话然后F12打开开发者工具从Application > Local Storage中找到`refresh_token`的值这将作为Authorization的Bearer Token值`Authorization: Bearer TOKEN`
进入kimi随便发起一个对话然后F12打开开发者工具从Application > Local Storage中找到refresh_token的值这将作为Authorization的Bearer Token值`Authorization: Bearer TOKEN`
![example0](./doc/example-0.png)
如果你看到的`refresh_token`是一个数组,请使用`.`拼接起来再使用。
![example8](./doc/example-8.jpg)
### 多账号接入
目前kimi限制普通账号每3小时内只能进行30轮长文本的问答(短文本不限)你可以通过提供多个账号的refresh_token并使用`,`拼接提供:
目前kimi限制普通账号每3小时内只能进行30轮长文本的问答你可以通过提供多个账号的refresh_token并使用`,`拼接提供:
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
@ -126,7 +84,7 @@ MiniMax海螺AI接口转API [hailuo-free-api](https://github.com/LLM-Red-T
## Docker部署
请准备能够部署Docker镜像且能够访问网络的设备或服务器并将8000端口开放。
请准备一台具有公网IP的服务器并将8000端口开放。
拉取镜像并启动服务
@ -168,39 +126,6 @@ services:
- TZ=Asia/Shanghai
```
### Render部署
**注意部分部署区域可能无法连接kimi如容器日志出现请求超时或无法连接新加坡实测不可用请切换其他区域部署**
**注意免费账户的容器实例将在一段时间不活动时自动停止运行这会导致下次请求时遇到50秒或更长的延迟建议查看[Render容器保活](https://github.com/LLM-Red-Team/free-api-hub/#Render%E5%AE%B9%E5%99%A8%E4%BF%9D%E6%B4%BB)**
1. fork本项目到你的github账号下。
2. 访问 [Render](https://dashboard.render.com/) 并登录你的github账号。
3. 构建你的 Web ServiceNew+ -> Build and deploy from a Git repository -> Connect你fork的项目 -> 选择部署区域 -> 选择实例类型为Free -> Create Web Service
4. 等待构建完成后复制分配的域名并拼接URL访问即可。
### Vercel部署
**注意Vercel免费账户的请求响应超时时间为10秒但接口响应通常较久可能会遇到Vercel返回的504超时错误**
请先确保安装了Node.js环境。
```shell
npm i -g vercel --registry http://registry.npmmirror.com
vercel login
git clone https://github.com/LLM-Red-Team/kimi-free-api
cd kimi-free-api
vercel --prod
```
### Zeabur部署
**注意:免费账户的容器实例可能无法稳定运行**
[![Deploy on Zeabur](https://zeabur.com/button.svg)](https://zeabur.com/templates/GRFYBP)
## 原生部署
请准备一台具有公网IP的服务器并将8000端口开放。
@ -249,14 +174,6 @@ 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/) 等线上服务接入使用。
@ -276,19 +193,6 @@ Authorization: Bearer [refresh_token]
请求数据:
```json
{
// 模型名称
// kimi默认模型
// kimi-search联网检索模型
// kimi-research探索版模型
// kimi-k1K1模型
// kimi-math数学模型
// kimi-silent不输出检索过程模型
// search/research/k1/math/silent可自由组合使用
// 如果使用kimi+智能体model请填写智能体ID就是浏览器地址栏上尾部的一串英文+数字20个字符的ID
"model": "kimi",
// 目前多轮对话基于消息合并实现某些场景可能导致能力下降且受单轮最大Token数限制
// 如果您想获得原生的多轮对话体验可以传入首轮消息获得的id来接续上下文注意如果使用这个首轮必须传none否则第二轮会空响应
// "conversation_id": "cnndivilnl96vah411dg",
"messages": [
{
"role": "user",
@ -305,7 +209,6 @@ Authorization: Bearer [refresh_token]
响应数据:
```json
{
// 如果想获得原生多轮对话体验此id你可以传入到下一轮对话的conversation_id来接续上下文
"id": "cnndivilnl96vah411dg",
"model": "kimi",
"object": "chat.completion",
@ -320,9 +223,9 @@ Authorization: Bearer [refresh_token]
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
"created": 1710152062
}
@ -343,16 +246,6 @@ Authorization: Bearer [refresh_token]
请求数据:
```json
{
// 模型名称
// kimi默认模型
// kimi-search联网检索模型
// kimi-research探索版模型
// kimi-k1K1模型
// kimi-math数学模型
// kimi-silent不输出检索过程模型
// search/research/k1/math/silent可自由组合使用
// 如果使用kimi+智能体model请填写智能体ID就是浏览器地址栏上尾部的一串英文+数字20个字符的ID
"model": "kimi",
"messages": [
{
"role": "user",
@ -392,15 +285,15 @@ Authorization: Bearer [refresh_token]
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
"created": 100920
}
```
### 图像OCR
### 图像解析
提供一个可访问的图像URL或者BASE64_URL进行解析。
@ -417,16 +310,6 @@ Authorization: Bearer [refresh_token]
请求数据:
```json
{
// 模型名称
// kimi默认模型
// kimi-search联网检索模型
// kimi-research探索版模型
// kimi-k1K1模型
// kimi-math数学模型
// kimi-silent不输出检索过程模型
// search/research/k1/math/silent可自由组合使用
// 如果使用kimi+智能体model请填写智能体ID就是浏览器地址栏上尾部的一串英文+数字20个字符的ID
"model": "kimi",
"messages": [
{
"role": "user",
@ -466,34 +349,14 @@ Authorization: Bearer [refresh_token]
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
"created": 1710123627
}
```
### refresh_token存活检测
检测refresh_token是否存活如果存活live为true否则为false请不要频繁小于10分钟调用此接口。
**POST /token/check**
请求数据:
```json
{
"token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9..."
}
```
响应数据:
```json
{
"live": true
}
```
## 注意事项
### Nginx反代优化
@ -512,11 +375,3 @@ tcp_nodelay on;
# 设置保持连接的超时时间这里设置为120秒。如果在这段时间内客户端和服务器之间没有进一步的通信连接将被关闭。
keepalive_timeout 120;
```
### Token统计
由于推理侧不在kimi-free-api因此token不可统计将以固定数字返回!!!!!
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=LLM-Red-Team/kimi-free-api&type=Date)](https://star-history.com/#LLM-Red-Team/kimi-free-api&Date)

View File

@ -1,504 +0,0 @@
# KIMI AI Free Service
<hr>
[![](https://img.shields.io/github/license/llm-red-team/kimi-free-api.svg)](LICENSE)
![](https://img.shields.io/github/stars/llm-red-team/kimi-free-api.svg)
![](https://img.shields.io/github/forks/llm-red-team/kimi-free-api.svg)
![](https://img.shields.io/docker/pulls/vinlic/kimi-free-api.svg)
Supports high-speed streaming output, multi-turn dialogues, internet search, long document reading, image analysis, zero-configuration deployment, multi-token support, and automatic session trace cleanup.
Fully compatible with the ChatGPT interface.
Also, the following free APIs are available for your attention:
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 (ChatGLM) API to API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
Meta Sota (metaso) API to API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-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) (OUT OF ORDER)
## Table of Contents
* [Announcement](#Announcement)
* [Online experience](#Online-Experience)
* [Effect Examples](#Effect-Examples)
* [Access preparation](#Access-Preparation)
* [Multiple account access](#Multi-Account-Access)
* [Docker Deployment](#Docker-Deployment)
* [Docker-compose Deployment](#Docker-compose-Deployment)
* [Render Deployment](Render-Deployment)
* [Vercel Deployment](#Vercel-Deployment)
* [Zeabur Deployment](#Zeabur-Deployment)
* [Native Deployment](#Native-Deployment)
* [Interface List](#Interface-List)
* [Conversation completion](#conversation-completion)
* [Document Interpretation](#document-interpretation)
* [Image analysis](#image-analysis)
* [refresh_token survival detection](#refresh_token-survival-detection)
* [Precautions](#Precautions)
* [Nginx anti-generation optimization](#Nginx-anti-generation-optimization)
* [Token statistics](#Token-statistics)
* [Star History](#star-history)
## Announcement
**This API is unstable. So we highly recommend you go to the [MoonshotAI](https://platform.moonshot.cn/) use the offical API, avoiding banned.**
**This organization and individuals do not accept any financial donations and transactions. This project is purely for research, communication, and learning purposes!**
**For personal use only, it is forbidden to provide services or commercial use externally to avoid causing service pressure on the official, otherwise, bear the risk yourself!**
**For personal use only, it is forbidden to provide services or commercial use externally to avoid causing service pressure on the official, otherwise, bear the risk yourself!**
**For personal use only, it is forbidden to provide services or commercial use externally to avoid causing service pressure on the official, otherwise, bear the risk yourself!**
## Online Experience
This link is only for temporary testing of functions and cannot be used for a long time. For long-term use, please deploy by yourself.
https://udify.app/chat/Po0F6BMJ15q5vu2P
## Effect Examples
### Identity Verification
![Identity Verification](./doc/example-1.png)
### Multi-turn Dialogue
![Multi-turn Dialogue](./doc/example-6.png)
### Internet Search
![Internet Search](./doc/example-2.png)
### Long Document Reading
![Long Document Reading](./doc/example-5.png)
### Image Analysis
![Image Analysis](./doc/example-3.png)
### Consistent Responsiveness
![Consistent Responsiveness](https://github.com/LLM-Red-Team/kimi-free-api/assets/20235341/48c7ec00-2b03-46c4-95d0-452d3075219b)
## Access Preparation
Get the `refresh_token` from [kimi.moonshot.cn](https://kimi.moonshot.cn)
Start a conversation with kimi at will, then open the developer tool with F12, and find the value of `refresh_token` from Application > Local Storage, which will be used as the value of the Bearer Token in Authorization: `Authorization: Bearer TOKEN`
![example0](./doc/example-0.png)
If you see `refresh_token` as an array, please use `.` to join it before using.
![example8](./doc/example-8.jpg)
### Multi-Account Access
Currently, kimi limits ordinary accounts to only 30 rounds of long-text Q&A within every 3 hours (short text is unlimited). You can provide multiple account refresh_tokens and use `,` to join them:
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
The service will pick one each time a request is made.
## Docker Deployment
Please prepare a server with a public IP and open port 8000.
Pull the image and start the service
```shell
docker run -it -d --init --name kimi-free-api -p 8000:8000 -e TZ=Asia/Shanghai vinlic/kimi-free-api:latest
```
check real-time service logs
```shell
docker logs -f kimi-free-api
```
Restart service
```shell
docker restart kimi-free-api
```
Shut down service
```shell
docker stop kimi-free-api
```
### Docker-compose Deployment
```yaml
version: '3'
services:
kimi-free-api:
container_name: kimi-free-api
image: vinlic/kimi-free-api:latest
restart: always
ports:
- "8000:8000"
environment:
- TZ=Asia/Shanghai
```
### Render Deployment
**Attention: Some deployment regions may not be able to connect to Kimi. If container logs show request timeouts or connection failures (Singapore has been tested and found unavailable), please switch to another deployment region!**
**Attention Container instances for free accounts will automatically stop after a period of inactivity, which may result in a 50-second or longer delay during the next request. It is recommended to check [Render Container Keepalive](https://github.com/LLM-Red-Team/free-api-hub/#Render%E5%AE%B9%E5%99%A8%E4%BF%9D%E6%B4%BB)**
1. Fork this project to your GitHub account.
2. Visit [Render](https://dashboard.render.com/) and log in with your GitHub account.
3. Build your Web Service (New+ -> Build and deploy from a Git repository -> Connect your forked project -> Select deployment region -> Choose instance type as Free -> Create Web Service).
4. After the build is complete, copy the assigned domain and append the URL to access it.
### Vercel Deployment
**Note: Vercel free accounts have a request response timeout of 10 seconds, but interface responses are usually longer, which may result in a 504 timeout error from Vercel!**
Please ensure that Node.js environment is installed first.
```shell
npm i -g vercel --registry http://registry.npmmirror.com
vercel login
git clone https://github.com/LLM-Red-Team/kimi-free-api
cd kimi-free-api
vercel --prod
```
### Zeabur Deployment
[![Deploy on Zeabur](https://zeabur.com/button.svg)](https://zeabur.com/templates/GRFYBP)
## Native Deployment
Please prepare a server with a public IP and open port 8000.
Please install the Node.js environment and configure the environment variables first, and confirm that the node command is available.
Install dependencies
```shell
npm i
```
Install PM2 for process guarding
```shell
npm i -g pm2
```
Compile and build. When you see the dist directory, the build is complete.
```shell
npm run build
```
Start service
```shell
pm2 start dist/index.js --name "kimi-free-api"
```
View real-time service logs
```shell
pm2 logs kimi-free-api
```
Restart service
```shell
pm2 reload kimi-free-api
```
Shut down service
```shell
pm2 stop kimi-free-api
```
## Recommended Clients
Using the following second-developed clients for free-api series projects is faster and easier, and supports document/image uploads!
[Clivia](https://github.com/Yanyutin753/lobe-chat)'s modified LobeChat [https://github.com/Yanyutin753/lobe-chat](https://github.com/Yanyutin753/lobe-chat)
[Time@](https://github.com/SuYxh)'s modified ChatGPT Web [https://github.com/SuYxh/chatgpt-web-sea](https://github.com/SuYxh/chatgpt-web-sea)
## 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.
### Conversation completion
Conversation completion interface, compatible with openai's [chat-completions-api](https://platform.openai.com/docs/guides/text-generation/chat-completions-api).
**POST /v1/chat/completions**
The header needs to set the Authorization header:
```
Authorization: Bearer [refresh_token]
```
Request data:
```json
{
// Model name
// kimi: default model
// kimi-search: online search model
// kimi-research: exploration version model
// kimi-k1: K1 model
// kimi-math: math model
// kimi-silent: model without search process output
// search/research/k1/math/silent: can be freely combined
// If using kimi+agent, fill in the agent ID for model, which is the 20-character ID of letters and numbers at the end of the browser address bar
"model": "kimi",
"messages": [
{
"role": "user",
"content": "test"
}
],
// Whether to enable online search, default false
"use_search": true,
// If using SSE stream, please set it to true, the default is false
"stream": false
}
```
Response data:
```json
{
"id": "cnndivilnl96vah411dg",
"model": "kimi",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I am Kimi, an artificial intelligence assistant developed by Dark Side of the Moon Technology Co., Ltd. I am good at conversation in Chinese and English. I can help you obtain information, answer questions, and read and understand the documents you provide. and web content. If you have any questions or need help, feel free to let me know!"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2
},
"created": 1710152062
}
```
### Document interpretation
Provide an accessible file URL or BASE64_URL to parse.
**POST /v1/chat/completions**
The header needs to set the Authorization header:
```
Authorization: Bearer [refresh_token]
```
Request data:
```json
{
// Model name
// kimi: default model
// kimi-search: online search model
// kimi-research: exploration version model
// kimi-k1: K1 model
// kimi-math: math model
// kimi-silent: model without search process output
// search/research/k1/math/silent: can be freely combined
// If using kimi+agent, fill in the agent ID for model, which is the 20-character ID of letters and numbers at the end of the browser address bar
"model": "kimi",
"messages": [
{
"role": "user",
"content": [
{
"type": "file",
"file_url": {
"url": "https://mj101-1317487292.cos.ap-shanghai.myqcloud.com/ai/test.pdf"
}
},
{
"type": "text",
"text": "What does the document say?"
}
]
}
],
// It is recommended to turn off online search to prevent interference in interpreting results.
"use_search": false
}
```
Response data:
```json
{
"id": "cnmuo7mcp7f9hjcmihn0",
"model": "kimi",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The document contains several examples of ancient magical spells from magical texts from the ancient Greek and Roman periods known as PGM (Papyri Graecae Magicae). The following are examples of several spells mentioned in the document Contents:\n\n1. The first spell (PMG 4.1390 1495) describes a ritual that requires leaving some of your leftover bread, dividing it into seven small pieces, and then going to the heroes, gladiators, and those who died violent deaths The place where people were killed. Spell a spell on the piece of bread and throw it out, then pick up some contaminated soil from the ritual site and throw it into the home of the woman you like, then go to sleep. The content of the spell is to pray to the goddess of fate (Moirai), The Roman goddesses of Fates and the forces of nature (Daemons) were invoked to help make wishes come true.\n\n2. The second incantation (PMG 4.1342 57) was a summoning spell performed by speaking a series of mystical names and Words to summon a being called Daemon to cause a person named Tereous (born from Apia) to be mentally and emotionally tortured until she came to the spellcaster Didymos (born from Taipiam).\n \n3. The third spell (PGM 4.1265 74) mentions a mysterious name called NEPHERIĒRI, which is related to Aphrodite, the goddess of love. In order to win the heart of a beautiful woman, one needs to keep it for three days of purity, offer frankincense and recite the name while offering the offering. Then, as you approach the lady, recite the name silently seven times in your mind and do this for seven consecutive days with the hope of success.\n\n4. The fourth mantra ( PGM 4.1496 1) describes an incantation recited while burning myrrh. This incantation is a prayer to myrrh in the hope that it will attract a person named [name ] woman (her mother's name was [name]), making her unable to sit, eat, look at or kiss other people, but instead had only the caster in her mind until she came to the caster.\n\nThese Spells reflect ancient people's beliefs in magic and supernatural powers, and the ways in which they attempted to influence the emotions and behavior of others through these spells."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2
},
"created": 100920
}
```
### Image analysis
Provide an accessible image URL or BASE64_URL to parse.
This format is compatible with the [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API format. You can also use this format to transmit documents for parsing.
**POST /v1/chat/completions**
The header needs to set the Authorization header:
```
Authorization: Bearer [refresh_token]
```
Request data:
```json
{
// Model name
// kimi: default model
// kimi-search: online search model
// kimi-research: exploration version model
// kimi-k1: K1 model
// kimi-math: math model
// kimi-silent: model without search process output
// search/research/k1/math/silent: can be freely combined
// If using kimi+agent, fill in the agent ID for model, which is the 20-character ID of letters and numbers at the end of the browser address bar
"model": "kimi",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://www.moonshot.cn/assets/logo/normal-dark.png"
}
},
{
"type": "text",
"text": "What does the image describe?"
}
]
}
],
// It is recommended to turn off online search to prevent interference in interpreting results.
"use_search": false
}
```
Response data:
```json
{
"id": "cnn6l8ilnl92l36tu8ag",
"model": "kimi",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The image shows the words "Moonshot AI", which may be the logo or brand identity of Dark Side of the Moon Technology Co., Ltd. (Moonshot AI). Usually such images are used to represent a company or product and convey brand information .Since the image is in PNG format, it could be a logo with a transparent background, used on a website, app, or other visual material."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2
},
"created": 1710123627
}
```
### refresh_token survival detection
Check whether refresh_token is alive. If live is not true, otherwise it is false. Please do not call this interface frequently (less than 10 minutes).
**POST /token/check**
Request data:
```json
{
"token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9..."
}
```
Response data:
```json
{
"live": true
}
```
## Notification
### Nginx anti-generation optimization
If you are using Nginx reverse proxy kimi-free-api, please add the following configuration items to optimize the output effect of the stream and optimize the experience.
```nginx
# Turn off proxy buffering. When set to off, Nginx will immediately send client requests to the backend server and immediately send responses received from the backend server back to the client.
proxy_buffering off;
# Enable chunked transfer encoding. Chunked transfer encoding allows servers to send data in chunks for dynamically generated content without knowing the size of the content in advance.
chunked_transfer_encoding on;
# Turn on TCP_NOPUSH, which tells Nginx to send as much data as possible before sending the packet to the client. This is usually used in conjunction with sendfile to improve network efficiency.
tcp_nopush on;
# Turn on TCP_NODELAY, which tells Nginx not to delay sending data and to send small data packets immediately. In some cases, this can reduce network latency.
tcp_nodelay on;
#Set the timeout to keep the connection, here it is set to 120 seconds. If there is no further communication between client and server during this time, the connection will be closed.
keepalive_timeout 120;
```
### Token statistics
Since the inference side is not in kimi-free-api, the token cannot be counted and will be returned as a fixed number!!!!!
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=LLM-Red-Team/kimi-free-api&type=Date)](https://star-history.com/#LLM-Red-Team/kimi-free-api&Date)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

View File

@ -1,6 +1,6 @@
{
"name": "kimi-free-api",
"version": "0.0.36",
"version": "0.0.12",
"description": "Kimi Free API Server",
"type": "module",
"main": "dist/index.js",
@ -13,8 +13,8 @@
"dist/"
],
"scripts": {
"dev": "tsup src/index.ts --format cjs,esm --sourcemap --dts --publicDir public --watch --onSuccess \"node --enable-source-maps --no-node-snapshot dist/index.js\"",
"start": "node --enable-source-maps --no-node-snapshot dist/index.js",
"dev": "tsup src/index.ts --format cjs,esm --sourcemap --dts --publicDir public --watch --onSuccess \"node dist/index.js\"",
"start": "node dist/index.js",
"build": "tsup src/index.ts --format cjs,esm --sourcemap --dts --clean --publicDir public"
},
"author": "Vinlic",

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>🚀 服务已启动</title>
</head>
<body>
<p>kimi-free-api已启动<br>请通过LobeChat / NextChat / Dify等客户端或OpenAI SDK接入</p>
</body>
</html>

View File

@ -5,6 +5,5 @@ export default {
API_TOKEN_EXPIRES: [-2002, 'Token已失效'],
API_FILE_URL_INVALID: [-2003, '远程文件URL非法'],
API_FILE_EXECEEDS_SIZE: [-2004, '远程文件超出大小'],
API_CHAT_STREAM_PUSHING: [-2005, '已有对话流正在输出'],
API_RESEARCH_EXCEEDS_LIMIT: [-2006, '探索版使用量已达到上限']
API_CHAT_STREAM_PUSHING: [-2005, '已有对话流正在输出']
}

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +0,0 @@
export default interface IStreamMessage {
id: string;
model: string;
object: string;
choices: {
index: number;
message: {
role: string;
content: string;
};
finish_reason: string;
}[];
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
segment_id?: string;
created: number;
}

View File

@ -13,26 +13,21 @@ 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);
let { model, conversation_id: convId, messages, stream, use_search } = request.body;
if(use_search)
model = 'kimi-search';
if (stream) {
const stream = await chat.createCompletionStream(model, messages, token, convId);
const messages = request.body.messages;
if (request.body.stream) {
const stream = await chat.createCompletionStream(request.body.messages, token, request.body.use_search);
return new Response(stream, {
type: "text/event-stream"
});
}
else
return await chat.createCompletion(model, messages, token, convId);
return await chat.createCompletion(messages, token, request.body.use_search);
}
}

View File

@ -1,27 +1,5 @@
import fs from 'fs-extra';
import Response from '@/lib/response/Response.ts';
import chat from "./chat.ts";
import ping from "./ping.ts";
import token from './token.ts';
import models from './models.ts';
export default [
{
get: {
'/': async () => {
const content = await fs.readFile('public/welcome.html');
return new Response(content, {
type: 'html',
headers: {
Expires: '-1'
}
});
}
}
},
chat,
ping,
token,
models
chat
];

View File

@ -1,41 +0,0 @@
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"
}
]
};
}
}
}

View File

@ -1,6 +0,0 @@
export default {
prefix: '/ping',
get: {
'': async () => "pong"
}
}

View File

@ -1,25 +0,0 @@
import _ from 'lodash';
import Request from '@/lib/request/Request.ts';
import Response from '@/lib/response/Response.ts';
import chat from '@/api/controllers/chat.ts';
import logger from '@/lib/logger.ts';
export default {
prefix: '/token',
post: {
'/check': async (request: Request) => {
request
.validate('body.token', _.isString)
const live = await chat.getTokenLiveStatus(request.body.token);
return {
live
}
}
}
}

View File

@ -29,11 +29,6 @@ export default class Exception extends Error {
this.errmsg = _errmsg || errmsg;
}
compare(exception: (string | number)[]) {
const [errcode] = exception as [number, string];
return this.errcode == errcode;
}
setHTTPStatusCode(value: number) {
this.httpStatusCode = value;
return this;

View File

@ -9,15 +9,13 @@ import { format as dateFormat } from 'date-fns';
import config from './config.ts';
import util from './util.ts';
const isVercelEnv = process.env.VERCEL;
class LogWriter {
#buffers = [];
constructor() {
!isVercelEnv && fs.ensureDirSync(config.system.logDirPath);
!isVercelEnv && this.work();
fs.ensureDirSync(config.system.logDirPath);
this.work();
}
push(content) {
@ -26,16 +24,16 @@ class LogWriter {
}
writeSync(buffer) {
!isVercelEnv && fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
}
async write(buffer) {
!isVercelEnv && await fs.appendFile(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
await fs.appendFile(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
}
flush() {
if(!this.#buffers.length) return;
!isVercelEnv && fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), Buffer.concat(this.#buffers));
fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), Buffer.concat(this.#buffers));
}
work() {

View File

@ -15,7 +15,7 @@ export default class FailureBody extends Body {
else if(error instanceof APIException || error instanceof Exception)
({ errcode, errmsg, data, httpStatusCode } = error);
else if(_.isError(error))
({ errcode, errmsg, data, httpStatusCode } = new Exception(EX.SYSTEM_ERROR, error.message));
error = new Exception(EX.SYSTEM_ERROR, error.message);
super({
code: errcode || -1,
message: errmsg || 'Internal error',

View File

@ -73,11 +73,7 @@ class Server {
this.app.use((ctx: any) => {
const request = new Request(ctx);
logger.debug(`-> ${ctx.request.method} ${ctx.request.url} request is not supported - ${request.remoteIP || "unknown"}`);
// const failureBody = new FailureBody(new Exception(EX.SYSTEM_NOT_ROUTE_MATCHING, "Request is not supported"));
// const response = new Response(failureBody);
const message = `[请求有误]: 正确请求为 POST -> /v1/chat/completions当前请求为 ${ctx.request.method} -> ${ctx.request.url} 请纠正`;
logger.warn(message);
const failureBody = new FailureBody(new Error(message));
const failureBody = new FailureBody(new Exception(EX.SYSTEM_NOT_ROUTE_MATCHING, "Request is not supported"));
const response = new Response(failureBody);
response.injectTo(ctx);
if(config.system.requestLog)

View File

@ -50,10 +50,13 @@ const util = {
generateCookie() {
const timestamp = util.unixTimestamp();
const items = [
`Hm_lvt_4532beacc312859e0aa3e4a80566b706=${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_YXD8W70SZP=GS1.1.${timestamp - Math.round(Math.random() * 2592000)}.1.1.${timestamp - Math.round(Math.random() * 2592000)}.0.0.0`,
`Hm_lpvt_358cae4815e85d48f7e8ab7f3680a74b=${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`,
`Hm_lpvt_4532beacc312859e0aa3e4a80566b706=${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('; ');
},

View File

@ -1,27 +0,0 @@
{
"builds": [
{
"src": "./dist/*.html",
"use": "@vercel/static"
},
{
"src": "./dist/index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/",
"dest": "/dist/welcome.html"
},
{
"src": "/(.*)",
"dest": "/dist",
"headers": {
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Content-Type, Authorization"
}
}
]
}

1555
yarn.lock

File diff suppressed because it is too large Load Diff