Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: LLM-Red-Team/qwen-free-api
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.0.10
Choose a base ref
...
head repository: LLM-Red-Team/qwen-free-api
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Apr 10, 2024

  1. update Dockerfile

    Vinlic committed Apr 10, 2024
    Copy the full SHA
    63995d7 View commit details
  2. Release 0.0.11

    Vinlic committed Apr 10, 2024
    Copy the full SHA
    95d2312 View commit details
  3. Copy the full SHA
    9dc3752 View commit details
  4. Release 0.0.12

    Vinlic committed Apr 10, 2024
    Copy the full SHA
    6ff7972 View commit details

Commits on Apr 11, 2024

  1. Copy the full SHA
    885e33e View commit details
  2. Copy the full SHA
    eb7d10a View commit details
  3. Release 0.0.13

    Vinlic committed Apr 11, 2024
    Copy the full SHA
    dcbf8b1 View commit details
  4. Update README.md

    Vinlic authored Apr 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ed2b2ef View commit details
  5. Update README.md

    Vinlic authored Apr 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    73501bc View commit details

Commits on Apr 12, 2024

  1. update README

    Vinlic committed Apr 12, 2024
    Copy the full SHA
    9121c23 View commit details
  2. update README

    Vinlic committed Apr 12, 2024
    Copy the full SHA
    648d23f View commit details

Commits on Apr 16, 2024

  1. Update README.md

    Vinlic authored Apr 16, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ee62522 View commit details

Commits on Apr 22, 2024

  1. Update README.md

    Vinlic authored Apr 22, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    fedb5ce View commit details

Commits on Apr 24, 2024

  1. Copy the full SHA
    88950b2 View commit details
  2. Merge pull request #1 from LLM-Red-Team/master

    Update README.md
    Yanyutin753 authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e389f58 View commit details
  3. Merge pull request #16 from Yanyutin753/main

    ✨feat support /v1/models to be better use lobechat
    Vinlic authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4f500d5 View commit details
  4. Update README.md

    Vinlic authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    1334aaa View commit details
  5. Update README.md

    KPCOFGS authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    53d0c36 View commit details
  6. Update README.md

    KPCOFGS authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    5785a3e View commit details
  7. Merge pull request #18 from KPCOFGS/master

    更新了README.md文件
    Vinlic authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    30e82c3 View commit details
  8. Update README.md

    KPCOFGS authored Apr 24, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    a3e4956 View commit details

Commits on Apr 25, 2024

  1. Merge pull request #19 from KPCOFGS/master

    更新了README.md文件
    Vinlic authored Apr 25, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    9a7eefa View commit details
  2. Copy the full SHA
    1455261 View commit details
  3. Release 0.0.15

    Vinlic committed Apr 25, 2024
    Copy the full SHA
    25d75dc View commit details
  4. Copy the full SHA
    e84aed2 View commit details
  5. Copy the full SHA
    a2db68f View commit details
  6. Release 0.0.16

    Vinlic committed Apr 25, 2024
    Copy the full SHA
    b3bfb22 View commit details
  7. Copy the full SHA
    84f5596 View commit details
  8. Merge pull request #20 from Yanyutin753/main

    Create sync.yml to update code
    Vinlic authored Apr 25, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    80d37ed View commit details

Commits on Apr 26, 2024

  1. Update README.md

    Vinlic authored Apr 26, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    0f05d42 View commit details

Commits on May 4, 2024

  1. Update README.md

    Vinlic authored May 4, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6234782 View commit details

Commits on May 10, 2024

  1. Copy the full SHA
    574e398 View commit details
  2. update README

    Vinlic committed May 10, 2024
    Copy the full SHA
    22e6f20 View commit details
  3. Copy the full SHA
    3cea08a View commit details

Commits on May 11, 2024

  1. Update README.md

    Vinlic authored May 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    7384f73 View commit details
  2. Update README.md

    Vinlic authored May 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    cbde4fc View commit details
  3. Update README.md

    Vinlic authored May 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    04eed50 View commit details

Commits on May 15, 2024

  1. 去除绘图前置提示词

    Vinlic committed May 15, 2024
    Copy the full SHA
    a8fb1bb View commit details
  2. Release 0.0.17

    Vinlic committed May 15, 2024
    Copy the full SHA
    9c4eabe View commit details
  3. Release 0.0.18

    Vinlic committed May 15, 2024
    Copy the full SHA
    b9d85f3 View commit details
  4. 处理AI绘图字段变更

    Vinlic committed May 15, 2024
    Copy the full SHA
    a9a4f7f View commit details
  5. Release 0.0.19

    Vinlic committed May 15, 2024
    Copy the full SHA
    f727286 View commit details

