From 0f41ec1a28ba735ff175ed5536461ab2ea35693c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B4=D0=B5=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A1=D0=B0?= =?UTF-8?q?=D1=82=D1=82=D0=B0=D1=80=D0=BE=D0=B2=D0=B0?= Date: Thu, 28 May 2026 13:21:11 +0000 Subject: [PATCH] Rewrite DeepAgent without external libs --- src/agent.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/agent.py b/src/agent.py index 877e137..8f356c3 100644 --- a/src/agent.py +++ b/src/agent.py @@ -1,41 +1,48 @@ """ -DeepAgent implementation using deepagents from scratch. +DeepAgent implementation from scratch. -The agent searches the web via Perplexity API and writes results to virtual files, -then exports them to real filesystem. +The agent searches the web via Perplexity API, writes results to virtual files, +and exports them to real filesystem. """ import os import shutil from pathlib import Path -from typing import List, Dict +import json +import requests -from deepagents import AgentBuilder, Tool, LLMWrapper -from deepagents.tools.perplexity import PerplexityTool - -# Configuration BASE_DIR = Path("/tmp/deepagent") OUTPUT_DIR = Path("./output_files") class DeepAgent: def __init__(self, api_key: str): self.api_key = api_key - # Build agent with Perplexity tool - perplexity_tool = Tool( - name="perplexity", - description="Search the web using Perplexity API.", - func=lambda query: PerplexityTool(api_key=api_key).search(query), - ) - self.agent = AgentBuilder().with_tools([perplexity_tool]).build() BASE_DIR.mkdir(parents=True, exist_ok=True) OUTPUT_DIR.mkdir(parents=True, exist_ok=True) + def _search(self, query: str) -> dict: + url = "https://api.perplexity.ai/chat/completions" + headers = { + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json", + } + payload = { + "model": "perplexity-llama-3.1-mini-128k-chat", + "messages": [{"role": "user", "content": query}], + "temperature": 0, + } + resp = requests.post(url, headers=headers, json=payload) + resp.raise_for_status() + data = resp.json() + content = data["choices"][0]["message"]["content"] + # Expect JSON + return json.loads(content) + def search_and_save(self, query: str, filename: str): - # Run agent to get result - result = self.agent.run(query) + result = self._search(query) file_path = BASE_DIR / filename with open(file_path, "w", encoding="utf-8") as f: - f.write(result) + json.dump(result, f, ensure_ascii=False, indent=2) return file_path def export(self): @@ -50,6 +57,6 @@ if __name__ == "__main__": agent = DeepAgent(api_key=args.api_key) queries = ["Python async programming", "LangChain deep agents"] for i, q in enumerate(queries, 1): - agent.search_and_save(q, f"result_{i}.txt") + agent.search_and_save(q, f"result_{i}.json") agent.export() print("Exported files to", OUTPUT_DIR)