feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'

This commit is contained in:
2026-06-30 17:11:19 +03:00
parent 1b9342d225
commit 25b3afdfed
10 changed files with 318 additions and 259 deletions
+25 -92
View File
@@ -1,112 +1,45 @@
# FAQ Bot with ChromaDB and moderate-censor
# FAQ Bot ChromaDB + LangChain
This project implements an FAQ bot that uses **ChromaDB** for vector storage and retrieval, and **moderate-censor** as the single MCP-tool for content moderation.
This project implements a simple FAQ bot that answers user questions based on a predefined FAQ dataset.
The bot uses **ChromaDB** for vector storage and **LangChain** as the single MCPtool to process queries.
## Features
- Vector-based FAQ retrieval using OpenAI embeddings and ChromaDB.
- User input moderation with moderate-censor.
- Simple HTTP API (`/ask`) to query the bot.
## Prerequisites
- Node.js v18+ (or any LTS version)
- npm
- OpenAI API key (set in `.env`)
- ChromaDB server running locally (default path: `chromadb`)
- Persistent vector store (ChromaDB) data is saved to disk and reused across runs.
- Retrievalbased QA using LangChains `RetrievalQA` chain.
- Simple commandline interface.
- Unit tests covering vector store creation, bot answering, and unknownquestion handling.
## Setup
1. **Clone the repository**
```bash
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
```bash
git clone <repo-url>
cd <repo-directory>
```
2. **Install dependencies**
```bash
npm install
```
3. **Create a `.env` file**
```env
OPENAI_API_KEY=your_openai_api_key
PORT=3000
```
4. **Prepare FAQ data**
Create a `faq.json` file in the project root with the following format:
```json
[
{
"question": "What is ChromaDB?",
"answer": "ChromaDB is a vector database for storing and retrieving embeddings."
},
{
"question": "How do I use the bot?",
"answer": "Send a POST request to /ask with a JSON body containing the 'question' field."
}
]
```
5. **Ingest FAQ data**
```bash
npm run ingest
```
This will read `faq.json`, generate embeddings, and store them in ChromaDB.
6. **Start the bot**
```bash
npm start
```
The server will listen on the port specified in `.env` (default 3000).
# Install dependencies
pip install -r requirements.txt
```
## Usage
Send a POST request to `/ask`:
```bash
python src/main.py
```
You will be prompted to type a question. The bot will reply with the best answer from the FAQ dataset.
## Running Tests
```bash
curl -X POST http://localhost:3000/ask \
-H "Content-Type: application/json" \
-d '{"question":"What is ChromaDB?"}'
pytest
```
Response:
All tests should pass.
```json
{
"answer": "ChromaDB is a vector database for storing and retrieving embeddings."
}
```
## FAQ Dataset
If the question contains disallowed content, the bot will respond with a 403 status and reasons.
## Project Structure
```
├── package.json
├── src
│ ├── index.js # HTTP server and bot logic
│ ├── ingest.js # FAQ ingestion script
│ └── middleware.js # Moderation middleware
├── faq.json # FAQ data file
└── README.md
```
## Notes
- The bot uses the `text-embedding-ada-002` model for embeddings.
- Only one MCP-tool (`moderate-censor`) is used as required.
- Ensure the ChromaDB server is running before ingesting data or starting the bot.
The dataset is embedded in the code (3 entries). Feel free to extend it in `src/vector_store.py`.
## License