> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Diffbot 集成

> 使用 LangChain Python 与 Diffbot 图谱进行集成。

> [Diffbot](https://docs.diffbot.com/docs/getting-started-with-diffbot) 是一套基于机器学习的产品，可轻松构建网络数据结构。
>
> Diffbot 的[自然语言处理 API](https://www.diffbot.com/products/natural-language/) 允许从非结构化文本数据中提取实体、关系和语义含义。
> [![在 Colab 中打开](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/langchain-ai/langchain/blob/v0.3/docs/docs/integrations/graphs/diffbot.ipynb)

## 用例

文本数据通常包含丰富的关系和洞察，可用于各种分析、推荐引擎或知识管理应用。

通过将 `Diffbot 的 NLP API` 与图数据库 `Neo4j` 结合，您可以基于从文本中提取的信息创建强大、动态的图结构。这些图结构完全可查询，并可集成到各种应用中。

这种组合允许以下用例：

* 从文本文档、网站或社交媒体源构建知识图谱（如 [Diffbot 的知识图谱](https://www.diffbot.com/products/knowledge-graph/)）。
* 基于数据中的语义关系生成推荐。
* 创建理解实体间关系的高级搜索功能。
* 构建允许用户探索数据中隐藏关系的分析仪表板。

## 概述

LangChain 提供了与图数据库交互的工具：

1. `从文本构建知识图谱`：使用图转换器和存储集成
2. `查询图数据库`：使用链进行查询创建和执行
3. `与图数据库交互`：使用代理进行健壮灵活的查询

## 设置

首先，获取所需包并设置环境变量：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU  langchain langchain-experimental langchain-openai langchain-neo4j neo4j wikipedia
```

### Diffbot NLP API

`Diffbot 的 NLP API` 是一个用于从非结构化文本数据中提取实体、关系和语义上下文的工具。
这些提取的信息可用于构建知识图谱。
要使用该 API，您需要从 [Diffbot 获取免费 API 令牌](https://app.diffbot.com/get-started/)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_experimental.graph_transformers.diffbot import DiffbotGraphTransformer

diffbot_api_key = "DIFFBOT_KEY"
diffbot_nlp = DiffbotGraphTransformer(diffbot_api_key=diffbot_api_key)
```

此代码获取关于“沃伦·巴菲特”的维基百科文章，然后使用 `DiffbotGraphTransformer` 提取实体和关系。
`DiffbotGraphTransformer` 输出结构化数据 `GraphDocument`，可用于填充图数据库。
请注意，由于 Diffbot 的[每次 API 请求字符限制](https://docs.diffbot.com/reference/introduction-to-natural-language-api)，因此避免了文本分块。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import WikipediaLoader

query = "Warren Buffett"
raw_documents = WikipediaLoader(query=query).load()
graph_documents = diffbot_nlp.convert_to_graph_documents(raw_documents)
```

## 将数据加载到知识图谱中

您需要一个正在运行的 Neo4j 实例。一个选项是在其 Aura 云服务中创建一个[免费的 Neo4j 数据库实例](https://neo4j.com/cloud/platform/aura-graph-database/)。您也可以使用 [Neo4j Desktop 应用程序](https://neo4j.com/download/)在本地运行数据库，或运行 docker 容器。您可以通过执行以下脚本运行本地 docker 容器：

```
docker run \
    --name neo4j \
    -p 7474:7474 -p 7687:7687 \
    -d \
    -e NEO4J_AUTH=neo4j/password \
    -e NEO4J_PLUGINS=\[\"apoc\"\]  \
    neo4j:latest
```

如果您使用的是 docker 容器，需要等待几秒钟让数据库启动。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_neo4j import Neo4jGraph

url = "bolt://localhost:7687"
username = "neo4j"
password = "password"

graph = Neo4jGraph(url=url, username=username, password=password)
```

可以使用 `add_graph_documents` 方法将 `GraphDocuments` 加载到知识图谱中。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
graph.add_graph_documents(graph_documents)
```

## 刷新图模式信息

如果数据库的模式发生变化，您可以刷新生成 Cypher 语句所需的模式信息

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
graph.refresh_schema()
```

## 查询图

我们现在可以使用图 Cypher QA 链向图提问。建议使用 **gpt-4** 来构建 Cypher 查询以获得最佳体验。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_neo4j import GraphCypherQAChain
from langchain_openai import ChatOpenAI

chain = GraphCypherQAChain.from_llm(
    cypher_llm=ChatOpenAI(temperature=0, model_name="gpt-4"),
    qa_llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo"),
    graph=graph,
    verbose=True,
    allow_dangerous_requests=True,
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
chain.run("Which university did Warren Buffett attend?")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 正在进入新的 GraphCypherQAChain 链...
生成的 Cypher：
MATCH (p:Person {name: "Warren Buffett"})-[:EDUCATED_AT]->(o:Organization)
RETURN o.name
完整上下文：
[{'o.name': 'New York Institute of Finance'}, {'o.name': 'Alice Deal Junior High School'}, {'o.name': 'Woodrow Wilson High School'}, {'o.name': 'University of Nebraska'}]

> 链已完成。
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'沃伦·巴菲特就读于内布拉斯加大学。'
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
chain.run("Who is or was working at Berkshire Hathaway?")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 正在进入新的 GraphCypherQAChain 链...
生成的 Cypher：
MATCH (p:Person)-[r:EMPLOYEE_OR_MEMBER_OF]->(o:Organization) WHERE o.name = 'Berkshire Hathaway' RETURN p.name
完整上下文：
[{'p.name': 'Charlie Munger'}, {'p.name': 'Oliver Chace'}, {'p.name': 'Howard Buffett'}, {'p.name': 'Howard'}, {'p.name': 'Susan Buffett'}, {'p.name': 'Warren Buffett'}]

> 链已完成。
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'查理·芒格、奥利弗·蔡斯、霍华德·巴菲特、苏珊·巴菲特和沃伦·巴菲特正在或曾在伯克希尔·哈撒韦工作。'
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/graphs/diffbot.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