Commits on Jun 16, 2024

  1. Copy the full SHA
    a9126f1 View commit details
  2. Release 0.0.20

    Vinlic committed Jun 16, 2024
    Copy the full SHA
    bf42bce View commit details

Commits on Dec 4, 2024

  1. Update README.md

    Vinlic authored Dec 4, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e95a7e4 View commit details

Commits on Dec 12, 2024

  1. Update README.md

    添加英文入口与目录层级对齐
    Alex-Yanggg authored Dec 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    2dd2517 View commit details
  2. Create README_EN.md

    Alex-Yanggg authored Dec 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    3015324 View commit details
  3. Update README.md

    Alex-Yanggg authored Dec 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    5e2cb0a View commit details
  4. Merge pull request #52 from Alex-Yanggg/master

    Update the Doc and Create the English version
    Vinlic authored Dec 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    33b16a3 View commit details

Commits on Dec 13, 2024

  1. 修复文档和图像上传

    Vinlic committed Dec 13, 2024
    Copy the full SHA
    9f2e172 View commit details
Showing with 971 additions and 136 deletions.
  1. +48 −0 .github/workflows/sync.yml
  2. +2 −1 .gitignore
  3. +6 −5 Dockerfile
  4. +142 −43 README.md
  5. +510 −0 README_EN.md
  6. BIN doc/example-0.png
  7. BIN doc/example-7.png
  8. +1 −1 package.json
  9. +133 −72 src/api/controllers/chat.ts
  10. +9 −8 src/api/routes/chat.ts
  11. +5 −1 src/api/routes/index.ts
  12. +56 −0 src/api/routes/models.ts
  13. +25 −0 src/api/routes/token.ts
  14. +7 −5 src/lib/logger.ts
  15. +27 −0 vercel.json
48 changes: 48 additions & 0 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
@@ -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/qwen-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/qwen-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/qwen-free-api 上游仓库的 workflow 文件变更,导致 GitHub 自动暂停了本次自动更新,你需要手动 Sync Fork 一次,
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
node_modules/
logs/
logs/
.vercel
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -4,14 +4,15 @@ WORKDIR /app

COPY . /app

RUN npm i --registry http://registry.npmmirror.com && npm run build
RUN yarn install --registry https://registry.npmmirror.com/ && yarn run build

FROM node:lts-alpine

COPY --from=BUILD_IMAGE /app/configs ./configs
COPY --from=BUILD_IMAGE /app/package.json ./package.json
COPY --from=BUILD_IMAGE /app/dist ./dist
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
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/public /app/public
COPY --from=BUILD_IMAGE /app/node_modules /app/node_modules

WORKDIR /app

185 changes: 142 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,94 +1,134 @@
# Qwen AI Free 服务

