Compare commits
76 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bbe58b70fe | ||
|
db50132167 | ||
|
9f2e17210a | ||
|
33b16a3d8c | ||
|
5e2cb0a8c9 | ||
|
30153245c7 | ||
|
2dd2517dc9 | ||
|
e95a7e49c6 | ||
|
bf42bce60f | ||
|
a9126f1d12 | ||
|
f727286a33 | ||
|
a9a4f7f659 | ||
|
b9d85f304f | ||
|
9c4eabe8e9 | ||
|
a8fb1bb16c | ||
|
04eed50513 | ||
|
cbde4fc17a | ||
|
7384f7316a | ||
|
3cea08a9b4 | ||
|
22e6f202bb | ||
|
574e398760 | ||
|
62347820c1 | ||
|
0f05d42a5e | ||
|
80d37ede42 | ||
|
84f5596a45 | ||
|
b3bfb220f6 | ||
|
a2db68f9c2 | ||
|
e84aed2c42 | ||
|
25d75dcd24 | ||
|
1455261a22 | ||
|
9a7eefaa5d | ||
|
a3e49569d0 | ||
|
30e82c322e | ||
|
5785a3e367 | ||
|
53d0c368bf | ||
|
1334aaa288 | ||
|
4f500d51c5 | ||
|
e389f58e26 | ||
|
88950b29aa | ||
|
fedb5ce890 | ||
|
ee62522589 | ||
|
648d23f742 | ||
|
9121c233d6 | ||
|
73501bca6f | ||
|
ed2b2eff33 | ||
|
dcbf8b10f5 | ||
|
eb7d10a60f | ||
|
885e33ea7a | ||
|
6ff797250f | ||
|
9dc37523af | ||
|
95d231242f | ||
|
63995d7a7e | ||
|
ebcbdbaac8 | ||
|
fb665edbb9 | ||
|
4924a01d05 | ||
|
66da9dbc19 | ||
|
7031611320 | ||
|
eab1767a4e | ||
|
c259afa34d | ||
|
b48c4efbd8 | ||
|
fb02581acf | ||
|
ca3c1142ac | ||
|
3bb2a93211 | ||
|
83d30a7dca | ||
|
a6a0806136 | ||
|
6abc2d760f | ||
|
c2c78ba9f9 | ||
|
3fc49512af | ||
|
05d106f956 | ||
|
7124650c1f | ||
|
c97630edcf | ||
|
6e0d9cac44 | ||
|
3a3af2ce0e | ||
|
308219bce8 | ||
|
41c9488e84 | ||
|
7ebc1d00ae |
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/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
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
dist/
|
dist/
|
||||||
node_modules/
|
node_modules/
|
||||||
logs/
|
logs/
|
||||||
|
.vercel
|
11
Dockerfile
@ -4,14 +4,15 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY . /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
|
FROM node:lts-alpine
|
||||||
|
|
||||||
COPY --from=BUILD_IMAGE /app/configs ./configs
|
COPY --from=BUILD_IMAGE /app/configs /app/configs
|
||||||
COPY --from=BUILD_IMAGE /app/package.json ./package.json
|
COPY --from=BUILD_IMAGE /app/package.json /app/package.json
|
||||||
COPY --from=BUILD_IMAGE /app/dist ./dist
|
COPY --from=BUILD_IMAGE /app/dist /app/dist
|
||||||
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
|
COPY --from=BUILD_IMAGE /app/public /app/public
|
||||||
|
COPY --from=BUILD_IMAGE /app/node_modules /app/node_modules
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
519
README.md
@ -1,48 +1,525 @@
|
|||||||
# Qwen AI Free 服务
|
# Qwen AI Free 服务
|
||||||
|
|
||||||

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

|

