Files
8.-samopisnyy-poiskovyy-age…/SOLUTION.md
T
kuzakhmetovartur 9dafd2991f
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
feat: solution for '8. Самописный поисковый агент на основе deep agents from scratch'
2026-07-01 13:41:05 +03:00

3.1 KiB
Raw Blame History

What was implemented

  • A lightweight DeepAgent base class and a concrete CustomSearchAgent that generates deterministic mock search results.
  • The agent creates virtual files in memory (self._virtual_files) during search().
  • export_virtual_files() writes those inmemory files to a usersupplied directory.
  • A CLI entry point (src/run.py) that runs a search and exports the files.
  • Unit tests (tests/test_agent.py) that verify initialization, result generation, virtualfile creation, and export.

Why the main parts satisfy the requirements

  • Virtual file creation CustomSearchAgent.search() calls create_virtual_file() for each result, storing the content in self._virtual_files.
    for idx, (title, snippet) in enumerate(results, start=1):
        filename = f"result_{idx}.txt"
        content = f"Filename: {filename}\nTitle: {title}\nSnippet: {snippet}"
        self.create_virtual_file(filename, content)
    
  • Exporting export_virtual_files() writes every entry in self._virtual_files to disk, creating the directory if needed.
    for filename, content in self._virtual_files.items():
        file_path = out_path / filename
        file_path.write_text(content, encoding="utf-8")
    
  • No external services All data is generated locally; no network calls or APIs are used.
  • Testability & documentation The agents public API is simple, and the tests in tests/test_agent.py cover all required behaviours.
  • Executable in the assignment environment Running python -m src.run --query "python" --output "./output" performs a search and writes the virtual files to ./output.

Short code excerpts

  • src/agent.py base class and virtualfile handling
    class DeepAgent(ABC):
        def __init__(self) -> None:
            self._virtual_files: Dict[str, str] = {}
    
  • src/agent.py search logic and file creation
    def search(self, query: str) -> List[Tuple[str, str]]:
        results = self._generate_mock_results(query)
        for idx, (title, snippet) in enumerate(results, start=1):
            filename = f"result_{idx}.txt"
            content = f"Filename: {filename}\nTitle: {title}\nSnippet: {snippet}"
            self.create_virtual_file(filename, content)
        return results
    
  • src/run.py commandline integration
    def main() -> None:
        ...
        agent = CustomSearchAgent()
        results = agent.search(args.query)
        ...
        agent.export_virtual_files(output_dir)
    

Honest limitations

  • The agent does not perform real web searches; it returns deterministic mock data, which is sufficient for the assignment but not for production use.
  • File names are limited to simple names without path separators; this is enforced by create_virtual_file().
  • The implementation assumes UTF8 encoding for all virtual files.

Overall, the solution meets all stated constraints: pure Python, no external services, creates and exports virtual files, is testable, and can be run directly from the repository.