2023-05-21 02:20:55 +03:00
|
|
|
import os
|
2023-10-30 12:18:04 +03:00
|
|
|
|
2023-08-08 18:01:31 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# import needed here when running main.py to debug backend
|
|
|
|
# you will need to run pip install python-dotenv
|
2023-08-25 13:03:13 +03:00
|
|
|
from dotenv import load_dotenv # type: ignore
|
|
|
|
|
2023-08-08 18:01:31 +03:00
|
|
|
load_dotenv()
|
2024-04-10 14:28:22 +03:00
|
|
|
import logging
|
|
|
|
|
2024-04-07 04:35:57 +03:00
|
|
|
import litellm
|
2024-04-10 14:28:22 +03:00
|
|
|
import sentry_sdk
|
2024-04-28 16:10:21 +03:00
|
|
|
from fastapi import FastAPI, HTTPException, Request
|
|
|
|
from fastapi.responses import HTMLResponse, JSONResponse
|
2023-05-30 14:02:48 +03:00
|
|
|
from logger import get_logger
|
2023-06-04 00:12:42 +03:00
|
|
|
from middlewares.cors import add_cors_middleware
|
2024-04-10 14:28:22 +03:00
|
|
|
from modules.analytics.controller.analytics_routes import analytics_router
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.api_key.controller import api_key_router
|
2024-04-10 14:28:22 +03:00
|
|
|
from modules.assistant.controller import assistant_router
|
2023-12-06 10:40:18 +03:00
|
|
|
from modules.brain.controller import brain_router
|
2023-12-04 20:38:54 +03:00
|
|
|
from modules.chat.controller import chat_router
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.contact_support.controller import contact_router
|
|
|
|
from modules.knowledge.controller import knowledge_router
|
|
|
|
from modules.misc.controller import misc_router
|
|
|
|
from modules.onboarding.controller import onboarding_router
|
|
|
|
from modules.prompt.controller import prompt_router
|
|
|
|
from modules.upload.controller import upload_router
|
|
|
|
from modules.user.controller import user_router
|
2023-12-04 20:38:54 +03:00
|
|
|
from packages.utils import handle_request_validation_error
|
2024-04-07 04:35:57 +03:00
|
|
|
from packages.utils.telemetry import maybe_send_telemetry
|
2024-04-28 16:10:21 +03:00
|
|
|
from pyinstrument import Profiler
|
2023-06-11 00:59:16 +03:00
|
|
|
from routes.crawl_routes import crawl_router
|
2023-07-11 19:20:31 +03:00
|
|
|
from routes.subscription_routes import subscription_router
|
2023-11-27 12:08:00 +03:00
|
|
|
from sentry_sdk.integrations.fastapi import FastApiIntegration
|
2023-11-28 16:27:39 +03:00
|
|
|
from sentry_sdk.integrations.starlette import StarletteIntegration
|
2023-05-31 14:51:23 +03:00
|
|
|
|
2024-04-07 04:35:57 +03:00
|
|
|
# Set the logging level for all loggers to WARNING
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("LiteLLM").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("litellm").setLevel(logging.WARNING)
|
|
|
|
litellm.set_verbose = False
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-10-30 12:18:04 +03:00
|
|
|
|
2024-04-07 04:35:57 +03:00
|
|
|
logger = get_logger(__name__)
|
2023-10-09 16:23:13 +03:00
|
|
|
|
|
|
|
|
2024-02-24 06:32:32 +03:00
|
|
|
def before_send(event, hint):
|
|
|
|
# If this is a transaction event
|
|
|
|
if event["type"] == "transaction":
|
|
|
|
# And the transaction name contains 'healthz'
|
|
|
|
if "healthz" in event["transaction"]:
|
|
|
|
# Drop the event by returning None
|
|
|
|
return None
|
|
|
|
# For other events, return them as is
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
sentry_dsn = os.getenv("SENTRY_DSN")
|
|
|
|
if sentry_dsn:
|
2023-07-01 22:12:13 +03:00
|
|
|
sentry_sdk.init(
|
2023-07-14 22:02:26 +03:00
|
|
|
dsn=sentry_dsn,
|
2023-11-28 16:27:39 +03:00
|
|
|
sample_rate=0.1,
|
|
|
|
enable_tracing=True,
|
2024-02-23 01:48:45 +03:00
|
|
|
traces_sample_rate=0.1,
|
2023-11-28 16:27:39 +03:00
|
|
|
integrations=[
|
2024-02-24 06:32:32 +03:00
|
|
|
StarletteIntegration(transaction_style="url"),
|
|
|
|
FastApiIntegration(transaction_style="url"),
|
2023-11-28 16:27:39 +03:00
|
|
|
],
|
2024-02-24 06:32:32 +03:00
|
|
|
before_send=before_send,
|
2023-07-01 22:12:13 +03:00
|
|
|
)
|
|
|
|
|
2023-05-21 02:20:55 +03:00
|
|
|
app = FastAPI()
|
|
|
|
|
2023-06-04 00:12:42 +03:00
|
|
|
add_cors_middleware(app)
|
2023-06-20 22:53:04 +03:00
|
|
|
|
2023-06-20 17:17:13 +03:00
|
|
|
app.include_router(brain_router)
|
2023-06-11 00:59:16 +03:00
|
|
|
app.include_router(chat_router)
|
|
|
|
app.include_router(crawl_router)
|
2024-04-10 14:28:22 +03:00
|
|
|
app.include_router(assistant_router)
|
2023-10-05 12:31:26 +03:00
|
|
|
app.include_router(onboarding_router)
|
2023-06-11 00:59:16 +03:00
|
|
|
app.include_router(misc_router)
|
2024-04-10 12:20:21 +03:00
|
|
|
app.include_router(analytics_router)
|
2023-10-05 12:31:26 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
app.include_router(upload_router)
|
|
|
|
app.include_router(user_router)
|
2023-06-14 22:21:13 +03:00
|
|
|
app.include_router(api_key_router)
|
2023-07-11 19:20:31 +03:00
|
|
|
app.include_router(subscription_router)
|
2023-08-03 10:53:38 +03:00
|
|
|
app.include_router(prompt_router)
|
2023-09-20 10:35:37 +03:00
|
|
|
app.include_router(knowledge_router)
|
2023-10-23 18:58:45 +03:00
|
|
|
app.include_router(contact_router)
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2024-04-28 16:10:21 +03:00
|
|
|
PROFILING = os.getenv("PROFILING", "false").lower() == "true"
|
|
|
|
|
|
|
|
|
|
|
|
if PROFILING:
|
|
|
|
|
|
|
|
@app.middleware("http")
|
|
|
|
async def profile_request(request: Request, call_next):
|
|
|
|
profiling = request.query_params.get("profile", False)
|
|
|
|
if profiling:
|
|
|
|
profiler = Profiler()
|
|
|
|
profiler.start()
|
|
|
|
await call_next(request)
|
|
|
|
profiler.stop()
|
|
|
|
return HTMLResponse(profiler.output_html())
|
|
|
|
else:
|
|
|
|
return await call_next(request)
|
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
@app.exception_handler(HTTPException)
|
2023-06-23 11:36:55 +03:00
|
|
|
async def http_exception_handler(_, exc):
|
2023-06-22 18:50:06 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=exc.status_code,
|
|
|
|
content={"detail": exc.detail},
|
|
|
|
)
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
handle_request_validation_error(app)
|
2023-08-08 18:01:31 +03:00
|
|
|
|
2024-02-08 07:44:37 +03:00
|
|
|
if os.getenv("TELEMETRY_ENABLED") == "true":
|
|
|
|
logger.info("Telemetry enabled, we use telemetry to collect anonymous usage data.")
|
|
|
|
logger.info(
|
|
|
|
"To disable telemetry, set the TELEMETRY_ENABLED environment variable to false."
|
|
|
|
)
|
2024-04-07 04:35:57 +03:00
|
|
|
maybe_send_telemetry("booting", {"status": "ok"})
|
2024-04-25 17:22:13 +03:00
|
|
|
maybe_send_telemetry("ping", {"ping": "pong"})
|
2024-02-08 07:44:37 +03:00
|
|
|
|
|
|
|
|
2023-08-08 18:01:31 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# run main.py to debug backend
|
|
|
|
import uvicorn
|
|
|
|
|
2024-04-07 04:35:57 +03:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=5050, log_level="warning", access_log=False)
|