This project provides a FastAPI wrapper, FastAgentAPI, that acts as an OpenAI-compatible endpoint (/v1/chat/completions). It leverages LangGraph to manage complex conversational flows or agentic behaviors with a backend Large Language Model (LLM). Key features include:
- LangGraph Integration: Orchestrates calls to the backend LLM, including state management, conditional logic, and retries. Suitable for building robust LLM agents.
- Structured JSON Output: Supports OpenAI's
response_format={ "type": "json_object" }including schema validation. - Tool Call Handling: Can interpret and format tool call requests/responses in text mode.
- Think Tag Stripping: Automatically removes
<think>...</think>tags from LLM responses when generating structured JSON. - Robust Logging: Implements Python's standard
loggingmodule, outputting to both console (fordocker logs) and a configurable log file. - Dockerized: Includes a Dockerfile for easy building and deployment, allowing multiple instances with different configurations.
- Configurable: Uses environment variables for essential settings like backend URL, API keys, logging paths, and verbosity.
- OpenAI Compatible
/v1/chat/completionsendpoint. - Handles
response_format: json_objectwith schema validation. - Handles standard text generation and OpenAI-like tool calls.
- Configurable retries for backend LLM calls.
- Detailed logging to console and file.
- Optional graph visualization output (
flow.png). - Ready for containerization with Docker.
- Python 3.11+
- Pip (Python package installer)
- Access to a backend LLM API service.
- (Optional) Graphviz system library (
sudo apt-get install graphvizorbrew install graphviz) if you wantpygraphvizto generate theflow.pnggraph image locally.
- Docker Engine installed and running.
-
Clone the repository:
git clone https://github.com/Teachings/FastAgentAPI.git cd FastAgentAPI -
Create and activate a virtual environment:
- Using Conda:
conda create -n fastagentapi python=3.11 -y conda activate fastagentapi
- Using venv:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Using Conda:
-
Install dependencies:
pip install -r requirements.txt
(Note:
pygraphvizinstallation might require the Graphviz system library first. See Optional Visualization section). -
Set Environment Variables: Create a
.envfile in the project root or export the following variables in your terminal. Ensure you have added.envto your.gitignorefile.# Example .env file BACKEND_API_URL="<your_backend_llm_api_url>/v1" # e.g., http://192.168.1.11:8001/v1 BACKEND_API_KEY="<your_backend_api_key>" # Optional, if your backend needs it LOCAL_PORT="8002" # Port for local Uvicorn server LOG_FILE_PATH="./logs/app_local.log" # Path for the local log file VERBOSE_LOGGING="true" # Set to "false" for less detailed logs # DEFAULT_MAX_RETRIES="1" # Optional: Default retry attempts
-
Run the application locally: The
--reloadflag enables auto-reloading during development. Do not use it in production. The port defaults to8002here based on the example env varLOCAL_PORT.# Ensure LOCAL_PORT is set in your environment or .env file uvicorn main_langgraph_enhanced:app --host 0.0.0.0 --port ${LOCAL_PORT:-8002} --reload
The server will be available at
http://localhost:8002(or the specifiedLOCAL_PORT).
This is the recommended way to run the application in production or testing environments. The container runs the application on port 8000 internally (hardcoded in the Dockerfile CMD).
-
Build the Docker Image: Ensure you are in the
FastAgentAPIdirectory containing theDockerfile.docker build -t fastagentapi .(You can also tag it more specifically, e.g.,
teachings/fastagentapi:latest). -
Environment Variables for Runtime: The Docker image uses environment variables for configuration. You can override the defaults set in the Dockerfile using the
-eflag withdocker run. Key variables include:Variable Default in Dockerfile Description BACKEND_API_URLhttp://localhost:8001/v1Required override: The full URL (including /v1) of your backend LLM.BACKEND_API_KEYsk-1111Optional API key for the backend LLM. Set to ""if not needed.LOG_FILE_PATH/app/logs/app.logPath inside the container where the log file will be written. VERBOSE_LOGGINGtrueSet to falseto reduce console log verbosity (detailed logs still go to file).DEFAULT_MAX_RETRIES1Default number of retries for backend calls if request doesn't specify. PORT(N/A) The internal container port is fixed at 8000 in the Dockerfile CMD. -
Running Containers: You can run multiple instances of the application from the same image, each configured differently. Remember the container always listens on port
8000.-
Create Host Directories for Logs (Recommended): This allows you to easily access log files from your host machine.
mkdir -p host_logs/instance1 mkdir -p host_logs/instance2
-
Example: Run Instance 1 (Host Port 8002): This maps host port
8002to the fixed container port8000, sets a specific backend URL, and configures a unique log file path mapped to the host.docker run -d --rm --name fastagentapi-instance1 \ -p 8002:8000 \ -e BACKEND_API_URL="http://192.168.1.11:8001/v1" \ -e LOG_FILE_PATH="/app/logs/instance1.log" \ -e BACKEND_API_KEY="<backend1_key_if_needed>" \ -e VERBOSE_LOGGING="true" \ -v $(pwd)/host_logs/instance1:/app/logs \ fastagentapi
(On Windows Command Prompt, use
"%cd%/host_logs/instance1"instead of$(pwd)/host_logs/instance1for the volume path). -
Example: Run Instance 2 (Host Port 8003): This maps host port
8003to the fixed container port8000, uses a different backend URL, and logs to a separate file, also mapped to the host.docker run -d --rm --name fastagentapi-instance2 \ -p 10003:8000 \ -e BACKEND_API_URL="http://192.168.1.11:10002/v1" \ -e LOG_FILE_PATH="/app/logs/instance2.log" \ -e BACKEND_API_KEY="<backend2_key_if_needed>" \ -e VERBOSE_LOGGING="true" \ -v $(pwd)/host_logs/instance2:/app/logs \ fastagentapi
(On Windows Command Prompt, use
"%cd%/host_logs/instance2"instead of$(pwd)/host_logs/instance2for the volume path).
Explanation of
docker runflags:-d: Run container in detached mode (background).--name: Assign a human-readable name to the container.-p <host_port>:<container_port>: Map a port on the host to a port inside the container (container port is fixed at8000).-e <VAR_NAME>="<value>": Set an environment variable inside the container.-v <host_path>:<container_path>: Mount a directory from the host into the container. This makes files written to<container_path>(like logs) appear in<host_path>.
-
-
Accessing Logs:
-
Console Logs: View the logs streamed by the application (INFO/DEBUG level based on
VERBOSE_LOGGING):docker logs fastagentapi-instance1 docker logs fastagentapi-instance2 # Add -f to follow logs in real-time docker logs -f fastagentapi-instance1 -
Log Files (via Volume Mount): Access the detailed log files directly on your host machine in the directories you created:
- Instance 1:
host_logs/instance1/instance1.log - Instance 2:
host_logs/instance2/instance2.log
- Instance 1:
-
POST /v1/chat/completions: The main OpenAI-compatible endpoint. Accepts standard OpenAI chat completion request bodies, includingmessages,model,tools,response_format, etc. Handled by the LangGraph workflow.GET /health: A simple health check endpoint. Returns{"status": "ok"}./{path:path}: (Catch-all) Forwards any other requests directly to theBACKEND_API_URLbase.
The application is configured primarily through environment variables:
| Variable | Purpose | Default (Dockerfile) | Runtime Override Example |
|---|---|---|---|
BACKEND_API_URL |
Backend LLM endpoint (full URL) | http://localhost:8001/v1 |
-e BACKEND_API_URL="<url>" |
BACKEND_API_KEY |
API Key for backend | sk-1111 |
-e BACKEND_API_KEY="<key>" |
LOG_FILE_PATH |
Internal path for log file | /app/logs/app.log |
-e LOG_FILE_PATH="/app/logs/custom.log" |
VERBOSE_LOGGING |
Enable DEBUG level logging to console (true or false) |
true |
-e VERBOSE_LOGGING="false" |
DEFAULT_MAX_RETRIES |
Default backend call retry attempts | 1 |
-e DEFAULT_MAX_RETRIES=3 |
PORT |
Internal container port is fixed at 8000 in the Docker CMD |
(Fixed: 8000) | (Not configurable via -e PORT=...) |
The application includes code to generate a visual representation of the LangGraph workflow, saving it as flow.png in the application's root directory upon startup. Generating this graph requires the pygraphviz Python library and the underlying graphviz system dependency.
This is not installed in the Docker container by default. If you wish to generate the graph, you should install these dependencies locally and run the Python script directly:
1. Install Graphviz System Dependency:
- macOS (using Homebrew):
brew install graphviz
- Debian/Ubuntu Linux (using apt):
sudo apt-get update sudo apt-get install graphviz libgraphviz-dev -y
- Fedora/CentOS/RHEL Linux (using dnf/yum):
sudo dnf install graphviz graphviz-devel -y # or using yum: # sudo yum install graphviz graphviz-devel -y
- Windows:
- Download an installer from the official Graphviz download page: https://graphviz.org/download/
- Ensure the Graphviz
bindirectory is added to your system's PATH environment variable during or after installation.
2. Install pygraphviz:
- Activate your Python virtual environment (if you created one for local development):
# e.g., conda activate fastagentapi or source venv/bin/activate - Install using pip:
(Note: This might take some time as it often compiles parts of the library).
pip install pygraphviz
3. Run the Application Locally:
- Ensure your necessary environment variables (
BACKEND_API_URL,LOCAL_PORTetc.) are set locally (e.g., using a.envfile andpython-dotenv). - Run the script directly:
# This will trigger graph generation if pygraphviz is installed python main_langgraph_enhanced.py- Alternatively, run with uvicorn (graph generates on import/startup):
uvicorn main_langgraph_enhanced:app --port ${LOCAL_PORT:-8002} --reload
- Alternatively, run with uvicorn (graph generates on import/startup):
- The
flow.pngfile should now be generated in your project's root directory.