安全公告 — 恶意 litellm 版本 1.82.7 与 1.82.8 已从 PyPI 移除(存在 API 密钥外泄风险)。请卸载、轮换已暴露凭据,并升级至安全版本(如 1.82.9+)。运行 pip show litellm 以确认。 PyPI · README

构建你的第一个智能体

分步指南:构建研究型智能体。

构建你的第一个智能体

本指南分步演示如何构建一个真实可用的研究型智能体。

我们要构建什么

一个研究型智能体,能够:

  1. 接收研究主题
  2. 在网络上搜索信息
  3. 将发现综合为结构化报告

第 1 步:环境准备

bash
1pip install agenticx
2export OPENAI_API_KEY="your-key"

第 2 步:定义工具

python
1# tools.py
2from agenticx.tools import tool
3import httpx
4
5@tool
6def search_web(query: str) -> str:
7 """Search the web for information about a topic.
8
9 Args:
10 query: The search query
11
12 Returns:
13 Search results as text
14 """
15 # Replace with your preferred search API
16 response = httpx.get(
17 "https://api.search.com/search",
18 params={"q": query, "key": "your-api-key"}
19 )
20 return response.text
21
22@tool
23def fetch_page(url: str) -> str:
24 """Fetch the content of a web page.
25
26 Args:
27 url: The URL to fetch
28
29 Returns:
30 Page content as text
31 """
32 response = httpx.get(url, follow_redirects=True)
33 return response.text[:5000] # First 5000 chars

第 3 步:定义智能体

python
1# agent.py
2from agenticx import Agent
3
4research_agent = Agent(
5 id="research-agent",
6 name="Research Assistant",
7 role="Senior Research Analyst",
8 goal=(
9 "Conduct thorough research on any given topic. "
10 "Find authoritative sources, synthesize information, "
11 "and produce clear, well-structured reports."
12 ),
13 backstory=(
14 "You are an expert researcher with a background in "
15 "information synthesis and critical analysis."
16 ),
17 organization_id="my-research-org",
18 max_iter=15,
19 verbose=True
20)

第 4 步:创建并运行任务

python
1# main.py
2from agenticx import Task, AgentExecutor
3from agenticx.llms import OpenAIProvider
4from tools import search_web, fetch_page
5from agent import research_agent
6
7def research(topic: str) -> str:
8 task = Task(
9 id="research-task",
10 description=f"Research: {topic}",
11 expected_output=(
12 "A structured research report with:\n"
13 "1. Executive summary\n"
14 "2. Key findings\n"
15 "3. Detailed analysis\n"
16 "4. Sources and references"
17 )
18 )
19
20 llm = OpenAIProvider(model="gpt-4o")
21 executor = AgentExecutor(
22 agent=research_agent,
23 llm=llm,
24 tools=[search_web, fetch_page]
25 )
26
27 return executor.run(task)
28
29if __name__ == "__main__":
30 result = research("Multi-agent AI systems impact on software development")
31 print(result)

第 5 步:运行

bash
1python main.py

增强能力

添加记忆

python
1from agenticx.memory import MemoryManager
2
3memory = MemoryManager()
4executor = AgentExecutor(agent=research_agent, llm=llm, tools=[...], memory=memory)

添加可观测性

python
1from agenticx.observability import ConsoleTracer
2
3tracer = ConsoleTracer()
4executor = AgentExecutor(agent=research_agent, llm=llm, tools=[...], tracer=tracer)

使用 CLI

bash
1agx project create research-bot --template basic
2cd research-bot
3agx run agent.py --verbose

下一步