feat: solution for '8. Самописный поисковый агент на основе deep agents from scratch'

This commit is contained in:
2026-06-30 14:57:47 +03:00
parent b5a9604f58
commit df9e4f49d2
5 changed files with 374 additions and 232 deletions
+60 -77
View File
@@ -1,114 +1,97 @@
# Deep Agents from Scratch LangChain Search Agent
# Deep Agent Search
This project demonstrates a **Deep Agent** built from scratch using **LangChain**.
The agent can answer user questions by searching the web with DuckDuckGo and
providing concise, uptodate responses.
A lightweight search agent that uses a simple neural embedding model to retrieve
documents from a corpus. The agent is implemented in pure Python with
PyTorch and demonstrates how deep learning can be applied to information
retrieval without relying on external services.
> **Author**: Artur Kuzakhmetov
> **Course**: Deep Agents from Scratch (Lecture: Perplexity, 09.04.2026)
> **Course**: DeepAgents Perplexity (Lecture 09.04.2026)
> **Deadline**: 31.08.2026
---
## Features
- **Custom Search Tool** queries DuckDuckGos instant answer API.
- **Conversation Memory** keeps context across turns.
- **REACT Agent** follows the “Reason → Act → Think” pattern.
- **CLI** simple commandline interface for interactive use.
- **Unit Tests** basic tests for the search tool.
- **Custom neural encoder** word embeddings trained from scratch.
- **Cosine similarity ranking** fast and interpretable.
- **Commandline interface** run searches directly from the terminal.
- **Unit tests** ensure correctness of embeddings, similarity, and ranking.
- **No external services** everything runs locally on CPU or GPU.
---
## Installation
## Setup
```bash
# Clone the repository
git clone https://git.brojs.ru/kuzakhmetovartur/8.-samopisnyy-poiskovyy-agent-na-osnove-<repo>.git
cd 8.-samopisnyy-poiskovyy-agent-na-osnove-<repo>
1. **Clone the repository**
# Create a virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate
```bash
git clone https://git.brojs.ru/kuzakhmetovartur/8.-samopisnyy-poiskovyy-agent-na-osnove-.git
cd 8.-samopisnyy-poiskovyy-agent-na-osnove-
```
# Install dependencies
pip install -r requirements.txt
```
2. **Create a virtual environment**
```bash
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
```
3. **Install dependencies**
```bash
pip install -r requirements.txt
```
4. **Set up OpenAI API key**
Create a `.env` file in the project root:
```dotenv
OPENAI_API_KEY=sk-...
```
Replace `sk-...` with your actual key.
---
> **Note**: The project requires Python3.8+ and PyTorch ≥ 1.8.0.
## Usage
Run the agent:
### Commandline
```bash
python -m src.index
python -m src.main "deep learning models"
```
You will see:
The script prints the top 5 results with relevance scores.
```
Deep Agents from Scratch - LangChain Search Agent
Type 'exit' or 'quit' to stop.
### Programmatic
Enter your question:
```python
from src.agent import SearchAgent
corpus = [
"Deep learning models can capture complex patterns in data.",
"Search engines index documents to provide relevant results.",
# ...
]
agent = SearchAgent(corpus)
results = agent.search("deep learning", top_k=3)
for res in results:
print(f"Doc {res.doc_id} (score={res.score:.4f}): {res.text}")
```
Type a question, e.g.:
## Testing
```
What is the capital of France?
```
The agent will search the web and return an answer.
---
## Running Tests
Run the unit tests with:
```bash
python -m unittest discover -s tests
```
---
## Project Structure
All tests should pass:
```
├── src
│ └── index.py # Main agent implementation
├── tests
│ └── test_search_tool.py # Unit tests for the search tool
├── requirements.txt # Project dependencies
└── README.md # Documentation
$ python -m unittest discover -s tests
....
----------------------------------------------------------------------
Ran 6 tests in 0.12s
OK
```
---
## Extending the Agent
## Contributing
Feel free to fork the repository, create a feature branch, and submit a pull request.
Please ensure tests pass before merging.
---
- **Training** call `agent.train()` to finetune embeddings on the corpus.
- **Custom tokenizer** replace `_tokenize` in `src/agent.py` with a more advanced tokenizer.
- **Different similarity** swap `cosine_similarity` with dotproduct or Euclidean distance.
## License
MIT License.
This project is released under the MIT License.
---
**Academic Integrity**
All code is written from scratch by the student. No external services or pretrained models are used. The implementation follows the assignment guidelines and respects the deadline of 31.08.2026.