AI 平台/部署

在 OpenRouter 上使用 NVIDIA Nemotron 构建报告生成 AI 智能体

与传统系统遵循预设路径不同,AI智能体依托大语言模型(LLM)进行决策,能够适应动态变化的需求,并执行复杂的推理任务。

在本关于构建报告生成智能体的自主培训指南中,您将获得:

  • 了解任何 AI 智能体的四个核心考虑因素,包括 NVIDIA Nemotron,这是一个具有开放数据和权重的开放模型系列。
  • 一个可以研究和编写报告的工作文档生成代理。
  • 了解如何使用 LangGraph 和 OpenRouter 构建智能体。
  • 一站式可移植开发环境。
  • 您自己的定制智能体,随时可以作为 NVIDIA Launchable 进行分享。

研讨会部署

NVIDIA Brev 可启动形式 开启研讨会:

image of the deploy now button
图 1。单击`Deploy Now` (立即部署) 按钮,在云端部署 NVIDIA DevX Workshop

用于设置机密的配置

要参加本次研讨会,您需提前收集并配置相关项目的机密信息。

在 JupyterLab 环境运行后,您可以通过 JupyterLab Launcher 中 NVIDIA DevX 学习路径下的 Secret Manager 组件,为 Workshop 开发环境配置相应的密钥。随后,请在“日志”选项卡中确认密钥是否已成功添加。

A screenshot of the Secrets Manager tile under NVIDIA DevX Learning Path
图 2。使用 NVIDIA DevX 学习路径部分下的“Secrets Manager”图块配置项目机密

接下来,请进入 JupyterLab Launcher 中的 NVIDIA DevX 学习路径 部分,选择编号 1 的“Introduction to Agents Tile”(代理图块简介),打开实验说明并开始操作。

A screenshot of the 1. Introduction to Agents tile
图 3。” 1. NVIDIA DevX 学习路径中的智能体图块简介,以打开实验室说明。

智能体架构简介

完成研讨会环境配置后,第一节将向开发者介绍智能体的基本概念。在深入探讨具体实现之前,理解智能体与传统简单AI应用之间的差异尤为关键。

与传统的基于大语言模型(LLM)的应用不同,智能体能够动态选择工具,整合复杂的推理过程,并根据实际情境灵活调整分析策略。开发者需要掌握智能体设计中的四个关键要点:

  • 模型: 作为大脑的 LLM,用于决定使用哪些工具以及如何应对。
  • 工具: 使 LLM 能够执行数学计算、数据库查询或 API 调用等操作的函数。
  • 内存和状态: LLM 在对话期间和对话之间可用的信息。
  • 路由: 根据当前状态和 LLM 决策确定智能体下一步应该做什么的逻辑。

您将学习如何将这些组件组合起来,在 code/intro_to_agents.ipynb 中构建首个具备计算器功能的基本智能体。完成本练习后,您将拥有一名能够执行以下任务的智能体:

[{'content': 'What is 3 plus 12?', 'role': 'user'},
 {'content': None,
  'role': 'assistant',
  'tool_calls': [{'function': {'arguments': '{"a": 3, "b": 12}', 'name': 'add'},
                  'id': 'chatcmpl-tool-b852128b6bdf4ee29121b88490174799',
                  'type': 'function'}]},
 {'content': '15',
  'name': 'add',
  'role': 'tool',
  'tool_call_id': 'chatcmpl-tool-b852128b6bdf4ee29121b88490174799'},
 {'content': 'The answer is 15.', 'role': 'assistant'}]

报告生成组件

本研讨会的其余部分将聚焦于利用 LangGraph NVIDIA NIM 作为 OpenRouter 端点托管,构建多层级的代理式系统。该架构包含四个相互连接的智能体组件,每个组件负责文档生成过程中不同的特定环节。

  1. 初步研究:收集有关该主题的全面信息。
  2. 大纲规划:根据研究成果创建结构化文档大纲。
  3. 章节写作:为每个章节生成详细内容,并根据需要进行其他研究。
  4. 最终编译:将所有部分组合成专业报告。

学习并实施代码

现在我们已经掌握了这些概念,接下来将深入探讨技术实现。我们将从上述提到的基本注意事项入手,逐步构建一个完整的智能体:

  1. 选择模型
  2. 选择工具
  3. 培养研究人员
  4. 构建作者
  5. 构建最终智能体
  6. 管理和路由智能体

基础:模型

研讨会依托 NVIDIA NIM 端点,为智能体提供核心模型的动力支持。NVIDIA NIM 具备高性能推理能力,主要功能包括:

  • 工具绑定: 对函数调用的原生支持。
  • 结构化输出: 内置对 Pydantic 模型的支持。
  • 异步操作: 完全支持并发处理的异步/ 等待。
  • 企业级可靠性: 生产级推理基础架构。

