构建你的第一个智能体
分步指南:构建研究型智能体。
构建你的第一个智能体
本指南分步演示如何构建一个真实可用的研究型智能体。
我们要构建什么
一个研究型智能体,能够:
- 接收研究主题
- 在网络上搜索信息
- 将发现综合为结构化报告
第 1 步:环境准备
bash
1pip install agenticx2export OPENAI_API_KEY="your-key"
第 2 步:定义工具
python
1# tools.py2from agenticx.tools import tool3import httpx45@tool6def search_web(query: str) -> str:7 """Search the web for information about a topic.89 Args:10 query: The search query1112 Returns:13 Search results as text14 """15 # Replace with your preferred search API16 response = httpx.get(17 "https://api.search.com/search",18 params={"q": query, "key": "your-api-key"}19 )20 return response.text2122@tool23def fetch_page(url: str) -> str:24 """Fetch the content of a web page.2526 Args:27 url: The URL to fetch2829 Returns:30 Page content as text31 """32 response = httpx.get(url, follow_redirects=True)33 return response.text[:5000] # First 5000 chars
第 3 步:定义智能体
python
1# agent.py2from agenticx import Agent34research_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=True20)
第 4 步:创建并运行任务
python
1# main.py2from agenticx import Task, AgentExecutor3from agenticx.llms import OpenAIProvider4from tools import search_web, fetch_page5from agent import research_agent67def 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 )1920 llm = OpenAIProvider(model="gpt-4o")21 executor = AgentExecutor(22 agent=research_agent,23 llm=llm,24 tools=[search_web, fetch_page]25 )2627 return executor.run(task)2829if __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 MemoryManager23memory = MemoryManager()4executor = AgentExecutor(agent=research_agent, llm=llm, tools=[...], memory=memory)
添加可观测性
python
1from agenticx.observability import ConsoleTracer23tracer = ConsoleTracer()4executor = AgentExecutor(agent=research_agent, llm=llm, tools=[...], tracer=tracer)
使用 CLI
bash
1agx project create research-bot --template basic2cd research-bot3agx run agent.py --verbose