Update api/utils.py
Browse files1234567891011 unmodified lines2223242526272829303121 unmodified lines5354555657585926 unmodified lines8687888990919293949596979899100101110 unmodified lines212213214215216217218219220221222223224225226227from datetime import datetimeimport jsonimport uuidimport asyncioimport randomimport stringimport refrom typing import Any, Dict, Optionalimport httpxfrom fastapi import HTTPException11 unmodified linesfrom api.logger import setup_loggerfrom api.validate import getHid # Import the asynchronous getHid function# Setup loggerlogger = setup_logger(__name__)# Define the blocked messageBLOCKED_MESSAGE = "Generated by BLACKBOX.AI, try unlimited chat https://www.blackbox.ai"# Helper function to create chat completion data21 unmodified linesif model_prefix:content = f"{model_prefix} {content}"if isinstance(message.content, list) and len(message.content) == 2 and "image_url" in message.content[1]:# Ensure base64 images are always included for all modelsimage_base64 = message.content[1]["image_url"]["url"]return {"role": message.role,26 unmodified linescontent = re.sub(r'\$~~~\$.*?\$~~~\$', '', content, flags=re.DOTALL)return content# Function to remove search results but keep the links intactdef remove_search_results(content: str) -> str:"""Remove search result sections, keeping the URLs intact."""# Regex to match search result JSON (structured with `title`, `snippet`, `link`)search_result_pattern = re.compile(r'(\{.*?"link":\s*"https?://[^\}]+.*?\})', re.DOTALL)# Remove all search results but keep URLs intact by using a regexcleaned_content = re.sub(search_result_pattern, '', content)# Optional: clean up any unnecessary newlines or extra spaces left by the removalcleaned_content = cleaned_content.strip()return cleaned_content110 unmodified linestrending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})model_prefix = MODEL_PREFIXES.get(request.model, "")# Adjust headers_api_chat and headers_chat since referer_url is removedheaders_api_chat = get_headers_api_chat(BASE_URL)headers_chat = get_headers_chat(BASE_URL,next_action=str(uuid.uuid4()),next_router_state_tree=json.dumps([""]),)if request.model == 'o1-preview':delay_seconds = random.randint(20, 60)logger.info(f"Introducing a delay of {delay_seconds} seconds for model 'o1-preview' "f"(Request ID: {request_id})"1234567891011 unmodified lines222324252627282921 unmodified lines51525354555626 unmodified lines83848586878889909192939495969798110 unmodified lines209210211212213214215216217218import refrom datetime import datetimeimport jsonimport uuidimport asyncioimport randomimport stringfrom typing import Any, Dict, Optionalimport httpxfrom fastapi import HTTPException11 unmodified linesfrom api.logger import setup_loggerfrom api.validate import getHid # Import the asynchronous getHid functionlogger = setup_logger(__name__)BLOCKED_MESSAGE = "Generated by BLACKBOX.AI, try unlimited chat https://www.blackbox.ai"# Helper function to create chat completion data21 unmodified linesif model_prefix:content = f"{model_prefix} {content}"if isinstance(message.content, list) and len(message.content) == 2 and "image_url" in message.content[1]:image_base64 = message.content[1]["image_url"]["url"]return {"role": message.role,26 unmodified linescontent = re.sub(r'\$~~~\$.*?\$~~~\$', '', content, flags=re.DOTALL)return content# Improved function to remove search results but keep the links intactdef remove_search_results(content: str) -> str:"""Remove search result sections, keeping the URLs intact."""# Regex pattern to match search result JSON structures (looking for "link" and capturing it)search_result_pattern = r'\$~~~\$.*?"link":\s*"(https?://[^\"]+).*?\$~~~\$'# Remove content inside `$~~~$` tags that contains search result data, keeping only the URLscleaned_content = re.sub(search_result_pattern, '', content)# Remove unnecessary newlines or extra spaces left by the removalcleaned_content = cleaned_content.strip()return cleaned_content110 unmodified linestrending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})model_prefix = MODEL_PREFIXES.get(request.model, "")headers_api_chat = get_headers_api_chat(BASE_URL)if request.model == 'o1-preview':delay_seconds = random.randint(1, 60)logger.info(f"Introducing a delay of {delay_seconds} seconds for model 'o1-preview' "f"(Request ID: {request_id})"
api/utils.py+7-16