Integrate xAI's Grok models with LangChain.js, including chat models and server-side live search capabilities for real-time web information retrieval.
This skill provides comprehensive integration between xAI's Grok models and LangChain.js, enabling both basic chat functionality and advanced server-side live search capabilities.
First, ensure the xAI API key is available:
```typescript
// Check for XAI_API_KEY in environment variables
// If not present, prompt user or use constructor parameter
const apiKey = process.env.XAI_API_KEY;
```
Install both `@langchain/xai` and `@langchain/core`:
```bash
npm install @langchain/xai @langchain/core
```
Create a basic xAI chat model instance:
```typescript
import { ChatXAI } from "@langchain/xai";
import { HumanMessage } from "@langchain/core/messages";
const model = new ChatXAI({
apiKey: process.env.XAI_API_KEY,
model: "grok-2-1212", // Specify model if needed
});
const message = new HumanMessage("Your question here");
const response = await model.invoke([message]);
```
Implement the built-in live search tool for real-time information:
```typescript
import { ChatXAI, tools } from "@langchain/xai";
// Create model instance
const model = new ChatXAI({
model: "grok-2-1212",
});
// Create live search tool with options
const searchTool = tools.xaiLiveSearch({
maxSearchResults: 5,
returnCitations: true,
// Optional: fromDate, toDate, allowedWebsites, etc.
});
// Bind tool to model
const modelWithSearch = model.bindTools([searchTool]);
// Use the model with search capabilities
const result = await modelWithSearch.invoke("Search query here");
```
Configure search parameters at model initialization or per request:
**At initialization:**
```typescript
const model = new ChatXAI({
model: "grok-2-1212",
searchParameters: {
mode: "auto", // "auto" | "on" | "off"
max_search_results: 5,
from_date: "2024-01-01",
return_citations: true,
},
});
```
**Per request override:**
```typescript
const result = await model.invoke("Query", {
searchParameters: {
mode: "on",
max_search_results: 10,
},
});
```
Configure specific data sources (web, news, X/Twitter, RSS):
```typescript
const result = await model.invoke("Query", {
searchParameters: {
mode: "on",
sources: [
{
type: "web",
allowed_websites: ["example.com", "another.com"],
},
{
type: "news",
excluded_websites: ["excluded.com"],
},
{
type: "x",
included_x_handles: ["handle1", "handle2"],
},
{
type: "rss",
links: ["https://example.com/feed.rss"],
},
],
},
});
```
Bind multiple tools including both server-side live search and custom function tools:
```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
{
type: "function",
function: {
name: "custom_tool_name",
description: "Description of what the tool does",
parameters: {
type: "object",
properties: {
param1: { type: "string" },
},
required: ["param1"],
},
},
},
]);
```
1. **Real-time News Queries**: Ask about current events and get up-to-date information
2. **Domain-Specific Research**: Restrict searches to specific websites or RSS feeds
3. **Social Media Monitoring**: Search X/Twitter handles for recent posts
4. **Hybrid Tool Usage**: Combine web search with custom business logic tools
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/langchain-xai-integration/raw