A modern FastAPI application that orchestrates calls between TorchServe (NER) and Mistral (Chat) to create a comprehensive NLP platform.
- π― Overview
- β¨ Features
- ποΈ Architecture
- π§ Installation
- π Getting Started
- π Usage
- π Endpoints
- π§ͺ Testing
- π Documentation
- π€ Contributing
This API Gateway combines the power of:
- TorchServe for Named Entity Recognition (NER)
- Mistral/vLLM for intelligent conversations
- FastAPI for a modern and performant interface
- π NER (Named Entity Recognition) - Extract entities from text
- π¬ Intelligent Chat - Conversations powered by Mistral
- π CORS Configured - Ready for web applications
- π Health Check - API status monitoring
- π Automatic Documentation - Built-in Swagger interface
- β‘ Optimized Performance - Asynchronous architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Web Client ββββββΆβ NLP Gateway ββββββΆβ TorchServe β
β β β (FastAPI) β β (NER) β
βββββββββββββββββββ β β βββββββββββββββββββ
β β
β β βββββββββββββββββββ
β ββββββΆβ Mistral/vLLM β
βββββββββββββββββββ β (Chat) β
βββββββββββββββββββ
- Python 3.8+
- FastAPI
- TorchServe configured
- Mistral/vLLM running
# Clone the repository
git clone <your-repo>
cd nlp-platform
# Install dependencies
pip install -r requirements.txtCreate a .env file for your environment variables:
# TorchServe
TORCHSERVE_URL=http://localhost:8080
TORCHSERVE_MODEL=ner_model
# Mistral/vLLM
MISTRAL_URL=http://localhost:8001
MISTRAL_MODEL=mistral-7b-instruct
# API Configuration
API_HOST=0.0.0.0
API_PORT=8000# Start the API in development mode
uvicorn main:app --reload --host 0.0.0.0 --port 8000# Start with Gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000# Build the image
docker build -t nlp-gateway .
# Run the container
docker run -p 8000:8000 nlp-gateway-
Check if the API is running:
curl http://localhost:8000/health
-
Test the chat:
curl -X POST http://localhost:8000/nlp/chat \ -H "Content-Type: application/json" \ -d '{"prompt": "Hello, how are you?"}'
-
Test NER:
curl -X POST http://localhost:8000/nlp/ner \ -H "Content-Type: application/json" \ -d '{"text": "Emmanuel Macron lives in Paris."}'
| Method | Endpoint | Description | Example |
|---|---|---|---|
GET |
/health |
Check API status | 200 {"status": "ok"} |
POST |
/nlp/chat |
Chat with Mistral | See below |
POST |
/nlp/ner |
Named Entity Recognition | See below |
Request:
{
"prompt": "Explain artificial intelligence to me",
"max_tokens": 150,
"temperature": 0.7
}Response:
{
"response": "Artificial intelligence is...",
"tokens_used": 142,
"model": "mistral-7b-instruct"
}Request:
{
"text": "Apple Inc. is based in Cupertino, California.",
"language": "en"
}Response:
{
"entities": [
{
"text": "Apple Inc.",
"label": "ORG",
"start": 0,
"end": 10,
"confidence": 0.99
},
{
"text": "Cupertino",
"label": "LOC",
"start": 23,
"end": 32,
"confidence": 0.95
}
]
}# Run unit tests
pytest tests/
# Tests with coverage
pytest --cov=app tests/
# Integration tests
pytest tests/integration/def test_health_endpoint():
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_chat_endpoint():
response = client.post("/nlp/chat", json={"prompt": "Hello"})
assert response.status_code == 200
assert "response" in response.json()- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - OpenAPI JSON:
http://localhost:8000/openapi.json
TODO
- Formatting: Black
- Linting: Flake8
- Type hints: mypy
- Imports: isort
# Format code
black app/ tests/
# Check linting
flake8 app/ tests/
# Check types
mypy app/-
"Not Found" on
/chat:- Use
/nlp/chatinstead of/chat - Check that the router is properly included with the prefix
- Use
-
TorchServe connection error:
- Verify TorchServe is running on the configured port
- Test connection:
curl http://localhost:8080/ping
-
Mistral timeout:
- Increase timeout in configuration
- Check that vLLM is started and accessible
# Enable detailed logs
export LOG_LEVEL=DEBUG
uvicorn main:app --log-level debugContributions are welcome! Here's how to get started:
- Fork the project
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Add tests for new features
- Follow code standards
- Update documentation if necessary
This project is licensed under the MIT License. See the LICENSE file for details.
LPMarcoB - @marcopalomo
- FastAPI for the web framework
- TorchServe for PyTorch model serving
- Mistral AI for language models
- The open source community for all the tools used
β Don't forget to star this project if it helped you!