跳到主要内容
此页面引用了由 xAI 提供的 Grok 模型 - 请勿与独立的 AI 硬件和软件公司 Groq 混淆。请参阅 Groq 提供商页面
xAI 提供了一个与 Grok 模型交互的 API。本示例介绍了如何使用 LangChain 与 xAI 模型进行交互。

安装

pip install -U langchain-xai

环境

要使用 xAI,您需要创建一个 API 密钥。API 密钥可以作为初始化参数 xai_api_key 传入,或设置为环境变量 XAI_API_KEY

示例

有关详细信息和支持的功能,请参阅ChatXAI 文档
# Querying chat models with xAI

from langchain_xai import ChatXAI

chat = ChatXAI(
    # xai_api_key="YOUR_API_KEY",
    model="grok-4",
)

# stream the response back from the model
for m in chat.stream("Tell me fun things to do in NYC"):
    print(m.content, end="", flush=True)

# if you don't want to do streaming, you can use the invoke method
# chat.invoke("Tell me fun things to do in NYC")

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.