Skip to main content
Parallel 通过兼容 OpenAI 的聊天接口提供实时网络研究能力,让您的 AI 应用能够访问来自网络的最新信息。
API 参考有关所有功能和配置选项的详细文档,请参阅 ChatParallelWeb API 参考。

概述

集成详情

可序列化JS/TS 支持下载量最新版本
ChatParallelWeblangchain-parallelDownloads per monthPyPI - Latest version

模型功能

工具调用结构化输出图像输入音频输入视频输入Token 级流式输出原生异步Token 用量对数概率

安装配置

要访问 Parallel 模型,您需要安装 langchain-parallel 集成包并获取 Parallel API 密钥。

安装

pip install -U langchain-parallel

凭据

前往 Parallel 注册并生成 API 密钥。完成后,在您的环境中设置 PARALLEL_API_KEY 环境变量:
import getpass
import os

if not os.environ.get("PARALLEL_API_KEY"):
    os.environ["PARALLEL_API_KEY"] = getpass.getpass("Enter your Parallel API key: ")

实例化

现在可以实例化模型对象并生成响应。默认模型为 "speed",可提供快速响应:
from langchain_parallel import ChatParallelWeb

llm = ChatParallelWeb(
    model="speed",
    # temperature=0.7,
    # max_tokens=None,
    # timeout=None,
    # max_retries=2,
    # api_key="...",  # 如果希望直接传入 API 密钥
    # base_url="https://api.parallel.ai",
    # 其他参数...
)
完整的可用模型参数列表,请参阅 ChatParallelWeb API 参考。
OpenAI 兼容性Parallel 支持许多与 OpenAI 兼容的参数,便于迁移(例如 response_formattoolstop_p),但这些参数大多会被 Parallel API 忽略。详情请参阅 OpenAI 兼容性 章节。

调用

messages = [
    (
        "system",
        "You are a helpful assistant with access to real-time web information.",
    ),
    ("human", "What are the latest developments in AI?"),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content='Here\'s a summary of the latest AI news and breakthroughs as of ...', additional_kwargs={}, response_metadata={'model': 'speed', 'finish_reason': 'stop', 'created': 1764043410}, id='run--3866fa98-6ac9-4585-8d23-99c5542b582b-0')
print(ai_msg.content)
Here's a summary of the latest AI news and breakthroughs as of...

链式调用

可以将模型与提示模板进行链式调用:
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate(
    [
        (
            "system",
            "You are a helpful research assistant with access to real-time web information. "
            "Provide comprehensive answers about {topic} with current data.",
        ),
        ("human", "{question}"),
    ]
)

chain = prompt | llm
chain.invoke(
    {
        "topic": "artificial intelligence",
        "question": "What are the most significant AI breakthroughs in 2025?",
    }
)
AIMessage(content="Based on the provided search results, here's a summary of the significant AI breakthroughs and trends...", additional_kwargs={}, response_metadata={'model': 'speed', 'finish_reason': 'stop', 'created': 1764043419}, id='run--9c521362-6724-4299-9e65-0565ec13d997-0')

流式输出

ChatParallelWeb 支持流式响应以实现实时输出:
for chunk in llm.stream(messages):
    print(chunk.content, end="")

异步

同样支持异步操作:
# 异步调用
ai_msg = await llm.ainvoke(messages)

# 异步流式输出
async for chunk in llm.astream(messages):
    print(chunk.content, end="")

Token 用量

不支持 Token 用量追踪Parallel 目前不提供 token 用量元数据,usage_metadata 字段将为 None
ai_msg = llm.invoke(messages)
print(ai_msg.usage_metadata)
None

响应元数据

访问来自 API 的响应元数据:
ai_msg = llm.invoke(messages)
print(ai_msg.response_metadata)
{'model': 'speed', 'finish_reason': 'stop', 'created': 1703123456}

错误处理

该集成为常见场景提供了增强的错误处理:
from langchain_parallel import ChatParallelWeb

try:
    llm = ChatParallelWeb(api_key="invalid-key")
    response = llm.invoke([("human", "Hello")])
except ValueError as e:
    if "Authentication failed" in str(e):
        print("Invalid API key provided")
    elif "Rate limit exceeded" in str(e):
        print("API rate limit exceeded, please try again later")

OpenAI 兼容性

兼容 OpenAI APIChatParallelWeb 与许多 OpenAI 聊天补全 API 参数完全兼容,迁移无缝。但是,大多数高级参数(如 response_formattoolstop_p)虽被接受,却会被 Parallel API 忽略。
llm = ChatParallelWeb(
    model="speed",
    # 以下参数被接受但会被 Parallel 忽略
    response_format={"type": "json_object"},
    tools=[{"type": "function", "function": {"name": "example"}}],
    tool_choice="auto",
    top_p=1.0,
    frequency_penalty=0.0,
    presence_penalty=0.0,
    logit_bias={},
    seed=42,
    user="user-123"
)

消息处理

该集成会自动处理消息格式化,并将相同类型的连续消息合并,以满足 API 要求:
from langchain.messages import HumanMessage, SystemMessage

# 这些连续的 system 消息将被自动合并
messages = [
    SystemMessage("You are a helpful assistant."),
    SystemMessage("Always be polite and concise."),
    HumanMessage("What is the weather like today?")
]

# 在 API 调用前自动合并为单个 system 消息
response = llm.invoke(messages)

API 参考

有关所有功能和配置选项的详细文档,请参阅 ChatParallelWeb API 参考或 Parallel 聊天 API 快速入门