>Parallel 是一个专为 LLMs 和 AI 应用构建的实时网络搜索和内容提取平台。
ParallelExtractTool 调用 Parallel 的 提取 API,返回来自网页的简洁 Markdown 格式内容,可通过以下方式获得可选的聚焦摘录 search_objective。将其与 ParallelSearchTool 配对使用,构建搜索 → 提取管道。
概述
集成详情
| 类 | 包 | 可序列化 | JS 支持 | 包最新版本 |
|---|---|---|---|---|
ParallelExtractTool | langchain-parallel | ❌ | ❌ | <a href="https://pypi.org/project/langchain-parallel/" target="_blank"><img src="https://img.shields.io/pypi/v/langchain-parallel?style=flat-square&label=%20&color=orange" alt="PyPI - Latest version" noZoom height="100" class="rounded" /></a> |
设置
该集成位于 langchain-parallel package.
pip install -U langchain-parallel
uv add langchain-parallel
凭证
前往 Parallel 注册并生成 API 密钥。设置 PARALLEL_API_KEY 在您的环境中:
if not os.environ.get("PARALLEL_API_KEY"):
os.environ["PARALLEL_API_KEY"] = getpass.getpass("Parallel API key:\n")
实例化
from langchain_parallel import ParallelExtractTool
tool = ParallelExtractTool()
# Or pass an explicit key, override the base URL, or cap per-URL `full_content` size:
# tool = ParallelExtractTool(
# api_key="your-api-key",
# base_url="https://api.parallel.ai",
# max_chars_per_extract=5000,
# )
调用
直接使用参数调用
result = tool.invoke(
{"urls": ["https://en.wikipedia.org/wiki/Artificial_intelligence"]}
)
print(result[0]["title"])
print(result[0]["url"])
print(result[0]["content"][:200], "...")
单次调用多个 URL:
result = tool.invoke(
{
"urls": [
"https://en.wikipedia.org/wiki/Machine_learning",
"https://en.wikipedia.org/wiki/Deep_learning",
"https://en.wikipedia.org/wiki/Natural_language_processing",
]
}
)
for item in result:
print(item["title"], "—", item["url"])
使用 ToolCall 调用
使用模型生成的 ToolCall 调用返回 ToolMessage:
model_generated_tool_call = {
"args": {
"urls": [
"https://en.wikipedia.org/wiki/Climate_change",
"https://en.wikipedia.org/wiki/Renewable_energy",
]
},
"id": "call_123",
"name": tool.name, # "parallel_extract"
"type": "tool_call",
}
result = tool.invoke(model_generated_tool_call)
异步使用
async def extract_async():
return await tool.ainvoke(
{
"urls": [
"https://en.wikipedia.org/wiki/Python_(programming_language)",
"https://en.wikipedia.org/wiki/JavaScript",
]
}
)
result = await extract_async()
聚焦摘录
使用以下方式驱动摘录选择 search_objective (or search_queries)。设置 full_content=False 跳过完整的 Markdown 正文,仅返回匹配的摘录:
result = tool.invoke(
{
"urls": ["https://en.wikipedia.org/wiki/Artificial_intelligence"],
"search_objective": "What are the main applications and ethical concerns of AI?",
"excerpts": {"max_chars_per_result": 2000},
"full_content": False,
}
)
获取策略和完整内容大小
控制缓存、超时和每个 URL 的 full_content 限制独立设置:
result = tool.invoke(
{
"urls": ["https://en.wikipedia.org/wiki/Quantum_computing"],
"fetch_policy": {
"max_age_seconds": 86400,
"timeout_seconds": 60,
"disable_cache_fallback": False,
},
"full_content": {"max_chars_per_result": 5000},
}
)
每个 URL 的错误处理
失败的 URL 作为项目返回,其中 error_type 设置,因此部分成功是默认行为:
result = tool.invoke(
{
"urls": [
"https://en.wikipedia.org/wiki/Artificial_intelligence",
"https://this-domain-does-not-exist-12345.com/",
]
}
)
for item in result:
if "error_type" in item:
print("failed:", item["url"], "—", item["content"])
else:
print("ok:", item["url"], f"({len(item['content'])} chars)")
参数
必填
- -
urls:要提取的 URL 列表。
可选
- -
search_objective:驱动摘录选择的自然语言描述。 - -
search_queries:关键字字符串列表,与(或替代)search_objective. - -
excerpts:每个结果的摘录设置。传递ExcerptSettings(max_chars_per_result=…)(或字典)来控制每个结果的摘录大小;省略则使用 API 默认值。 - -
full_content:True返回完整 Markdown 内容(由工具级别的max_chars_per_extract),False跳过,或FullContentSettings(max_chars_per_result=…)进行细粒度控制。 - -
fetch_policy:缓存控制,例如{"max_age_seconds": 86400, "timeout_seconds": 60}. - -
max_chars_total: 所有 URL 的组合输出长度上限。 - -
client_model/session_id: 转发至 Parallel 用于下游归因。
链式调用
将工具绑定到任何支持工具调用的聊天模型,并使用 create_agent:
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
llm = init_chat_model(model="claude-opus-4-8")
agent = create_agent(model=llm, tools=[tool])
agent.invoke({"messages": [("human", "Summarize https://en.wikipedia.org/wiki/Quantum_computing")]})
搜索 → 提取
手动 ParallelSearchTool 和 ParallelExtractTool 到同一个代理。模型使用搜索来查找 URL,使用提取来深入了解其选择的页面。
from langchain_parallel import ParallelSearchTool
search = ParallelSearchTool()
extract = ParallelExtractTool()
agent = create_agent(model=llm, tools=[search, extract])
agent.invoke({
"messages": [
("human", "Find a recent peer-reviewed paper on net-energy-gain fusion and summarize it."),
]
})
响应格式
[
{
"url": "https://example.com/article",
"title": "Article Title",
"content": "# Article Title\n\nMain content formatted as markdown...",
"publish_date": "2026-01-15",
"excerpts": ["...", "..."], # if excerpts/search_objective requested
},
# Failed extractions:
{
"url": "https://failed-site.com",
"title": None,
"content": "Error: 404 Not Found",
"error_type": "http_error",
},
]
API 参考
有关详细文档,请参阅 ParallelExtractTool API 参考或 Parallel Extract 参考.