跳到主要內容
Open In ColabOpen on GitHub

xAI

xAI 提供 API 以與 Grok 模型互動。

此範例說明如何使用 LangChain 與 xAI 模型互動。

安裝

%pip install --upgrade langchain-xai

環境

若要使用 xAI,您需要建立 API 金鑰。API 金鑰可以作為 init 參數 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")
API 參考文件:ChatXAI

此頁面是否對您有幫助?