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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, Response, stream_with_context
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import uuid
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
MODELS = {
|
| 11 |
+
'claude-3-5-haiku': 'bedrock-anthropic.claude-3-5-haiku',
|
| 12 |
+
'claude-3-5-sonnet': 'bedrock-anthropic.claude-3-5-sonnet-v2-0',
|
| 13 |
+
'nova-lite-v1-0': 'bedrock-amazon.nova-lite-v1-0',
|
| 14 |
+
'nova-pro-v1-0': 'bedrock-amazon.nova-pro-v1-0',
|
| 15 |
+
'llama3-1-7b': 'bedrock-meta.llama3-1-8b-instruct-v1',
|
| 16 |
+
'llama3-1-70b': 'bedrock-meta.llama3-1-70b-instruct-v1',
|
| 17 |
+
'mistral-small': 'bedrock-mistral.mistral-small-2402-v1-0',
|
| 18 |
+
'mistral-large': 'bedrock-mistral.mistral-large-2407-v1-0'
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def validate_key(key):
|
| 22 |
+
try:
|
| 23 |
+
parts = key.split('|||', 3)
|
| 24 |
+
if len(parts) != 3:
|
| 25 |
+
return None, None, None
|
| 26 |
+
return parts[0], parts[2], parts[1]
|
| 27 |
+
except:
|
| 28 |
+
return None, None, None
|
| 29 |
+
|
| 30 |
+
def create_partyrock_request(openai_req, app_id):
|
| 31 |
+
return {
|
| 32 |
+
"messages": [
|
| 33 |
+
{
|
| 34 |
+
"role": msg["role"],
|
| 35 |
+
"content": [{"text": msg["content"]}]
|
| 36 |
+
}
|
| 37 |
+
for msg in openai_req['messages']
|
| 38 |
+
],
|
| 39 |
+
"modelName": MODELS.get(openai_req.get('model', 'claude-3-5-haiku')),
|
| 40 |
+
"context": {"type": "chat-widget", "appId": app_id},
|
| 41 |
+
"options": {"temperature": 0},
|
| 42 |
+
"apiVersion": 3
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
@app.route('/', methods=['GET'])
|
| 46 |
+
def home():
|
| 47 |
+
return {"status": "PartyRock API Service Running", "port": 8803}
|
| 48 |
+
|
| 49 |
+
@app.route('/v1/chat/completions', methods=['POST'])
|
| 50 |
+
def chat():
|
| 51 |
+
try:
|
| 52 |
+
api_key = request.headers.get('Authorization', '').replace('Bearer ', '')
|
| 53 |
+
app_id, cookie, csrf_token = validate_key(api_key)
|
| 54 |
+
|
| 55 |
+
print(f"App ID: {app_id}")
|
| 56 |
+
print(f"Cookie length: {len(cookie) if cookie else 'None'}")
|
| 57 |
+
print(f"CSRF Token: {csrf_token}")
|
| 58 |
+
|
| 59 |
+
if not app_id or not cookie or not csrf_token:
|
| 60 |
+
return Response("Invalid API key format. Use: appId|||csrf_token|||cookies", status=401)
|
| 61 |
+
|
| 62 |
+
headers = {
|
| 63 |
+
'accept': 'text/event-stream',
|
| 64 |
+
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
| 65 |
+
'anti-csrftoken-a2z': csrf_token,
|
| 66 |
+
'content-type': 'application/json',
|
| 67 |
+
'origin': 'https://partyrock.aws',
|
| 68 |
+
'referer': f'https://partyrock.aws/u/chatyt/{app_id}',
|
| 69 |
+
'cookie': cookie,
|
| 70 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
response = requests.post(
|
| 74 |
+
'https://partyrock.aws/stream/getCompletion',
|
| 75 |
+
headers=headers,
|
| 76 |
+
json=create_partyrock_request(request.json, app_id),
|
| 77 |
+
stream=True
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if response.status_code != 200:
|
| 81 |
+
return Response(f"PartyRock API error: {response.text}", status=response.status_code)
|
| 82 |
+
|
| 83 |
+
if not request.json.get('stream', False):
|
| 84 |
+
try:
|
| 85 |
+
full_content = ""
|
| 86 |
+
buffer = ""
|
| 87 |
+
for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
|
| 88 |
+
if chunk:
|
| 89 |
+
buffer += chunk
|
| 90 |
+
while '\n' in buffer:
|
| 91 |
+
line, buffer = buffer.split('\n', 1)
|
| 92 |
+
if line.startswith('data: '):
|
| 93 |
+
try:
|
| 94 |
+
data = json.loads(line[6:])
|
| 95 |
+
if data["type"] == "text":
|
| 96 |
+
content = data["text"]
|
| 97 |
+
if isinstance(content, str):
|
| 98 |
+
content = content.encode('latin1').decode('utf-8')
|
| 99 |
+
if content and content != " ":
|
| 100 |
+
full_content += content
|
| 101 |
+
except:
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
return {
|
| 105 |
+
"id": str(uuid.uuid4()),
|
| 106 |
+
"object": "chat.completion",
|
| 107 |
+
"created": int(time.time()),
|
| 108 |
+
"model": request.json.get('model', 'claude-3-haiku'),
|
| 109 |
+
"choices": [{
|
| 110 |
+
"message": {
|
| 111 |
+
"role": "assistant",
|
| 112 |
+
"content": full_content
|
| 113 |
+
},
|
| 114 |
+
"finish_reason": "stop",
|
| 115 |
+
"index": 0
|
| 116 |
+
}]
|
| 117 |
+
}
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error processing response: {str(e)}")
|
| 120 |
+
return Response("Failed to process response", status=500)
|
| 121 |
+
|
| 122 |
+
def generate():
|
| 123 |
+
try:
|
| 124 |
+
buffer = ""
|
| 125 |
+
for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
|
| 126 |
+
if chunk:
|
| 127 |
+
buffer += chunk
|
| 128 |
+
while '\n' in buffer:
|
| 129 |
+
line, buffer = buffer.split('\n', 1)
|
| 130 |
+
if line.startswith('data: '):
|
| 131 |
+
try:
|
| 132 |
+
data = json.loads(line[6:])
|
| 133 |
+
if data["type"] == "text":
|
| 134 |
+
content = data["text"]
|
| 135 |
+
if isinstance(content, str):
|
| 136 |
+
content = content.encode('latin1').decode('utf-8')
|
| 137 |
+
chunk_resp = {
|
| 138 |
+
"id": str(uuid.uuid4()),
|
| 139 |
+
"object": "chat.completion.chunk",
|
| 140 |
+
"created": int(time.time()),
|
| 141 |
+
"model": request.json.get('model', 'claude-3-haiku'),
|
| 142 |
+
"choices": [{
|
| 143 |
+
"delta": {"content": content},
|
| 144 |
+
"index": 0,
|
| 145 |
+
"finish_reason": None
|
| 146 |
+
}]
|
| 147 |
+
}
|
| 148 |
+
yield f"data: {json.dumps(chunk_resp, ensure_ascii=False)}\n\n"
|
| 149 |
+
except:
|
| 150 |
+
continue
|
| 151 |
+
|
| 152 |
+
yield f"data: {json.dumps({'choices':[{'delta':{'content':''},'index':0,'finish_reason':'stop'}]})}\n\n"
|
| 153 |
+
yield "data: [DONE]\n\n"
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"Error in generate: {str(e)}")
|
| 156 |
+
return
|
| 157 |
+
|
| 158 |
+
return Response(
|
| 159 |
+
stream_with_context(generate()),
|
| 160 |
+
content_type='text/event-stream'
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"Error: {str(e)}")
|
| 165 |
+
return Response(f"Internal server error: {str(e)}", status=500)
|
| 166 |
+
|
| 167 |
+
if __name__ == '__main__':
|
| 168 |
+
app.run(host='0.0.0.0', port=8803, debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
requests
|