feat: solution for '8. Самописный поисковый агент на основе deep agents from scratch'
This commit is contained in:
@@ -1,76 +1,87 @@
|
||||
# Deep Agent Search with Virtual File System
|
||||
# Deep Agents from Scratch – Search Agent
|
||||
|
||||
This project demonstrates a simple deep agent search system that operates on **virtual files** stored entirely in memory. It uses **PyTorch**, **scikit-learn**, and **NumPy** to perform TF‑IDF vectorization and cosine similarity ranking.
|
||||
This project implements a simple web‑search agent using the **LangChain** framework, following the “Deep Agents from Scratch” template.
|
||||
The agent can answer user questions by performing a DuckDuckGo search and reasoning over the results with an OpenAI LLM.
|
||||
|
||||
## Features
|
||||
|
||||
- **Virtual File System**: Create, read, write, unload, and delete virtual files.
|
||||
- **Search Agent**: Rank lines from a virtual file based on a query using TF‑IDF and cosine similarity.
|
||||
- **Deep Learning Integration**: Uses PyTorch tensors for similarity calculations.
|
||||
- **Easy to Extend**: Replace the search logic with more sophisticated models (e.g., transformers) without changing the file system.
|
||||
- **Zero‑shot React** agent powered by LangChain.
|
||||
- Uses **DuckDuckGo** for web search (no API key required).
|
||||
- Powered by **OpenAI** (requires an API key).
|
||||
- Conversation memory to keep context across turns.
|
||||
- Simple command‑line interface.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.10+
|
||||
- An OpenAI API key (set in `OPENAI_API_KEY` environment variable).
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/8.-samopisnyy-poiskovyy-agent-na-osnove-
|
||||
cd 8.-samopisnyy-poiskovyy-agent-na-osnove-
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/8.-samopisnyy-poiskovyy-agent-na-osnove.git
|
||||
cd 8.-samopisnyy-poiskovyy-agent-na-osnove
|
||||
|
||||
# Create a virtual environment (optional but recommended)
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||
# Create a virtual environment (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 export the variable directly):
|
||||
|
||||
```dotenv
|
||||
OPENAI_API_KEY=sk-...
|
||||
```
|
||||
|
||||
> **Note**: The DuckDuckGo search tool does not require any API key.
|
||||
|
||||
## Usage
|
||||
|
||||
Run the example script:
|
||||
Run the agent from the command line:
|
||||
|
||||
```bash
|
||||
python src/main.py
|
||||
python main.py "What is the capital of France?"
|
||||
```
|
||||
|
||||
You should see output similar to:
|
||||
You should see the agent perform a search and return an answer.
|
||||
|
||||
```
|
||||
Search results for query: 'neural networks'
|
||||
1. Neural networks can approximate complex functions.
|
||||
2. Deep learning has revolutionized many fields.
|
||||
3. PyTorch provides dynamic computation graphs.
|
||||
After unload: Cannot read from unloaded file 'sample.txt'.
|
||||
## Example
|
||||
|
||||
```bash
|
||||
$ python main.py "Who is the current CEO of Tesla?"
|
||||
=== Agent Response ===
|
||||
Elon Musk is the current CEO of Tesla. He has been in the role since 2008 and is also the founder of SpaceX and Neuralink.
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
├── src
|
||||
│ ├── main.py # Entry point and demo
|
||||
│ └── virtual_file_system.py # Virtual file system implementation
|
||||
├── requirements.txt # Dependencies
|
||||
└── README.md # Documentation
|
||||
│ └── agent.py # Agent implementation
|
||||
├── main.py # CLI entry point
|
||||
├── requirements.txt # Dependencies
|
||||
├── README.md # Documentation
|
||||
└── .env # (Optional) Environment variables
|
||||
```
|
||||
|
||||
## Extending the Search Agent
|
||||
## Extending the Agent
|
||||
|
||||
The `SearchAgent` class in `src/main.py` can be replaced with any model that accepts a query and returns ranked results. For example, you could:
|
||||
- **Add more tools**: Import additional tools from `langchain_community.tools` and add them to the `tools` list in `src/agent.py`.
|
||||
- **Change the LLM**: Replace `ChatOpenAI` with another LLM provider (e.g., Anthropic, Gemini) by adjusting the import and initialization.
|
||||
- **Adjust temperature**: Modify the `temperature` parameter in `ChatOpenAI` to control creativity.
|
||||
|
||||
- Load a pre‑trained transformer (e.g., BERT) and compute embeddings.
|
||||
- Use a neural ranking model trained on relevance data.
|
||||
- Integrate with external search APIs.
|
||||
## Troubleshooting
|
||||
|
||||
Just ensure that the agent receives a `VirtualFileSystem` instance and uses `VirtualFile.read()` to access data.
|
||||
|
||||
## Testing
|
||||
|
||||
Unit tests are not included in this minimal example, but you can add tests using `pytest` to verify:
|
||||
|
||||
- Virtual file read/write/unload behavior.
|
||||
- Search agent ranking correctness.
|
||||
- Integration of the virtual file system with the agent.
|
||||
- **Missing OpenAI key**: Ensure `OPENAI_API_KEY` is set in your environment or `.env` file.
|
||||
- **Network errors**: Check your internet connection and retry.
|
||||
- **Agent hangs**: Increase the `timeout` in the DuckDuckGo tool or switch to a different search provider.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
```
|
||||
MIT License.
|
||||
Reference in New Issue
Block a user