Spaces:
Paused
Paused
from flask import Flask, request, Response, stream_with_context, jsonify | |
import requests | |
import json | |
app = Flask(__name__) | |
def index(): | |
return "Hello, this is the root page of your Flask application!" | |
def forward_to_target(subpath): | |
try: | |
# 构建目标 URL | |
target_url = f'http://{subpath}' | |
# 获取请求数据 | |
data = request.json | |
# 检查是否是特定路径需要特殊处理 | |
if '/v1/chat/completions' in subpath: | |
auth_header = request.headers.get('Authorization') | |
if not auth_header or not auth_header.startswith('Bearer '): | |
return jsonify({"error": "Unauthorized"}), 401 | |
api_key = auth_header.split(" ")[1] | |
target_url = f"http://{subpath.split('/')[0]}" | |
model = data['model'] | |
messages = data['messages'] | |
temperature = data.get('temperature', 0.7) # 默认值0.7 | |
top_p = data.get('top_p', 1.0) # 默认值1.0 | |
n = data.get('n', 1) # 默认值1 | |
stream = data.get('stream', False) # 默认值False | |
functions = data.get('functions', None) # Functions for function calling | |
function_call = data.get('function_call', None) # Specific function call request | |
headers = { | |
'Authorization': f'Bearer {api_key}', | |
'Content-Type': 'application/json' | |
} | |
payload = { | |
'model': model, | |
'messages': messages, | |
'temperature': temperature, | |
'top_p': top_p, | |
'n': n, | |
'stream': stream, | |
'functions': functions, | |
'function_call': function_call | |
} | |
if stream: | |
def generate(): | |
with requests.post(target_url, headers=headers, json=payload, stream=True) as r: | |
for chunk in r.iter_content(chunk_size=8192): | |
if chunk: | |
yield f"data: {chunk.decode('utf-8')}\n\n" | |
return Response(stream_with_context(generate()), content_type='text/event-stream') | |
else: | |
response = requests.post(target_url, headers=headers, json=payload) | |
return jsonify(response.json()) | |
else: | |
# 获取请求头 | |
headers = {key: value for key, value in request.headers if key != 'Host'} | |
# 转发请求到目标 URL | |
response = requests.post(target_url, headers=headers, json=data) | |
# 返回目标 URL 的响应 | |
return Response(response.content, status=response.status_code, content_type=response.headers['Content-Type']) | |
except Exception as e: | |
print("Exception:", e) | |
return jsonify({"error": str(e)}), 500 | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=7860, threaded=True) | |