Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
from flask import Flask, request, Response, jsonify
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
-
|
| 6 |
@app.route('/')
|
| 7 |
def index():
|
| 8 |
return "Hello, this is the root page of your Flask application!"
|
|
@@ -10,57 +11,73 @@ def index():
|
|
| 10 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
| 11 |
def chat():
|
| 12 |
try:
|
| 13 |
-
#
|
| 14 |
auth_header = request.headers.get('Authorization')
|
| 15 |
if not auth_header or not auth_header.startswith('Bearer '):
|
| 16 |
-
return
|
| 17 |
|
| 18 |
-
api_key
|
| 19 |
-
|
| 20 |
-
# 构建目标 URL
|
| 21 |
-
target_url = f"{base_url}/v1/chat/completions"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
json=request.json,
|
| 37 |
-
stream=is_stream
|
| 38 |
)
|
| 39 |
-
print(target_url,api_key)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
def generate():
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
return Response(
|
| 48 |
-
generate(),
|
| 49 |
-
content_type=response.headers['Content-Type'],
|
| 50 |
-
status=response.status_code
|
| 51 |
-
)
|
| 52 |
else:
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
print("Exception:", e)
|
| 62 |
return jsonify({"error": str(e)}), 500
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
app.run(host='0.0.0.0', port=7860, threaded=True)
|
| 66 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, Response, stream_with_context, jsonify
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
@app.route('/')
|
| 8 |
def index():
|
| 9 |
return "Hello, this is the root page of your Flask application!"
|
|
|
|
| 11 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
| 12 |
def chat():
|
| 13 |
try:
|
| 14 |
+
# 验证请求头中的API密钥
|
| 15 |
auth_header = request.headers.get('Authorization')
|
| 16 |
if not auth_header or not auth_header.startswith('Bearer '):
|
| 17 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 18 |
|
| 19 |
+
api_key = auth_header.split(" ")[1]
|
| 20 |
+
base_url= auth_header.split(" ")[2]
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
data = request.json
|
| 23 |
+
#print("Received data:", data) # 打印请求体以进行调试
|
| 24 |
+
|
| 25 |
+
# 验证请求格式
|
| 26 |
+
if not data or 'messages' not in data or 'model' not in data:
|
| 27 |
+
return jsonify({"error": "Missing 'messages' or 'model' in request body"}), 400
|
| 28 |
|
| 29 |
+
model = data['model']
|
| 30 |
+
messages = data['messages']
|
| 31 |
+
temperature = data.get('temperature', 0.7) # 默认值0.7
|
| 32 |
+
#max_tokens = calculate_max_tokens(model, messages, requested_max_tokens)
|
| 33 |
+
top_p = data.get('top_p', 1.0) # 默认值1.0
|
| 34 |
+
n = data.get('n', 1) # 默认值1
|
| 35 |
+
stream = data.get('stream', False) # 默认值False
|
| 36 |
+
functions = data.get('functions', None) # Functions for function calling
|
| 37 |
+
function_call = data.get('function_call', None) # Specific function call request
|
| 38 |
|
| 39 |
+
# 创建每个请求的 OpenAI 客户端实例
|
| 40 |
+
client = OpenAI(
|
| 41 |
+
api_key=api_key,
|
| 42 |
+
base_url=base_url,
|
|
|
|
|
|
|
| 43 |
)
|
|
|
|
| 44 |
|
| 45 |
+
# 处理模型响应
|
| 46 |
+
if stream:
|
| 47 |
+
# 处理流式响应
|
| 48 |
def generate():
|
| 49 |
+
response = client.chat.completions.create(
|
| 50 |
+
model=model,
|
| 51 |
+
messages=messages,
|
| 52 |
+
temperature=temperature,
|
| 53 |
+
#max_tokens=max_tokens,
|
| 54 |
+
top_p=top_p,
|
| 55 |
+
n=n,
|
| 56 |
+
stream=True,
|
| 57 |
+
functions=functions,
|
| 58 |
+
function_call=function_call
|
| 59 |
+
)
|
| 60 |
+
for chunk in response:
|
| 61 |
+
yield f"data: {json.dumps(chunk.to_dict())}\n\n"
|
| 62 |
|
| 63 |
+
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
else:
|
| 65 |
+
# 处理非流式响应
|
| 66 |
+
response = client.chat.completions.create(
|
| 67 |
+
model=model,
|
| 68 |
+
messages=messages,
|
| 69 |
+
temperature=temperature,
|
| 70 |
+
#max_tokens=max_tokens,
|
| 71 |
+
top_p=top_p,
|
| 72 |
+
n=n,
|
| 73 |
+
functions=functions,
|
| 74 |
+
function_call=function_call,
|
| 75 |
)
|
| 76 |
+
return jsonify(response.to_dict())
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
print("Exception:", e)
|
| 80 |
return jsonify({"error": str(e)}), 500
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
app.run(host='0.0.0.0', port=7860, threaded=True)
|
|
|