Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,45 +38,82 @@ key_manager = APIKeyManager()
|
|
| 38 |
|
| 39 |
@app.route('/v1/models', methods=['GET'])
|
| 40 |
def list_models():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"group": None,
|
| 59 |
-
"is_blocking": False
|
| 60 |
-
}],
|
| 61 |
-
"root": "gpt-3.5-turbo",
|
| 62 |
-
"parent": None
|
| 63 |
-
}
|
| 64 |
-
]
|
| 65 |
-
return Response(json.dumps({"data": models, "object": "list"}), mimetype='application/json')
|
| 66 |
|
| 67 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 68 |
def proxy_request():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
headers = {
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
@app.route('/key_info', methods=['GET'])
|
| 82 |
def key_info():
|
|
|
|
| 38 |
|
| 39 |
@app.route('/v1/models', methods=['GET'])
|
| 40 |
def list_models():
|
| 41 |
+
key = key_manager.get_available_key()
|
| 42 |
+
if not key:
|
| 43 |
+
return Response(json.dumps({"error": "No available API keys"}), status=429, mimetype='application/json')
|
| 44 |
+
|
| 45 |
+
headers = {
|
| 46 |
+
"Authorization": f"Bearer {key}"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
response = requests.get(f"{CHATANYWHERE_ENDPOINT}/v1/models", headers=headers)
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
return Response(response.text, mimetype='application/json')
|
| 53 |
+
else:
|
| 54 |
+
error_message = f"Error: {response.status_code}, {response.text}"
|
| 55 |
+
return Response(json.dumps({"error": error_message}), status=response.status_code, mimetype='application/json')
|
| 56 |
+
except requests.RequestException as e:
|
| 57 |
+
return Response(json.dumps({"error": str(e)}), status=500, mimetype='application/json')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 60 |
def proxy_request():
|
| 61 |
+
data = request.get_json()
|
| 62 |
+
messages = data.get('messages', [])
|
| 63 |
+
model = data.get('model', 'gpt-4o-mini')
|
| 64 |
+
max_tokens = data.get('max_tokens', 4096)
|
| 65 |
+
temperature = data.get('temperature', 1)
|
| 66 |
+
stream = data.get('stream', False)
|
| 67 |
+
key = key_manager.get_available_key()
|
| 68 |
+
|
| 69 |
+
if not key:
|
| 70 |
+
return Response(json.dumps({"error": "No available API keys"}), status=429, mimetype='application/json')
|
| 71 |
|
| 72 |
headers = {
|
| 73 |
+
"Content-Type": "application/json",
|
| 74 |
+
"Authorization": f"Bearer {key}"
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
payload = {
|
| 78 |
+
"model": model,
|
| 79 |
+
"messages": messages,
|
| 80 |
+
"max_tokens": max_tokens,
|
| 81 |
+
"temperature": temperature,
|
| 82 |
+
"stream": stream
|
| 83 |
}
|
| 84 |
|
| 85 |
+
if stream:
|
| 86 |
+
def generate():
|
| 87 |
+
response = requests.post(f"{CHATANYWHERE_ENDPOINT}/v1/chat/completions", headers=headers, json=payload, stream=True)
|
| 88 |
+
for line in response.iter_lines():
|
| 89 |
+
if line:
|
| 90 |
+
try:
|
| 91 |
+
line = line.decode('utf-8')
|
| 92 |
+
if line.startswith("data: "):
|
| 93 |
+
json_str = line[6:] # 去掉 "data: " 前缀
|
| 94 |
+
if json_str.strip() == "[DONE]":
|
| 95 |
+
yield f"data: [DONE]\n\n"
|
| 96 |
+
else:
|
| 97 |
+
data = json.loads(json_str)
|
| 98 |
+
if 'choices' in data and len(data['choices']) > 0:
|
| 99 |
+
choice = data['choices'][0]
|
| 100 |
+
if 'delta' in choice:
|
| 101 |
+
delta = choice['delta']
|
| 102 |
+
if 'role' in delta:
|
| 103 |
+
yield f"data: {json.dumps({'choices': [{'delta': {'role': 'assistant'}}]})}\n\n"
|
| 104 |
+
elif 'content' in delta:
|
| 105 |
+
yield f"data: {json.dumps({'choices': [{'delta': {'content': delta['content']}}]})}\n\n"
|
| 106 |
+
except json.JSONDecodeError:
|
| 107 |
+
continue
|
| 108 |
+
|
| 109 |
+
return Response(generate(), content_type='text/event-stream')
|
| 110 |
+
else:
|
| 111 |
+
response = requests.post(f"{CHATANYWHERE_ENDPOINT}/v1/chat/completions", headers=headers, json=payload)
|
| 112 |
+
if response.status_code == 200:
|
| 113 |
+
return Response(response.content, mimetype='application/json')
|
| 114 |
+
else:
|
| 115 |
+
return Response(json.dumps({"error": f"Error: {response.status_code} - {response.text}"}),
|
| 116 |
+
status=response.status_code, mimetype='application/json')
|
| 117 |
|
| 118 |
@app.route('/key_info', methods=['GET'])
|
| 119 |
def key_info():
|