[feat] 支持自定义本项目接口前缀

This commit is contained in:
Wizerd
2023-12-19 19:36:34 +08:00
parent 474e3477f8
commit b7904fda98
3 changed files with 21 additions and 8 deletions

View File

@@ -34,12 +34,12 @@
- [x] 支持 接口保活
- [x] 支持 自定义接口前缀
- [ ] 支持 gpt-4-vision
- [ ] 支持 日志等级划分
- [ ] 支持 自定义接口前缀
- [ ] 优化 偶现的【0†source】引用bug
## 注意

View File

@@ -14,6 +14,7 @@ services:
- GPT_4_S_New_Name=gpt-4-s # gpt-4-s模型的自定义模型名称支持同时设置多个用英文逗号分隔
- GPT_4_MOBILE_NEW_NAME=gpt-4-mobile # gpt-4-mobile模型的自定义模型名称支持同时设置多个用英文逗号分隔
- GPT_3_5_NEW_NAME=gpt-3.5-turbo # gpt-3.5-turbo模型的自定义模型名称支持同时设置多个用英文逗号分隔
- API_PREFIX=<本项目接口前缀> # 本项目/v1接口的前缀示例666如果留空默认为原版本一致
volumes:
- ./log:/app/log
- ./images:/app/images

24
main.py
View File

@@ -104,6 +104,9 @@ BASE_URL = os.getenv('BASE_URL', '')
PROXY_API_PREFIX = os.getenv('PROXY_API_PREFIX', '')
UPLOAD_BASE_URL = os.getenv('UPLOAD_BASE_URL', '')
KEY_FOR_GPTS_INFO = os.getenv('KEY_FOR_GPTS_INFO', '')
# 添加环境变量配置
API_PREFIX = os.getenv('API_PREFIX', '')
VERSION = '0.1.7'
# VERSION = 'test'
@@ -137,6 +140,15 @@ with app.app_context():
print("KEY_FOR_GPTS_INFO 未设置,请将 gpts.json 中仅保留 “{}” 作为内容")
else:
print(f"KEY_FOR_GPTS_INFO: {KEY_FOR_GPTS_INFO}")
if not API_PREFIX:
print("API_PREFIX 未设置,安全性会有所下降")
print(f'Chat 接口 URI: /v1/chat/completions')
print(f'绘图接口 URI: /v1/images/generations')
else:
print(f"API_PREFIX: {API_PREFIX}")
print(f'Chat 接口 URI: /{API_PREFIX}/v1/chat/completions')
print(f'绘图接口 URI: /{API_PREFIX}/v1/images/generations')
print(f"==========================================")
@@ -751,7 +763,7 @@ def keep_alive(last_data_time, stop_event, queue, model, chat_message_id):
import threading
import time
# 定义 Flask 路由
@app.route('/v1/chat/completions', methods=['POST'])
@app.route(f'/{API_PREFIX}/v1/chat/completions' if API_PREFIX else '/v1/chat/completions', methods=['POST'])
def chat_completions():
print(f"[{datetime.now()}] New Request")
data = request.json
@@ -862,7 +874,7 @@ def chat_completions():
return Response(generate(), mimetype='text/event-stream')
@app.route('/v1/images/generations', methods=['POST'])
@app.route(f'/{API_PREFIX}/v1/images/generations' if API_PREFIX else '/v1/images/generations', methods=['POST'])
def images_generations():
print(f"[{datetime.now()}] New Img Request")
data = request.json
@@ -1228,20 +1240,20 @@ def after_request(response):
# 特殊的 OPTIONS 请求处理器
@app.route('/v1/chat/completions', methods=['OPTIONS'])
@app.route(f'/{API_PREFIX}/v1/chat/completions' if API_PREFIX else '/v1/chat/completions', methods=['OPTIONS'])
def options_handler():
print(f"[{datetime.now()}] Options Request")
return Response(status=200)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
@app.route('/', defaults={'path': ''}, methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
@app.route('/<path:path>', methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
def catch_all(path):
print(f"[{datetime.now()}] 未知请求: {path}")
print(f"[{datetime.now()}] 请求方法: {request.method}")
print(f"[{datetime.now()}] 请求头: {request.headers}")
print(f"[{datetime.now()}] 请求体: {request.data}")
return jsonify({"message": "Unknown"}), 200
return jsonify({"message": "Welcome to Inker's World"}), 200
@app.route('/images/<filename>')