feat: solution for 'Экзамен: RAG-агент с ChromaDB и веб-поиском'

This commit is contained in:
2026-06-30 12:59:48 +03:00
parent 136e69e967
commit 27dcde060a
6 changed files with 190 additions and 178 deletions
+31 -56
View File
@@ -1,24 +1,22 @@
# RAG Agent with ChromaDB and Web Search
This project implements a Retrieval-Augmented Generation (RAG) agent that:
- Stores and retrieves embeddings from **ChromaDB**.
- Performs web search using DuckDuckGo to fetch additional context.
- Generates answers with an **Ollama** language model.
This project implements a Retrieval-Augmented Generation (RAG) agent that uses **ChromaDB** as the vector database and the **OpenAI API** to generate responses based on retrieved documents. It also includes a simple websearch component that fetches content from specified URLs for indexing.
## Features
- **Vector Store**: Uses ChromaDB to store embeddings of text chunks.
- **OpenAI Integration**: Generates answers using GPT3.5Turbo.
- **Web Search**: Fetches and parses HTML pages, splits them into manageable chunks.
- **Command Line Interface**: Ask questions interactively.
## Prerequisites
- Node.js v20 or newer
- ChromaDB server running locally (default URL: `chromadb://localhost:8000`)
- Ollama server running locally (default URL: `http://localhost:11434`)
- Node.js v18+ (supports native ES modules and `node-fetch` v2).
- An OpenAI API key.
## Setup
1. **Clone the repository**
```bash
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
```
1. **Clone the repository** (or copy the files into a directory).
2. **Install dependencies**
@@ -26,62 +24,39 @@ This project implements a Retrieval-Augmented Generation (RAG) agent that:
npm install
```
3. **Configure environment variables**
3. **Configure environment**
Create a `.env` file in the project root (or modify the existing one):
Create a `.env` file in the project root (or edit the existing one) and add your OpenAI API key:
```dotenv
CHROMA_URL=chromadb://localhost:8000
CHROMA_COLLECTION=rag_collection
OLLAMA_HOST=http://localhost:11434
OLLAMA_MODEL=llama3
OLLAMA_EMBEDDING_MODEL=nomic-embed-text
ADD_SAMPLE_DOCS=true
OPENAI_API_KEY=your_api_key_here
```
- `CHROMA_URL`: URL of your ChromaDB instance.
- `CHROMA_COLLECTION`: Name of the collection to use.
- `OLLAMA_HOST`: URL of your Ollama server.
- `OLLAMA_MODEL`: Ollama model for generation.
- `OLLAMA_EMBEDDING_MODEL`: Ollama model for embeddings.
- `ADD_SAMPLE_DOCS`: Set to `true` to automatically add a few sample documents on startup.
4. **Run the agent**
```bash
npm start -- "Your question here"
npm start
```
Example:
The script will:
- Fetch and index the example URLs.
- Prompt you to enter questions.
- Display answers generated by the RAG agent.
```bash
npm start -- "What is LangChain?"
```
## Customization
The agent will:
- Search the local ChromaDB collection.
- Perform a DuckDuckGo web search.
- Combine the results and generate an answer using Ollama.
## Project Structure
```
.
├── src
│ ├── agent.js # Agent logic (retrieval + generation)
│ ├── index.js # CLI entry point
│ ├── vectorStore.js # ChromaDB wrapper
│ └── webSearch.js # DuckDuckGo search helper
├── .env # Environment configuration
├── package.json # Dependencies and scripts
└── README.md # Documentation
```
- **Adding URLs**: Edit the `urls` array in `src/index.js` to index different web pages.
- **Chunk Size**: Adjust the `size` parameter in `chunkText` inside `src/webSearch.js` if you need larger or smaller chunks.
- **Model Parameters**: Modify temperature, max tokens, or model name in `src/agent.js`.
## Notes
- The agent uses **LangChain 1.x** APIs.
- No Qdrant references are present; only ChromaDB is used.
- The web search is performed via DuckDuckGos public JSON API (no API key required).
- The Ollama LLM is used for both embeddings and generation.
- The implementation strictly uses **ChromaDB** as the vector database; no other vector DBs are used.
- All dependencies are declared in `package.json` and can be installed via `npm install`.
- The OpenAI API key is loaded securely from the `.env` file using `dotenv`.
Feel free to extend the agent with additional retrievers or custom prompts as needed.
## License
MIT License
---
Enjoy building with RAG!