> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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 提供商页面](/oss/python/integrations/providers/groq)。
</Warning>

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

## 安装

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -U langchain-xai
```

## 环境

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

## 示例

有关详细信息和支持的功能，请参见 [ChatXAI 文档](/oss/python/integrations/chat/xai)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 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")
```

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\providers\xai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
