This project is a question-answering system built with FastAPI, PostgreSQL with pgvector, pgAdmin, and Docker. It allows users to store questions, retrieve similar questions using vector embeddings, generate follow-up questions with an LLM, and send questions/answers via SMS using the TextBelt API.
- FastAPI: Backend API with endpoints for storing questions, retrieving answers, querying LLM, and sending SMS.
- PostgreSQL with pgvector: Vector search for similar questions.
- pgAdmin: Database administration through a web interface.
- Docker: Easy containerization and deployment.
- TextBelt API: SMS integration for interactive Q&A.
- MQTT Integration: Connect IoT devices to receive and answer questions.
- OpenAI API (Optional): Enhanced question generation with conversation history.
- Docker and Docker Compose installed
- Python 3.10+ (if running locally without Docker)
git clone https://github.com/JoeLorenzoMontano/question-answering-system.git
cd question-answering-system
Create a .env
file in the root directory and set your environment variables:
# PostgreSQL
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=ollama_db
# PGAdmin
PGADMIN_DEFAULT_EMAIL=admin@example.com
PGADMIN_DEFAULT_PASSWORD=adminpassword
# Backend
DATABASE_URL=postgresql://user:password@postgres:5432/ollama_db
OPEN_WEBUI_API_URL=http://your-server-ip:3001
OPEN_WEBUI_API_KEY=your-open-webui-api-key
TEXTBELT_API_KEY=your-textbelt-api-key
# MQTT Broker
MQTT_BROKER_HOST=mqtt-broker
MQTT_BROKER_PORT=1883
# Optional: OpenAI Integration
USE_OPENAI=false
OPENAI_API_KEY=your-openai-api-key
docker-compose up --build
- The FastAPI backend will be accessible at:
http://localhost:8001
- pgAdmin will be accessible at:
http://localhost:5050
- Login:
admin@example.com
- Password:
adminpassword
- Login:
- MQTT Broker will be accessible at:
localhost:1883
(MQTT) andlocalhost:9001
(WebSockets)
.
βββ backend/
β βββ main.py # FastAPI application
β βββ database.py # Database connection handler
β βββ embeddings.py # Vector embedding handler
β βββ open_webui_api.py # API handler for querying LLM
β βββ openai_api.py # OpenAI API integration (optional)
β βββ mqtt_service.py # MQTT service for IoT device integration
β βββ family_endpoints.py # Family management endpoints
β βββ textbelt_api.py # API handler for TextBelt
β βββ migrations/ # Database migrations
βββ mqtt/
β βββ config/ # MQTT broker configuration
β βββ data/ # MQTT broker data
β βββ log/ # MQTT broker logs
βββ examples/
β βββ mqtt_client.py # Example Python MQTT client
β βββ esp32_mqtt_client/ # Example ESP32 MQTT client
βββ docker-compose.yml # Docker Compose file
βββ Dockerfile # Dockerfile for FastAPI backend
- Endpoint:
POST /store/
- Request Body:
{ "question": "What is the capital of France?", "category": "geography" }
- Response:
{ "question_id": "550e8400-e29b-41d4-a716-446655440000" }
- Endpoint:
GET /similar/
- Query Parameters:
query
,top_k
- Example:
GET /similar/?query=capital of France&top_k=5
- Response:
[ { "question_id": "123", "question_text": "What is the capital city of France?", "similarity": 0.01 } ]
- Endpoint:
POST /ask/
- Request Body:
{ "prompt": "What is the capital of Italy?", "model": "deepseek-r1:7b" }
- Response:
{ "response": "The capital of Italy is Rome." }
- Endpoint:
POST /answer/
- Request Body:
{ "question_id": "550e8400-e29b-41d4-a716-446655440000", "answer": "The capital of France is Paris." }
- Response:
{ "answer_id": "660e950f-cb32-4c2a-a07d-d36dcdc2617b", "message": "Answer stored successfully." }
- Endpoint:
POST /send_sms/
- Request Body:
{ "phone": "+1234567890", "message": "What is the capital of Spain?" }
- Response:
{ "success": true, "message": "SMS sent successfully." }
-
Table
questions
question_id
: UUID (Primary Key)question_text
: TEXTcategory
: TEXTembedding
: VECTORanswer_seed
: UUID
-
Table
answers
answer_id
: UUID (Primary Key)question_id
: UUID (Foreign Key)answer_text
: TEXTembedding
: VECTORcreated_at
: TIMESTAMP
If running locally with Python:
pip install -r requirements.txt
pytest tests/
docker-compose down
- OpenWebUI URL: Replace
OPEN_WEBUI_API_URL
with your server's actual URL. - API Keys: Keep your
OPEN_WEBUI_API_KEY
,TEXTBELT_API_KEY
, andOPENAI_API_KEY
secure. - Docker Volumes: Data persists between restarts via Docker volumes.
- OpenAI Integration: Set
USE_OPENAI=true
in your.env
file to enable OpenAI for enhanced question generation with conversation history. - MQTT Integration: See the
examples/
directory for sample MQTT clients that connect to the system.
The system includes an MQTT broker for IoT device integration, allowing devices to receive and answer questions.
scribe/clients/{device_id}/status
- Device status messagesscribe/families/{family_id}/questions
- Questions for the familyscribe/families/{family_id}/answers
- Answers to questionsscribe/families/{family_id}/notifications
- Notifications for the familyscribe/families/{family_id}/devices/{device_id}/#
- Device-specific messages
GET /api/families/{family_id}/mqtt
- Get MQTT configuration for a familyPUT /api/families/{family_id}/mqtt
- Update MQTT configurationPOST /api/families/{family_id}/mqtt/devices
- Add a new allowed MQTT deviceDELETE /api/families/{family_id}/mqtt/devices/{device_id}
- Remove an allowed MQTT deviceGET /api/families/{family_id}/mqtt/connected-devices
- Get list of connected devicesPOST /api/families/{family_id}/mqtt/send-message
- Send a message to devices
Contributions are welcome! Please open an issue or submit a pull request for improvements.
Contact me for information: JoeLorenzoMontano@gmail.com
β¨ Built with β€οΈ and FastAPI! ππ‘