A modern, modular FastAPI template for building chat completion APIs using LangChain and Anthropic's Claude.
- FastAPI-based REST API
- LangChain integration with Anthropic's Claude
- Modular service architecture
- Async support
- Conversation history management
- Environment-based configuration
- Type hints and validation
- Python 3.8+
- Anthropic API key
- Clone the repository:
git clone https://github.com/yourusername/fastAPI-chat-completion-template.git
cd fastAPI-chat-completion-template- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
cp .env.EXAMPLE .env
# Edit .env with your Anthropic API key.
├── api/
│ └── routes/
│ └── chat_routes.py # API endpoints
├── models/
│ └── chat.py # Pydantic models
├── services/
│ ├── base.py # Base service class
│ └── chat/
│ ├── base.py # Base chat service
│ └── anthropic.py # Anthropic implementation
├── main.py # Application entry point
├── requirements.txt # Project dependencies
└── .env # Environment variables
- Start the server:
python -m uvicorn main:app --reload- Access the API documentation:
- Swagger UI: http://localhost:8001/docs
- ReDoc: http://localhost:8001/redoc
POST /api/chat/completions{ "message": { "content": "Your message here", "role": "user" // optional, defaults to "user" } }
GET /api/chat/history- Get conversation historyDELETE /api/chat/history- Clear conversation history
- Create a new service class in
services/chat/ - Inherit from
BaseChatService - Implement the
get_chat_responsemethod - Update the service factory in
api/routes/chat_routes.py
pytest- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI
- LangChain
- Anthropic