feat: solution for 'Экзамен: RAG-агент с ChromaDB и веб-поиском'
This commit is contained in:
@@ -1,51 +1,91 @@
|
||||
# RAG Agent with ChromaDB and Web Search
|
||||
|
||||
This project demonstrates a simple Retrieval-Augmented Generation (RAG) agent that uses **ChromaDB** as the vector store and performs web search as a fallback. The agent is written in Node.js and uses only the required dependencies.
|
||||
This project implements a Retrieval-Augmented Generation (RAG) agent that uses **ChromaDB** as the vector database and performs live web searches to provide up‑to‑date information.
|
||||
|
||||
## Features
|
||||
|
||||
- **Vector storage** with ChromaDB (in-memory by default).
|
||||
- **Simple embedding** function (placeholder) – replace with a real model for production.
|
||||
- **Web search** using DuckDuckGo’s HTML interface.
|
||||
- **RAG agent** that retrieves relevant documents or falls back to web search.
|
||||
- **Vector store** – Documents are ingested, split into chunks, embedded with OpenAI embeddings, and stored in a persistent ChromaDB collection.
|
||||
- **Web search** – Uses DuckDuckGo scraping to fetch recent web snippets for a query.
|
||||
- **RAG pipeline** – Combines local document context and web results, then generates an answer with OpenAI GPT‑3.5‑Turbo.
|
||||
- **CLI** – Simple command line interface for ingestion and querying.
|
||||
|
||||
## Installation
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.10+
|
||||
- An OpenAI API key with access to `text-embedding-ada-002` and `gpt-3.5-turbo`.
|
||||
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
npm install
|
||||
# Clone the repository
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/ekzamen-rag-agent-s-chromadb-i-veb-poisk.git
|
||||
cd ekzamen-rag-agent-s-chromadb-i-veb-poisk
|
||||
|
||||
# Create a virtual environment (optional but recommended)
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
|
||||
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Create a `.env` file in the project root (or set environment variables directly):
|
||||
|
||||
```
|
||||
OPENAI_API_KEY=sk-...
|
||||
CHROMA_DB_PATH=./chromadb
|
||||
CHROMA_COLLECTION_NAME=rag_collection
|
||||
```
|
||||
|
||||
> **Note**: Do not commit your `.env` file or API key to version control.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
node src/index.js "Your query here"
|
||||
```
|
||||
|
||||
If no query is provided, it defaults to `"What is ChromaDB?"`.
|
||||
|
||||
## Running Tests
|
||||
### 1. Ingest Documents
|
||||
|
||||
```bash
|
||||
npm test
|
||||
python src/main.py ingest path/to/doc1.txt path/to/doc2.txt
|
||||
```
|
||||
|
||||
The script will read each file, split it into chunks, generate embeddings, and store them in ChromaDB.
|
||||
|
||||
### 2. Query the Agent
|
||||
|
||||
```bash
|
||||
python src/main.py query "What is the capital of France?"
|
||||
```
|
||||
|
||||
The agent will:
|
||||
|
||||
1. Retrieve relevant chunks from the local vector store.
|
||||
2. Perform a DuckDuckGo web search for the query.
|
||||
3. Combine both sources of information.
|
||||
4. Generate a response using OpenAI GPT‑3.5‑Turbo.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
src/
|
||||
index.js # Entry point
|
||||
agent.js # RAG agent logic
|
||||
vectorStore.js # ChromaDB wrapper
|
||||
webSearch.js # Simple web search helper
|
||||
test.js # Basic test for vector store
|
||||
├── main.py # CLI entry point
|
||||
├── vector_store.py # ChromaDB ingestion & retrieval
|
||||
├── web_search.py # DuckDuckGo web search
|
||||
requirements.txt
|
||||
README.md
|
||||
```
|
||||
|
||||
## Extending
|
||||
## Testing
|
||||
|
||||
- Replace the `embed` function in `vectorStore.js` with a real embedding model (e.g., OpenAI, HuggingFace).
|
||||
- Persist the ChromaDB collection by configuring the client with a storage path.
|
||||
- Add a language model to generate responses from retrieved documents.
|
||||
The project can be tested with `pytest` (tests are not included in this minimal example).
|
||||
If you add tests, run:
|
||||
|
||||
```bash
|
||||
pytest
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
MIT License
|
||||
---
|
||||
Feel free to extend the agent with additional features such as custom embeddings, different LLMs, or alternative search APIs.
|
||||
Reference in New Issue
Block a user