此示例展示了如何使用 NVIDIA NIM 托管的 ChatNVIDIA 连接器,通过 OpenRouter 端点进行连接。

from langchain_nvidia_ai_endpoints import ChatNVIDIA

llm = ChatNVIDIA(
    base_url="https://openrouter.ai/api/v1",
    model="nvidia/nemotron-nano-9b-v2:free", 
    api_key=os.getenv("OPENROUTER_API_KEY")
)
llm_with_tools = llm.bind_tools([tools.search_tavily])

虽然清晰的质量指令在任何基于大语言模型的应用中都至关重要,但对于智能体而言尤为如此,因为这类指令能够消除歧义,并使决策过程更加明确。以下是 code/docgen_agent/prompts.py 中的一个相关示例:

research_prompt: Final[str] = """
Your goal is to generate targeted web search queries that will gather comprehensive information for writing a technical report section.

Topic for this section:
{topic}

When generating {number_of_queries} search queries, ensure they:
1. Cover different aspects of the topic (e.g., core features, real-world applications, technical architecture)
2. Include specific technical terms related to the topic
3. Target recent information by including year markers where relevant (e.g., "2024")
4. Look for comparisons or differentiators from similar technologies/approaches
5. Search for both official documentation and practical implementation examples

Your queries should be:
- Specific enough to avoid generic results
- Technical enough to capture detailed implementation information
- Diverse enough to cover all aspects of the section plan
- Focused on authoritative sources (documentation, technical blogs, academic papers)"""

该提示说明了构建可靠的大语言模型提示时应遵循的一些关键原则。

  • 角色规范: 明确定义智能体的专业知识和职责。
  • 任务分解: 将复杂的要求分解为具体、可操作的步骤或标准。
  • 特异性: 时间特异性和权威来源的参考示例。
  • 结构化输入/ 输出: 针对所需响应结构和预期输入结构的具体指令。

基础:工具

智能体的能力由其工具定义。本次研讨会采用专为AI智能体设计的搜索API——Davily,作为信息收集的主要工具。

# imports and constants omitted
@tool(parse_docstring=True)
async def search_tavily(
    queries: list[str],
    topic: Literal["general", "news", "finance"] = "news",
) -> str:
    """Search the web using the Tavily API.

    Args:
        queries: List of queries to search.
        topic: The topic of the provided queries.
          general - General search.
          news - News search.
          finance - Finance search.

    Returns:
        A string of the search results.
    """
    search_jobs = []
    for query in queries:
        search_jobs.append(
            asyncio.create_task(
                tavily_client.search(
                    query,
                    max_results=MAX_RESULTS,
                    include_raw_content=INCLUDE_RAW_CONTENT,
                    topic=topic,
                    days=days,  # type: ignore[arg-type]
                )
            )
        )
    search_docs = await asyncio.gather(*search_jobs)
    return _deduplicate_and_format_sources(
        search_docs,
        max_tokens_per_source=MAX_TOKENS_PER_SOURCE,
        include_raw_content=INCLUDE_RAW_CONTENT,
    )

工具模块实现中的关键架构决策包括:

  • 异步操作: 使用`asyncio.gather () ` 进行并发搜索
  • 重复数据删除: 辅助功能可防止多次搜索出现冗余
  • 结构化文档: Google 样式的文档字符串有助于 LLM 了解工具使用情况

现在,我们已经对代码中的模型和工具有了基本了解,接下来将它们整合为一个实际运行的智能体。虽然状态管理和路由的相关问题尚未讨论,但我们会先构建智能体的组件,之后再回过头来探讨这些内容。

实施研究人员

智能体的研究组件实现了推理与行动相结合的模式(ReAct),这是当前应用于工具调用的智能体中较为高效的架构之一。该模式构建了一个循环机制:智能体首先思考应采取的行动,然后执行该行动,并根据执行结果判断下一步操作。这一过程将持续进行,直至智能体判断任务已完成,无需进一步操作为止。

 A diagram showcasing a simple React Agent loop
图 4。ReAct 循环是使用工具的智能体完成任务的有效架构

该智能体的研究人员组件代码在 code/docgen_agent/researcher.py 中实现,并可通过 code/researcher_client.ipynb 进行测试。

state = ResearcherState(
    topic="Examples of AI agents in various industries.",
    number_of_queries=3,
)
state = await graph.ainvoke(state)

for message in state["messages"]:
    print("ROLE: ", getattr(message, "role", "tool_call"))
    print(message.content[:500] or message.additional_kwargs)
    print("")

您还可以查看研究人员在执行过程中所采取的每一个操作。

