本页还没有译文
本页 23 段还没译成Español,下面显示的是英文原文。ChatXAI integration
还没翻译译自英文(docs.langchain.com)本页 23 段都还没译
Warning
This page documents Grok models from xAI. Do not confuse xAI with Groq, a separate inference provider. See the Groq integration.
xAI develops Grok chat models. See the xAI model documentation for available model IDs.
This guide will help you getting started with ChatXAI chat models. For detailed documentation of all ChatXAI features and configurations head to the API reference.
Overview
Integration details
| Class | Package | Serializable | PY support | Downloads | Version |
|---|---|---|---|---|---|
ChatXAI | @langchain/xai | ✅ | ✅ |
Model features
See the links in the table headers below for guides on how to use specific features.
| Tool calling | Structured output | Image input | Audio input | Video input | Token-level streaming | Token usage | Logprobs |
|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
Setup
To access ChatXAI models, create an xAI account, get an API key, and install the @langchain/xai integration package.
Credentials
Head to the xAI website to sign up and generate an API key. Set the XAI_API_KEY environment variable:
export XAI_API_KEY="your-api-key"If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"Installation
The LangChain ChatXAI integration lives in the @langchain/xai package:
npm install @langchain/xai @langchain/coreyarn add @langchain/xai @langchain/corepnpm add @langchain/xai @langchain/coreInstantiation
Now we can instantiate our model object and generate chat completions:
import { ChatXAI } from "@langchain/xai";
const llm = new ChatXAI({
model: "grok-3-fast",
temperature: 0,
maxTokens: undefined,
maxRetries: 2,
// other params...
})Invocation
const aiMsg = await llm.invoke([
[
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
],
["human", "I love programming."],
])
console.log(aiMsg)AIMessage {
"id": "71d7e3d8-30dd-472c-8038-b6b283dcee63",
"content": "J'adore programmer.",
"additional_kwargs": {},
"response_metadata": {
"tokenUsage": {
"promptTokens": 30,
"completionTokens": 6,
"totalTokens": 36
},
"finish_reason": "stop",
"usage": {
"prompt_tokens": 30,
"completion_tokens": 6,
"total_tokens": 36
},
"system_fingerprint": "fp_3e3898d4ce"
},
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"output_tokens": 6,
"input_tokens": 30,
"total_tokens": 36,
"input_token_details": {},
"output_token_details": {}
}
}console.log(aiMsg.content)J'adore programmer.API reference
For detailed documentation of all ChatXAI features and configurations head to the API reference.