feat: solution for '8. Самописный поисковый агент на основе deep agents from scratch'
CI / build (3.1) (push) Has been cancelled
CI / build (3.11) (push) Has been cancelled
CI / build (3.8) (push) Has been cancelled
CI / build (3.9) (push) Has been cancelled

This commit is contained in:
2026-07-01 13:50:40 +03:00
parent 9dafd2991f
commit fea117b469
6 changed files with 160 additions and 124 deletions
+34 -51
View File
@@ -1,87 +1,70 @@
# Custom Search Agent DeepAgents from Scratch
# Deep Agent Search
This repository contains a minimal implementation of a **deep search agent** that:
This project demonstrates a simple search agent built with **LangChain**'s `DeepAgent` and the **OpenAI** language model. The agent can answer user queries and perform web searches when needed.
* Generates deterministic mock search results.
* Creates *virtual files* in memory during execution.
* Exports those virtual files to a specified directory on disk.
## Prerequisites
The agent is fully selfcontained, does not rely on external APIs, and is fully testable.
- Node.js 18+ (ES modules support)
- An OpenAI API key. Set it in your environment:
## Project Structure
```
.
├── src
│ ├── agent.py # Core agent implementation
│ └── run.py # CLI entry point
├── tests
│ └── test_agent.py # Unit tests
├── requirements.txt
└── README.md
```bash
export OPENAI_API_KEY="your-api-key-here"
```
## Installation
```bash
# Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
npm install
```
## Usage
### Commandline
### CLI
Run the agent interactively:
```bash
python -m src.run --query "python" --output "./search_results"
npm start
```
This will:
1. Search for `"python"` (mock results).
2. Create two virtual files (`result_1.txt`, `result_2.txt`) in memory.
3. Export those files to `./search_results`.
You will be prompted to enter a question. The agent will respond.
### Programmatic
```python
from src.agent import CustomSearchAgent
```js
import { ask } from "./src/index.js";
agent = CustomSearchAgent(max_results=3)
results = agent.search("deep learning")
print(results) # List of (title, snippet) tuples
agent.export_virtual_files("./output")
async function main() {
const answer = await ask("Who wrote 'Pride and Prejudice'?");
console.log(answer);
}
main();
```
## Testing
Run the unit tests with:
A simple test script is provided:
```bash
python -m unittest discover -s tests
npm test
```
All tests should pass, confirming that:
It queries the agent with a sample question and prints the answer.
* The agent initializes correctly.
* Search results are deterministic.
* Virtual files are created during search.
* Export writes the correct files to disk.
## Project Structure
## Extending the Agent
- `src/agent.js` Configures the `DeepAgent` with OpenAI LLM and the search tool.
- `src/index.js` Exposes the `ask` function and a CLI demo.
- `test.js` Quick test script.
- `package.json` Project metadata and dependencies.
The `CustomSearchAgent` inherits from `DeepAgent`. To add real search logic:
## Dependencies
1. Override `search` to perform actual queries (e.g., to a local index).
2. Use `create_virtual_file` to store any generated data.
3. Call `export_virtual_files` when you need to persist the data.
The base class already provides a convenient inmemory store and export logic.
- `langchain` Core LangChain library.
- `langchain-openai` OpenAI wrapper for LangChain.
- `langchain-community` Community tools, including the web search tool.
## License
This project is released under the MIT License.
MIT