xAI integration for LangChain.js
npm install @langchain/xaiThis package contains the LangChain.js integrations for xAI.
``bash npm2yarn`
npm install @langchain/xai @langchain/core
This package adds support for xAI chat model inference.
Set the necessary environment variable (or pass it in via the constructor):
`bash`
export XAI_API_KEY=
`typescript
import { ChatXAI } from "@langchain/xai";
import { HumanMessage } from "@langchain/core/messages";
const model = new ChatXAI({
apiKey: process.env.XAI_API_KEY, // Default value.
});
const message = new HumanMessage("What color is the sky?");
const res = await model.invoke([message]);
`
xAI supports server-side tools that are executed by the API rather than requiring client-side execution. The live_search tool enables the model to search the web for real-time information.
`typescript
import { ChatXAI, tools } from "@langchain/xai";
const model = new ChatXAI({
model: "grok-2-1212",
});
// Create the built-in live_search tool with optional parameters
const searchTool = tools.xaiLiveSearch({
maxSearchResults: 5,
returnCitations: true,
});
// Bind the live_search tool to the model
const modelWithSearch = model.bindTools([searchTool]);
// The model will search the web for real-time information
const result = await modelWithSearch.invoke(
"What happened in tech news today?"
);
console.log(result.content);
`
`typescript
import { ChatXAI } from "@langchain/xai";
const model = new ChatXAI({
model: "grok-2-1212",
searchParameters: {
mode: "auto", // "auto" | "on" | "off"
max_search_results: 5,
from_date: "2024-01-01", // ISO date string
return_citations: true,
},
});
const result = await model.invoke("What are the latest AI developments?");
`
`typescript`
const result = await model.invoke("Find recent news about SpaceX", {
searchParameters: {
mode: "on",
max_search_results: 10,
sources: [
{
type: "web",
allowed_websites: ["spacex.com", "nasa.gov"],
},
],
},
});
You can configure which data sources Live Search should use via the sources fieldsearchParameters
in . Each entry corresponds to one of the sources described in theweb
official xAI Live Search docs (, news, x, rss).
`typescript`
const result = await model.invoke(
"What are the latest updates from xAI and related news?",
{
searchParameters: {
mode: "on",
sources: [
{
type: "web",
// Only search on these websites
allowed_websites: ["x.ai"],
},
{
type: "news",
// Exclude specific news websites
excluded_websites: ["bbc.co.uk"],
},
{
type: "x",
// Focus on specific X handles
included_x_handles: ["xai"],
},
],
},
}
);
You can also use RSS feeds as a data source:
`typescript`
const result = await model.invoke("Summarize the latest posts from this feed", {
searchParameters: {
mode: "on",
sources: [
{
type: "rss",
links: ["https://example.com/feed.rss"],
},
],
},
});
> Notes:
>
> - The xaiLiveSearch tool options use camelCase field names in TypeScriptmaxSearchResults
> (for example , fromDate, returnCitations,allowedWebsites
> , excludedWebsites, includedXHandles). These aresearch_parameters
> automatically mapped to the underlying JSON API's snake_case
> object, which uses field names as documented in the official
> xAI Live Search docs.
`typescript
import { ChatXAI, tools } from "@langchain/xai";
const model = new ChatXAI({ model: "grok-2-1212" });
const modelWithTools = model.bindTools([
tools.xaiLiveSearch(), // Built-in server tool
{
// Custom function tool
type: "function",
function: {
name: "get_stock_price",
description: "Get the current stock price",
parameters: {
type: "object",
properties: {
symbol: { type: "string" },
},
required: ["symbol"],
},
},
},
]);
`
To develop the @langchain/xai package, you'll need to follow these instructions:
`bash`
pnpm install
`bash`
pnpm build
Or from the repo root:
`bash`
pnpm build --filter @langchain/xai
Test files should live within a tests/ file in the src/ folder. Unit tests should end in .test.ts and integration tests should.int.test.ts
end in :
`bash`
$ pnpm test
$ pnpm test:int
Run the linter & formatter to ensure your code is up to standard:
`bash`
pnpm lint && pnpm format
If you add a new file to be exported, either import & re-export from src/index.ts, or add it to the exports field in the package.json file and run pnpm build` to generate the new entrypoint.