Add deep_search_agent.py
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def web_search(query: str) -> str:
|
||||
"""Deterministic demo search tool; replace with a real search API if needed."""
|
||||
return (
|
||||
f"Search results for {query}: LangGraph helps build stateful agents; "
|
||||
"Deep Agents combine planning, tools, memory, and file writing."
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScratchDeepAgent:
|
||||
virtual_files: dict[str, str] = field(default_factory=dict)
|
||||
|
||||
def plan(self, task: str) -> list[str]:
|
||||
return [
|
||||
f"Сформировать поисковый запрос по теме: {task}",
|
||||
"Собрать краткую справку через web_search",
|
||||
"Сохранить результат в виртуальный файл",
|
||||
"Выгрузить виртуальные файлы на диск",
|
||||
]
|
||||
|
||||
def run(self, task: str) -> str:
|
||||
steps = self.plan(task)
|
||||
evidence = web_search(task)
|
||||
report = "# Search report\n\n" + "\n".join(f"- {step}" for step in steps)
|
||||
report += f"\n\n## Evidence\n\n{evidence}\n"
|
||||
self.virtual_files["report.md"] = report
|
||||
self.virtual_files["notes.txt"] = evidence
|
||||
return report
|
||||
|
||||
def flush_files(self, output_dir: str = "agent_output") -> list[Path]:
|
||||
root = Path(output_dir)
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
written: list[Path] = []
|
||||
for name, content in self.virtual_files.items():
|
||||
path = root / name
|
||||
path.write_text(content, encoding="utf-8")
|
||||
written.append(path)
|
||||
return written
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("task", nargs="*", default=["LangGraph agents"])
|
||||
args = parser.parse_args()
|
||||
agent = ScratchDeepAgent()
|
||||
print(agent.run(" ".join(args.task)))
|
||||
for path in agent.flush_files():
|
||||
print(f"written: {path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user