feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
# FAQ Bot with ChromaDB and LangChain
|
||||
# FAQ Bot with ChromaDB and moderate-censor
|
||||
|
||||
This project implements a simple FAQ bot that uses **ChromaDB** for vector storage and **LangChain** for building an intelligent agent. The bot can answer questions based on a small knowledge base stored in ChromaDB.
|
||||
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.
|
||||
|
||||
## Features
|
||||
|
||||
- Stores documents in ChromaDB with embeddings from OpenAI.
|
||||
- Uses the latest LangChain agent creation method (`initializeAgentExecutorWithOptions`).
|
||||
- Simple CLI interface for interacting with the bot.
|
||||
- Easy to extend with more documents or tools.
|
||||
- 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`)
|
||||
|
||||
## Setup
|
||||
|
||||
1. **Clone the repository**
|
||||
|
||||
```bash
|
||||
git clone https://github.com/your-username/faq-bot-chromadb.git
|
||||
cd faq-bot-chromadb
|
||||
git clone <repo-url>
|
||||
cd <repo-directory>
|
||||
```
|
||||
|
||||
2. **Install dependencies**
|
||||
@@ -24,26 +30,83 @@ This project implements a simple FAQ bot that uses **ChromaDB** for vector stora
|
||||
npm install
|
||||
```
|
||||
|
||||
3. **Configure environment**
|
||||
|
||||
Create a `.env` file in the project root:
|
||||
3. **Create a `.env` file**
|
||||
|
||||
```env
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
CHROMA_DB_PATH=./chromadb
|
||||
PORT=3000
|
||||
```
|
||||
|
||||
4. **Run the bot**
|
||||
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
|
||||
```
|
||||
|
||||
You can also use `npm run dev` for automatic restarts with nodemon.
|
||||
The server will listen on the port specified in `.env` (default 3000).
|
||||
|
||||
## Adding Documents
|
||||
## Usage
|
||||
|
||||
The bot comes with two sample FAQ entries. To add more, edit `src/index.js` or use the `addDocument` function from `src/vectorstore.js`.
|
||||
Send a POST request to `/ask`:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:3000/ask \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"question":"What is ChromaDB?"}'
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"answer": "ChromaDB is a vector database for storing and retrieving embeddings."
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
Reference in New Issue
Block a user