|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
支持高速流式输出、支持多轮对话、支持无水印AI绘图、支持长文档解读(正在开发)、图像解析(正在开发),零配置部署,多路token支持,自动清理会话痕迹。
|
支持高速流式输出、支持多轮对话、支持无水印AI绘图、支持长文档解读、图像解析、联网检索,零配置部署,多路token支持,自动清理会话痕迹。
|
||||||
|
|
||||||
与ChatGPT接口完全兼容。
|
与ChatGPT接口完全兼容。
|
||||||
|
|
||||||
还有以下三个free-api欢迎关注:
|
还有以下十个free-api欢迎关注:
|
||||||
|
|
||||||
Moonshot AI(Kimi.ai)接口转API [kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api)
|
Moonshot AI(Kimi.ai)接口转API [kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api)
|
||||||
|
|
||||||
ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
|
阶跃星辰 (跃问StepChat) 接口转API [step-free-api](https://github.com/LLM-Red-Team/step-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)
|
聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
|
||||||
|
|
||||||
## 声明
|
|
||||||
|
|
||||||
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
|
|
||||||
|
|
||||||
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
|
|
||||||
|
|
||||||
仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!
|
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
* [声明](#声明)
|
- [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,避免封禁的风险。**
|
||||||
|
|
||||||
此链接仅临时测试功能,长期使用请自行部署。
|
**本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
|
||||||
|
|
||||||
https://udify.app/chat/qOXzVl5kkvhQXM8r
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
|
|
||||||
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
|
|
||||||
|
**仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
|
||||||
|
|
||||||
|
## 效果示例
|
||||||
|
|
||||||
|
### 验明正身Demo
|
||||||
|
|
||||||
|

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

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

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

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

|
||||||
|
|
||||||
|
### 10线程并发测试
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
## 接入准备
|
## 接入准备
|
||||||
|
|
||||||
|
### 方法1
|
||||||
|
|
||||||
从 [通义千问](https://tongyi.aliyun.com/qianwen) 登录
|
从 [通义千问](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`
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
文档还在持续完善。
|
### 方法2
|
||||||
|
|
||||||
|
从 [阿里云](https://www.aliyun.com/) 登录(如果该账号有服务器等重要资产不建议使用),如果该账号之前未进入过[通义千问](https://tongyi.aliyun.com/qianwen) ,需要先进入同意协议,否则无法生效。
|
||||||
|
|
||||||
|
然后F12打开开发者工具,从Application > Cookies中找到`login_aliyunid_ticket`的值,这将作为Authorization的Bearer Token值:`Authorization: Bearer TOKEN`
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 多账号接入
|
||||||
|
|
||||||
|
你可以通过提供多个账号的`tongyi_sso_ticket`或`login_aliyunid_ticket`,并使用,拼接提供:
|
||||||
|
|
||||||
|
`Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
|
||||||
|
|
||||||
|
每次请求服务会从中挑选一个。
|
||||||
|
|
||||||
|
## Docker部署
|
||||||
|
|
||||||
|
请准备一台具有公网IP的服务器并将8000端口开放。
|
||||||
|
|
||||||
|
拉取镜像并启动服务
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker run -it -d --init --name qwen-free-api -p 8000:8000 -e TZ=Asia/Shanghai vinlic/qwen-free-api:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
查看服务实时日志
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker logs -f qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
重启服务
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker restart qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
停止服务
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker stop qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker-compose部署
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
qwen-free-api:
|
||||||
|
container_name: qwen-free-api
|
||||||
|
image: vinlic/qwen-free-api:latest
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
environment:
|
||||||
|
- 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端口开放。
|
||||||
|
|
||||||
|
请先安装好Node.js环境并且配置好环境变量,确认node命令可用。
|
||||||
|
|
||||||
|
安装依赖
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm i
|
||||||
|
```
|
||||||
|
|
||||||
|
安装PM2进行进程守护
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm i -g pm2
|
||||||
|
```
|
||||||
|
|
||||||
|
编译构建,看到dist目录就是构建完成
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
启动服务
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pm2 start dist/index.js --name "qwen-free-api"
|
||||||
|
```
|
||||||
|
|
||||||
|
查看服务实时日志
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pm2 logs qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
重启服务
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pm2 reload qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
停止服务
|
||||||
|
|
||||||
|
```shell
|
||||||
|
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/) 等线上服务接入使用。
|
||||||
|
|
||||||
|
### 对话补全
|
||||||
|
|
||||||
|
对话补全接口,与openai的 [chat-completions-api](https://platform.openai.com/docs/guides/text-generation/chat-completions-api) 兼容。
|
||||||
|
|
||||||
|
**POST /v1/chat/completions**
|
||||||
|
|
||||||
|
header 需要设置 Authorization 头部:
|
||||||
|
|
||||||
|
```
|
||||||
|
Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
|
||||||
|
```
|
||||||
|
|
||||||
|
请求数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
// 模型名称随意填写
|
||||||
|
"model": "qwen",
|
||||||
|
// 目前多轮对话基于消息合并实现,某些场景可能导致能力下降且受单轮最大token数限制
|
||||||
|
// 如果您想获得原生的多轮对话体验,可以传入上一轮消息获得的id,来接续上下文
|
||||||
|
// "conversation_id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": "你是谁?"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
// 如果使用SSE流请设置为true,默认false
|
||||||
|
"stream": false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
响应数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
// 如果想获得原生多轮对话体验,此id,你可以传入到下一轮对话的conversation_id来接续上下文
|
||||||
|
"id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
|
||||||
|
"model": "qwen",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "我是阿里云研发的超大规模语言模型,我叫通义千问。"
|
||||||
|
},
|
||||||
|
"finish_reason": "stop"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 1,
|
||||||
|
"completion_tokens": 1,
|
||||||
|
"total_tokens": 2
|
||||||
|
},
|
||||||
|
"created": 1710152062
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### AI绘图
|
||||||
|
|
||||||
|
对话补全接口,与openai的 [images-create-api](https://platform.openai.com/docs/api-reference/images/create) 兼容。
|
||||||
|
|
||||||
|
**POST /v1/images/generations**
|
||||||
|
|
||||||
|
header 需要设置 Authorization 头部:
|
||||||
|
|
||||||
|
```
|
||||||
|
Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
|
||||||
|
```
|
||||||
|
|
||||||
|
请求数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
// 可以乱填
|
||||||
|
"model": "wanxiang",
|
||||||
|
"prompt": "一只可爱的猫"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
响应数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"created": 1711507734,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"url": "https://wanx.alicdn.com/wanx/1111111111/text_to_image/7248e85cfda6491aae59c54e7e679b17_0.png"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 文档解读
|
||||||
|
|
||||||
|
提供一个可访问的文件URL或者BASE64_URL进行解析。
|
||||||
|
|
||||||
|
**POST /v1/chat/completions**
|
||||||
|
|
||||||
|
header 需要设置 Authorization 头部:
|
||||||
|
|
||||||
|
```
|
||||||
|
Authorization: Bearer [refresh_token]
|
||||||
|
```
|
||||||
|
|
||||||
|
请求数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"model": "qwen",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "file",
|
||||||
|
"file_url": {
|
||||||
|
"url": "https://mj101-1317487292.cos.ap-shanghai.myqcloud.com/ai/test.pdf"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"text": "文档里说了什么?"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
响应数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "b56ea6c9e86140429fa2de6a6ec028ff",
|
||||||
|
"model": "qwen",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "文档中包含了四个古代魔法仪式或咒语的描述,它们似乎旨在影响或控制一个特定女性的情感和行为,使其对施术者产生强烈的爱意。以下是每个仪式的具体内容:\n\n1. **仪式一**(PMG 4.1390 – 1495):\n - 施术者需留下一些面包,将其掰成七小块。\n - 前往一处英雄、角斗士或其他暴力死亡者丧生的地方。\n - 对着面包碎片念诵咒语后丢弃,并从该地取一些受污染的泥土扔进目标女性的住所。\n - 咒语内容包括向命运三女神(Moirai)、罗马版的命运女神(Fates)、自然力量(Daemons)、饥荒与嫉妒之神以及非正常死亡者献祭食物,并请求他们以痛苦折磨目标,使她在梦中惊醒,心生忧虑与恐惧,最终跟随施术者的步伐并顺从其意愿。此过程以赫卡忒(Hecate)女神为命令的源泉。\n\n2. **仪式二**(PMG 4.1342 – 57):\n - 施术者召唤恶魔(Daemon),通过一系列神秘的神祇名号(如Erekisephthe Araracharara Ephthesikere)要求其将名为Tereous的女子(Apia所生)带至施术者Didymos(Taipiam所生)身边。\n - 请求该女子在灵魂、心智及女性器官上遭受剧烈痛苦,直至她主动找寻Didymos并与之紧密相连(唇对唇、发对发、腹部对腹部)。整个过程要求立即执行。\n\n3. **仪式三**(PGM 4.1265 – 74):\n - 揭示了阿佛洛狄忒(Aphrodite)鲜为人知的名字——NEPHERIĒRI[nfr-iry-t]。\n - 如果想赢得一位美丽女子的芳心,施术者应保持三天纯净,献上乳香,并在心中默念该名字七次。\n - 这样的做法需持续七天,据说这样便能成功吸引女子。\n\n4. **仪式四**(PGM 4.1496 – 1):\n - 施术者在燃烧的煤炭上供奉没药(myrrh),同时念诵咒语。\n - 咒语将没药称为“苦涩的调和者”、“热力的激发者”,并命令它前往指定的女子(及其母亲的名字)处,阻止她进行日常活动(如坐、饮、食、注视他人、亲吻他人),迫使她心中只有施术者,对其产生强烈的欲望与爱意。\n - 咒语还指示没药直接穿透女子的灵魂,驻留在其心中,焚烧其内脏、胸部、肝脏、气息、骨骼、骨髓,直到她来到施术者身边。\n\n这些仪式反映了古代魔法实践中试图借助超自然力量操控他人情感与行为的企图,涉及对神灵、恶魔、神秘名字及特定物质(如面包、泥土、乳香、没药)的运用,通常伴随着严格的仪式规程和咒语念诵。此类行为在现代伦理和法律框架下被视为不恰当甚至违法,且缺乏科学依据。"
|
||||||
|
},
|
||||||
|
"finish_reason": "stop"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 1,
|
||||||
|
"completion_tokens": 1,
|
||||||
|
"total_tokens": 2
|
||||||
|
},
|
||||||
|
"created": 1712253736
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 图像解析
|
||||||
|
|
||||||
|
提供一个可访问的图像URL或者BASE64_URL进行解析。
|
||||||
|
|
||||||
|
此格式兼容 [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API格式,您也可以用这个格式传送文档进行解析。
|
||||||
|
|
||||||
|
**POST /v1/chat/completions**
|
||||||
|
|
||||||
|
header 需要设置 Authorization 头部:
|
||||||
|
|
||||||
|
```
|
||||||
|
Authorization: Bearer [refresh_token]
|
||||||
|
```
|
||||||
|
|
||||||
|
请求数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"model": "qwen",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "file",
|
||||||
|
"file_url": {
|
||||||
|
"url": "https://img.alicdn.com/imgextra/i1/O1CN01CC9kic1ig1r4sAY5d_!!6000000004441-2-tps-880-210.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"text": "图像描述了什么?"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
响应数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "895fbe7fa22442d499ba67bb5213e842",
|
||||||
|
"model": "qwen",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "图像展示了通义千问的标志,一个紫色的六边形和一个蓝色的三角形,以及“通义千问”四个白色的汉字。"
|
||||||
|
},
|
||||||
|
"finish_reason": "stop"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 1,
|
||||||
|
"completion_tokens": 1,
|
||||||
|
"total_tokens": 2
|
||||||
|
},
|
||||||
|
"created": 1712254066
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ticket存活检测
|
||||||
|
|
||||||
|
检测tongyi_sso_ticket或login_aliyunid_ticket是否存活,如果存活live未true,否则为false,请不要频繁(小于10分钟)调用此接口。
|
||||||
|
|
||||||
|
**POST /token/check**
|
||||||
|
|
||||||
|
请求数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"token": "QIhaHrrXUaIrWMUmL..."
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
响应数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"live": true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
### Nginx反代优化
|
||||||
|
|
||||||
|
如果您正在使用Nginx反向代理qwen-free-api,请添加以下配置项优化流的输出效果,优化体验感。
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
# 关闭代理缓冲。当设置为off时,Nginx会立即将客户端请求发送到后端服务器,并立即将从后端服务器接收到的响应发送回客户端。
|
||||||
|
proxy_buffering off;
|
||||||
|
# 启用分块传输编码。分块传输编码允许服务器为动态生成的内容分块发送数据,而不需要预先知道内容的大小。
|
||||||
|
chunked_transfer_encoding on;
|
||||||
|
# 开启TCP_NOPUSH,这告诉Nginx在数据包发送到客户端之前,尽可能地发送数据。这通常在sendfile使用时配合使用,可以提高网络效率。
|
||||||
|
tcp_nopush on;
|
||||||
|
# 开启TCP_NODELAY,这告诉Nginx不延迟发送数据,立即发送小数据包。在某些情况下,这可以减少网络的延迟。
|
||||||
|
tcp_nodelay on;
|
||||||
|
# 设置保持连接的超时时间,这里设置为120秒。如果在这段时间内,客户端和服务器之间没有进一步的通信,连接将被关闭。
|
||||||
|
keepalive_timeout 120;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Token统计
|
||||||
|
|
||||||
|
由于推理侧不在qwen-free-api,因此token不可统计,将以固定数字返回。
|
||||||
|
|
||||||
|
## Star History
|
||||||
|
|
||||||
|
[](https://star-history.com/#LLM-Red-Team/qwen-free-api&Date)
|
||||||
|
510
README_EN.md
Normal file
@ -0,0 +1,510 @@
|
|||||||
|
# Qwen AI Free Service
|
||||||
|
|
||||||
|
[](LICENSE)
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
Moonshot AI (Kimi.ai) API to API [kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api/tree/master)
|
||||||
|
|
||||||
|
StepFun (StepChat) API to API [step-free-api](https://github.com/LLM-Red-Team/step-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)
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
* [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)
|
||||||
|
* [Native Deployment](#Native-Deployment)
|
||||||
|
* [Recommended Clients](#Recommended-Clients)
|
||||||
|
* [Interface List](#Interface-List)
|
||||||
|
* [Conversation completion](#conversation-completion)
|
||||||
|
* [AI Drawing](#AI-Drawing)
|
||||||
|
* [Document Interpretation](#document-interpretation)
|
||||||
|
* [Image analysis](#image-analysis)
|
||||||
|
* [refresh_token survival detection](#refresh_token-survival-detection)
|
||||||
|
* [Notification](#Notification)
|
||||||
|
* [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 [Ali](https://dashscope.console.aliyun.com/) 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/qOXzVl5kkvhQXM8r
|
||||||
|
|
||||||
|
## Effect Examples
|
||||||
|
|
||||||
|
### Identity Verification
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### Multi-turn Dialogue
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### AI Drawing
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### Long Document Reading
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### Image Analysis
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 10-Thread Concurrency Test
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Access Preparation
|
||||||
|
|
||||||
|
### Method1
|
||||||
|
|
||||||
|
Log in to [Tongyi Qianwen](https://tongyi.aliyun.com/qianwen)
|
||||||
|
|
||||||
|
Enter Tongyi Qianwen and start a random conversation, then press F12 to open the developer tools. Find the value of `tongyi_sso_ticket` in Application > Cookies, which will be used as the Bearer Token value for Authorization: `Authorization: Bearer TOKEN`
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### Method2
|
||||||
|
|
||||||
|
Log in to [Alibaba Cloud](https://www.aliyun.com/) (not recommended if the account has important assets such as servers). If the account has not previously entered [Tongyi Qianwen](https://tongyi.aliyun.com/qianwen), you need to first agree to the terms, otherwise it will not take effect.
|
||||||
|
|
||||||
|
Then press F12 to open the developer tools. Find the value of `login_aliyunid_ticket` in Application > Cookies, which will be used as the Bearer Token value for Authorization: `Authorization: Bearer TOKEN`
|
||||||
|

|
||||||
|
|
||||||
|
### Multi-Account Access
|
||||||
|
|
||||||
|
You can provide multiple account `tongyi_sso_ticket` or `login_aliyunid_ticket` 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 qwen-free-api -p 8000:8000 -e TZ=Asia/Shanghai vinlic/qwen-free-api:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
check real-time service logs
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker logs -f step-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
Restart service
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker restart step-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
Shut down service
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker stop step-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker-compose Deployment
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
qwen-free-api:
|
||||||
|
container_name: qwen-free-api
|
||||||
|
image: vinlic/qwen-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/qwen-free-api
|
||||||
|
cd qwen-free-api
|
||||||
|
vercel --prod
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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 "qwen-free-api"
|
||||||
|
```
|
||||||
|
|
||||||
|
View real-time service logs
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pm2 logs qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
Restart service
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pm2 reload qwen-free-api
|
||||||
|
```
|
||||||
|
|
||||||
|
Shut down service
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pm2 stop qwen-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 [tongyi_sso_ticket/login_aliyunid_ticket]
|
||||||
|
```
|
||||||
|
|
||||||
|
Request data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
// Fill in the model name as you like.
|
||||||
|
"model": "qwen",
|
||||||
|
// Currently, multi-round conversations are realized based on message merging, which in some scenarios may lead to capacity degradation and is limited by the maximum number of tokens in a single round.
|
||||||
|
// If you want a native multi-round dialog experience, you can pass in the ids obtained from the last round of messages to pick up the context
|
||||||
|
// "conversation_id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": "who RU?"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
// If using SSE stream, please set it to true, the default is false
|
||||||
|
"stream": false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
// For a native multi-round conversation experience, this id, you can pass in the conversation_id for the next round of conversation to pick up the context
|
||||||
|
"id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
|
||||||
|
"model": "qwen",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "I'm Qwen."
|
||||||
|
},
|
||||||
|
"finish_reason": "stop"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 1,
|
||||||
|
"completion_tokens": 1,
|
||||||
|
"total_tokens": 2
|
||||||
|
},
|
||||||
|
"created": 1710152062
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### AI Drawing
|
||||||
|
|
||||||
|
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 [tongyi_sso_ticket/login_aliyunid_ticket]
|
||||||
|
```
|
||||||
|
|
||||||
|
Request data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
// 可以乱填
|
||||||
|
"model": "wanxiang",
|
||||||
|
"prompt": "A cut cat"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"created": 1711507734,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"url": "https://wanx.alicdn.com/wanx/1111111111/text_to_image/7248e85cfda6491aae59c54e7e679b17_0.png"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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": "qwen",
|
||||||
|
"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?"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "b56ea6c9e86140429fa2de6a6ec028ff",
|
||||||
|
"model": "qwen",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "This is a doc about the magic of love. balabala..."
|
||||||
|
},
|
||||||
|
"finish_reason": "stop"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 1,
|
||||||
|
"completion_tokens": 1,
|
||||||
|
"total_tokens": 2
|
||||||
|
},
|
||||||
|
"created": 1712253736
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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]
|
||||||
|
```
|
||||||
|
|
||||||
|
请求数据:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"model": "qwen",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "file",
|
||||||
|
"file_url": {
|
||||||
|
"url": "https://img.alicdn.com/imgextra/i1/O1CN01CC9kic1ig1r4sAY5d_!!6000000004441-2-tps-880-210.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"text": "What does the image describe?"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "895fbe7fa22442d499ba67bb5213e842",
|
||||||
|
"model": "qwen",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "It's the logo of Qwen."
|
||||||
|
},
|
||||||
|
"finish_reason": "stop"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 1,
|
||||||
|
"completion_tokens": 1,
|
||||||
|
"total_tokens": 2
|
||||||
|
},
|
||||||
|
"created": 1712254066
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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": "QIhaHrrXUaIrWMUmL..."
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response data:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"live": true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notification
|
||||||
|
|
||||||
|
### Nginx anti-generation optimization
|
||||||
|
|
||||||
|
If you are using Nginx reverse proxy `qwen-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 qwen-free-api, the token cannot be counted and will be returned as a fixed number!!!!!
|
||||||
|
|
||||||
|
## Star History
|
||||||
|
|
||||||
|
[](https://star-history.com/#LLM-Red-Team/qwen-free-api&Date)
|
Before Width: | Height: | Size: 177 KiB After Width: | Height: | Size: 375 KiB |
BIN
doc/example-1.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
doc/example-2.png
Normal file
After Width: | Height: | Size: 185 KiB |
BIN
doc/example-3.png
Normal file
After Width: | Height: | Size: 567 KiB |
BIN
doc/example-4.png
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
doc/example-5.png
Normal file
After Width: | Height: | Size: 483 KiB |
BIN
doc/example-6.png
Normal file
After Width: | Height: | Size: 114 KiB |
BIN
doc/example-7.png
Normal file
After Width: | Height: | Size: 58 KiB |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "qwen-free-api",
|
"name": "qwen-free-api",
|
||||||
"version": "0.0.2",
|
"version": "0.0.21",
|
||||||
"description": "Qwen Free API Server",
|
"description": "Qwen Free API Server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -26,6 +26,7 @@
|
|||||||
"cron": "^3.1.6",
|
"cron": "^3.1.6",
|
||||||
"date-fns": "^3.3.1",
|
"date-fns": "^3.3.1",
|
||||||
"eventsource-parser": "^1.1.2",
|
"eventsource-parser": "^1.1.2",
|
||||||
|
"form-data": "^4.0.0",
|
||||||
"fs-extra": "^11.2.0",
|
"fs-extra": "^11.2.0",
|
||||||
"koa": "^2.15.0",
|
"koa": "^2.15.0",
|
||||||
"koa-body": "^5.0.0",
|
"koa-body": "^5.0.0",
|
||||||
|
10
public/welcome.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<title>🚀 服务已启动</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>qwen-free-api已启动!<br>请通过LobeChat / NextChat / Dify等客户端或OpenAI SDK接入!</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -5,5 +5,7 @@ export default {
|
|||||||
API_TOKEN_EXPIRES: [-2002, 'Token已失效'],
|
API_TOKEN_EXPIRES: [-2002, 'Token已失效'],
|
||||||
API_FILE_URL_INVALID: [-2003, '远程文件URL非法'],
|
API_FILE_URL_INVALID: [-2003, '远程文件URL非法'],
|
||||||
API_FILE_EXECEEDS_SIZE: [-2004, '远程文件超出大小'],
|
API_FILE_EXECEEDS_SIZE: [-2004, '远程文件超出大小'],
|
||||||
API_CHAT_STREAM_PUSHING: [-2005, '已有对话流正在输出']
|
API_CHAT_STREAM_PUSHING: [-2005, '已有对话流正在输出'],
|
||||||
|
API_CONTENT_FILTERED: [-2006, '内容由于合规问题已被阻止生成'],
|
||||||
|
API_IMAGE_GENERATION_FAILED: [-2007, '图像生成失败']
|
||||||
}
|
}
|
@ -1,8 +1,10 @@
|
|||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import { PassThrough } from "stream";
|
import { PassThrough } from "stream";
|
||||||
|
import http2 from "http2";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import mime from "mime";
|
import mime from "mime";
|
||||||
|
import FormData from "form-data";
|
||||||
import axios, { AxiosResponse } from "axios";
|
import axios, { AxiosResponse } from "axios";
|
||||||
|
|
||||||
import APIException from "@/lib/exceptions/APIException.ts";
|
import APIException from "@/lib/exceptions/APIException.ts";
|
||||||
@ -14,7 +16,7 @@ import util from "@/lib/util.ts";
|
|||||||
// 模型名称
|
// 模型名称
|
||||||
const MODEL_NAME = "qwen";
|
const MODEL_NAME = "qwen";
|
||||||
// 最大重试次数
|
// 最大重试次数
|
||||||
const MAX_RETRY_COUNT = 0;
|
const MAX_RETRY_COUNT = 3;
|
||||||
// 重试延迟
|
// 重试延迟
|
||||||
const RETRY_DELAY = 5000;
|
const RETRY_DELAY = 5000;
|
||||||
// 伪装headers
|
// 伪装headers
|
||||||
@ -22,7 +24,7 @@ const FAKE_HEADERS = {
|
|||||||
Accept: "application/json, text/plain, */*",
|
Accept: "application/json, text/plain, */*",
|
||||||
"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",
|
||||||
"Cache-Control": "no-cahce",
|
"Cache-Control": "no-cache",
|
||||||
Origin: "https://tongyi.aliyun.com",
|
Origin: "https://tongyi.aliyun.com",
|
||||||
Pragma: "no-cache",
|
Pragma: "no-cache",
|
||||||
"Sec-Ch-Ua":
|
"Sec-Ch-Ua":
|
||||||
@ -36,7 +38,7 @@ const FAKE_HEADERS = {
|
|||||||
"User-Agent":
|
"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",
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
||||||
"X-Platform": "pc_tongyi",
|
"X-Platform": "pc_tongyi",
|
||||||
"X-Xsrf-Token": "4506064f-1da8-4d22-a004-b36092db2789",
|
"X-Xsrf-Token": "48b9ee49-a184-45e2-9f67-fa87213edcdc",
|
||||||
};
|
};
|
||||||
// 文件最大大小
|
// 文件最大大小
|
||||||
const FILE_MAX_SIZE = 100 * 1024 * 1024;
|
const FILE_MAX_SIZE = 100 * 1024 * 1024;
|
||||||
@ -46,7 +48,7 @@ const FILE_MAX_SIZE = 100 * 1024 * 1024;
|
|||||||
*
|
*
|
||||||
* 在对话流传输完毕后移除会话,避免创建的会话出现在用户的对话列表中
|
* 在对话流传输完毕后移除会话,避免创建的会话出现在用户的对话列表中
|
||||||
*
|
*
|
||||||
* @param ticket login_tongyi_ticket值
|
* @param ticket tongyi_sso_ticket或login_aliyunid_ticket
|
||||||
*/
|
*/
|
||||||
async function removeConversation(convId: string, ticket: string) {
|
async function removeConversation(convId: string, ticket: string) {
|
||||||
const result = await axios.post(
|
const result = await axios.post(
|
||||||
@ -71,54 +73,74 @@ async function removeConversation(convId: string, ticket: string) {
|
|||||||
*
|
*
|
||||||
* @param model 模型名称
|
* @param model 模型名称
|
||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
* @param ticket login_tongyi_ticket值
|
* @param ticket tongyi_sso_ticket或login_aliyunid_ticket
|
||||||
|
* @param refConvId 引用的会话ID
|
||||||
* @param retryCount 重试次数
|
* @param retryCount 重试次数
|
||||||
*/
|
*/
|
||||||
async function createCompletion(
|
async function createCompletion(
|
||||||
model = MODEL_NAME,
|
model = MODEL_NAME,
|
||||||
messages: any[],
|
messages: any[],
|
||||||
|
searchType: string = '',
|
||||||
ticket: string,
|
ticket: string,
|
||||||
|
refConvId = '',
|
||||||
retryCount = 0
|
retryCount = 0
|
||||||
) {
|
) {
|
||||||
|
let session: http2.ClientHttp2Session;
|
||||||
return (async () => {
|
return (async () => {
|
||||||
logger.info(messages);
|
logger.info(messages);
|
||||||
|
|
||||||
// 提取引用文件URL并上传qwen获得引用的文件ID列表
|
// 提取引用文件URL并上传qwen获得引用的文件ID列表
|
||||||
const refFileUrls = extractRefFileUrls(messages);
|
const refFileUrls = extractRefFileUrls(messages);
|
||||||
// const refs = refFileUrls.length
|
const refs = refFileUrls.length
|
||||||
// ? await Promise.all(
|
? await Promise.all(
|
||||||
// refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
|
refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
|
||||||
// )
|
)
|
||||||
// : [];
|
: [];
|
||||||
|
|
||||||
// 创建会话并获得流
|
// 如果引用对话ID不正确则重置引用
|
||||||
const result = await axios.post(
|
if (!/[0-9a-z]{32}/.test(refConvId))
|
||||||
"https://qianwen.biz.aliyun.com/dialog/conversation",
|
refConvId = '';
|
||||||
{
|
|
||||||
model: "",
|
// 请求流
|
||||||
action: "next",
|
const session: http2.ClientHttp2Session = await new Promise(
|
||||||
mode: "chat",
|
(resolve, reject) => {
|
||||||
userAction: "chat",
|
const session = http2.connect("https://qianwen.biz.aliyun.com");
|
||||||
requestId: util.uuid(false),
|
session.on("connect", () => resolve(session));
|
||||||
sessionId: "",
|
session.on("error", reject);
|
||||||
sessionType: "text_chat",
|
|
||||||
parentMsgId: "",
|
|
||||||
contents: messagesPrepare(messages),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Cookie: generateCookie(ticket),
|
|
||||||
...FAKE_HEADERS,
|
|
||||||
Accept: 'text/event-stream'
|
|
||||||
},
|
|
||||||
timeout: 120000,
|
|
||||||
validateStatus: () => true,
|
|
||||||
responseType: "stream",
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
const [sessionId, parentMsgId = ''] = refConvId.split('-');
|
||||||
|
const req = session.request({
|
||||||
|
":method": "POST",
|
||||||
|
":path": "/dialog/conversation",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
Accept: "text/event-stream",
|
||||||
|
});
|
||||||
|
req.setTimeout(120000);
|
||||||
|
req.write(
|
||||||
|
JSON.stringify({
|
||||||
|
mode: "chat",
|
||||||
|
model: "",
|
||||||
|
action: "next",
|
||||||
|
userAction: "chat",
|
||||||
|
requestId: util.uuid(false),
|
||||||
|
sessionId,
|
||||||
|
sessionType: "text_chat",
|
||||||
|
parentMsgId,
|
||||||
|
params: {
|
||||||
|
"fileUploadBatchId": util.uuid(),
|
||||||
|
"searchType": searchType,
|
||||||
|
},
|
||||||
|
contents: messagesPrepare(messages, refs, !!refConvId),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
req.setEncoding("utf8");
|
||||||
const streamStartTime = util.timestamp();
|
const streamStartTime = util.timestamp();
|
||||||
// 接收流为输出文本
|
// 接收流为输出文本
|
||||||
const answer = await receiveStream(result.data);
|
const answer = await receiveStream(req);
|
||||||
|
session.close();
|
||||||
logger.success(
|
logger.success(
|
||||||
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
||||||
);
|
);
|
||||||
@ -128,12 +150,13 @@ async function createCompletion(
|
|||||||
|
|
||||||
return answer;
|
return answer;
|
||||||
})().catch((err) => {
|
})().catch((err) => {
|
||||||
|
session && session.close();
|
||||||
if (retryCount < MAX_RETRY_COUNT) {
|
if (retryCount < MAX_RETRY_COUNT) {
|
||||||
logger.error(`Stream response error: ${err.message}`);
|
logger.error(`Stream response error: ${err.message}`);
|
||||||
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, ticket, retryCount + 1);
|
return createCompletion(model, messages, ticket, refConvId, retryCount + 1);
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
@ -145,55 +168,73 @@ async function createCompletion(
|
|||||||
*
|
*
|
||||||
* @param model 模型名称
|
* @param model 模型名称
|
||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
* @param ticket login_tongyi_ticket值
|
* @param ticket tongyi_sso_ticket或login_aliyunid_ticket
|
||||||
* @param useSearch 是否开启联网搜索
|
* @param refConvId 引用的会话ID
|
||||||
* @param retryCount 重试次数
|
* @param retryCount 重试次数
|
||||||
*/
|
*/
|
||||||
async function createCompletionStream(
|
async function createCompletionStream(
|
||||||
model = MODEL_NAME,
|
model = MODEL_NAME,
|
||||||
messages: any[],
|
messages: any[],
|
||||||
|
searchType: string = '',
|
||||||
ticket: string,
|
ticket: string,
|
||||||
|
refConvId = '',
|
||||||
retryCount = 0
|
retryCount = 0
|
||||||
) {
|
) {
|
||||||
|
let session: http2.ClientHttp2Session;
|
||||||
return (async () => {
|
return (async () => {
|
||||||
logger.info(messages);
|
logger.info(messages);
|
||||||
|
|
||||||
// 提取引用文件URL并上传qwen获得引用的文件ID列表
|
// 提取引用文件URL并上传qwen获得引用的文件ID列表
|
||||||
const refFileUrls = extractRefFileUrls(messages);
|
const refFileUrls = extractRefFileUrls(messages);
|
||||||
// const refs = refFileUrls.length
|
const refs = refFileUrls.length
|
||||||
// ? await Promise.all(
|
? await Promise.all(
|
||||||
// refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
|
refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
|
||||||
// )
|
)
|
||||||
// : [];
|
: [];
|
||||||
|
|
||||||
|
// 如果引用对话ID不正确则重置引用
|
||||||
|
if (!/[0-9a-z]{32}/.test(refConvId))
|
||||||
|
refConvId = ''
|
||||||
|
|
||||||
// 请求流
|
// 请求流
|
||||||
const result = await axios.post(
|
session = await new Promise((resolve, reject) => {
|
||||||
"https://qianwen.biz.aliyun.com/dialog/conversation",
|
const session = http2.connect("https://qianwen.biz.aliyun.com");
|
||||||
{
|
session.on("connect", () => resolve(session));
|
||||||
|
session.on("error", reject);
|
||||||
|
});
|
||||||
|
const [sessionId, parentMsgId = ''] = refConvId.split('-');
|
||||||
|
const req = session.request({
|
||||||
|
":method": "POST",
|
||||||
|
":path": "/dialog/conversation",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
Accept: "text/event-stream",
|
||||||
|
});
|
||||||
|
req.setTimeout(120000);
|
||||||
|
req.write(
|
||||||
|
JSON.stringify({
|
||||||
|
mode: "chat",
|
||||||
model: "",
|
model: "",
|
||||||
action: "next",
|
action: "next",
|
||||||
mode: "chat",
|
|
||||||
userAction: "chat",
|
userAction: "chat",
|
||||||
requestId: util.uuid(false),
|
requestId: util.uuid(false),
|
||||||
sessionId: "",
|
sessionId,
|
||||||
sessionType: "text_chat",
|
sessionType: "text_chat",
|
||||||
parentMsgId: "",
|
parentMsgId,
|
||||||
contents: messagesPrepare(messages),
|
params: {
|
||||||
},
|
"fileUploadBatchId": util.uuid(),
|
||||||
{
|
"searchType": searchType,
|
||||||
headers: {
|
|
||||||
Cookie: generateCookie(ticket),
|
|
||||||
...FAKE_HEADERS,
|
|
||||||
Accept: 'text/event-stream'
|
|
||||||
},
|
},
|
||||||
timeout: 120000,
|
contents: messagesPrepare(messages, refs, !!refConvId),
|
||||||
validateStatus: () => true,
|
})
|
||||||
responseType: "stream",
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
req.setEncoding("utf8");
|
||||||
const streamStartTime = util.timestamp();
|
const streamStartTime = util.timestamp();
|
||||||
// 创建转换流将消息格式转换为gpt兼容格式
|
// 创建转换流将消息格式转换为gpt兼容格式
|
||||||
return createTransStream(result.data, (convId: string) => {
|
return createTransStream(req, (convId: string) => {
|
||||||
|
// 关闭请求会话
|
||||||
|
session.close();
|
||||||
logger.success(
|
logger.success(
|
||||||
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
||||||
);
|
);
|
||||||
@ -201,12 +242,84 @@ async function createCompletionStream(
|
|||||||
removeConversation(convId, ticket).catch((err) => console.error(err));
|
removeConversation(convId, ticket).catch((err) => console.error(err));
|
||||||
});
|
});
|
||||||
})().catch((err) => {
|
})().catch((err) => {
|
||||||
|
session && session.close();
|
||||||
if (retryCount < MAX_RETRY_COUNT) {
|
if (retryCount < MAX_RETRY_COUNT) {
|
||||||
logger.error(`Stream response error: ${err.message}`);
|
logger.error(`Stream response error: ${err.message}`);
|
||||||
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, ticket, retryCount + 1);
|
return createCompletionStream(model, messages, ticket, refConvId, retryCount + 1);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateImages(
|
||||||
|
model = MODEL_NAME,
|
||||||
|
prompt: string,
|
||||||
|
ticket: string,
|
||||||
|
retryCount = 0
|
||||||
|
) {
|
||||||
|
let session: http2.ClientHttp2Session;
|
||||||
|
return (async () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: "user", content: prompt.indexOf('画') == -1 ? `请画:${prompt}` : prompt },
|
||||||
|
];
|
||||||
|
// 请求流
|
||||||
|
const session: http2.ClientHttp2Session = await new Promise(
|
||||||
|
(resolve, reject) => {
|
||||||
|
const session = http2.connect("https://qianwen.biz.aliyun.com");
|
||||||
|
session.on("connect", () => resolve(session));
|
||||||
|
session.on("error", reject);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const req = session.request({
|
||||||
|
":method": "POST",
|
||||||
|
":path": "/dialog/conversation",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
Accept: "text/event-stream",
|
||||||
|
});
|
||||||
|
req.setTimeout(120000);
|
||||||
|
req.write(
|
||||||
|
JSON.stringify({
|
||||||
|
mode: "chat",
|
||||||
|
model: "",
|
||||||
|
action: "next",
|
||||||
|
userAction: "chat",
|
||||||
|
requestId: util.uuid(false),
|
||||||
|
sessionId: "",
|
||||||
|
sessionType: "text_chat",
|
||||||
|
parentMsgId: "",
|
||||||
|
params: {
|
||||||
|
"fileUploadBatchId": util.uuid()
|
||||||
|
},
|
||||||
|
contents: messagesPrepare(messages),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
req.setEncoding("utf8");
|
||||||
|
const streamStartTime = util.timestamp();
|
||||||
|
// 接收流为输出文本
|
||||||
|
const { convId, imageUrls } = await receiveImages(req);
|
||||||
|
session.close();
|
||||||
|
logger.success(
|
||||||
|
`Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
|
||||||
|
);
|
||||||
|
|
||||||
|
// 异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
|
||||||
|
removeConversation(convId, ticket).catch((err) => console.error(err));
|
||||||
|
|
||||||
|
return imageUrls;
|
||||||
|
})().catch((err) => {
|
||||||
|
session && session.close();
|
||||||
|
if (retryCount < MAX_RETRY_COUNT) {
|
||||||
|
logger.error(`Stream response error: ${err.message}`);
|
||||||
|
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
|
||||||
|
return (async () => {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
||||||
|
return generateImages(model, prompt, ticket, retryCount + 1);
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
@ -219,29 +332,34 @@ async function createCompletionStream(
|
|||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
*/
|
*/
|
||||||
function extractRefFileUrls(messages: any[]) {
|
function extractRefFileUrls(messages: any[]) {
|
||||||
return messages.reduce((urls, message) => {
|
const urls = [];
|
||||||
if (_.isArray(message.content)) {
|
// 如果没有消息,则返回[]
|
||||||
message.content.forEach((v) => {
|
if (!messages.length) {
|
||||||
if (!_.isObject(v) || !["file", "image_url"].includes(v["type"]))
|
|
||||||
return;
|
|
||||||
// qwen-free-api支持格式
|
|
||||||
if (
|
|
||||||
v["type"] == "file" &&
|
|
||||||
_.isObject(v["file_url"]) &&
|
|
||||||
_.isString(v["file_url"]["url"])
|
|
||||||
)
|
|
||||||
urls.push(v["file_url"]["url"]);
|
|
||||||
// 兼容gpt-4-vision-preview API格式
|
|
||||||
else if (
|
|
||||||
v["type"] == "image_url" &&
|
|
||||||
_.isObject(v["image_url"]) &&
|
|
||||||
_.isString(v["image_url"]["url"])
|
|
||||||
)
|
|
||||||
urls.push(v["image_url"]["url"]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return urls;
|
return urls;
|
||||||
}, []);
|
}
|
||||||
|
// 只获取最新的消息
|
||||||
|
const lastMessage = messages[messages.length - 1];
|
||||||
|
if (_.isArray(lastMessage.content)) {
|
||||||
|
lastMessage.content.forEach((v) => {
|
||||||
|
if (!_.isObject(v) || !["file", "image_url"].includes(v["type"])) return;
|
||||||
|
// glm-free-api支持格式
|
||||||
|
if (
|
||||||
|
v["type"] == "file" &&
|
||||||
|
_.isObject(v["file_url"]) &&
|
||||||
|
_.isString(v["file_url"]["url"])
|
||||||
|
)
|
||||||
|
urls.push(v["file_url"]["url"]);
|
||||||
|
// 兼容gpt-4-vision-preview API格式
|
||||||
|
else if (
|
||||||
|
v["type"] == "image_url" &&
|
||||||
|
_.isObject(v["image_url"]) &&
|
||||||
|
_.isString(v["image_url"]["url"])
|
||||||
|
)
|
||||||
|
urls.push(v["image_url"]["url"]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
logger.info("本次请求上传:" + urls.length + "个文件");
|
||||||
|
return urls;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -253,25 +371,46 @@ function extractRefFileUrls(messages: any[]) {
|
|||||||
* user:新消息
|
* user:新消息
|
||||||
*
|
*
|
||||||
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
|
||||||
|
* @param refs 参考文件列表
|
||||||
|
* @param isRefConv 是否为引用会话
|
||||||
*/
|
*/
|
||||||
function messagesPrepare(messages: any[]) {
|
function messagesPrepare(messages: any[], refs: any[] = [], isRefConv = false) {
|
||||||
const content = messages.reduce((content, message) => {
|
let content;
|
||||||
if (_.isArray(message.content)) {
|
if (isRefConv || messages.length < 2) {
|
||||||
return message.content.reduce((_content, v) => {
|
content = messages.reduce((content, message) => {
|
||||||
if (!_.isObject(v) || v["type"] != "text") return _content;
|
if (_.isArray(message.content)) {
|
||||||
return _content + (v["text"] || "");
|
return (
|
||||||
}, content);
|
message.content.reduce((_content, v) => {
|
||||||
}
|
if (!_.isObject(v) || v["type"] != "text") return _content;
|
||||||
return (content += `<|im_start|>${message.role || "user"}\n${
|
return _content + (v["text"] || "") + "\n";
|
||||||
message.content
|
}, content)
|
||||||
}<|im_end|>\n`);
|
);
|
||||||
}, "");
|
}
|
||||||
|
return content + `${message.content}\n`;
|
||||||
|
}, "");
|
||||||
|
logger.info("\n透传内容:\n" + content);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
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 + `<|im_start|>${message.role || "user"}\n${v["text"] || ""}<|im_end|>\n`;
|
||||||
|
}, content);
|
||||||
|
}
|
||||||
|
return (content += `<|im_start|>${message.role || "user"}\n${
|
||||||
|
message.content
|
||||||
|
}<|im_end|>\n`);
|
||||||
|
}, "").replace(/\!\[.*\]\(.+\)/g, "");
|
||||||
|
logger.info("\n对话合并:\n" + content);
|
||||||
|
}
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
role: "user",
|
|
||||||
contentType: "text",
|
|
||||||
content,
|
content,
|
||||||
|
contentType: "text",
|
||||||
|
role: "user",
|
||||||
},
|
},
|
||||||
|
...refs
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,15 +459,19 @@ async function receiveStream(stream: any): Promise<any> {
|
|||||||
const result = _.attempt(() => JSON.parse(event.data));
|
const result = _.attempt(() => JSON.parse(event.data));
|
||||||
if (_.isError(result))
|
if (_.isError(result))
|
||||||
throw new Error(`Stream response invalid: ${event.data}`);
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
if (!data.id && result.sessionId) data.id = result.sessionId;
|
if (!data.id && result.sessionId && result.msgId)
|
||||||
|
data.id = `${result.sessionId}-${result.msgId}`;
|
||||||
const text = (result.contents || []).reduce((str, part) => {
|
const text = (result.contents || []).reduce((str, part) => {
|
||||||
const { role, content } = part;
|
const { contentType, role, content } = part;
|
||||||
|
if (contentType != "text" && contentType != "text2image") return str;
|
||||||
if (role != "assistant" && !_.isString(content)) return str;
|
if (role != "assistant" && !_.isString(content)) return str;
|
||||||
return str + content;
|
return str + content;
|
||||||
}, "");
|
}, "");
|
||||||
const exceptCharIndex = text.indexOf('<27>');
|
const exceptCharIndex = text.indexOf("<22>");
|
||||||
let chunk = text.substring(
|
let chunk = text.substring(
|
||||||
data.choices[0].message.content.length,
|
exceptCharIndex != -1
|
||||||
|
? Math.min(data.choices[0].message.content.length, exceptCharIndex)
|
||||||
|
: data.choices[0].message.content.length,
|
||||||
exceptCharIndex == -1 ? text.length : exceptCharIndex
|
exceptCharIndex == -1 ? text.length : exceptCharIndex
|
||||||
);
|
);
|
||||||
if (chunk && result.contentType == "text2image") {
|
if (chunk && result.contentType == "text2image") {
|
||||||
@ -362,6 +505,7 @@ async function receiveStream(stream: any): Promise<any> {
|
|||||||
stream.on("data", (buffer) => parser.feed(buffer.toString()));
|
stream.on("data", (buffer) => parser.feed(buffer.toString()));
|
||||||
stream.once("error", (err) => reject(err));
|
stream.once("error", (err) => reject(err));
|
||||||
stream.once("close", () => resolve(data));
|
stream.once("close", () => resolve(data));
|
||||||
|
stream.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,12 +548,18 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|||||||
if (_.isError(result))
|
if (_.isError(result))
|
||||||
throw new Error(`Stream response invalid: ${event.data}`);
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
const text = (result.contents || []).reduce((str, part) => {
|
const text = (result.contents || []).reduce((str, part) => {
|
||||||
const { role, content } = part;
|
const { contentType, role, content } = part;
|
||||||
|
if (contentType != "text" && contentType != "text2image") return str;
|
||||||
if (role != "assistant" && !_.isString(content)) return str;
|
if (role != "assistant" && !_.isString(content)) return str;
|
||||||
return str + content;
|
return str + content;
|
||||||
}, "");
|
}, "");
|
||||||
const exceptCharIndex = text.indexOf('<27>');
|
const exceptCharIndex = text.indexOf("<22>");
|
||||||
let chunk = text.substring(content.length, exceptCharIndex == -1 ? text.length : exceptCharIndex);
|
let chunk = text.substring(
|
||||||
|
exceptCharIndex != -1
|
||||||
|
? Math.min(content.length, exceptCharIndex)
|
||||||
|
: content.length,
|
||||||
|
exceptCharIndex == -1 ? text.length : exceptCharIndex
|
||||||
|
);
|
||||||
if (chunk && result.contentType == "text2image") {
|
if (chunk && result.contentType == "text2image") {
|
||||||
chunk = chunk.replace(
|
chunk = chunk.replace(
|
||||||
/https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi,
|
/https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi,
|
||||||
@ -424,7 +574,7 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|||||||
if (chunk && result.contentType == "text") {
|
if (chunk && result.contentType == "text") {
|
||||||
content += chunk;
|
content += chunk;
|
||||||
const data = `data: ${JSON.stringify({
|
const data = `data: ${JSON.stringify({
|
||||||
id: result.sessionId,
|
id: `${result.sessionId}-${result.msgId}`,
|
||||||
model: MODEL_NAME,
|
model: MODEL_NAME,
|
||||||
object: "chat.completion.chunk",
|
object: "chat.completion.chunk",
|
||||||
choices: [
|
choices: [
|
||||||
@ -441,7 +591,7 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|||||||
if (result.errorCode)
|
if (result.errorCode)
|
||||||
delta.content += `服务暂时不可用,第三方响应错误:${result.errorCode}`;
|
delta.content += `服务暂时不可用,第三方响应错误:${result.errorCode}`;
|
||||||
const data = `data: ${JSON.stringify({
|
const data = `data: ${JSON.stringify({
|
||||||
id: result.sessionId,
|
id: `${result.sessionId}-${result.msgId}`,
|
||||||
model: MODEL_NAME,
|
model: MODEL_NAME,
|
||||||
object: "chat.completion.chunk",
|
object: "chat.completion.chunk",
|
||||||
choices: [
|
choices: [
|
||||||
@ -476,9 +626,281 @@ function createTransStream(stream: any, endCallback?: Function) {
|
|||||||
"close",
|
"close",
|
||||||
() => !transStream.closed && transStream.end("data: [DONE]\n\n")
|
() => !transStream.closed && transStream.end("data: [DONE]\n\n")
|
||||||
);
|
);
|
||||||
|
stream.end();
|
||||||
return transStream;
|
return transStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从流接收图像
|
||||||
|
*
|
||||||
|
* @param stream 消息流
|
||||||
|
*/
|
||||||
|
async function receiveImages(
|
||||||
|
stream: any
|
||||||
|
): Promise<{ convId: string; imageUrls: string[] }> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let convId = "";
|
||||||
|
const imageUrls = [];
|
||||||
|
const parser = createParser((event) => {
|
||||||
|
try {
|
||||||
|
if (event.type !== "event") return;
|
||||||
|
if (event.data == "[DONE]") return;
|
||||||
|
// 解析JSON
|
||||||
|
const result = _.attempt(() => JSON.parse(event.data));
|
||||||
|
if (_.isError(result))
|
||||||
|
throw new Error(`Stream response invalid: ${event.data}`);
|
||||||
|
if (!convId && result.sessionId) convId = result.sessionId;
|
||||||
|
const text = (result.contents || []).reduce((str, part) => {
|
||||||
|
const { role, content } = part;
|
||||||
|
if (role != "assistant" && !_.isString(content)) return str;
|
||||||
|
return str + content;
|
||||||
|
}, "");
|
||||||
|
if (result.contentFrom == "text2image") {
|
||||||
|
const urls =
|
||||||
|
text.match(
|
||||||
|
/https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi
|
||||||
|
) || [];
|
||||||
|
urls.forEach((url) => {
|
||||||
|
const urlObj = new URL(url);
|
||||||
|
urlObj.search = "";
|
||||||
|
const imageUrl = urlObj.toString();
|
||||||
|
if (imageUrls.indexOf(imageUrl) != -1) return;
|
||||||
|
imageUrls.push(imageUrl);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (result.msgStatus == "finished") {
|
||||||
|
if (!result.canShare || imageUrls.length == 0)
|
||||||
|
throw new APIException(EX.API_CONTENT_FILTERED);
|
||||||
|
if (result.errorCode)
|
||||||
|
throw new APIException(
|
||||||
|
EX.API_REQUEST_FAILED,
|
||||||
|
`服务暂时不可用,第三方响应错误:${result.errorCode}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 将流数据喂给SSE转换器
|
||||||
|
stream.on("data", (buffer) => parser.feed(buffer.toString()));
|
||||||
|
stream.once("error", (err) => reject(err));
|
||||||
|
stream.once("close", () => resolve({ convId, imageUrls }));
|
||||||
|
stream.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上传参数
|
||||||
|
*
|
||||||
|
* @param ticket tongyi_sso_ticket或login_aliyunid_ticket
|
||||||
|
*/
|
||||||
|
async function acquireUploadParams(ticket: string) {
|
||||||
|
const result = await axios.post(
|
||||||
|
"https://qianwen.biz.aliyun.com/dialog/uploadToken",
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
timeout: 15000,
|
||||||
|
headers: {
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
},
|
||||||
|
validateStatus: () => true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const { data } = checkResult(result);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 预检查文件URL有效性
|
||||||
|
*
|
||||||
|
* @param fileUrl 文件URL
|
||||||
|
*/
|
||||||
|
async function checkFileUrl(fileUrl: string) {
|
||||||
|
if (util.isBASE64Data(fileUrl)) return;
|
||||||
|
const result = await axios.head(fileUrl, {
|
||||||
|
timeout: 15000,
|
||||||
|
validateStatus: () => true,
|
||||||
|
});
|
||||||
|
if (result.status >= 400)
|
||||||
|
throw new APIException(
|
||||||
|
EX.API_FILE_URL_INVALID,
|
||||||
|
`File ${fileUrl} is not valid: [${result.status}] ${result.statusText}`
|
||||||
|
);
|
||||||
|
// 检查文件大小
|
||||||
|
if (result.headers && result.headers["content-length"]) {
|
||||||
|
const fileSize = parseInt(result.headers["content-length"], 10);
|
||||||
|
if (fileSize > FILE_MAX_SIZE)
|
||||||
|
throw new APIException(
|
||||||
|
EX.API_FILE_EXECEEDS_SIZE,
|
||||||
|
`File ${fileUrl} is not valid`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param fileUrl 文件URL
|
||||||
|
* @param ticket tongyi_sso_ticket或login_aliyunid_ticket
|
||||||
|
*/
|
||||||
|
async function uploadFile(fileUrl: string, ticket: string) {
|
||||||
|
// 预检查远程文件URL可用性
|
||||||
|
await checkFileUrl(fileUrl);
|
||||||
|
|
||||||
|
let filename, fileData, mimeType;
|
||||||
|
// 如果是BASE64数据则直接转换为Buffer
|
||||||
|
if (util.isBASE64Data(fileUrl)) {
|
||||||
|
mimeType = util.extractBASE64DataFormat(fileUrl);
|
||||||
|
const ext = mime.getExtension(mimeType);
|
||||||
|
filename = `${util.uuid()}.${ext}`;
|
||||||
|
fileData = Buffer.from(util.removeBASE64DataHeader(fileUrl), "base64");
|
||||||
|
}
|
||||||
|
// 下载文件到内存,如果您的服务器内存很小,建议考虑改造为流直传到下一个接口上,避免停留占用内存
|
||||||
|
else {
|
||||||
|
filename = path.basename(fileUrl);
|
||||||
|
({ data: fileData } = await axios.get(fileUrl, {
|
||||||
|
responseType: "arraybuffer",
|
||||||
|
// 100M限制
|
||||||
|
maxContentLength: FILE_MAX_SIZE,
|
||||||
|
// 60秒超时
|
||||||
|
timeout: 60000,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件的MIME类型
|
||||||
|
mimeType = mimeType || mime.getType(filename);
|
||||||
|
|
||||||
|
// 获取上传参数
|
||||||
|
const { accessId, policy, signature, dir } = await acquireUploadParams(
|
||||||
|
ticket
|
||||||
|
);
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("OSSAccessKeyId", accessId);
|
||||||
|
formData.append("policy", policy);
|
||||||
|
formData.append("signature", signature);
|
||||||
|
formData.append("key", `${dir}${filename}`);
|
||||||
|
formData.append("dir", dir);
|
||||||
|
formData.append("success_action_status", "200");
|
||||||
|
formData.append("file", fileData, {
|
||||||
|
filename,
|
||||||
|
contentType: mimeType,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 上传文件到OSS
|
||||||
|
await axios.request({
|
||||||
|
method: "POST",
|
||||||
|
url: "https://broadscope-dialogue-new.oss-cn-beijing.aliyuncs.com/",
|
||||||
|
data: formData,
|
||||||
|
// 100M限制
|
||||||
|
maxBodyLength: FILE_MAX_SIZE,
|
||||||
|
// 60秒超时
|
||||||
|
timeout: 120000,
|
||||||
|
headers: {
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
"X-Requested-With": "XMLHttpRequest"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const isImage = [
|
||||||
|
'image/jpeg',
|
||||||
|
'image/jpg',
|
||||||
|
'image/tiff',
|
||||||
|
'image/png',
|
||||||
|
'image/bmp',
|
||||||
|
'image/gif',
|
||||||
|
'image/svg+xml',
|
||||||
|
'image/webp',
|
||||||
|
'image/ico',
|
||||||
|
'image/heic',
|
||||||
|
'image/heif',
|
||||||
|
'image/bmp',
|
||||||
|
'image/x-icon',
|
||||||
|
'image/vnd.microsoft.icon',
|
||||||
|
'image/x-png'
|
||||||
|
].includes(mimeType);
|
||||||
|
|
||||||
|
if(isImage) {
|
||||||
|
const result = await axios.post(
|
||||||
|
"https://qianwen.biz.aliyun.com/dialog/downloadLink",
|
||||||
|
{
|
||||||
|
fileKey: filename,
|
||||||
|
fileType: "image",
|
||||||
|
dir
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 15000,
|
||||||
|
headers: {
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
},
|
||||||
|
validateStatus: () => true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const { data } = checkResult(result);
|
||||||
|
return {
|
||||||
|
role: "user",
|
||||||
|
contentType: "image",
|
||||||
|
content: data.url
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let result = await axios.post(
|
||||||
|
"https://qianwen.biz.aliyun.com/dialog/downloadLink/batch",
|
||||||
|
{
|
||||||
|
fileKeys: [filename],
|
||||||
|
fileType: "file",
|
||||||
|
dir
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 15000,
|
||||||
|
headers: {
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
},
|
||||||
|
validateStatus: () => true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const { data } = checkResult(result);
|
||||||
|
if(!data.results[0] || !data.results[0].url)
|
||||||
|
throw new Error(`文件上传失败:${data.results[0] ? data.results[0].errorMsg : '未知错误'}`);
|
||||||
|
const url = data.results[0].url;
|
||||||
|
const startTime = util.timestamp();
|
||||||
|
while(true) {
|
||||||
|
result = await axios.post(
|
||||||
|
"https://qianwen.biz.aliyun.com/dialog/secResult/batch",
|
||||||
|
{
|
||||||
|
urls: [url]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 15000,
|
||||||
|
headers: {
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
},
|
||||||
|
validateStatus: () => true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const { data } = checkResult(result);
|
||||||
|
if(data.pollEndFlag) {
|
||||||
|
if(data.statusList[0] && data.statusList[0].status === 0)
|
||||||
|
throw new Error(`文件处理失败:${data.statusList[0].errorMsg || '未知错误'}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(util.timestamp() > startTime + 120000)
|
||||||
|
throw new Error("文件处理超时:超出120秒");
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
role: "user",
|
||||||
|
contentType: "file",
|
||||||
|
content: url,
|
||||||
|
ext: { fileSize: fileData.byteLength }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Token切分
|
* Token切分
|
||||||
*
|
*
|
||||||
@ -491,18 +913,14 @@ function tokenSplit(authorization: string) {
|
|||||||
/**
|
/**
|
||||||
* 生成Cookies
|
* 生成Cookies
|
||||||
*
|
*
|
||||||
* @param ticket login_tongyi_ticket值
|
* @param ticket tongyi_sso_ticket或login_aliyunid_ticket
|
||||||
*/
|
*/
|
||||||
function generateCookie(ticket: string) {
|
function generateCookie(ticket: string) {
|
||||||
return [
|
return [
|
||||||
`login_tongyi_ticket=${ticket}`,
|
`${ticket.length > 100 ? 'login_aliyunid_ticket' : 'tongyi_sso_ticket'}=${ticket}`,
|
||||||
'_samesite_flag_=true',
|
'aliyun_choice=intl',
|
||||||
|
"_samesite_flag_=true",
|
||||||
`t=${util.uuid(false)}`,
|
`t=${util.uuid(false)}`,
|
||||||
'channel=oug71n2fX3Jd5ualEfKACRvnsceUtpjUC5jHBpfWnSOXKhkvBNuSO8bG3v4HHjCgB722h7LqbHkB6sAxf3OvgA%3D%3D',
|
|
||||||
'currentRegionId=cn-shenzhen',
|
|
||||||
'aliyun_country=CN',
|
|
||||||
'aliyun_lang=zh',
|
|
||||||
'aliyun_site=CN',
|
|
||||||
// `login_aliyunid_csrf=_csrf_tk_${util.generateRandomString({ charset: 'numeric', length: 15 })}`,
|
// `login_aliyunid_csrf=_csrf_tk_${util.generateRandomString({ charset: 'numeric', length: 15 })}`,
|
||||||
// `cookie2=${util.uuid(false)}`,
|
// `cookie2=${util.uuid(false)}`,
|
||||||
// `munb=22${util.generateRandomString({ charset: 'numeric', length: 11 })}`,
|
// `munb=22${util.generateRandomString({ charset: 'numeric', length: 11 })}`,
|
||||||
@ -518,8 +936,35 @@ function generateCookie(ticket: string) {
|
|||||||
].join("; ");
|
].join("; ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Token存活状态
|
||||||
|
*/
|
||||||
|
async function getTokenLiveStatus(ticket: string) {
|
||||||
|
const result = await axios.post(
|
||||||
|
"https://qianwen.biz.aliyun.com/dialog/session/list",
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Cookie: generateCookie(ticket),
|
||||||
|
...FAKE_HEADERS,
|
||||||
|
},
|
||||||
|
timeout: 15000,
|
||||||
|
validateStatus: () => true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
const { data } = checkResult(result);
|
||||||
|
return _.isArray(data);
|
||||||
|
}
|
||||||
|
catch(err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
createCompletion,
|
createCompletion,
|
||||||
createCompletionStream,
|
createCompletionStream,
|
||||||
|
generateImages,
|
||||||
|
getTokenLiveStatus,
|
||||||
tokenSplit,
|
tokenSplit,
|
||||||
};
|
};
|
||||||
|
@ -1,36 +1,42 @@
|
|||||||
import _ from 'lodash';
|
import _ from "lodash";
|
||||||
|
|
||||||
import Request from '@/lib/request/Request.ts';
|
import Request from "@/lib/request/Request.ts";
|
||||||
import Response from '@/lib/response/Response.ts';
|
import Response from "@/lib/response/Response.ts";
|
||||||
import chat from '@/api/controllers/chat.ts';
|
import chat from "@/api/controllers/chat.ts";
|
||||||
import logger from '@/lib/logger.ts';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
prefix: "/v1/chat",
|
||||||
|
|
||||||
prefix: '/v1/chat',
|
post: {
|
||||||
|
"/completions": async (request: Request) => {
|
||||||
post: {
|
request
|
||||||
|
.validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
|
||||||
'/completions': async (request: Request) => {
|
.validate("body.messages", _.isArray)
|
||||||
request
|
.validate("headers.authorization", _.isString);
|
||||||
.validate('body.messages', _.isArray)
|
// ticket切分
|
||||||
.validate('headers.authorization', _.isString)
|
const tokens = chat.tokenSplit(request.headers.authorization);
|
||||||
// refresh_token切分
|
// 随机挑选一个ticket
|
||||||
const tokens = chat.tokenSplit(request.headers.authorization);
|
const token = _.sample(tokens);
|
||||||
// 随机挑选一个refresh_token
|
const { model, conversation_id: convId, messages, search_type, stream } = request.body;
|
||||||
const token = _.sample(tokens);
|
if (stream) {
|
||||||
const model = request.body.model;
|
const stream = await chat.createCompletionStream(
|
||||||
const messages = request.body.messages;
|
model,
|
||||||
if (request.body.stream) {
|
messages,
|
||||||
const stream = await chat.createCompletionStream(model, messages, token);
|
search_type,
|
||||||
return new Response(stream, {
|
token,
|
||||||
type: "text/event-stream"
|
convId
|
||||||
});
|
);
|
||||||
}
|
return new Response(stream, {
|
||||||
else
|
type: "text/event-stream",
|
||||||
return await chat.createCompletion(model, messages, token, request.body.use_search);
|
});
|
||||||
}
|
} else
|
||||||
|
return await chat.createCompletion(
|
||||||
}
|
model,
|
||||||
|
messages,
|
||||||
}
|
search_type,
|
||||||
|
token,
|
||||||
|
convId
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
39
src/api/routes/images.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
|
import Request from "@/lib/request/Request.ts";
|
||||||
|
import chat from "@/api/controllers/chat.ts";
|
||||||
|
import util from "@/lib/util.ts";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
prefix: "/v1/images",
|
||||||
|
|
||||||
|
post: {
|
||||||
|
"/generations": async (request: Request) => {
|
||||||
|
request
|
||||||
|
.validate("body.prompt", _.isString)
|
||||||
|
.validate("headers.authorization", _.isString);
|
||||||
|
// refresh_token切分
|
||||||
|
const tokens = chat.tokenSplit(request.headers.authorization);
|
||||||
|
// 随机挑选一个refresh_token
|
||||||
|
const token = _.sample(tokens);
|
||||||
|
const prompt = request.body.prompt;
|
||||||
|
const responseFormat = _.defaultTo(request.body.response_format, "url");
|
||||||
|
const model = request.body.model;
|
||||||
|
const imageUrls = await chat.generateImages(model, prompt, token);
|
||||||
|
let data = [];
|
||||||
|
if (responseFormat == "b64_json") {
|
||||||
|
data = (
|
||||||
|
await Promise.all(imageUrls.map((url) => util.fetchFileBASE64(url)))
|
||||||
|
).map((b64) => ({ b64_json: b64 }));
|
||||||
|
} else {
|
||||||
|
data = imageUrls.map((url) => ({
|
||||||
|
url,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
created: util.unixTimestamp(),
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
@ -1,5 +1,29 @@
|
|||||||
|
import fs from 'fs-extra';
|
||||||
|
|
||||||
|
import Response from '@/lib/response/Response.ts';
|
||||||
import chat from "./chat.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 [
|
export default [
|
||||||
chat
|
{
|
||||||
|
get: {
|
||||||
|
'/': async () => {
|
||||||
|
const content = await fs.readFile('public/welcome.html');
|
||||||
|
return new Response(content, {
|
||||||
|
type: 'html',
|
||||||
|
headers: {
|
||||||
|
Expires: '-1'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
chat,
|
||||||
|
images,
|
||||||
|
ping,
|
||||||
|
token,
|
||||||
|
models
|
||||||
];
|
];
|
56
src/api/routes/models.ts
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
6
src/api/routes/ping.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default {
|
||||||
|
prefix: "/ping",
|
||||||
|
get: {
|
||||||
|
"": async () => "pong",
|
||||||
|
},
|
||||||
|
};
|
25
src/api/routes/token.ts
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,13 +9,15 @@ import { format as dateFormat } from 'date-fns';
|
|||||||
import config from './config.ts';
|
import config from './config.ts';
|
||||||
import util from './util.ts';
|
import util from './util.ts';
|
||||||
|
|
||||||
|
const isVercelEnv = process.env.VERCEL;
|
||||||
|
|
||||||
class LogWriter {
|
class LogWriter {
|
||||||
|
|
||||||
#buffers = [];
|
#buffers = [];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
fs.ensureDirSync(config.system.logDirPath);
|
!isVercelEnv && fs.ensureDirSync(config.system.logDirPath);
|
||||||
this.work();
|
!isVercelEnv && this.work();
|
||||||
}
|
}
|
||||||
|
|
||||||
push(content) {
|
push(content) {
|
||||||
@ -24,16 +26,16 @@ class LogWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeSync(buffer) {
|
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) {
|
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() {
|
flush() {
|
||||||
if(!this.#buffers.length) return;
|
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() {
|
work() {
|
||||||
|
@ -15,7 +15,7 @@ export default class FailureBody extends Body {
|
|||||||
else if(error instanceof APIException || error instanceof Exception)
|
else if(error instanceof APIException || error instanceof Exception)
|
||||||
({ errcode, errmsg, data, httpStatusCode } = error);
|
({ errcode, errmsg, data, httpStatusCode } = error);
|
||||||
else if(_.isError(error))
|
else if(_.isError(error))
|
||||||
error = new Exception(EX.SYSTEM_ERROR, error.message);
|
({ errcode, errmsg, data, httpStatusCode } = new Exception(EX.SYSTEM_ERROR, error.message));
|
||||||
super({
|
super({
|
||||||
code: errcode || -1,
|
code: errcode || -1,
|
||||||
message: errmsg || 'Internal error',
|
message: errmsg || 'Internal error',
|
||||||
|
@ -73,7 +73,11 @@ class Server {
|
|||||||
this.app.use((ctx: any) => {
|
this.app.use((ctx: any) => {
|
||||||
const request = new Request(ctx);
|
const request = new Request(ctx);
|
||||||
logger.debug(`-> ${ctx.request.method} ${ctx.request.url} request is not supported - ${request.remoteIP || "unknown"}`);
|
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 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 response = new Response(failureBody);
|
const response = new Response(failureBody);
|
||||||
response.injectTo(ctx);
|
response.injectTo(ctx);
|
||||||
if(config.system.requestLog)
|
if(config.system.requestLog)
|
||||||
|
457
src/lib/util.ts
@ -1,258 +1,307 @@
|
|||||||
import os from 'os';
|
import os from "os";
|
||||||
import path from 'path';
|
import path from "path";
|
||||||
import crypto from 'crypto';
|
import crypto from "crypto";
|
||||||
import { Readable, Writable } from 'stream';
|
import { Readable, Writable } from "stream";
|
||||||
|
|
||||||
import 'colors';
|
import "colors";
|
||||||
import mime from 'mime';
|
import mime from "mime";
|
||||||
import fs from 'fs-extra';
|
import fs from "fs-extra";
|
||||||
import { v1 as uuid } from 'uuid';
|
import { v1 as uuid } from "uuid";
|
||||||
import { format as dateFormat } from 'date-fns';
|
import { format as dateFormat } from "date-fns";
|
||||||
import CRC32 from 'crc-32';
|
import CRC32 from "crc-32";
|
||||||
import randomstring from 'randomstring';
|
import randomstring from "randomstring";
|
||||||
import _ from 'lodash';
|
import _ from "lodash";
|
||||||
import { CronJob } from 'cron';
|
import { CronJob } from "cron";
|
||||||
|
|
||||||
import HTTP_STATUS_CODE from './http-status-codes.ts';
|
import HTTP_STATUS_CODE from "./http-status-codes.ts";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
const autoIdMap = new Map();
|
const autoIdMap = new Map();
|
||||||
|
|
||||||
const util = {
|
const util = {
|
||||||
|
is2DArrays(value: any) {
|
||||||
|
return (
|
||||||
|
_.isArray(value) &&
|
||||||
|
(!value[0] || (_.isArray(value[0]) && _.isArray(value[value.length - 1])))
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
is2DArrays(value: any) {
|
uuid: (separator = true) => (separator ? uuid() : uuid().replace(/\-/g, "")),
|
||||||
return _.isArray(value) && (!value[0] || (_.isArray(value[0]) && _.isArray(value[value.length - 1])));
|
|
||||||
},
|
|
||||||
|
|
||||||
uuid: (separator = true) => separator ? uuid() : uuid().replace(/\-/g, ""),
|
autoId: (prefix = "") => {
|
||||||
|
let index = autoIdMap.get(prefix);
|
||||||
|
if (index > 999999) index = 0; //超过最大数字则重置为0
|
||||||
|
autoIdMap.set(prefix, (index || 0) + 1);
|
||||||
|
return `${prefix}${index || 1}`;
|
||||||
|
},
|
||||||
|
|
||||||
autoId: (prefix = '') => {
|
ignoreJSONParse(value: string) {
|
||||||
let index = autoIdMap.get(prefix);
|
const result = _.attempt(() => JSON.parse(value));
|
||||||
if(index > 999999) index = 0; //超过最大数字则重置为0
|
if (_.isError(result)) return null;
|
||||||
autoIdMap.set(prefix, (index || 0) + 1);
|
return result;
|
||||||
return `${prefix}${index || 1}`;
|
},
|
||||||
},
|
|
||||||
|
|
||||||
ignoreJSONParse(value: string) {
|
generateRandomString(options: any): string {
|
||||||
const result = _.attempt(() => JSON.parse(value));
|
return randomstring.generate(options);
|
||||||
if(_.isError(result))
|
},
|
||||||
return null;
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
|
|
||||||
generateRandomString(options: any): string {
|
getResponseContentType(value: any): string | null {
|
||||||
return randomstring.generate(options);
|
return value.headers
|
||||||
},
|
? value.headers["content-type"] || value.headers["Content-Type"]
|
||||||
|
: null;
|
||||||
|
},
|
||||||
|
|
||||||
getResponseContentType(value: any): string | null {
|
mimeToExtension(value: string) {
|
||||||
return value.headers ? (value.headers["content-type"] || value.headers["Content-Type"]) : null;
|
let extension = mime.getExtension(value);
|
||||||
},
|
if (extension == "mpga") return "mp3";
|
||||||
|
return extension;
|
||||||
|
},
|
||||||
|
|
||||||
mimeToExtension(value: string) {
|
extractURLExtension(value: string) {
|
||||||
let extension = mime.getExtension(value);
|
const extname = path.extname(new URL(value).pathname);
|
||||||
if(extension == "mpga")
|
return extname.substring(1).toLowerCase();
|
||||||
return "mp3";
|
},
|
||||||
return extension;
|
|
||||||
},
|
|
||||||
|
|
||||||
extractURLExtension(value: string) {
|
createCronJob(cronPatterns: any, callback?: Function) {
|
||||||
const extname = path.extname(new URL(value).pathname);
|
if (!_.isFunction(callback))
|
||||||
return extname.substring(1).toLowerCase();
|
throw new Error("callback must be an Function");
|
||||||
},
|
return new CronJob(
|
||||||
|
cronPatterns,
|
||||||
|
() => callback(),
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
"Asia/Shanghai"
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
createCronJob(cronPatterns: any, callback?: Function) {
|
getDateString(format = "yyyy-MM-dd", date = new Date()) {
|
||||||
if(!_.isFunction(callback)) throw new Error("callback must be an Function");
|
return dateFormat(date, format);
|
||||||
return new CronJob(cronPatterns, () => callback(), null, false, "Asia/Shanghai");
|
},
|
||||||
},
|
|
||||||
|
|
||||||
getDateString(format = "yyyy-MM-dd", date = new Date()) {
|
getIPAddressesByIPv4(): string[] {
|
||||||
return dateFormat(date, format);
|
const interfaces = os.networkInterfaces();
|
||||||
},
|
const addresses = [];
|
||||||
|
for (let name in interfaces) {
|
||||||
|
const networks = interfaces[name];
|
||||||
|
const results = networks.filter(
|
||||||
|
(network) =>
|
||||||
|
network.family === "IPv4" &&
|
||||||
|
network.address !== "127.0.0.1" &&
|
||||||
|
!network.internal
|
||||||
|
);
|
||||||
|
if (results[0] && results[0].address) addresses.push(results[0].address);
|
||||||
|
}
|
||||||
|
return addresses;
|
||||||
|
},
|
||||||
|
|
||||||
getIPAddressesByIPv4(): string[] {
|
getMACAddressesByIPv4(): string[] {
|
||||||
const interfaces = os.networkInterfaces();
|
const interfaces = os.networkInterfaces();
|
||||||
const addresses = [];
|
const addresses = [];
|
||||||
for (let name in interfaces) {
|
for (let name in interfaces) {
|
||||||
const networks = interfaces[name];
|
const networks = interfaces[name];
|
||||||
const results = networks.filter(network => network.family === "IPv4" && network.address !== "127.0.0.1" && !network.internal);
|
const results = networks.filter(
|
||||||
if (results[0] && results[0].address)
|
(network) =>
|
||||||
addresses.push(results[0].address);
|
network.family === "IPv4" &&
|
||||||
}
|
network.address !== "127.0.0.1" &&
|
||||||
return addresses;
|
!network.internal
|
||||||
},
|
);
|
||||||
|
if (results[0] && results[0].mac) addresses.push(results[0].mac);
|
||||||
|
}
|
||||||
|
return addresses;
|
||||||
|
},
|
||||||
|
|
||||||
getMACAddressesByIPv4(): string[] {
|
generateSSEData(event?: string, data?: string, retry?: number) {
|
||||||
const interfaces = os.networkInterfaces();
|
return `event: ${event || "message"}\ndata: ${(data || "")
|
||||||
const addresses = [];
|
.replace(/\n/g, "\\n")
|
||||||
for (let name in interfaces) {
|
.replace(/\s/g, "\\s")}\nretry: ${retry || 3000}\n\n`;
|
||||||
const networks = interfaces[name];
|
},
|
||||||
const results = networks.filter(network => network.family === "IPv4" && network.address !== "127.0.0.1" && !network.internal);
|
|
||||||
if (results[0] && results[0].mac)
|
|
||||||
addresses.push(results[0].mac);
|
|
||||||
}
|
|
||||||
return addresses;
|
|
||||||
},
|
|
||||||
|
|
||||||
generateSSEData(event?: string, data?: string, retry?: number) {
|
buildDataBASE64(type, ext, buffer) {
|
||||||
return `event: ${event || "message"}\ndata: ${(data || "").replace(/\n/g, "\\n").replace(/\s/g, "\\s")}\nretry: ${retry || 3000}\n\n`;
|
return `data:${type}/${ext.replace("jpg", "jpeg")};base64,${buffer.toString(
|
||||||
},
|
"base64"
|
||||||
|
)}`;
|
||||||
|
},
|
||||||
|
|
||||||
buildDataBASE64(type, ext, buffer) {
|
isLinux() {
|
||||||
return `data:${type}/${ext.replace("jpg", "jpeg")};base64,${buffer.toString("base64")}`;
|
return os.platform() !== "win32";
|
||||||
},
|
},
|
||||||
|
|
||||||
isLinux() {
|
isIPAddress(value) {
|
||||||
return os.platform() !== "win32";
|
return (
|
||||||
},
|
_.isString(value) &&
|
||||||
|
(/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(
|
||||||
isIPAddress(value) {
|
value
|
||||||
return _.isString(value) && (/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(value) || /\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*/.test(value));
|
) ||
|
||||||
},
|
/\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*/.test(
|
||||||
|
value
|
||||||
|
))
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
isPort(value) {
|
isPort(value) {
|
||||||
return _.isNumber(value) && value > 0 && value < 65536;
|
return _.isNumber(value) && value > 0 && value < 65536;
|
||||||
},
|
},
|
||||||
|
|
||||||
isReadStream(value): boolean {
|
isReadStream(value): boolean {
|
||||||
return value && (value instanceof Readable || "readable" in value || value.readable);
|
return (
|
||||||
},
|
value &&
|
||||||
|
(value instanceof Readable || "readable" in value || value.readable)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
isWriteStream(value): boolean {
|
isWriteStream(value): boolean {
|
||||||
return value && (value instanceof Writable || "writable" in value || value.writable);
|
return (
|
||||||
},
|
value &&
|
||||||
|
(value instanceof Writable || "writable" in value || value.writable)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
isHttpStatusCode(value) {
|
isHttpStatusCode(value) {
|
||||||
return _.isNumber(value) && Object.values(HTTP_STATUS_CODE).includes(value);
|
return _.isNumber(value) && Object.values(HTTP_STATUS_CODE).includes(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
isURL(value) {
|
isURL(value) {
|
||||||
return !_.isUndefined(value) && /^(http|https)/.test(value);
|
return !_.isUndefined(value) && /^(http|https)/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
isSrc(value) {
|
isSrc(value) {
|
||||||
return !_.isUndefined(value) && /^\/.+\.[0-9a-zA-Z]+(\?.+)?$/.test(value);
|
return !_.isUndefined(value) && /^\/.+\.[0-9a-zA-Z]+(\?.+)?$/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
isBASE64(value) {
|
isBASE64(value) {
|
||||||
return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
|
return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
isBASE64Data(value) {
|
isBASE64Data(value) {
|
||||||
return /^data:/.test(value);
|
return /^data:/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
extractBASE64DataFormat(value): string | null {
|
extractBASE64DataFormat(value): string | null {
|
||||||
const match = value.trim().match(/^data:(.+);base64,/);
|
const match = value.trim().match(/^data:(.+);base64,/);
|
||||||
if(!match) return null;
|
if (!match) return null;
|
||||||
return match[1];
|
return match[1];
|
||||||
},
|
},
|
||||||
|
|
||||||
removeBASE64DataHeader(value): string {
|
removeBASE64DataHeader(value): string {
|
||||||
return value.replace(/^data:(.+);base64,/, "");
|
return value.replace(/^data:(.+);base64,/, "");
|
||||||
},
|
},
|
||||||
|
|
||||||
isDataString(value): boolean {
|
isDataString(value): boolean {
|
||||||
return /^(base64|json):/.test(value);
|
return /^(base64|json):/.test(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
isStringNumber(value) {
|
isStringNumber(value) {
|
||||||
return _.isFinite(Number(value));
|
return _.isFinite(Number(value));
|
||||||
},
|
},
|
||||||
|
|
||||||
isUnixTimestamp(value) {
|
isUnixTimestamp(value) {
|
||||||
return /^[0-9]{10}$/.test(`${value}`);
|
return /^[0-9]{10}$/.test(`${value}`);
|
||||||
},
|
},
|
||||||
|
|
||||||
isTimestamp(value) {
|
isTimestamp(value) {
|
||||||
return /^[0-9]{13}$/.test(`${value}`);
|
return /^[0-9]{13}$/.test(`${value}`);
|
||||||
},
|
},
|
||||||
|
|
||||||
isEmail(value) {
|
isEmail(value) {
|
||||||
return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value);
|
return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(
|
||||||
},
|
value
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
isAsyncFunction(value) {
|
isAsyncFunction(value) {
|
||||||
return Object.prototype.toString.call(value) === "[object AsyncFunction]";
|
return Object.prototype.toString.call(value) === "[object AsyncFunction]";
|
||||||
},
|
},
|
||||||
|
|
||||||
async isAPNG(filePath) {
|
async isAPNG(filePath) {
|
||||||
let head;
|
let head;
|
||||||
const readStream = fs.createReadStream(filePath, { start: 37, end: 40 });
|
const readStream = fs.createReadStream(filePath, { start: 37, end: 40 });
|
||||||
const readPromise = new Promise((resolve, reject) => {
|
const readPromise = new Promise((resolve, reject) => {
|
||||||
readStream.once("end", resolve);
|
readStream.once("end", resolve);
|
||||||
readStream.once("error", reject);
|
readStream.once("error", reject);
|
||||||
});
|
});
|
||||||
readStream.once("data", data => head = data);
|
readStream.once("data", (data) => (head = data));
|
||||||
await readPromise;
|
await readPromise;
|
||||||
return head.compare(Buffer.from([0x61, 0x63, 0x54, 0x4c])) === 0;
|
return head.compare(Buffer.from([0x61, 0x63, 0x54, 0x4c])) === 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
unixTimestamp() {
|
unixTimestamp() {
|
||||||
return parseInt(`${Date.now() / 1000}`);
|
return parseInt(`${Date.now() / 1000}`);
|
||||||
},
|
},
|
||||||
|
|
||||||
timestamp() {
|
timestamp() {
|
||||||
return Date.now();
|
return Date.now();
|
||||||
},
|
},
|
||||||
|
|
||||||
urlJoin(...values) {
|
urlJoin(...values) {
|
||||||
let url = "";
|
let url = "";
|
||||||
for (let i = 0; i < values.length; i++)
|
for (let i = 0; i < values.length; i++)
|
||||||
url += `${i > 0 ? "/" : ""}${values[i].replace(/^\/*/, "").replace(/\/*$/, "")}`;
|
url += `${i > 0 ? "/" : ""}${values[i]
|
||||||
return url;
|
.replace(/^\/*/, "")
|
||||||
},
|
.replace(/\/*$/, "")}`;
|
||||||
|
return url;
|
||||||
|
},
|
||||||
|
|
||||||
millisecondsToHmss(milliseconds) {
|
millisecondsToHmss(milliseconds) {
|
||||||
if (_.isString(milliseconds)) return milliseconds;
|
if (_.isString(milliseconds)) return milliseconds;
|
||||||
milliseconds = parseInt(milliseconds);
|
milliseconds = parseInt(milliseconds);
|
||||||
const sec = Math.floor(milliseconds / 1000);
|
const sec = Math.floor(milliseconds / 1000);
|
||||||
const hours = Math.floor(sec / 3600);
|
const hours = Math.floor(sec / 3600);
|
||||||
const minutes = Math.floor((sec - hours * 3600) / 60);
|
const minutes = Math.floor((sec - hours * 3600) / 60);
|
||||||
const seconds = sec - hours * 3600 - minutes * 60;
|
const seconds = sec - hours * 3600 - minutes * 60;
|
||||||
const ms = milliseconds % 60000 - seconds * 1000;
|
const ms = (milliseconds % 60000) - seconds * 1000;
|
||||||
return `${hours > 9 ? hours : "0" + hours}:${minutes > 9 ? minutes : "0" + minutes}:${seconds > 9 ? seconds : "0" + seconds}.${ms}`;
|
return `${hours > 9 ? hours : "0" + hours}:${
|
||||||
},
|
minutes > 9 ? minutes : "0" + minutes
|
||||||
|
}:${seconds > 9 ? seconds : "0" + seconds}.${ms}`;
|
||||||
|
},
|
||||||
|
|
||||||
millisecondsToTimeString(milliseconds) {
|
millisecondsToTimeString(milliseconds) {
|
||||||
if(milliseconds < 1000)
|
if (milliseconds < 1000) return `${milliseconds}ms`;
|
||||||
return `${milliseconds}ms`;
|
if (milliseconds < 60000)
|
||||||
if(milliseconds < 60000)
|
return `${parseFloat((milliseconds / 1000).toFixed(2))}s`;
|
||||||
return `${parseFloat((milliseconds / 1000).toFixed(2))}s`;
|
return `${Math.floor(milliseconds / 1000 / 60)}m${Math.floor(
|
||||||
return `${Math.floor(milliseconds / 1000 / 60)}m${Math.floor(milliseconds / 1000 % 60)}s`;
|
(milliseconds / 1000) % 60
|
||||||
},
|
)}s`;
|
||||||
|
},
|
||||||
|
|
||||||
rgbToHex(r, g, b): string {
|
rgbToHex(r, g, b): string {
|
||||||
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
||||||
},
|
},
|
||||||
|
|
||||||
hexToRgb(hex) {
|
hexToRgb(hex) {
|
||||||
const value = parseInt(hex.replace(/^#/, ""), 16);
|
const value = parseInt(hex.replace(/^#/, ""), 16);
|
||||||
return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
|
return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
|
||||||
},
|
},
|
||||||
|
|
||||||
md5(value) {
|
md5(value) {
|
||||||
return crypto.createHash("md5").update(value).digest("hex");
|
return crypto.createHash("md5").update(value).digest("hex");
|
||||||
},
|
},
|
||||||
|
|
||||||
crc32(value) {
|
crc32(value) {
|
||||||
return _.isBuffer(value) ? CRC32.buf(value) : CRC32.str(value);
|
return _.isBuffer(value) ? CRC32.buf(value) : CRC32.str(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
arrayParse(value): any[] {
|
arrayParse(value): any[] {
|
||||||
return _.isArray(value) ? value : [value];
|
return _.isArray(value) ? value : [value];
|
||||||
},
|
},
|
||||||
|
|
||||||
booleanParse(value) {
|
booleanParse(value) {
|
||||||
return value === "true" || value === true ? true : false
|
return value === "true" || value === true ? true : false;
|
||||||
},
|
},
|
||||||
|
|
||||||
encodeBASE64(value) {
|
encodeBASE64(value) {
|
||||||
return Buffer.from(value).toString("base64");
|
return Buffer.from(value).toString("base64");
|
||||||
},
|
},
|
||||||
|
|
||||||
decodeBASE64(value) {
|
decodeBASE64(value) {
|
||||||
return Buffer.from(value, "base64").toString();
|
return Buffer.from(value, "base64").toString();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async fetchFileBASE64(url: string) {
|
||||||
|
const result = await axios.get(url, {
|
||||||
|
responseType: "arraybuffer",
|
||||||
|
});
|
||||||
|
return result.data.toString("base64");
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default util;
|
export default util;
|
||||||
|
27
vercel.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|