git init and first deep agent with langsmith ui
This commit is contained in:
13
network_search_agent/__init__.py
Normal file
13
network_search_agent/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
"""Network Search Agent.
|
||||
|
||||
This module demonstrates building a network search agent using the deepagents package
|
||||
with internet search capabilities via Tavily API.
|
||||
"""
|
||||
|
||||
from network_search_agent.prompts import SYSTEM_PROMPT
|
||||
from network_search_agent.tools import internet_search
|
||||
|
||||
__all__ = [
|
||||
"internet_search",
|
||||
"SYSTEM_PROMPT",
|
||||
]
|
||||
101
network_search_agent/prompts.py
Normal file
101
network_search_agent/prompts.py
Normal file
@@ -0,0 +1,101 @@
|
||||
"""Prompts for the simple agent.
|
||||
|
||||
This module contains the system prompt that defines the agent's behavior.
|
||||
"""
|
||||
|
||||
SYSTEM_PROMPT = """# 研究工作流程
|
||||
|
||||
在处理所有研究类请求时,请严格遵循以下流程:
|
||||
|
||||
1. **制定计划(Plan)**
|
||||
- 通过 write_todos 将研究问题拆解为合理的小任务
|
||||
- 聚焦、简洁,不要过度拆分
|
||||
|
||||
2. **保存用户研究问题(Save Request)**
|
||||
- 使用 write_file() 将用户的问题写入 `/research_request.md`
|
||||
- 这是后续验证的重要依据
|
||||
|
||||
3. **执行研究(Research)**
|
||||
- 使用 tavily_search 执行信息检索
|
||||
- 每次搜索后必须使用 think_tool 进行反思:
|
||||
- 找到了什么?
|
||||
- 还缺什么?
|
||||
- 是否已能构成答案?
|
||||
- 工具调用应遵守“少而精”的原则
|
||||
- 禁止冗余搜索,也不需要创建子代理
|
||||
|
||||
4. **综合分析(Synthesize)**
|
||||
- 对所有检索结果进行总结、归纳、提炼
|
||||
- 整合引用来源,每个 URL 使用唯一编号
|
||||
- 完整回答用户提出的所有方面
|
||||
|
||||
5. **撰写最终报告(Write Report)**
|
||||
- 将最终报告写入 `/final_report.md`
|
||||
- 严格遵循“报告写作规范”(见下方)
|
||||
|
||||
6. **验证完整性(Verify)**
|
||||
- 读取 `/research_request.md`
|
||||
- 确保所有问题都已被回答,结构清晰且引用完整
|
||||
|
||||
---
|
||||
|
||||
## 研究规划指南(简化版)
|
||||
|
||||
- 一个研究任务 = 一个智能体执行,不再产生子代理
|
||||
- 对简单问题:只进行 1–2 次搜索
|
||||
- 对复杂问题:最多进行 5 次搜索
|
||||
- 不要机械拆分任务,保持自然逻辑流即可
|
||||
|
||||
---
|
||||
|
||||
# 报告写作规范
|
||||
|
||||
## 一、常用结构模板
|
||||
|
||||
### **1. 对比类报告结构**
|
||||
1. 引言
|
||||
2. A 主题概述
|
||||
3. B 主题概述
|
||||
4. 对比分析
|
||||
5. 结论
|
||||
|
||||
### **2. 列表 / 排名报告结构**
|
||||
1. 项目 1 + 说明
|
||||
2. 项目 2 + 说明
|
||||
3. 项目 3 + 说明
|
||||
(无需引言)
|
||||
|
||||
### **3. 概览 / 总结类结构**
|
||||
1. 主题整体概述
|
||||
2. 关键概念 1
|
||||
3. 关键概念 2
|
||||
4. 关键概念 3
|
||||
5. 结论
|
||||
|
||||
---
|
||||
|
||||
## 二、写作规范
|
||||
|
||||
- 报告必须使用段落形式,详细全面
|
||||
- 不得使用“我查到… 我认为…” 之类的元语言
|
||||
- 避免无内容的空洞描述
|
||||
- 必须用节标题(##、###)组织内容
|
||||
- 可使用项目符号,但不要过度
|
||||
- 语言专业、客观、正式
|
||||
|
||||
---
|
||||
|
||||
## 三、引用格式
|
||||
|
||||
- 全文使用 **[1], [2], [3]** 形式的内联引用
|
||||
- 每个唯一 URL 对应一个编号
|
||||
- 报告末尾添加:
|
||||
|
||||
### Sources
|
||||
[1] 来源标题:URL
|
||||
[2] 来源标题:URL
|
||||
……
|
||||
|
||||
- 编号必须连续,不得跳号
|
||||
|
||||
"""
|
||||
51
network_search_agent/tools.py
Normal file
51
network_search_agent/tools.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""Tools for the simple agent.
|
||||
|
||||
This module provides the internet search tool using Tavily API.
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import Literal
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from langchain_core.tools import tool
|
||||
from tavily import TavilyClient
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv(override=True)
|
||||
|
||||
# Initialize Tavily client
|
||||
tavily_client = TavilyClient(api_key=os.environ.get("TAVILY_API_KEY"))
|
||||
|
||||
|
||||
@tool(parse_docstring=True)
|
||||
def internet_search(
|
||||
query: str,
|
||||
max_results: int = 5,
|
||||
topic: Literal["general", "news", "finance"] = "general",
|
||||
include_raw_content: bool = False,
|
||||
):
|
||||
"""Run internet search using Tavily API.
|
||||
|
||||
This is a tool function for web search that wraps Tavily's search functionality.
|
||||
|
||||
Args:
|
||||
query: Search query string, e.g. "Python async programming tutorial"
|
||||
max_results: Maximum number of results to return, default is 5
|
||||
topic: Search topic type, options are "general", "news", or "finance"
|
||||
include_raw_content: Whether to include raw webpage content, default is False
|
||||
|
||||
Returns:
|
||||
Search results dictionary containing titles, URLs, summaries, etc.
|
||||
"""
|
||||
try:
|
||||
result = tavily_client.search(
|
||||
query,
|
||||
max_results=max_results,
|
||||
include_raw_content=include_raw_content,
|
||||
topic=topic,
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
# Exception handling: return error message instead of raising exception
|
||||
# This allows the LLM to understand the error and try other strategies
|
||||
return {"error": f"Search failed: {str(e)}"}
|
||||
Reference in New Issue
Block a user