上个月,谷歌宣布了数据库用生成式人工智能(Gen AI)Toolbox的公开测试版发布。今天,我们非常高兴地宣布,工具箱现已支持Neo4j,这是一种图数据库管理系统。Neo4j一直是工具箱支持与开发过程中的重要合作伙伴。其加入使得使用Neo4j的开发者和组织能够创建代理工具,这些工具可以利用广受欢迎的图关系感知生成(GraphRAG)技术来访问数据,从而拓展了工具箱在生成式人工智能应用中的实用性。
Neo4j — 图数据库管理系统
Neo4j是领先的开源图数据库,它管理信息的方式不是使用表格,而是使用实体(节点)及其之间的关系,从而实现对连接信息的灵活且强大的表示。图数据库为许多领域(如生物医学、供应链、制造业、欺诈检测和运输物流等)增添了独特的功能。知识图谱,你可以将其视为组织的数字孪生(包括人员、流程、产品、合作伙伴等),是大型语言模型(LLM)语言技能的强大“事实记忆”伴侣。使用图关系感知生成(GraphRAG)技术可以提高生成式人工智能(GenAI)应用的准确性、可靠性和可解释性。
Gen AI Toolbox
在创建访问数据库的工具时,开发人员在生产环境中构建人工智能代理时会迅速遇到一系列挑战:身份验证、授权、数据清理、连接池管理等。这些挑战可能成为负担,减慢开发速度,并且在反复实现相同的样板代码时容易出错。
于是,Gen AI Toolbox——这是一个开源服务器,可以帮助你更轻松、更快速、更安全地开发工具。工具箱位于你的应用程序和数据库之间,处理连接池管理、身份验证等复杂性。工具箱使你能够在集中位置定义工具,并仅需不到3行代码即可与你的代理集成。
from toolbox_langchain import ToolboxClient
# update the url to point to your server
client = ToolboxClient("http://127.0.0.1:5000")
# these tools can be passed to your agent!
tools = await client.aload_toolset()
使用 Toolbox 访问 Neo4j
安装 Toolbox 服务器后,你就可以配置你的 tools.yaml 文件了。要创建一个查询 Neo4j 的工具,你需要配置两项内容:
Neo4j 源,它定义了如何连接到你的 Neo4j 实例并进行身份验证。以下是我们示例中的 Neo4j 源:
sources:
companies-graph:
kind: "neo4j"
uri: "neo4j+s://demo.neo4jlabs.com"
user: "companies"
password: "companies"
database: "companies"
一旦定义了源,接下来就是定义工具。neo4j-cypher 工具定义了当工具被你的代理调用时应该运行的 Cypher 查询。工具可以执行你可以在 Cypher 语句中表达的任何操作,包括读取或写入!为了使其能够在代理设置中使用,非常重要的一点是,要详细描述工具、参数和结果,以便大型语言模型(LLM)能够判断其适用性。
以下是我们示例中工具配置的一部分,包括源、语句、描述和参数:
tools:
articles_in_month:
kind: neo4j-cypher
source: companies-graph
statement: |
MATCH (a:Article)
WHERE date($date) <= date(a.date) < date($date) + duration('P1M')
RETURN a.id as article_id, a.author as author, a.title as title, toString(a.date) as date, a.sentiment as sentiment
LIMIT 25
description: List of Articles (article_id, author, title, date, sentiment) in a month timeframe from the given date
parameters:
- name: date
type: string
description: Start date in yyyy-mm-dd format
companies_in_articles:
kind: neo4j-cypher
source: companies-graph
statement: |
MATCH (a:Article)-[:MENTIONS]->(c)
WHERE a.id = $article_id AND not exists { (c)<-[:HAS_SUBSIDARY]-() }
RETURN c.id as company_id, c.name as name, c.summary as summary
description: Companies (company_id, name, summary) mentioned in articles by article id
parameters:
- name: article_id
type: string
description: Article id to find companies mentioned in
投资研究代理
这是一个代理式LangChain应用的演示,该应用使用的工具结合了GraphRAG模式,实现了全文搜索和图搜索的结合。此示例展示了一个投资研究代理,可用于查找有关公司、其投资者、竞争对手、合作伙伴和行业的最新新闻。它利用的是从diffbot知识图中导入到Neo4j的数据。通过一组基本实体之间的关系,它捕捉到了该领域的复杂性。
探索公共图数据集
该数据集是一个关于公司、相关行业、在这些公司工作或投资的人员,以及报道这些公司的文章的图数据集。
新闻文章被分割成多个块,并且这些块也存储在图中。
该数据库以只读用户身份公开可用,你可以通过以下URL探索数据:https://demo.neo4jlabs.com:7473/browser/。URI: neo4j+s://demo.neo4jlabs.com。用户: companies。密码: companies。数据库名: companies。
在我们的配置中,我们提供了利用全文索引的工具,以及执行图检索查询的工具,这些查询可以获取以下附加信息:
我们利用与Vertex AI的代理式LangChain集成,这使我们能够自动将我们向Toolbox注册的工具传递给大型语言模型(LLM)以供工具调用。我们将同时使用混合搜索和GraphRAG检索器。
将我们的代理连接到Toolbox
现在,我们可以使用带有Vertex AI Gemini 2.0 Flash模型的LangChain,将我们的工具定义提供给模型,并进行快速测试。我们可以遵循Toolbox文档中的快速入门示例。
import asyncio
import os
from langgraph.prebuilt import create_react_agent
from langchain_google_vertexai import ChatVertexAI
from langgraph.checkpoint.memory import MemorySaver
from toolbox_langchain import ToolboxClient
prompt = """
You're a helpful investment research assistant.
You can use the provided tools to search for companies,
people at companies, industries, and news articles from 2023.
Don't ask for confirmations from the user.
User:
"""
queries = [
"What industries deal with computer manufacturing?",
"List 5 companies in the computer manufacturing industry with their description.",
]
def main():
model = ChatVertexAI(model_name="gemini-1.5-pro")
client = ToolboxClient("http://127.0.0.1:5000")
tools = client.load_toolset()
agent = create_react_agent(model, tools, checkpointer=MemorySaver())
config = {"configurable": {"thread_id": "thread-1"}}
for query in queries:
inputs = {"messages": [("user", prompt + query)]}
response = agent.invoke(inputs, stream_mode="values", config=config)
print(response["messages"][-1].content)
main()
The industries that deal with computer manufacturing are: Computer Hardware Companies, Electronic Products Manufacturers, and Computer Storage Companies.with computer manufacturing are: Computer Hardware Companies, Electronic Products Manufacturers, and Computer Storage Companies.
Here are 5 companies in the computer hardware industry along with their descriptions:
1. **Microsoft Egypt:** Microsoft branch in Egypt
2. **Apigee:** Software company based in San Jose, California, United States and owned by Google
3. **Microsemi:** Communications corporation
4. **Intermec:** American electronics manufacturing company
5. **Elitegroup Computer Systems:** No summary available
总结
在本文中,我们演示了如何轻松构建代理式应用程序,将基于数据库的工具与谷歌的Gen AI Toolbox集成。虽然我们关注的是Neo4j知识图中的GraphRAG用例,但你可以在同一个代理式应用程序中结合不同的数据源和数据库类型。