安装
环境
要使用 xAI,您需要 创建一个 API 密钥。API 密钥可以作为初始化参数xai_api_key 传入,或设置为环境变量 XAI_API_KEY。
示例
有关详细信息和支持的功能,请参阅 ChatXAI 文档。官方原文:src/oss/python/integrations/providers/xai.mdx · 非官方翻译,译文由 AI 生成,以英文原文为准。
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
使用 LangChain Python 与 xAI 集成。
pip install -U langchain-xai
xai_api_key 传入,或设置为环境变量 XAI_API_KEY。
# 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 · 非官方翻译,译文由 AI 生成,以英文原文为准。