跳转到主要内容

xAI

xAI 提供了一个 API 来与 Grok 模型交互。

此示例介绍了如何使用 LangChain 与 xAI 模型交互。

安装

%pip install --upgrade langchain-xai

环境

要使用 xAI,你需要创建一个 API 密钥。API 密钥可以作为初始化参数 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-beta",
)

# 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")

此页面是否对您有帮助?