以编程方式使用文档

>Parallel 是一个专为 LLM 和 AI 应用构建的实时网络搜索和内容提取平台。

ParallelSearchTool 调用 Parallel 的 搜索 API,它将传统的搜索 → 抓取 → 提取流程合并为一次调用,并返回结构化的、针对 LLM 优化的摘录。

概览

集成详情

可序列化JS 支持包最新版本
ParallelSearchToollangchain-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 ParallelSearchTool

tool = ParallelSearchTool()

# Or pass an explicit key / override the base URL:
# tool = ParallelSearchTool(
#     api_key="your-api-key",
#     base_url="https://api.parallel.ai",  # default
# )

调用

直接使用参数调用

该工具需要 search_queries (一个或多个关键字字符串)。将其与 objective 配对以获得更丰富的相关性排名,并根据需要添加域名过滤、抓取策略和其他设置。

result = tool.invoke(
    {
        "search_queries": [
            "AI breakthroughs 2026",
            "machine learning advances",
            "generative AI news",
        ],
        "objective": "What are the latest developments in AI?",
        "max_results": 8,
        "excerpts": {"max_chars_per_result": 2000},
        "mode": "advanced",
        "source_policy": {
            "include_domains": ["arxiv.org", "nature.com"],
            "exclude_domains": ["reddit.com", "twitter.com"],
        },
        "fetch_policy": {
            "max_age_seconds": 86400,
            "timeout_seconds": 60,
        },
        "include_metadata": True,
    }
)

print(f"Found {len(result['results'])} results")
for r in result["results"][:3]:
    print(r["title"], "—", r["url"])
Found 8 results
Latest AI Developments 2026 — https://arxiv.org/abs/...
...

mode="basic" 是低延迟设置; mode="advanced" 运行更高质量的搜索。

使用 ToolCall 调用

使用模型生成的 ToolCall 返回 ToolMessage:

model_generated_tool_call = {
    "args": {
        "search_queries": [
            "climate change initiatives",
            "global climate policy 2026",
        ],
        "objective": "Find recent news about climate change initiatives",
        "max_results": 3,
        "source_policy": {
            "include_domains": ["ipcc.ch", "unfccc.int", "nature.com"],
        },
        "include_metadata": True,
    },
    "id": "call_123",
    "name": tool.name,  # "parallel_web_search"
    "type": "tool_call",
}

result = tool.invoke(model_generated_tool_call)

异步用法

async def search_async():
    return await tool.ainvoke(
        {
            "search_queries": ["quantum computing breakthroughs"],
            "objective": "Latest quantum computing breakthroughs",
            "max_results": 5,
        }
    )

result = await search_async()

参数

必需

  • - search_queries:关键字字符串列表(每个 3-6 个单词效果最佳)。

可选

  • - objective:检索目标的自然语言描述。
  • - max_results:返回结果数量(默认为 10)。
  • - excerpts:每个结果的摘录设置,例如 {"max_chars_per_result": 1500}.
  • - mode: "basic" (更低的延迟)或 "advanced" (更高质量)。
  • - source_policy:域名过滤。接受 SourcePolicy pydantic 模型或包含 include_domains / exclude_domains / after_date.
  • - fetch_policy:缓存控制,例如 {"max_age_seconds": 86400, "timeout_seconds": 60}.
  • - max_chars_total:所有结果中摘录的总长度上限。
  • - client_model / session_id / location:转发给 Parallel 用于下游归属和个性化。
  • - include_metadata:在响应中包含客户端时间(默认为 True).
  • - timeout:每次请求的超时时间(秒)。

SourcePolicy pydantic 模型

SourcePolicy 镜像 API 的 include_domains / exclude_domains / after_date:用于类型安全;也接受原始字典。

from langchain_parallel import SourcePolicy

result = tool.invoke({
    "search_queries": ["renewable energy policy Europe"],
    "source_policy": SourcePolicy(
        include_domains=["europa.eu", "iea.org", "irena.org"],
        exclude_domains=["wikipedia.org", "reddit.com"],
    ),
    "max_results": 15,
})

链接

将工具绑定到任何支持工具调用的聊天模型,并驱动一个智能体 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", "What are the latest breakthroughs in quantum computing?")]})

响应格式

{
    "search_id": "search_abc123...",
    "session_id": "sess_...",
    "results": [
        {
            "url": "https://example.com/page",
            "title": "Page Title",
            "publish_date": "2026-04-01",
            "excerpts": [
                "First relevant excerpt...",
                "Second relevant excerpt...",
            ],
        },
    ],
    "warnings": [...],
    "usage": {...},
    "search_metadata": {  # added by this tool when include_metadata=True (the default)
        "search_duration_seconds": 2.451,
        "search_timestamp": "2026-04-15T10:30:00",
        "actual_results_returned": 5,
    },
}

API 参考文档

有关详细文档,请前往 ParallelSearchTool API 参考文档或 并行搜索参考.