> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ParallelSearchRetriever 集成

> 使用 LangChain Python 集成 ParallelSearchRetriever 检索器。

@\[`ParallelSearchRetriever`] 是基于 [Parallel](https://platform.parallel.ai/) 的 [Search API](https://docs.parallel.ai/search/search-quickstart) 支持的 LangChain [`BaseRetriever`](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/retrieval)。它返回 `list[Document]` 并包含丰富的 `metadata`（`url`, `title`, `publish_date`, `search_id`, `excerpts`, `query`），可无缝集成到任何 RAG 管道中。

> [!NOTE]
>
> 需要返回原始搜索响应而非 `Document` 的 LLM 调用工具？请参考 [ParallelSearchTool](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/tools/parallel_search.md)。

## 概述

### 集成细节

| 类别 | 包 | JS 支持 | 最近的包版本 |
| :- | :- | :-: | :-: |
| @\[`ParallelSearchRetriever`] | @\[`langchain-parallel`] | ❌ | [![PyPI - Latest version](https://img.shields.io/pypi/v/langchain-parallel?style=flat-square\&label=%20\&color=orange)](https://pypi.org/project/langchain-parallel/) |

## 安装

集成模块位于 `langchain-parallel` 包中。

```bash pip theme={null}
pip install -U langchain-parallel
```

```bash uv theme={null}
uv add langchain-parallel
```

### 凭据

前往 [Parallel](https://platform.parallel.ai) 注册并生成 API 密钥。在环境中设置 `PARALLEL_API_KEY`：

```python theme={null}
import getpass
import os

if not os.environ.get("PARALLEL_API_KEY"):
    os.environ["PARALLEL_API_KEY"] = getpass.getpass("Parallel API key:\n")
```

## 实例化

```python theme={null}
from langchain_parallel import ParallelSearchRetriever

retriever = ParallelSearchRetriever(
    max_results=3,
    excerpts={"max_chars_per_result": 800},
)
```

## 使用

每个返回的 `Document` 将其摘要合并为 `page_content`，并暴露来源 URL、标题和发布日期到 `metadata`：

```python theme={null}
docs = retriever.invoke("breakthroughs in fusion energy 2025")
for d in docs:
    print(d.metadata.get("title"), "—", d.metadata.get("url"))
    print(d.page_content[:200], "...\n")
```

```text theme={null}
Net energy gain in fusion: NIF results — https://www.nature.com/articles/...
The National Ignition Facility achieved net energy gain on December 5, 2022 ...

Commonwealth Fusion's SPARC milestone — https://news.mit.edu/...
SPARC is on track for first plasma in 2026 ...
```

### 配置搜索

为 `objective` 添加更丰富的检索目标（与关键词 `search_queries` 配合）。检索器将向底层 Search API 转发来源和请求策略。

```python theme={null}
configured = ParallelSearchRetriever(
    max_results=5,
    excerpts={"max_chars_per_result": 1500},
    mode="basic",  # 'basic' (lower latency) or 'advanced' (higher quality)
    source_policy={
        "include_domains": ["nature.com", "science.org", "iter.org"],
    },
)

docs = configured.invoke(
    "What's the latest peer-reviewed result on net-energy-gain fusion?"
)
```

```python theme={null}
docs = await retriever.ainvoke("Latest GLP-1 trial results 2025")
```

### 异步

## 在链中调用

`ParallelSearchRetriever` 可无缝接入任何 LangChain 链：

```python theme={null}
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain.chat_models import init_chat_model

llm = init_chat_model(model="claude-opus-4-8")

prompt = ChatPromptTemplate.from_messages([
    ("system", "Answer using only the context below. Cite URLs."),
    ("human", "Context:\n{context}\n\nQuestion: {question}"),
])

def format_docs(docs):
    return "\n\n".join(
        f"[{d.metadata.get('url')}] {d.page_content[:500]}" for d in docs
    )

chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

chain.invoke("What was the most recent fusion energy breakthrough?")
```

## API 参考

详细文档请查阅 @\[`ParallelSearchRetriever`] API 参考或 [Parallel 搜索 API 指南](https://docs.parallel.ai/search/search-quickstart)。

***

> 官方原文：[src/oss/python/integrations/retrievers/parallel.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/retrievers/parallel.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.