INFO:docgen_agent.researcher:Calling model.
INFO:docgen_agent.researcher:Executing tool calls.
INFO:docgen_agent.researcher:Executing tool call: search_tavily
INFO:docgen_agent.tools:Searching the web using the Tavily API
INFO:docgen_agent.tools:Searching for query: Technical architecture of AI agents in healthcare 2024
INFO:docgen_agent.tools:Searching for query: Comparison of machine learning frameworks for AI agents in finance
INFO:docgen_agent.tools:Searching for query: Real-world applications of natural language processing in AI agents for customer service 2024
INFO:httpx:HTTP Request: POST https://api.tavily.com/search "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://api.tavily.com/search "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://api.tavily.com/search "HTTP/1.1 200 OK"
INFO:docgen_agent.researcher:Calling model.

以及最终的输出,包括工具调用请求、工具执行结果和总结性回复。

ROLE:  assistant
{'tool_calls': [{'id': 'chatcmpl-tool-b7185ba8eb3a44259b0bdf930495ece5', 'type': 'function', 'function': {'name': 'search_tavily', 'arguments': '{"queries": ["Technical architecture of AI agents in healthcare 2024", "Comparison of machine learning frameworks for AI agents in finance", "Real-world applications of natural language processing in AI agents for customer service 2024"], "topic": "general"}'}}]}

ROLE:  tool_call
"Sources:\n\nSource AI Agents in Modern Healthcare: From Foundation to Pioneer:\n===\nURL: https://www.preprints.org/manuscript/202503.1352/v1\n===\nMost relevant content from source: T. Guo et al., \"Large language model based multi-agents: A survey of progress and challenges,\" arXiv preprint arXiv:2402.01680, 2024. J. Ruan et al., \"TPTU: Large Language Model-based AI Agents for Task Planning and Tool Usage. | **Partner Agent** | * True healthcare team partners * Generates clinical hypotheses

ROLE:  assistant
Based on the search results, here are some potential search queries that could be used to gather comprehensive information for writing a technical report section on examples of AI agents in various industries:

1. "Technical architecture of AI agents in healthcare 2024"
2. "Comparison of machine learning frameworks for AI agents in finance"
3. "Real-world applications of natural language processing in AI agents for customer service 2024"

实现作者

简单的 ReAct 智能体虽然功能强大,但通常需要与其他步骤结合,以支持更复杂的工作流程。本节的作者需在收到请求后,才执行与本节相关的必要研究。研究完成后,必须依据研究成果撰写相应内容。

在修改后的架构图中,于 React 式智能体之前引入了门控函数,用于判断是否需要开展额外的研究,并最终完成编写步骤。

A diagram showing the decision tree leading to the React Agent loop.
图 5。添加了章节作者的 ReAct 智能体循环,可根据请求执行其他与章节相关的研究

该智能体的研究组件代码在 code/docgen_agent/author.py 中实现,并可通过 code/author_client.ipynb 进行测试。

state = SectionWriterState(
    index=1,
    topic="Examples of AI agents in various industries.",
    section=Section(
        name="Real-World Applications",
        description="Examples of AI agents in various industries",
        research=True,
        content=""
    ),
)

state = await graph.ainvoke(state)
Markdown(state["section"].content)

您还可以查看作者在执行过程中所采取的每一步操作。

INFO:docgen_agent.author:Researching section: Real-World Applications
INFO:docgen_agent.author:Executing tool calls for section: Real-World Applications
INFO:docgen_agent.author:Executing tool call: search_tavily
INFO:docgen_agent.tools:Searching the web using the Tavily API
INFO:docgen_agent.tools:Searching for query: AI agents in healthcare industry 2024
INFO:docgen_agent.tools:Searching for query: Implementation of AI agents in finance sector 2023
...
INFO:httpx:HTTP Request: POST https://api.tavily.com/search "HTTP/1.1 200 OK"
INFO:docgen_agent.author:Researching section: Real-World Applications
INFO:docgen_agent.author:Writing section: Real-World Applications

以及最终输出,包含以 markdown 格式呈现的书面内容。

## Real-World Applications

AI agents have numerous applications across various industries...

### Healthcare

AI agents in healthcare are revolutionizing patient care and medical innovation. They are being used to automate administrative tasks, enhance diagnostics, and improve workflow efficiency. For instance...

### Finance

AI agents in finance are driving innovation, success, and compliance. They are being used to automate tasks such as data entry, transaction processing, and compliance checks. AI agents are also being used to detect fraud, improve customer service, and provide personalized investment advice. For example...

实现最终智能体

借助这两个组件,我们现在能够将文档生成智能体的工作流整合起来。这种架构构成了一种简洁的线性工作流程,可用于研究主题、撰写章节,并最终汇编成完整的报告。

