> ## 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.

# xAI 集成

> 使用 LangChain Python 与 xAI 集成。

> [!WARNING]
>
> 本页面引用了由 [xAI](https://docs.x.ai/docs/overview) 提供的 Grok 模型——请注意不要与 [Groq](https://console.groq.com/docs/overview) 混淆，后者是一家独立的 AI 硬件和软件公司。请参阅 [Groq 提供商页面](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/providers/groq.md)。

[xAI](https://console.x.ai) 提供了一个 API 用于与 Grok 模型交互。本示例将介绍如何使用 LangChain 与 xAI 模型进行交互。

## 安装

```python theme={null}
pip install -U langchain-xai
```

## 环境

要使用 xAI，您需要 [创建一个 API 密钥](https://console.x.ai/)。API 密钥可以作为初始化参数 `xai_api_key` 传入，或设置为环境变量 `XAI_API_KEY`。

## 示例

有关详细信息和支持的功能，请参阅 [ChatXAI 文档](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/xai.md)。

```python theme={null}
# 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
stream = chat.stream_events("Tell me fun things to do in NYC", version="v3")
for token in stream.text:
    print(token, 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")
```

***

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


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