langchain-pubmed
v0.1.2
Published
LangChain.js integration for PubMed API - search biomedical literature and retrieve article metadata
Downloads
14
Maintainers
Readme
🧬 langchain-pubmed
LangChain.js integration for PubMed API - search biomedical literature and retrieve article metadata.
Features
- 🔍 Search PubMed's 35+ million biomedical literature citations
- 🤖 Ready-to-use LangChain Tool for AI agents
- 📄 Returns structured article metadata (title, abstract, publication date)
- ⚡ Built-in retry logic and rate limit handling
- 🔄 Supports streaming results with async iterators
- 🎯 Converts to LangChain Documents for RAG applications
Installation
npm install langchain-pubmed @langchain/coreQuick Start
import { PubMedTool } from "langchain-pubmed";
const tool = new PubMedTool({
topKResults: 3,
email: "[email protected]", // Recommended for better rate limits
});
const result = await tool.invoke("covid-19 vaccine efficacy");
console.log(result);Environment Variables
Set these environment variables to avoid hardcoding credentials:
export PUBMED_EMAIL="[email protected]"
export PUBMED_API_KEY="your_ncbi_api_key" # Optional, for higher rate limitsThen use without passing credentials:
const tool = new PubMedTool({ topKResults: 3 });Usage
1. As a LangChain Tool
import { PubMedTool } from "langchain-pubmed";
const tool = new PubMedTool({
topKResults: 3,
email: "[email protected]",
});
// Direct invocation
const result = await tool.invoke("diabetes treatment");
console.log(result);2. With an AI Agent
import { ChatOpenAI } from "@langchain/openai";
import { createAgent } from "langchain";
import { PubMedTool } from "langchain-pubmed";
import dotenv from "dotenv";
dotenv.config();
const model = new ChatOpenAI({
model: "gpt-5-nano",
});
const tools = [
new PubMedTool({
topKResults: 5,
}),
];
const agent = createAgent({ model, tools });
const result = await agent.invoke({
messages: [
{
role: "user",
content: "What are the latest treatments for Alzheimer's disease?",
},
],
});
console.log(result.messages[result.messages.length - 1].content);3. Direct API Wrapper
import { PubMedAPIWrapper } from "langchain-pubmed";
const pubmed = new PubMedAPIWrapper({
topKResults: 5,
email: "[email protected]",
apiKey: "your_ncbi_api_key", // Optional: for higher rate limits
});
// Get structured metadata
const articles = await pubmed.load("cancer immunotherapy");
articles.forEach((article) => {
console.log(article.Title);
console.log(article.Summary);
});
// Get LangChain Documents
const documents = await pubmed.loadDocs("machine learning healthcare");
// Stream results
for await (const article of pubmed.lazyLoad("alzheimer disease")) {
console.log(article.Title);
}4. For RAG Applications
import { PubMedAPIWrapper } from "langchain-pubmed";
const pubmed = new PubMedAPIWrapper({
topKResults: 10,
email: "[email protected]",
});
// Get LangChain Documents for vector store
const documents = await pubmed.loadDocs("CRISPR gene editing");
// Add to your vector store
// await vectorStore.addDocuments(documents);Configuration Options
| Option | Type | Default | Description |
| -------------------------------------------------- | -------- | -------------------------- | ------------------------------- |
| topKResults | number | 3 | Number of results to return |
| maxQueryLength | number | 300 | Max query length (chars) |
| docContentCharsMax | number | 2000 | Max content length (chars) |
| maxRetry | number | 5 | Max retries on rate limit |
| sleepTime | number | 200 | Initial retry delay (ms) |
| email | string | "[email protected]" | Email for PubMed API |
| apiKey | string | "" | NCBI API key (optional) |
| Plus all ToolParams from @langchain/core/tools | | | Callbacks, tags, metadata, etc. |
Rate Limits
- Without API key: 3 requests/second
- With API key: 10 requests/second
Get a free API key at: https://www.ncbi.nlm.nih.gov/account/settings/
Development
# Install dependencies
npm install
# Build
npm run build
# Run tests
npm test
# Run e2e tests (calls actual PubMed API)
npm run test:e2e
# Run unit tests
npm run test:unit
# Lint
npm run lint
# Format
npm run formatExample
See example.ts for a complete working example.
# Build first, then run the example
npm run build
npx ts-node example.tsAPI
PubMedTool
Main tool class for LangChain agents.
const tool = new PubMedTool(options);
const result = await tool.invoke(query);PubMedAPIWrapper
Core API wrapper with multiple access methods.
Methods:
run(query)- Get formatted search results stringload(query)- Get array of article metadataloadDocs(query)- Get array of LangChain DocumentslazyLoad(query)- Async iterator over article metadatalazyLoadDocs(query)- Async iterator over Documents
License
MIT
Credits
TypeScript port of the Python LangChain PubMed integration.
