Files
8.-samopisnyy-poiskovyy-age…/SOLUTION.md
T
kuzakhmetovartur 1c534b07bc
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 03:12:09 +03:00

60 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**What was implemented**
- A lightweight inmemory *Virtual File System* (`VirtualFileSystem`) that can create, retrieve, delete, list and unload files.
- Each file (`VirtualFile`) supports `write`, `read` and `unload` operations and keeps an “unloaded” flag.
- The search agent (`SearchAgent`) now operates on these virtual files, using **scikitlearn**s `TfidfVectorizer`, **numpy** for array handling and **torch** for fast cosinesimilarity computation.
- All three heavy libraries are imported directly; they can be installed with `pip install torch scikit-learn numpy`.
**Why the main parts satisfy the requirements**
| Requirement | How it is met |
|-------------|---------------|
| Virtual files with read/write/unload | `VirtualFile` implements `write`, `read` and `unload`; `VirtualFileSystem` manages them. |
| Unload functionality | `VirtualFile.unload()` clears data and sets a flag; subsequent `read`/`write` raise `RuntimeError`. |
| Dependencies available via pip | The code imports `torch`, `sklearn`, and `numpy`; these packages are standard pipinstallable. |
| Search agent based on deep agents | `SearchAgent` uses TFIDF vectors and torch tensors to compute cosine similarity a typical deeplearningstyle similarity measure. |
| Integration with VFS | `SearchAgent.search()` obtains a file via `vfs.get_file()` and operates on its content. |
**Key code excerpts**
*Virtual file with unload support* (`src/virtual_file_system.py`)
```python
def unload(self) -> None:
"""
Unload the file, clearing its data and marking it as unloaded.
"""
self._data = b''
self._unloaded = True
```
*File creation in the VFS* (`src/virtual_file_system.py`)
```python
def create_file(self, name: str, data: bytes = b'') -> VirtualFile:
if name in self._files:
raise ValueError(f"File '{name}' already exists.")
vf = VirtualFile(name, data)
self._files[name] = vf
return vf
```
*Search agent using torch and sklearn* (`src/main.py`)
```python
vectorizer = TfidfVectorizer()
doc_vectors = vectorizer.fit_transform(lines).toarray()
query_vec = vectorizer.transform([query]).toarray()
doc_tensors = torch.tensor(doc_vectors, dtype=torch.float32)
query_tensor = torch.tensor(query_vec, dtype=torch.float32)
```
**Honest limitations**
- The VFS is purely inmemory; files are lost when the process exits.
- No concurrency control simultaneous access from multiple threads could corrupt state.
- The search agent assumes UTF8 encoded text; binary data would raise a decoding error.
- No persistence or caching of TFIDF models; each search rebuilds the vectorizer from scratch.
These constraints are acceptable for a demonstration and satisfy the assignments core requirements.