Skip to main content
Nimble 的 Extract API 通过无头浏览器浏览指定 URL 来提取渲染后的内容,而非依赖缓存或受 API 限制的数据。该检索器可处理 JavaScript 渲染、动态内容和复杂的导航流程,适用于需要访问特定网页的 RAG 应用,包括分页、过滤器和客户端渲染背后的内容。
我们可以将其用作检索器。本文将展示该集成的特定功能。阅读完毕后,建议探索相关用例页面,以了解如何将此检索器作为更大链的一部分使用。

安装

pip install -U langchain-nimble
我们还需要设置 Nimble API 密钥。你可以在 Nimble 注册后获取 API 密钥。
import getpass
import os

if not os.environ.get("NIMBLE_API_KEY"):
    os.environ["NIMBLE_API_KEY"] = getpass.getpass("Nimble API key:\n")

使用

现在可以实例化我们的检索器:
from langchain_nimble import NimbleExtractRetriever

# 基础检索器 - 需要提供要提取的 URL
retriever = NimbleExtractRetriever()

在链中使用

我们可以轻松地将此检索器集成到 RAG 链中,用于提取和分析特定网页内容:
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI

# 创建 RAG 提示
prompt = ChatPromptTemplate.from_template(
    """Analyze the extracted content from the provided URLs.
Answer the question based only on the extracted content.
If you cannot answer based on the content, say so.

Content: {content}

Question: {question}

Answer:"""
)

llm = ChatOpenAI(model="gpt-4o-mini")

# 配置内容提取检索器
retriever = NimbleExtractRetriever(
    parsing_type="markdown",
    wait=3000
)


def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)


# 示例:从 LangChain 文档中提取并分析内容
urls = [
    "https://python.langchain.com/docs/concepts/retrievers/",
    "https://python.langchain.com/docs/concepts/tools/",
    "https://python.langchain.com/docs/tutorials/agents/"
]

# 创建一个将 URL 传递给检索器的自定义可运行对象
def get_docs(question):
    # 在实际场景中,URL 可能由前置步骤确定
    return retriever.invoke(urls)


# 构建 RAG 链
chain = (
    {
        "content": lambda _: get_docs(_) | format_docs,
        "question": RunnablePassthrough()
    }
    | prompt
    | llm
    | StrOutputParser()
)
# 对提取的内容提问
response = chain.invoke("What are the key differences between retrievers and tools in LangChain?")
print(response)
Based on the extracted LangChain documentation, here are the key differences:

**Retrievers:**
- Interface for document retrieval based on unstructured queries
- Primary use case is RAG (Retrieval Augmented Generation)
- Returns documents from various sources like vector stores
- Focuses on semantic search and information retrieval
- Core component for question-answering systems

**Tools:**
- Interface for agents to interact with external systems
- Enables actions beyond text generation (API calls, calculations, web search)
- Used by agents to extend capabilities dynamically
- Supports both synchronous and asynchronous execution
- Can be chained together for complex workflows

**Agents:**
- High-level orchestrators that use tools to accomplish tasks
- Make decisions about which tools to use and when
- Can combine multiple tools to solve complex problems
- Tutorial shows how to build agent workflows with tool integration

The documentation emphasizes that retrievers are specialized for information retrieval, while tools provide broader action capabilities for agents.

高级配置

该检索器支持对 URL 提取进行广泛配置:
参数类型默认值说明
parsing_typestr”plain_text”输出格式:“plain_text”、“markdown” 或 “simplified_html”
driverstr”vx6”浏览器驱动版本:“vx6”(快速)、“vx8”(均衡)或 “vx10”(全面)
waitintNone等待页面加载的毫秒数(0-60000)
renderboolTrue启用 JavaScript 渲染
localestr”en”页面语言区域偏好(如 “en-US”)
countrystr”US”本地化内容的国家代码(如 “US”)
api_keystr环境变量Nimble API 密钥(默认读取 NIMBLE_API_KEY 环境变量)
高级配置示例:
from langchain_nimble import NimbleExtractRetriever

# 针对 JavaScript 密集型文档网站优化的检索器
retriever = NimbleExtractRetriever(
    parsing_type="markdown",
    driver="vx10",  # 对复杂 SPA 使用全面驱动
    wait=5000,  # 等待最多 5 秒以完整渲染页面
    render=True,  # 启用 JavaScript 渲染
    locale="en-US",
    country="US"
)

# 从特定 LangChain 文档页面提取内容
docs = retriever.invoke([
    "https://python.langchain.com/docs/concepts/chat_models/",
    "https://python.langchain.com/docs/concepts/prompts/"
])

最佳实践

驱动程序选择

  • vx6(默认):适用于标准网站的快速提取
  • vx8:适用于中等复杂度网站的均衡性能
  • vx10:适用于 JavaScript 密集型 SPA 和复杂动态内容的全面渲染

页面加载配置

  • 不等待wait=None):适用于大多数现代网站的默认设置
  • 短暂等待wait=1000-2000):适用于懒加载或延迟内容的页面
  • 较长等待wait=5000+):适用于加载缓慢的 SPA 或需要时间完整渲染的重度 JavaScript 页面

输出格式选择

  • 纯文本(默认):快速提取原始文本内容
  • Markdown:最适合 RAG——保留带标题、列表和代码块的结构
  • HTML:需要保留详细样式或结构信息时使用

性能优化

  1. 调整等待时间:仅在必要时使用——快速网站不需要等待时间
  2. 批量处理相关 URL:并行提取同一域名下的多个页面
  3. 选择合适格式:RAG 使用 Markdown,简单处理使用纯文本
  4. 使用异步:利用 ainvoke() 进行并发 URL 提取
  5. 验证内容:在处理前确认页面加载成功

API 参考

有关所有 NimbleExtractRetriever 功能和配置的详细文档,请访问 Nimble API 文档