Spaces:
Sleeping
Sleeping
Update 2api.py
Browse files
2api.py
CHANGED
|
@@ -335,54 +335,22 @@ def chat_completions():
|
|
| 335 |
data = request.get_json()
|
| 336 |
print("Received OpenAI request:", json.dumps(data, indent=2))
|
| 337 |
|
| 338 |
-
#
|
| 339 |
-
client_id = request.remote_addr
|
| 340 |
|
| 341 |
-
#
|
|
|
|
|
|
|
|
|
|
| 342 |
current_time = datetime.now()
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
print(f"New session created for client {client_id}: {new_session}")
|
| 352 |
-
else:
|
| 353 |
-
# Option 2: If session creation fails, switch account
|
| 354 |
-
print("Failed to create new session. Switching to next account.")
|
| 355 |
-
current_client = get_next_client()
|
| 356 |
-
if current_client.sign_in():
|
| 357 |
-
new_session = current_client.create_session()
|
| 358 |
-
if new_session:
|
| 359 |
-
CLIENT_SESSIONS[client_id] = {
|
| 360 |
-
"session_id": new_session,
|
| 361 |
-
"last_time": current_time,
|
| 362 |
-
"user_id": current_client.user_id,
|
| 363 |
-
"company_id": current_client.company_id,
|
| 364 |
-
"token": current_client.token
|
| 365 |
-
}
|
| 366 |
-
print(f"Switched account and created new session for client {client_id}: {new_session}")
|
| 367 |
-
else:
|
| 368 |
-
return {"error": "Failed to create session with new account"}, 500
|
| 369 |
-
else:
|
| 370 |
-
return {"error": "Failed to login with new account"}, 500
|
| 371 |
-
else:
|
| 372 |
-
# New client, use current session or create one
|
| 373 |
-
if not current_client.session_id:
|
| 374 |
-
if not current_client.sign_in() or not current_client.create_session():
|
| 375 |
-
return {"error": "Failed to initialize client session"}, 500
|
| 376 |
-
CLIENT_SESSIONS[client_id] = {
|
| 377 |
-
"session_id": current_client.session_id,
|
| 378 |
-
"last_time": current_time,
|
| 379 |
-
"user_id": current_client.user_id,
|
| 380 |
-
"company_id": current_client.company_id,
|
| 381 |
-
"token": current_client.token
|
| 382 |
-
}
|
| 383 |
-
|
| 384 |
-
# Update last interaction time
|
| 385 |
-
CLIENT_SESSIONS[client_id]["last_time"] = current_time
|
| 386 |
|
| 387 |
# Extract parameters from OpenAI request
|
| 388 |
messages = data.get('messages', [])
|
|
@@ -402,7 +370,7 @@ def chat_completions():
|
|
| 402 |
return {"error": "No user message found in request"}, 400
|
| 403 |
|
| 404 |
# Add explicit instruction to reply in Chinese and be direct
|
| 405 |
-
query =
|
| 406 |
print(f"Constructed Query for on-demand.io (relying on session_id for context, with Chinese instruction): {query}")
|
| 407 |
|
| 408 |
# Map the model ID to on-demand.io endpoint ID
|
|
|
|
| 335 |
data = request.get_json()
|
| 336 |
print("Received OpenAI request:", json.dumps(data, indent=2))
|
| 337 |
|
| 338 |
+
# 提取客户端 ID
|
| 339 |
+
client_id = request.remote_addr
|
| 340 |
|
| 341 |
+
# 强制创建新会话
|
| 342 |
+
new_session = current_client.create_session()
|
| 343 |
+
if not new_session:
|
| 344 |
+
return {"error": "Failed to create new session"}, 500
|
| 345 |
current_time = datetime.now()
|
| 346 |
+
CLIENT_SESSIONS[client_id] = {
|
| 347 |
+
"session_id": new_session,
|
| 348 |
+
"last_time": current_time,
|
| 349 |
+
"user_id": current_client.user_id,
|
| 350 |
+
"company_id": current_client.company_id,
|
| 351 |
+
"token": current_client.token
|
| 352 |
+
}
|
| 353 |
+
print(f"New session created for client {client_id}: {new_session}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
# Extract parameters from OpenAI request
|
| 356 |
messages = data.get('messages', [])
|
|
|
|
| 370 |
return {"error": "No user message found in request"}, 400
|
| 371 |
|
| 372 |
# Add explicit instruction to reply in Chinese and be direct
|
| 373 |
+
query = latest_user_query
|
| 374 |
print(f"Constructed Query for on-demand.io (relying on session_id for context, with Chinese instruction): {query}")
|
| 375 |
|
| 376 |
# Map the model ID to on-demand.io endpoint ID
|