Skip to content

Commit

Permalink
[feat] 支持自定义本项目接口前缀
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizerd authored and Wizerd committed Dec 19, 2023
1 parent 474e347 commit b7904fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

- [x] 支持 接口保活

- [x] 支持 自定义接口前缀

- [ ] 支持 gpt-4-vision

- [ ] 支持 日志等级划分

- [ ] 支持 自定义接口前缀

- [ ] 优化 偶现的【0†source】引用bug

## 注意
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def generate_gpts_payload(model, messages):
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'
Expand Down Expand Up @@ -137,6 +140,15 @@ def generate_gpts_payload(model, messages):
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"==========================================")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -862,7 +874,7 @@ def generate():
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
Expand Down Expand Up @@ -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>')
Expand Down

0 comments on commit b7904fd

Please sign in to comment.