> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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. `Construct knowledge graphs from text` 使用图转换器和存储集成
2. `Query a graph database` 使用链进行查询创建和执行
3. `Interact with a graph database` 使用代理进行稳健和灵活的查询

## 设置

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

```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"}}
> Entering new GraphCypherQAChain chain...
Generated Cypher:
MATCH (p:Person {name: "Warren Buffett"})-[:EDUCATED_AT]->(o:Organization)
RETURN o.name
Full Context:
[{'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'}]

> Finished chain.
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'Warren Buffett attended the University of Nebraska.'
```

```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"}}
> Entering new GraphCypherQAChain chain...
Generated Cypher:
MATCH (p:Person)-[r:EMPLOYEE_OR_MEMBER_OF]->(o:Organization) WHERE o.name = 'Berkshire Hathaway' RETURN p.name
Full Context:
[{'p.name': 'Charlie Munger'}, {'p.name': 'Oliver Chace'}, {'p.name': 'Howard Buffett'}, {'p.name': 'Howard'}, {'p.name': 'Susan Buffett'}, {'p.name': 'Warren Buffett'}]

> Finished chain.
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'Charlie Munger, Oliver Chace, Howard Buffett, Susan Buffett, and Warren Buffett are or were working at Berkshire Hathaway.'
```

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\graphs\diffbot.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