A diagram depicting the entire end-to-end flow.
图 6。报告智能体的工作流:研究主题、编写章节和编译报告

该智能体的研究组件代码在 code/docgen_agent/agent.py 中实现,并可通过 code/agent_client.ipynb 进行测试。

state = AgentState(
    topic="The latest developments with AI Agents in 2025.",
    report_structure="This article should be..."
)

state = await graph.ainvoke(state)
Markdown(state["report"])

您还可以查看作者在执行过程中所采取的每一步操作。

INFO:docgen_agent.agent:Performing initial topic research.
INFO:docgen_agent.researcher:Calling model.
INFO:docgen_agent.researcher:Executing tool calls.
INFO:docgen_agent.researcher:Executing tool call: search_tavily
INFO:docgen_agent.tools:Searching the web using the Tavily API
INFO:docgen_agent.tools:Searching for query: AI Agents 2025 core features
INFO:docgen_agent.tools:Searching for query: Real-world applications of AI Agents in 2025
...
INFO:httpx:HTTP Request: POST https://api.tavily.com/search "HTTP/1.1 200 OK"
INFO:docgen_agent.researcher:Calling model.
INFO:docgen_agent.agent:Calling report planner.
INFO:docgen_agent.agent:Orchestrating the section authoring process.
INFO:docgen_agent.agent:Creating author agent for section: Introduction
INFO:docgen_agent.agent:Creating author agent for section: Autonomous Decision-Making
INFO:docgen_agent.agent:Creating author agent for section: Integration with Physical World
INFO:docgen_agent.agent:Creating author agent for section: Agentic AI Trends
INFO:docgen_agent.agent:Creating author agent for section: AI Agents in Customer Support
INFO:docgen_agent.agent:Creating author agent for section: AI Agents in Healthcare
INFO:docgen_agent.agent:Creating author agent for section: Conclusion
INFO:docgen_agent.agent:Throttling LLM calls.
INFO:docgen_agent.author:Writing section: Introduction
INFO:docgen_agent.author:Researching section: Autonomous Decision-Making
...

最终报告将以 Markdown 格式生成。我们提供了一份由该智能体生成的示例研究报告:示例标记输出

基础:状态管理和路由

在构建好智能体组件之后,让我们后退一步,探讨如何将 LangGraph 作为高级状态管理和流程控制的智能体框架,将三个组件整合为一个代理式 AI系统。LangGraph 具备多项关键优势:

  • 条件路由: 条件边缘可根据运行时条件实现动态流控制,使智能体能够对其后续操作做出智能决策。
  • 图形编译和执行: 编译后的图形可以异步调用,支持多智能体系统所必需的并发执行和复杂编排模式。

在以下 code/docgen_agent/agent.py 示例中,可以清楚地看出之前构建的组件分别对应哪个节点,以及连接各节点、传递中间输出的边是如何进行路由的。

main_workflow = StateGraph(AgentState)
main_workflow.add_node("topic_research", topic_research)
main_workflow.add_node("report_planner", report_planner)
main_workflow.add_node("section_author_orchestrator", section_author_orchestrator)
main_workflow.add_node("report_author", report_author)

main_workflow.add_edge(START, "topic_research")
main_workflow.add_edge("topic_research", "report_planner")
main_workflow.add_edge("report_planner", "section_author_orchestrator")
main_workflow.add_edge("section_author_orchestrator", "report_author")
main_workflow.add_edge("report_author", END)

恭喜!在本次开发者研讨会的每个步骤中,您已成功构建了自己的 LangGraph 智能体。现在可以使用 code/agent_client.ipynb Notebook 来测试您的新智能体。

总结

构建 AI 智能体需要掌握理论基础并理解实际应用中的挑战。本次研讨会系统地介绍了从基础概念到复杂代理式系统的完整路径,注重通过生产级工具与技术实现实践性学习。

完成本研讨会后,开发者将获得以下实践技能:

  • 智能体基本概念: 了解工作流与智能体之间的区别。
  • 状态管理: 实现复杂的状态转换和持久性。
  • 工具集成: 创建和管理外部工具功能。
  • 现代 AI 堆栈: 使用 LangGraph、NVIDIA NIM 和相关工具。

了解详情

欢迎在北京时间 9 月 16 日(星期二)上午 11 点观看我们的 Nemotron Labs 直播,主题为“在 OpenRouter 上使用 NVIDIA Nemotron 构建用于报告生成的 AI 智能体”,了解实战学习方法及相关实用技巧。

通过订阅 NVIDIA 新闻加入社区,以及在 LinkedInInstagramXFacebook 上关注 NVIDIA AI,及时了解 Agentic AI、Nemotron 等前沿动态。点击此处,探索丰富的自定进度视频教程与直播内容。

 

标签