This documentation provides an overview of the code used to implement a RESTful API server for the pipeline using
the SuperagentX library and FastAPI. The server processes incoming requests, authenticates users via API tokens,
and handles search queries related to the trip planner.
The code sets up a FastAPI server that processes search requests using the AgentXPipe class from SuperagentX.
It incorporates a custom authentication mechanism to validate client requests via an API token. The API exposes
an endpoint that allows users to submit a query and receive results from the trip planner pipeline.
The lifespan function initializes the trip planner pipeline when the FastAPI application starts and clears
it when the app shuts down. The pipes dictionary is used to store the pipeline for later use in request handling.
A custom dependency function, verify_api_token, checks the validity of the API token passed in the api-token header.
If the token is invalid, the function raises an HTTP 401 Unauthorized error.
Copy
async def verify_api_token( api_token: str = Depends(APIKeyHeader(name='api-token', auto_error=False))): if api_token != AUTH_TOKEN: raise HTTPException(status_code=401, detail='Invalid API Token!')
The main API application (ecom_app) is created using FastAPI. The /search endpoint processes search queries
related to the trip planner. The verify_api_token function is included as a dependency to authenticate requests before processing them.
This RESTful API provides a simple yet powerful interface for interacting with the trip planner pipeline, ensuring
secure access via API token validation.
(venv) ➜ superagentx_examples git:(dev) ✗ fastapi dev trip_planner/trip_planner/restpipe.pyINFO Using path trip_planner/trip_planner/restpipe.pyINFO Resolved absolute path /home/john/Projects/superagentX-examples/superagentx_examples/trip_planner/trip_planner/restpipe.pyINFO Searching for package john structure from directories with __init__.py filesINFO Importing from /home/john/Projects/superagentX-examples/superagentx_examples/trip_planner ╭─ Python package file structure ─╮ │ │ │ 📁 trip_planner │ │ ├── 🐍 __init__.py │ │ └── 🐍 restpipe.py │ │ │ ╰─────────────────────────────────╯INFO Importing module trip_planner.restpipeWarning: Synchronous WebCrawler is not available. Install crawl4ai[sync] for synchronous support. However, please note that the synchronous version will be deprecated soon.INFO Found importable FastAPI app ╭─────────── Importable FastAPI app ───────────╮ │ │ │ from trip_planner.restpipe import ecom_app │ │ │ ╰──────────────────────────────────────────────╯INFO Using import string trip_planner.restpipe:ecom_app ╭────────── FastAPI CLI - Development mode ───────────╮ │ │ │ Serving at: http://127.0.0.1:8000 │ │ │ │ API docs: http://127.0.0.1:8000/docs │ │ │ │ Running in development mode, for production use: │ │ │ │ fastapi run │ │ │ ╰─────────────────────────────────────────────────────╯INFO: Will watch for changes in these directories: ['/home/john/Projects/superagentX-examples/superagentx_examples']INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)INFO: Started reloader process [8869] using WatchFilesWarning: Synchronous WebCrawler is not available. Install crawl4ai[sync] for synchronous support. However, please note that the synchronous version will be deprecated soon.INFO: Started server process [8882]INFO: Waiting for application startup.INFO:watchfiles.main:4 changes detectedINFO: Application startup complete.INFO:watchfiles.main:4 changes detected