![](https://img.shields.io/github/license/llm-red-team/qwen-free-api.svg)
<hr>

<span>[ 中文 | <a href="README_EN.md">English</a> ]</span>

[![](https://img.shields.io/github/license/llm-red-team/qwen-free-api.svg)](LICENSE)
![](https://img.shields.io/github/stars/llm-red-team/qwen-free-api.svg)
![](https://img.shields.io/github/forks/llm-red-team/qwen-free-api.svg)
![](https://img.shields.io/docker/pulls/vinlic/qwen-free-api.svg)

支持高速流式输出、支持多轮对话、支持无水印AI绘图、支持长文档解读、图像解析,零配置部署,多路token支持,自动清理会话痕迹。
支持高速流式输出、支持多轮对话、支持无水印AI绘图、支持长文档解读、图像解析、联网检索,零配置部署,多路token支持,自动清理会话痕迹。

与ChatGPT接口完全兼容。

还有以下四个free-api欢迎关注:
还有以下十个free-api欢迎关注:

Moonshot AI(Kimi.ai)接口转API [kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api)

阶跃星辰 (跃问StepChat) 接口转API [step-free-api](https://github.com/LLM-Red-Team/step-free-api)

ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)

聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-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)

* [声明](#声明)
* [在线体验](#在线体验)
* [效果示例](#效果示例)
* [接入准备](#接入准备)
* [Docker部署](#Docker部署)
* [Docker-compose部署](#Docker-compose部署)
* [原生部署](#原生部署)
* [接口列表](#接口列表)
* [对话补全](#对话补全)
* [AI绘图](#AI绘图)
* [文档解读](#文档解读)
* [图像解析](#图像解析)
* [注意事项](#注意事项)
* [Nginx反代优化](#Nginx反代优化)
深度求索(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)

此链接仅临时测试功能,长期使用请自行部署。
## 目录

https://udify.app/chat/qOXzVl5kkvhQXM8r
- [Qwen AI Free 服务](#qwen-ai-free-服务)
- [目录](#目录)
- [免责声明](#免责声明)
- [效果示例](#效果示例)
- [验明正身Demo](#验明正身demo)
- [多轮对话Demo](#多轮对话demo)
- [AI绘图Demo](#ai绘图demo)
- [长文档解读Demo](#长文档解读demo)
- [图像解析Demo](#图像解析demo)
- [10线程并发测试](#10线程并发测试)
- [接入准备](#接入准备)
- [方法1](#方法1)
- [方法2](#方法2)
- [多账号接入](#多账号接入)
- [Docker部署](#docker部署)
- [Docker-compose部署](#docker-compose部署)
- [Render部署](#render部署)
- [Vercel部署](#vercel部署)
- [原生部署](#原生部署)
- [推荐使用客户端](#推荐使用客户端)
- [接口列表](#接口列表)
- [对话补全](#对话补全)
- [AI绘图](#ai绘图)
- [文档解读](#文档解读)
- [图像解析](#图像解析)
- [ticket存活检测](#ticket存活检测)
- [注意事项](#注意事项)
- [Nginx反代优化](#nginx反代优化)
- [Token统计](#token统计)
- [Star History](#star-history)

## 免责声明

**逆向API是不稳定的,建议前往阿里云官方 https://dashscope.console.aliyun.com/ 付费使用API,避免封禁的风险。**

**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**

**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**

**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**

**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**

## 效果示例

### 验明正身
### 验明正身Demo

![验明正身](./doc/example-1.png)

### 多轮对话
### 多轮对话Demo

![多轮对话](./doc/example-2.png)

### AI绘图
### AI绘图Demo

![AI绘图](./doc/example-3.png)

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

![AI绘图](./doc/example-5.png)
![长文本解读](./doc/example-5.png)

### 图像解析
### 图像解析Demo

![AI绘图](./doc/example-6.png)
![图像解析](./doc/example-6.png)

### 10线程并发测试

![10线程并发测试](./doc/example-4.png)

## 接入准备

### 方法1

[通义千问](https://tongyi.aliyun.com/qianwen) 登录

进入通义千问随便发起一个对话,然后F12打开开发者工具,从Application > Cookies中找到`login_tongyi_ticket`的值,这将作为Authorization的Bearer Token值:`Authorization: Bearer TOKEN`
进入通义千问随便发起一个对话,然后F12打开开发者工具,从Application > Cookies中找到`tongyi_sso_ticket`的值,这将作为Authorization的Bearer Token值:`Authorization: Bearer TOKEN`

![获取tongyi_sso_ticket](./doc/example-0.png)

### 方法2

![获取login_tongyi_ticket](./doc/example-0.png)
[阿里云](https://www.aliyun.com/) 登录(如果该账号有服务器等重要资产不建议使用),如果该账号之前未进入过[通义千问](https://tongyi.aliyun.com/qianwen) ,需要先进入同意协议,否则无法生效。

然后F12打开开发者工具,从Application > Cookies中找到`login_aliyunid_ticket`的值,这将作为Authorization的Bearer Token值:`Authorization: Bearer TOKEN`

![获取login_aliyunid_ticket](./doc/example-7.png)

### 多账号接入

你可以通过提供多个账号的login_tongyi_ticket,并使用,拼接提供:
你可以通过提供多个账号的`tongyi_sso_ticket``login_aliyunid_ticket`,并使用,拼接提供:

Authorization: Bearer TOKEN1,TOKEN2,TOKEN3
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`

每次请求服务会从中挑选一个。

@@ -136,6 +176,33 @@ services:
- TZ=Asia/Shanghai
```
### Render部署
**注意:部分部署区域可能无法连接qwen,如容器日志出现请求超时或无法连接,请切换其他区域部署!**
**注意:免费账户的容器实例将在一段时间不活动时自动停止运行,这会导致下次请求时遇到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 Service(New+ -> 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/qwen-free-api
cd qwen-free-api
vercel --prod
```

## 原生部署

请准备一台具有公网IP的服务器并将8000端口开放。
@@ -184,6 +251,14 @@ pm2 reload qwen-free-api
pm2 stop qwen-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/) 等线上服务接入使用。
@@ -197,14 +272,17 @@ pm2 stop qwen-free-api
header 需要设置 Authorization 头部:

```
Authorization: Bearer [login_tongyi_ticket]
Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
```

请求数据:
```json
{
// 模型名称随意填写
"model": "qwen",
// 目前多轮对话基于消息合并实现,某些场景可能导致能力下降且受单轮最大token数限制
// 如果您想获得原生的多轮对话体验,可以传入上一轮消息获得的id,来接续上下文
// "conversation_id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
"messages": [
{
"role": "user",
@@ -219,7 +297,8 @@ Authorization: Bearer [login_tongyi_ticket]
响应数据:
```json
{
"id": "4c4267e7919a41baad8199414ceb5cea",
// 如果想获得原生多轮对话体验,此id,你可以传入到下一轮对话的conversation_id来接续上下文
"id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
"model": "qwen",
"object": "chat.completion",
"choices": [
@@ -250,7 +329,7 @@ Authorization: Bearer [login_tongyi_ticket]
header 需要设置 Authorization 头部:

```
Authorization: Bearer [login_tongyi_ticket]
Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
```

请求数据:
@@ -398,6 +477,26 @@ Authorization: Bearer [refresh_token]
}
```

### ticket存活检测

检测tongyi_sso_ticket或login_aliyunid_ticket是否存活,如果存活live未true,否则为false,请不要频繁(小于10分钟)调用此接口。

**POST /token/check**

请求数据:
```json
{
"token": "QIhaHrrXUaIrWMUmL..."
}
```

响应数据:
```json
{
"live": true
}
```

## 注意事项

### Nginx反代优化
@@ -423,4 +522,4 @@ keepalive_timeout 120;

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=LLM-Red-Team/qwen-free-api&type=Date)](https://star-history.com/#LLM-Red-Team/qwen-free-api&Date)
[![Star History Chart](https://api.star-history.com/svg?repos=LLM-Red-Team/qwen-free-api&type=Date)](https://star-history.com/#LLM-Red-Team/qwen-free-api&Date)
510 changes: 510 additions & 0 deletions README_EN.md

Large diffs are not rendered by default.

Binary file modified doc/example-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/example-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qwen-free-api",
"version": "0.0.10",
"version": "0.0.21",
"description": "Qwen Free API Server",
"type": "module",
"main": "dist/index.js",
205 changes: 133 additions & 72 deletions src/api/controllers/chat.ts

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/api/routes/chat.ts
Original file line number Diff line number Diff line change
@@ -3,27 +3,27 @@ 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: "/v1/chat",

post: {
"/completions": async (request: Request) => {
request
.validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
.validate("body.messages", _.isArray)
.validate("headers.authorization", _.isString);
// refresh_token切分
// ticket切分
const tokens = chat.tokenSplit(request.headers.authorization);
// 随机挑选一个refresh_token
// 随机挑选一个ticket
const token = _.sample(tokens);
const model = request.body.model;
const messages = request.body.messages;
if (request.body.stream) {
const { model, conversation_id: convId, messages, stream } = request.body;
if (stream) {
const stream = await chat.createCompletionStream(
model,
messages,
token
token,
convId
);
return new Response(stream, {
type: "text/event-stream",
@@ -32,7 +32,8 @@ export default {
return await chat.createCompletion(
model,
messages,
token
token,
convId
);
},
},
6 changes: 5 additions & 1 deletion src/api/routes/index.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ import Response from '@/lib/response/Response.ts';
import chat from "./chat.ts";
import images from "./images.ts";
import ping from "./ping.ts";
import token from './token.ts';
import models from './models.ts';

export default [
{
@@ -21,5 +23,7 @@ export default [
},
chat,
images,
ping
ping,
token,
models
];
56 changes: 56 additions & 0 deletions src/api/routes/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import _ from 'lodash';

export default {

prefix: '/v1',

get: {
'/models': async () => {
return {
"data": [
{
"id": "qwen-max",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-max-longcontext",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-plus",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-turbo",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-vl-max",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-vl-plus",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-v1",
"object": "model",
"owned_by": "qwen-free-api"
},
{
"id": "qwen-v1-vision",
"object": "model",
"owned_by": "qwen-free-api"
}
]
};
}

}
}
25 changes: 25 additions & 0 deletions src/api/routes/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
}
}

}

}
12 changes: 7 additions & 5 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -9,13 +9,15 @@ 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() {
fs.ensureDirSync(config.system.logDirPath);
this.work();
!isVercelEnv && fs.ensureDirSync(config.system.logDirPath);
!isVercelEnv && this.work();
}

push(content) {
@@ -24,16 +26,16 @@ class LogWriter {
}

writeSync(buffer) {
fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
!isVercelEnv && fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
}

async write(buffer) {
await fs.appendFile(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
!isVercelEnv && await fs.appendFile(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
}

flush() {
if(!this.#buffers.length) return;
fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), Buffer.concat(this.#buffers));
!isVercelEnv && fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), Buffer.concat(this.#buffers));
}

work() {
27 changes: 27 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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"
}
}
]
}