Niansuh commited on
Commit
d25e8ce
·
verified ·
1 Parent(s): bd6345a

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +15 -2
api/utils.py CHANGED
@@ -1,5 +1,6 @@
1
- # main.py or your main application file
2
 
 
3
  from datetime import datetime
4
  import json
5
  import uuid
@@ -29,6 +30,9 @@ logger = setup_logger(__name__)
29
  # Define the blocked message
30
  BLOCKED_MESSAGE = "Generated by BLACKBOX.AI, try unlimited chat https://www.blackbox.ai"
31
 
 
 
 
32
  # Helper function to create chat completion data
33
  def create_chat_completion_data(
34
  content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
@@ -147,7 +151,12 @@ async def process_streaming_response(request: ChatRequest):
147
  cleaned_content = strip_model_prefix(content, model_prefix)
148
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
149
 
150
- yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
 
 
 
 
 
151
  yield "data: [DONE]\n\n"
152
  except httpx.HTTPStatusError as e:
153
  logger.error(f"HTTP error occurred for Request ID {request_id}: {e}")
@@ -233,6 +242,10 @@ async def process_non_streaming_response(request: ChatRequest):
233
 
234
  cleaned_full_response = strip_model_prefix(full_response, model_prefix)
235
 
 
 
 
 
236
  return {
237
  "id": f"chatcmpl-{uuid.uuid4()}",
238
  "object": "chat.completion",
 
1
+ # utils.py or your main application file
2
 
3
+ import os # Import os to access environment variables
4
  from datetime import datetime
5
  import json
6
  import uuid
 
30
  # Define the blocked message
31
  BLOCKED_MESSAGE = "Generated by BLACKBOX.AI, try unlimited chat https://www.blackbox.ai"
32
 
33
+ # Get the custom message from the environment variable
34
+ FREE_USER_MESSAGE = os.getenv('FREE_USER_MESSAGE', '')
35
+
36
  # Helper function to create chat completion data
37
  def create_chat_completion_data(
38
  content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
 
151
  cleaned_content = strip_model_prefix(content, model_prefix)
152
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
153
 
154
+ # Append the custom message for free users
155
+ if request.is_free_user and FREE_USER_MESSAGE:
156
+ timestamp = int(datetime.now().timestamp())
157
+ yield f"data: {json.dumps(create_chat_completion_data(FREE_USER_MESSAGE, request.model, timestamp, 'stop'))}\n\n"
158
+ else:
159
+ yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
160
  yield "data: [DONE]\n\n"
161
  except httpx.HTTPStatusError as e:
162
  logger.error(f"HTTP error occurred for Request ID {request_id}: {e}")
 
242
 
243
  cleaned_full_response = strip_model_prefix(full_response, model_prefix)
244
 
245
+ # Append the custom message for free users
246
+ if request.is_free_user and FREE_USER_MESSAGE:
247
+ cleaned_full_response += f"\n\n{FREE_USER_MESSAGE}"
248
+
249
  return {
250
  "id": f"chatcmpl-{uuid.uuid4()}",
251
  "object": "chat.completion",