xiaohongshu
xiaohongshu
langchain-js
Builds LLM-powered applications with LangChain.js for chat, agents, and RAG. Use when creating AI applications with chains, memory, tools, and retrieval-augmented generation in JavaScript.
Full skill instructions
Framework for building LLM-powered applications. Provides abstractions for chat models, chains, agents, RAG, and memory with support for multiple providers.
npm install langchain @langchain/openai @langchain/core
import { ChatOpenAI } from '@langchain/openai';
const model = new ChatOpenAI({
modelName: 'gpt-4o',
temperature: 0.7,
});
const response = await model.invoke('What is the capital of France?');
console.log(response.content);
import { ChatOpenAI } from '@langchain/openai';
const model = new ChatOpenAI({
modelName: 'gpt-4o',
temperature: 0,
maxTokens: 1000,
});
const response = await model.invoke([
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' },
]);
npm install @langchain/anthropic
import { ChatAnthropic } from '@langchain/anthropic';
const model = new ChatAnthropic({
modelName: 'claude-sonnet-4-20250514',
temperature: 0,
});
const response = await model.invoke('Explain quantum computing');
npm install @langchain/google-genai
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
const model = new ChatGoogleGenerativeAI({
modelName: 'gemini-pro',
temperature: 0,
});
import { ChatOpenAI } from '@langchain/openai';
const model = new ChatOpenAI({
modelName: 'gpt-4o',
streaming: true,
});
const stream = await model.stream('Tell me a story');
for await (const chunk of stream) {
process.stdout.write(chunk.content as string);
}
import { ChatPromptTemplate } from '@langchain/core/prompts';
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a {role} assistant.'],
['user', '{input}'],
]);
const formattedPrompt = await prompt.format({
role: 'helpful',
input: 'What is TypeScript?',
});
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';
const model = new ChatOpenAI();
const prompt = ChatPromptTemplate.fromMessages([
['system', 'Translate the following to {language}'],
['user', '{text}'],
]);
const chain = prompt.pipe(model);
const response = await chain.invoke({
language: 'French',
text: 'Hello, how are you?',
});
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { StringOutputParser, JsonOutputParser } from '@langchain/core/output_parsers';
// String output
const stringChain = prompt.pipe(model).pipe(new StringOutputParser());
const text = await stringChain.invoke({ input: 'Hello' });
// JSON output
const jsonChain = prompt.pipe(model).pipe(new JsonOutputParser());
const data = await jsonChain.invoke({ input: 'List 3 colors as JSON' });
import { z } from 'zod';
import { ChatOpenAI } from '@langchain/openai';
const model = new ChatOpenAI({ modelName: 'gpt-4o' });
const schema = z.object({
name: z.string().describe('The person name'),
age: z.number().describe('The person age'),
occupation: z.string().describe('The person occupation'),
});
const structuredModel = model.withStructuredOutput(schema);
const result = await structuredModel.invoke(
'Extract info: John is 30 years old and works as a developer.'
);
// { name: 'John', age: 30, occupation: 'developer' }
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate, MessagesPlaceholder } from '@langchain/core/prompts';
import { RunnableWithMessageHistory } from '@langchain/core/runnables';
import { ChatMessageHistory } from 'langchain/memory';
const model = new ChatOpenAI();
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a helpful assistant.'],
new MessagesPlaceholder('history'),
['user', '{input}'],
]);
const chain = prompt.pipe(model);
// In-memory store
const messageHistories: Record<string, ChatMessageHistory> = {};
const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: async (sessionId) => {
if (!messageHistories[sessionId]) {
messageHistories[sessionId] = new ChatMessageHistory();
}
return messageHistories[sessionId];
},
inputMessagesKey: 'input',
historyMessagesKey: 'history',
});
// Use with session
const response = await chainWithHistory.invoke(
{ input: 'My name is Alice' },
{ configurable: { sessionId: 'user-123' } }
);
const response2 = await chainWithHistory.invoke(
{ input: 'What is my name?' },
{ configurable: { sessionId: 'user-123' } }
);
// Response includes "Alice"
npm install @langchain/openai langchain
import { TextLoader } from 'langchain/document_loaders/fs/text';
import { PDFLoader } from 'langchain/document_loaders/fs/pdf';
import { WebLoader } from 'langchain/document_loaders/web/cheerio';
// Load text file
const textLoader = new TextLoader('./document.txt');
const textDocs = await textLoader.load();
// Load PDF
const pdfLoader = new PDFLoader('./document.pdf');
const pdfDocs = await pdfLoader.load();
// Load webpage
const webLoader = new WebLoader('https://example.com');
const webDocs = await webLoader.load();
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
});
const chunks = await splitter.splitDocuments(docs);
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { OpenAIEmbeddings } from '@langchain/openai';
const embeddings = new OpenAIEmbeddings();
const vectorStore = await MemoryVectorStore.fromDocuments(
chunks,
embeddings
);
// Search
const results = await vectorStore.similaritySearch('query', 4);
import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { createRetrievalChain } from 'langchain/chains/retrieval';
import { createStuffDocumentsChain } from 'langchain/chains/combine_documents';
// Setup
const model = new ChatOpenAI({ modelName: 'gpt-4o' });
const embeddings = new OpenAIEmbeddings();
const vectorStore = await MemoryVectorStore.fromDocuments(chunks, embeddings);
const retriever = vectorStore.asRetriever();
// Prompt
const prompt = ChatPromptTemplate.fromTemplate(`
Answer the question based only on the following context:
{context}
Question: {input}
`);
// Chain
const documentChain = await createStuffDocumentsChain({ llm: model, prompt });
const retrievalChain = await createRetrievalChain({
combineDocsChain: documentChain,
retriever,
});
// Query
const response = await retrievalChain.invoke({
input: 'What is the main topic?',
});
console.log(response.answer);
import { ChatOpenAI } from '@langchain/openai';
import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
import { Calculator } from '@langchain/community/tools/calculator';
import { createReactAgent } from '@langchain/langgraph/prebuilt';
const model = new ChatOpenAI({ modelName: 'gpt-4o' });
const tools = [
new TavilySearchResults({ maxResults: 3 }),
new Calculator(),
];
const agent = createReactAgent({
llm: model,
tools,
});
const result = await agent.invoke({
messages: [{ role: 'user', content: 'What is the weather in Tokyo?' }],
});
import { tool } from '@langchain/core/tools';
import { z } from 'zod';
const weatherTool = tool(
async ({ location }) => {
// Call weather API
return `The weather in ${location} is sunny, 72°F`;
},
{
name: 'get_weather',
description: 'Get the current weather for a location',
schema: z.object({
location: z.string().describe('The city name'),
}),
}
);
const tools = [weatherTool];
npm install @langchain/langgraph
import { StateGraph, Annotation } from '@langchain/langgraph';
const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (x, y) => x.concat(y),
}),
});
const workflow = new StateGraph(StateAnnotation)
.addNode('agent', agentNode)
.addNode('tools', toolNode)
.addEdge('__start__', 'agent')
.addConditionalEdges('agent', shouldContinue)
.addEdge('tools', 'agent');
const app = workflow.compile();
const result = await app.invoke({
messages: [{ role: 'user', content: 'Search for AI news' }],
});
// app/api/chat/route.ts
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { StringOutputParser } from '@langchain/core/output_parsers';
import { StreamingTextResponse, LangChainAdapter } from 'ai';
const model = new ChatOpenAI({ streaming: true });
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a helpful assistant.'],
['user', '{input}'],
]);
const chain = prompt.pipe(model).pipe(new StringOutputParser());
export async function POST(request: Request) {
const { message } = await request.json();
const stream = await chain.stream({ input: message });
return new StreamingTextResponse(LangChainAdapter.toAIStream(stream));
}
OPENAI_API_KEY=sk-xxxxxxxx
ANTHROPIC_API_KEY=sk-ant-xxxxxxxx
GOOGLE_API_KEY=xxxxxxxx
TAVILY_API_KEY=tvly-xxxxxxxx
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience