Update api/utils.py
Browse files9 unmodified lines10111213141516171819202156 unmodified lines787980818283844 unmodified lines899091929394959697989910054 unmodified lines1551561571581591601615 unmodified lines1671681691701711721731741751761771781791801811821831841851861879 unmodified linesfrom fastapi import HTTPExceptionfrom api.config import (MODEL_MAPPING,BASE_URL,AGENT_MODE,TRENDING_AGENT_MODE,MODEL_PREFIXES,MODEL_REFERERS,common_headers)from api.models import ChatRequestfrom api.logger import setup_logger56 unmodified lineslogger.debug("No prefix to strip from content.")return content# Process streaming response with combined headersasync def process_streaming_response(request: ChatRequest):chat_id = generate_chat_id()logger.info(f"Generated Chat ID: {chat_id}")4 unmodified linesreferer_path = MODEL_REFERERS.get(request.model, f"/?model={request.model}")referer_url = f"{BASE_URL}{referer_path}"# Combined headers for API chatheaders_api_chat = {**common_headers,'Content-Type': 'application/json','Referer': referer_url}# Introduce delay for 'o1-preview' modelif request.model == 'o1-preview':54 unmodified lineslogger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")raise HTTPException(status_code=500, detail=str(e))# Process non-streaming response with combined headersasync def process_non_streaming_response(request: ChatRequest):chat_id = generate_chat_id()logger.info(f"Generated Chat ID: {chat_id}")5 unmodified linesreferer_url = f"{BASE_URL}{referer_path}"chat_url = f"{BASE_URL}/chat/{chat_id}?model={request.model}"# Combined headers for API chat and chat requestheaders_api_chat = {**common_headers,'Content-Type': 'application/json','Referer': referer_url}headers_chat = {**common_headers,'Accept': 'text/x-component','Content-Type': 'text/plain;charset=UTF-8','Referer': chat_url,'next-action': str(uuid.uuid4()),'next-router-state-tree': json.dumps([""]),'next-url': '/'}# Introduce delay for 'o1-preview' modelif request.model == 'o1-preview':9 unmodified lines1011121314151617181920212256 unmodified lines798081828384854 unmodified lines909192939495969754 unmodified lines1521531541551561571585 unmodified lines1641651661671681691701711729 unmodified linesfrom fastapi import HTTPExceptionfrom api.config import (MODEL_MAPPING,get_headers_api_chat,get_headers_chat,BASE_URL,AGENT_MODE,TRENDING_AGENT_MODE,MODEL_PREFIXES,MODEL_REFERERS)from api.models import ChatRequestfrom api.logger import setup_logger56 unmodified lineslogger.debug("No prefix to strip from content.")return content# Process streaming response with headers from config.pyasync def process_streaming_response(request: ChatRequest):chat_id = generate_chat_id()logger.info(f"Generated Chat ID: {chat_id}")4 unmodified linesreferer_path = MODEL_REFERERS.get(request.model, f"/?model={request.model}")referer_url = f"{BASE_URL}{referer_path}"# Generate headers for API chat request with dynamic Refererheaders_api_chat = get_headers_api_chat(referer_url)# Introduce delay for 'o1-preview' modelif request.model == 'o1-preview':54 unmodified lineslogger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")raise HTTPException(status_code=500, detail=str(e))# Process non-streaming response with headers from config.pyasync def process_non_streaming_response(request: ChatRequest):chat_id = generate_chat_id()logger.info(f"Generated Chat ID: {chat_id}")5 unmodified linesreferer_url = f"{BASE_URL}{referer_path}"chat_url = f"{BASE_URL}/chat/{chat_id}?model={request.model}"# Generate headers for API chat request and chat request with dynamic Refererheaders_api_chat = get_headers_api_chat(referer_url)headers_chat = get_headers_chat(chat_url, next_action=str(uuid.uuid4()), next_router_state_tree=json.dumps([""]))# Introduce delay for 'o1-preview' modelif request.model == 'o1-preview':
api/utils.py+10-25