add main
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
|||||||
|
"""
|
||||||
|
Deep Agents from Scratch example.
|
||||||
|
This script demonstrates a simple agent that searches the web and writes results to files.
|
||||||
|
It uses the `deep-agents-from-scratch` package as required by the assignment.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Ensure dependencies are available
|
||||||
|
try:
|
||||||
|
from deep_agents_from_scratch.research_tools import tavily_search, think_tool
|
||||||
|
except Exception as e: # pragma: no cover - defensive
|
||||||
|
raise RuntimeError("deep-agents-from-scratch not installed") from e
|
||||||
|
|
||||||
|
from langchain.agents import create_agent
|
||||||
|
from langchain.chat_models import init_chat_model
|
||||||
|
from deep_agents_from_scratch.state import DeepAgentState
|
||||||
|
from deep_agents_from_scratch.file_tools import ls, read_file, write_file
|
||||||
|
|
||||||
|
# Simple prompt for the agent
|
||||||
|
SYSTEM_PROMPT = """
|
||||||
|
You are a research assistant. Use web search to gather information and store results in files.
|
||||||
|
After each search, reflect on what you found.
|
||||||
|
"""
|
||||||
|
|
||||||
|
model = init_chat_model(model="anthropic:claude-sonnet-4-20250514", temperature=0)
|
||||||
|
|
||||||
|
# Tools available to the agent
|
||||||
|
TOOLS = [tavily_search, think_tool, ls, read_file, write_file]
|
||||||
|
|
||||||
|
agent = create_agent(
|
||||||
|
model,
|
||||||
|
TOOLS,
|
||||||
|
system_prompt=SYSTEM_PROMPT,
|
||||||
|
state_schema=DeepAgentState,
|
||||||
|
)
|
||||||
|
|
||||||
|
def run_query(query: str) -> None:
|
||||||
|
"""Run a single query and print the resulting messages."""
|
||||||
|
result = agent.invoke({"messages": [{"role": "user", "content": query}]})
|
||||||
|
for msg in result["messages"]:
|
||||||
|
print(msg.content)
|
||||||
|
|
||||||
|
if __name__ == "__main__": # pragma: no cover - entry point
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description="Run deep agent example")
|
||||||
|
parser.add_argument("query", help="Query to search for")
|
||||||
|
args = parser.parse_args()
|
||||||
|
run_query(args.query)
|
||||||
Reference in New Issue
Block a user