自动化您的报告流程:使用crewai和neo4j实施动态报告生成代理
使用 crewAI 和 Neo4j 自动构建动态数据驱动报告
我喜欢使用 LLM 的 agentic 流程。它们不仅可以实现更高级的 Text2Cypher 实现,还为 各种语义层实现 打开了大门。这是一种非常强大且通用的方法。
在这篇博文中,我着手实现一种不同的 agent。与通常的问答用例不同,此 agent 旨在生成关于给定位置的特定行业的详细报告。该实现利用了 crewAI,这是一个使开发人员能够轻松编排 AI 代理的平台。
组成一个团队的三个代理
该系统编排了三个协同工作的代理,以提供一份全面的商业报告:
- 数据研究员代理:专门为给定城市的组织收集和分析特定行业的数据,提供关于公司数量、上市公司、总收入和表现最佳的组织的信息
- 新闻分析师代理:侧重于提取和总结有关相关公司的最新新闻,提供趋势、市场动向和情绪分析的快照
- 报告撰写代理:将研究和新闻见解综合成一个结构良好、可操作的 markdown 报告,确保清晰度和准确性,且不添加任何不支持的信息
这些代理共同构成了一个流程,用于生成针对特定位置的见解性行业报告。
代码可在 GitHub 上找到。
数据集
我们将使用 Neo4j 演示服务器上提供的 companies 数据库,其中包括有关组织、个人甚至其中一些组织的最新新闻的详细信息。这些数据是通过 Diffbot API 获取的。
图模式
该数据集侧重于投资者、董事会成员和相关方面等细节,使其成为演示行业报告生成的绝佳资源。
## Neo4j connection setup
URI = "neo4j+s://demo.neo4jlabs.com"
AUTH = ("companies", "companies")
driver = GraphDatabase.driver(URI, auth=AUTH)
接下来,我们需要定义 OpenAI 密钥,因为我们将在整个博文中都使用 GPT-4o:
## Set your OpenAI API key
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI key: ")
llm = LLM(model='gpt-4o', temperature=0)
基于知识图谱的工具
我们将首先实现一些工具,使 agent/LLM 能够从数据库中检索相关信息。第一个工具将侧重于获取给定城市特定行业内公司的关键统计数据:
industry_options = ["Software Companies", "Professional Service Companies", "Enterprise Software Companies", "Manufacturing Companies", "Software As A Service Companies", "Computer Hardware Companies", "Media And Information Companies", "Financial Services Companies", "Artificial Intelligence Companies", "Advertising Companies"]
class GetCityInfoInput(BaseModel):
"""Input schema for MyCustomTool."""
city: str = Field(..., description="City name")
industry: str = Field(..., description=f"Industry name, available options are: {industry_options}")
class GetCityInfo(BaseTool):
name: str = "Get information about a specific city"
description: str = "You can use this tools when you want to find information about specific industry within a city."
args_schema: Type[BaseModel] = GetCityInfoInput
def _run(self, city: str, industry: str) -> str:
data, _, _ = driver.execute_query("""MATCH (c:City)<-[:IN_CITY]-(o:Organization)-[:HAS_CATEGORY]->(i:IndustryCategory)
WHERE c.name = $city AND i.name = $industry
WITH o
ORDER BY o.nbrEmployees DESC
RETURN count(o) AS organizationCount,
sum(CASE WHEN o.isPublic THEN 1 ELSE 0 END) AS publicCompanies,
sum(o.revenue) AS combinedRevenue,
collect(CASE WHEN o.nbrEmployees IS NOT NULL THEN o END)[..5] AS topFiveOrganizations""", city=city, industry=industry)
return [el.data() for el in data]
GetCityInfo 工具检索关于给定城市特定行业内公司的关键统计数据。它提供的信息包括组织总数、上市公司数量、总收入以及按员工人数排名前五的组织的详细信息。此工具可以扩展,但为了我们的目的,我保持了它的简单性。
第二个工具可用于获取有关给定公司的最新信息:
class GetNews(BaseTool):
name: str = "Get the latest news for a specific company"
description: str = "You can use this tool when you want to find the latest news about specific company"
def _run(self, company: str) -> str:
data, _, _ = driver.execute_query("""MATCH (c:Chunk)<-[:HAS_CHUNK]-(a:Article)-[:MENTIONS]->(o:Organization)
WHERE o.name = $company AND a.date IS NOT NULL
WITH c, a
ORDER BY a.date DESC
LIMIT 5
RETURN a.title AS title, a.date AS date, a.sentiment AS sentiment, collect(c.text) AS chunks""", company=company)
return [el.data() for el in data]
GetNews 工具检索有关特定公司的最新新闻。它提供文章标题、发布日期、情绪分析以及文章关键摘录等详细信息。此工具非常适合了解与特定组织相关的最新发展和市场趋势,从而使我们能够生成更详细的摘要。
Agents
如前所述,我们将实现三个 Agent。使用 crewAI,只需最少的 Prompt Engineering,因为平台会处理其余部分。
Agent 的实现如下:
## Define Agents
class ReportAgents:
def __init__(self):
self.researcher = Agent(
role='Data Researcher',
goal='Gather comprehensive information about specific companies that are in relevant cities and industries',
backstory="""You are an expert data researcher with deep knowledge of
business ecosystems and city demographics. You excel at analyzing
complex data relationships.""",
verbose=True,
allow_delegation=False,
tools=[GetCityInfo()],
llm=llm
)
self.news_analyst = Agent(
role='News Analyst',
goal='Find and analyze recent news about relevant companies in the specified industry and city',
backstory="""You are a seasoned news analyst with expertise in
business journalism and market research. You can identify key trends
and developments from news articles.""",
verbose=True,
allow_delegation=False,
tools=[GetNews()],
llm=llm
)
self.report_writer = Agent(
role='Report Writer',
goal='Create comprehensive, well-structured reports combining the provided research and news analysis. Do not include any information that isnt explicitly provided.',
backstory="""You are a professional report writer with experience in
business intelligence and market analysis. You excel at synthesizing
information into clear, actionable insights. Do not include any information that isn't explicitly provided.""",
verbose=True,
allow_delegation=False,
llm=llm
)
在 crewAI 中,Agent 通过指定其角色、目标和背景故事来定义,并可选择使用工具来增强其能力。在此设置中,实现了三个 Agent:一个负责收集有关特定城市和行业公司详细信息的 Data Researcher,使用 GetCityInfo
工具;一个负责分析有关相关公司最新新闻的 News Analyst,使用 GetNews
工具;以及一个 Report Writer,它将收集的信息和新闻综合成结构化、可操作的报告,无需依赖外部工具。这种对角色和目标的明确定义确保了 Agent 之间的有效协作。
Tasks
除了定义 Agent,我们还需要概述他们将要处理的任务。在这种情况下,我们将定义三个不同的任务:
## Define Tasks
city_research_task = Task(
description=f"""Research and analyze {city_name} and its business ecosystem in {industry_name} industry:
1. Get city summary and key information
2. Find organizations in the specified industry
3. Analyze business relationships and economic indicators""",
agent=agents.researcher,
expected_output="Basic statistics about the companies in the given city and industry as well as top performers"
)
news_analysis_task = Task(
description=f"""Analyze recent news about the companies provided by the city researcher""",
agent=agents.news_analyst,
expected_output="Summarization of the latest news for the company and how it might affect the market",
context=[city_research_task]
)
report_writing_task = Task(
description=f"""Create a detailed markdown report about the
results you got from city research and news analysis tasks.
Do not include any information that isn't provided""",
agent=agents.report_writer,
expected_output="Markdown summary",
context=[city_research_task, news_analysis_task]
)
这些任务旨在与 Agent 的能力保持一致。City Research Task 侧重于分析特定城市和行业的商业生态系统,收集关键统计数据并确定表现最佳的组织,由 Data Researcher 处理。News Analysis Task 检查与这些公司相关的最新发展,总结关键趋势和市场影响,使用 City Research 的输出,由 News Analyst 执行。最后,Report Writing Task 将之前任务的结果综合成一份全面的 Markdown 报告,由 Report Writer 完成。
最后,我们只需要把它们放在一起:
## Create and run the crew
crew = Crew(
agents=[agents.researcher, agents.news_analyst, agents.report_writer],
tasks=[city_research_task, news_analysis_task, report_writing_task],
verbose=True,
process=Process.sequential,
)
让我们来测试一下!
city = "Seattle"
industry = "Hardware Companies"
report = generate_report(city, industry)
print(report)
Agent 的中间步骤过于详细,无法在此处包含,但该过程本质上是从收集指定行业的关键统计数据并识别相关公司开始的,然后检索有关这些公司的最新新闻。
结果:
## Seattle Computer Hardware Industry Report
### Overview
The Computer Hardware Companies industry in Seattle comprises 24 organizations, including 4 public companies. The combined revenue of these companies is approximately $229.14 billion. This report highlights the top performers in this industry and recent news developments affecting them.
### Top Performers
1. **Microsoft Corporation**
- **Revenue**: $198.27 billion
- **Employees**: 221,000
- **Status**: Public Company
- **Mission**: To empower every person and organization on the planet to achieve more.
2. **Nvidia Corporation**
- **Revenue**: $26.97 billion
- **Employees**: 26,196
- **Status**: Public Company
- **Formerly Known As**: Mellanox Technologies and Cumulus Networks
3. **F5 Networks**
- **Revenue**: $2.695 billion
- **Employees**: 7,089
- **Status**: Public Company
- **Focus**: Multi-cloud cybersecurity and application delivery
4. **Quest Software**
- **Revenue**: $857.415 million
- **Employees**: 4,055
- **Status**: Public Company
- **Base**: California
5. **SonicWall**
- **Revenue**: $310 million
- **Employees**: 1,600
- **Status**: Private Company
- **Focus**: Cybersecurity
These companies significantly contribute to Seattle's economic landscape, driving growth and innovation in the hardware industry.
近期新闻与发展
-
Microsoft Corporation: 其收购 Activision Blizzard 面临法律挑战,这可能会影响其游戏市场战略。
-
Nvidia Corporation: 在中国市场对 GPU 的需求强劲,突显了其在人工智能发展中的关键作用,并可能提升其市场地位。
-
F5 Networks: 其网络安全解决方案获得了认可,提升了其行业声誉。
-
Quest Software: 推出新的数据智能平台,旨在改善数据可访问性和人工智能模型开发。
-
SonicWall: 经历领导层变动并发布威胁报告,强调其对网络安全增长和挑战的关注。
这些发展态势将影响行业内的市场动态、投资者认知和竞争策略。
请注意,演示数据集已过时;我们不会定期导入新闻。
总结
使用 agentic flows、Neo4j 和 crewAI 构建自动化报告生成管道,展示了 LLM 如何超越简单的问答交互。通过为一组代理分配专门的任务,并为他们配备正确的工具,我们可以编排一个动态的、探索性的工作流程,该流程提取相关数据、处理数据并撰写结构良好的见解。
通过这种方法,代理协同工作,以揭示有关给定城市某个行业的主要统计数据,收集最新新闻,并将所有内容综合成一份精美的 markdown 报告。这表明 LLM 可以部署在创造性的、多步骤的过程中,从而实现更复杂的用例,例如自动化商业智能、数据驱动的内容创建等。
代码可在 GitHub 上找到。