feat: solution for '8. Самописный поисковый агент на основе deep agents from scratch'
This commit is contained in:
@@ -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 self‑contained, 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
|
||||
|
||||
### Command‑line
|
||||
### 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 in‑memory 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
|
||||
Reference in New Issue
Block a user