mirror of
https://github.com/Yanyutin753/RefreshToV1Api.git
synced 2025-10-15 15:41:21 +00:00
[feat] 支持自定义本项目接口前缀
This commit is contained in:
@@ -34,12 +34,12 @@
|
|||||||
|
|
||||||
- [x] 支持 接口保活
|
- [x] 支持 接口保活
|
||||||
|
|
||||||
|
- [x] 支持 自定义接口前缀
|
||||||
|
|
||||||
- [ ] 支持 gpt-4-vision
|
- [ ] 支持 gpt-4-vision
|
||||||
|
|
||||||
- [ ] 支持 日志等级划分
|
- [ ] 支持 日志等级划分
|
||||||
|
|
||||||
- [ ] 支持 自定义接口前缀
|
|
||||||
|
|
||||||
- [ ] 优化 偶现的【0†source】引用bug
|
- [ ] 优化 偶现的【0†source】引用bug
|
||||||
|
|
||||||
## 注意
|
## 注意
|
||||||
|
@@ -14,6 +14,7 @@ services:
|
|||||||
- GPT_4_S_New_Name=gpt-4-s # gpt-4-s模型的自定义模型名称,支持同时设置多个,用英文逗号分隔
|
- GPT_4_S_New_Name=gpt-4-s # gpt-4-s模型的自定义模型名称,支持同时设置多个,用英文逗号分隔
|
||||||
- GPT_4_MOBILE_NEW_NAME=gpt-4-mobile # gpt-4-mobile模型的自定义模型名称,支持同时设置多个,用英文逗号分隔
|
- GPT_4_MOBILE_NEW_NAME=gpt-4-mobile # gpt-4-mobile模型的自定义模型名称,支持同时设置多个,用英文逗号分隔
|
||||||
- GPT_3_5_NEW_NAME=gpt-3.5-turbo # gpt-3.5-turbo模型的自定义模型名称,支持同时设置多个,用英文逗号分隔
|
- GPT_3_5_NEW_NAME=gpt-3.5-turbo # gpt-3.5-turbo模型的自定义模型名称,支持同时设置多个,用英文逗号分隔
|
||||||
|
- API_PREFIX=<本项目接口前缀> # 本项目/v1接口的前缀,示例:666,如果留空默认为原版本一致
|
||||||
volumes:
|
volumes:
|
||||||
- ./log:/app/log
|
- ./log:/app/log
|
||||||
- ./images:/app/images
|
- ./images:/app/images
|
||||||
|
24
main.py
24
main.py
@@ -104,6 +104,9 @@ BASE_URL = os.getenv('BASE_URL', '')
|
|||||||
PROXY_API_PREFIX = os.getenv('PROXY_API_PREFIX', '')
|
PROXY_API_PREFIX = os.getenv('PROXY_API_PREFIX', '')
|
||||||
UPLOAD_BASE_URL = os.getenv('UPLOAD_BASE_URL', '')
|
UPLOAD_BASE_URL = os.getenv('UPLOAD_BASE_URL', '')
|
||||||
KEY_FOR_GPTS_INFO = os.getenv('KEY_FOR_GPTS_INFO', '')
|
KEY_FOR_GPTS_INFO = os.getenv('KEY_FOR_GPTS_INFO', '')
|
||||||
|
# 添加环境变量配置
|
||||||
|
API_PREFIX = os.getenv('API_PREFIX', '')
|
||||||
|
|
||||||
|
|
||||||
VERSION = '0.1.7'
|
VERSION = '0.1.7'
|
||||||
# VERSION = 'test'
|
# VERSION = 'test'
|
||||||
@@ -138,6 +141,15 @@ with app.app_context():
|
|||||||
else:
|
else:
|
||||||
print(f"KEY_FOR_GPTS_INFO: {KEY_FOR_GPTS_INFO}")
|
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"==========================================")
|
print(f"==========================================")
|
||||||
|
|
||||||
# 从环境变量中读取模型名称,支持用逗号分隔的多个名称
|
# 从环境变量中读取模型名称,支持用逗号分隔的多个名称
|
||||||
@@ -751,7 +763,7 @@ def keep_alive(last_data_time, stop_event, queue, model, chat_message_id):
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
# 定义 Flask 路由
|
# 定义 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():
|
def chat_completions():
|
||||||
print(f"[{datetime.now()}] New Request")
|
print(f"[{datetime.now()}] New Request")
|
||||||
data = request.json
|
data = request.json
|
||||||
@@ -862,7 +874,7 @@ def chat_completions():
|
|||||||
return Response(generate(), mimetype='text/event-stream')
|
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():
|
def images_generations():
|
||||||
print(f"[{datetime.now()}] New Img Request")
|
print(f"[{datetime.now()}] New Img Request")
|
||||||
data = request.json
|
data = request.json
|
||||||
@@ -1228,20 +1240,20 @@ def after_request(response):
|
|||||||
|
|
||||||
|
|
||||||
# 特殊的 OPTIONS 请求处理器
|
# 特殊的 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():
|
def options_handler():
|
||||||
print(f"[{datetime.now()}] Options Request")
|
print(f"[{datetime.now()}] Options Request")
|
||||||
return Response(status=200)
|
return Response(status=200)
|
||||||
|
|
||||||
@app.route('/', defaults={'path': ''})
|
@app.route('/', defaults={'path': ''}, methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
||||||
@app.route('/<path:path>')
|
@app.route('/<path:path>', methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
||||||
def catch_all(path):
|
def catch_all(path):
|
||||||
print(f"[{datetime.now()}] 未知请求: {path}")
|
print(f"[{datetime.now()}] 未知请求: {path}")
|
||||||
print(f"[{datetime.now()}] 请求方法: {request.method}")
|
print(f"[{datetime.now()}] 请求方法: {request.method}")
|
||||||
print(f"[{datetime.now()}] 请求头: {request.headers}")
|
print(f"[{datetime.now()}] 请求头: {request.headers}")
|
||||||
print(f"[{datetime.now()}] 请求体: {request.data}")
|
print(f"[{datetime.now()}] 请求体: {request.data}")
|
||||||
|
|
||||||
return jsonify({"message": "Unknown"}), 200
|
return jsonify({"message": "Welcome to Inker's World"}), 200
|
||||||
|
|
||||||
|
|
||||||
@app.route('/images/<filename>')
|
@app.route('/images/<filename>')
|
||||||
|
Reference in New Issue
Block a user