跳至主要内容

Diffbot

Diffbot 是一套基于机器学习的产品,可以轻松地构建网页数据。

Diffbot 的 Extract API 是一项服务,它可以结构化和规范化来自网页的数据。

与传统的网页抓取工具不同,Diffbot Extract 不需要任何规则来读取页面上的内容。它使用计算机视觉模型将页面分类为 20 种可能的类型之一,然后将原始 HTML 标记转换为 JSON。生成的结构化 JSON 遵循一致的 基于类型的本体,这使得使用相同的模式从多个不同的网络来源中提取数据变得容易。

Open In Colab

概述

本指南介绍如何使用 Diffbot Extract API 从 URL 列表中提取数据到结构化的 JSON 中,我们可以将其用于下游处理。

设置

首先安装所需的软件包。

%pip install --upgrade --quiet langchain-community

Diffbot 的 Extract API 需要 API 令牌。按照以下说明 获取免费 API 令牌,然后设置环境变量。

%env DIFFBOT_API_TOKEN REPLACE_WITH_YOUR_TOKEN

使用文档加载器

导入 DiffbotLoader 模块并使用 URL 列表和 Diffbot 令牌实例化它。

import os

from langchain_community.document_loaders import DiffbotLoader

urls = [
"https://python.langchain.ac.cn/",
]

loader = DiffbotLoader(urls=urls, api_token=os.environ.get("DIFFBOT_API_TOKEN"))
API 参考:DiffbotLoader

使用 .load() 方法,您可以看到已加载的文档

loader.load()
[Document(page_content="LangChain is a framework for developing applications powered by large language models (LLMs).\nLangChain simplifies every stage of the LLM application lifecycle:\nDevelopment: Build your applications using LangChain's open-source building blocks and components. Hit the ground running using third-party integrations and Templates.\nProductionization: Use LangSmith to inspect, monitor and evaluate your chains, so that you can continuously optimize and deploy with confidence.\nDeployment: Turn any chain into an API with LangServe.\nlangchain-core: Base abstractions and LangChain Expression Language.\nlangchain-community: Third party integrations.\nPartner packages (e.g. langchain-openai, langchain-anthropic, etc.): Some integrations have been further split into their own lightweight packages that only depend on langchain-core.\nlangchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture.\nlanggraph: Build robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph.\nlangserve: Deploy LangChain chains as REST APIs.\nThe broader ecosystem includes:\nLangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications and seamlessly integrates with LangChain.\nGet started\nWe recommend following our Quickstart guide to familiarize yourself with the framework by building your first LangChain application.\nSee here for instructions on how to install LangChain, set up your environment, and start building.\nnote\nThese docs focus on the Python LangChain library. Head here for docs on the JavaScript LangChain library.\nUse cases\nIf you're looking to build something specific or are more of a hands-on learner, check out our use-cases. They're walkthroughs and techniques for common end-to-end tasks, such as:\nQuestion answering with RAG\nExtracting structured output\nChatbots\nand more!\nExpression Language\nLangChain Expression Language (LCEL) is the foundation of many of LangChain's components, and is a declarative way to compose chains. LCEL was designed from day 1 to support putting prototypes in production, with no code changes, from the simplest “prompt + LLM” chain to the most complex chains.\nGet started: LCEL and its benefits\nRunnable interface: The standard interface for LCEL objects\nPrimitives: More on the primitives LCEL includes\nand more!\nEcosystem\n🦜🛠️ LangSmith\nTrace and evaluate your language model applications and intelligent agents to help you move from prototype to production.\n🦜🕸️ LangGraph\nBuild stateful, multi-actor applications with LLMs, built on top of (and intended to be used with) LangChain primitives.\n🦜🏓 LangServe\nDeploy LangChain runnables and chains as REST APIs.\nSecurity\nRead up on our Security best practices to make sure you're developing safely with LangChain.\nAdditional resources\nComponents\nLangChain provides standard, extendable interfaces and integrations for many different components, including:\nIntegrations\nLangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it. Check out our growing list of integrations.\nGuides\nBest practices for developing with LangChain.\nAPI reference\nHead to the reference section for full documentation of all classes and methods in the LangChain and LangChain Experimental Python packages.\nContributing\nCheck out the developer's guide for guidelines on contributing and help getting your dev environment set up.\nHelp us out by providing feedback on this documentation page:", metadata={'source': 'https://python.langchain.ac.cn/'})]

将提取的文本转换为图文档

结构化页面内容可以通过 DiffbotGraphTransformer 进一步处理,以将实体和关系提取到图中。

%pip install --upgrade --quiet langchain-experimental
from langchain_experimental.graph_transformers.diffbot import DiffbotGraphTransformer

diffbot_nlp = DiffbotGraphTransformer(
diffbot_api_key=os.environ.get("DIFFBOT_API_TOKEN")
)
graph_documents = diffbot_nlp.convert_to_graph_documents(loader.load())

要继续将数据加载到知识图中,请按照 DiffbotGraphTransformer 指南 进行操作。


此页面是否有帮助?


您也可以在 GitHub 上留下详细的反馈 on GitHub.