Integrate LangChain.js core abstractions for building AI applications with language models, chains, prompts, and runnables. Supports modular provider integration.
Integrate LangChain.js core abstractions and schemas into JavaScript/TypeScript projects for building sophisticated AI applications.
This skill helps you integrate @langchain/core into your project, which provides the foundational abstractions for:
Install the core package:
```bash
pnpm install @langchain/core
```
If using specific providers (recommended), install them alongside core:
```bash
pnpm install @langchain/openai
pnpm install @langchain/anthropic
```
**Important**: All LangChain packages must share the same version of @langchain/core for compatibility.
Create a simple chain using core abstractions:
```typescript
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
const prompt = ChatPromptTemplate.fromTemplate(
`Answer the following question to the best of your ability:\n{question}`
);
const model = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0.8,
});
const outputParser = new StringOutputParser();
const chain = prompt.pipe(model).pipe(outputParser);
const stream = await chain.stream({
question: "Why is the sky blue?",
});
for await (const chunk of stream) {
console.log(chunk);
}
```
When helping users integrate LangChain, focus on these core abstractions:
Always ensure version alignment:
Organize LangChain code as follows:
Implement proper error handling:
```typescript
try {
const stream = await chain.stream({ question: userInput });
for await (const chunk of stream) {
// Process streaming chunks
console.log(chunk);
}
} catch (error) {
console.error("LangChain error:", error);
// Handle specific error types
}
```
Ensure API keys are properly configured:
```typescript
// .env file
OPENAI_API_KEY=your_key_here
ANTHROPIC_API_KEY=your_key_here
// Access in code
const model = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
```
If the user needs to create a custom LangChain provider package:
1. Add @langchain/core as both peer and dev dependency (never direct dependency)
2. Use tilde version range for compatibility
3. Extend base classes from @langchain/core
4. Build for both ESM and CJS compatibility
5. Example package.json structure:
```json
{
"peerDependencies": {
"@langchain/core": "~0.3.0"
},
"devDependencies": {
"@langchain/core": "~0.3.0"
}
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/langchain-core-integration/raw