Skip to main content
本页面引用了由 xAI 提供的 Grok 模型——请勿与 Groq 混淆,后者是一家独立的 AI 硬件和软件公司。请参阅 Groq 提供商页面
xAI 提供 API 以与 Grok 模型交互。本示例介绍如何使用 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")