A comprehensive tutorial project for learning LangChain with JavaScript/TypeScript, featuring interactive examples and hands-on implementations.
This project provides a structured approach to learning LangChain concepts through practical examples. It includes multiple tutorials covering different aspects of LangChain, from basic output parsing to advanced retrieval chains using real-world data about Vishwas Gopinath, a Developer Relations Manager at Builder.io.
- Node.js (v16 or higher)
- npm or yarn
- A valid Google AI API key (for Gemini integration)
- Optional: LangSmith API key (for tracing and debugging)
-
Clone the repository
git clone <your-repo-url> cd langchain-js-tutorial
-
Install dependencies
npm install
-
Set up environment variables
cp .env.sample .env
Then edit
.envand add your API keys:GOOGLE_API_KEY=your_actual_google_api_key_here LANGSMITH_API_KEY=your_langsmith_api_key_here # Optional LANGCHAIN_TRACING_V2=true # Optional
- Go to Google AI Studio
- Sign in with your Google account
- Click "Create API Key"
- Copy the generated key and paste it in your
.envfile
- Go to LangSmith
- Sign up or sign in
- Go to Settings β API Keys
- Create a new API key
- Add it to your
.envfile for tracing capabilities
-
Install dependencies
npm install
-
Set up your API keys in
.envfile (copy from.env.sample) -
Run the application
npm start
-
Select a tutorial from the interactive menu:
=== LangChain JS Tutorial Menu === Please select a tutorial to run: 1. Output Parser 2. Retrieval Chain 3. Conversation Retrieval Chain 0. Exit ================================== Enter your choice (0-3):
That's it! The application will automatically compile TypeScript and run your selected tutorial.
If you want to run tutorials individually or explore the code:
If you want to run tutorials individually or explore the code:
npx tsx app/tutorial_01/output_parser.ts
npx tsx app/tutorial_01/retrieval_chain.ts
npx tsx app/tutorial_01/conversation_retrieval_chain.tsnpx tsc
node dist/app/tutorial_01/output_parser.js
node dist/app/tutorial_01/retrieval_chain.js
node dist/app/tutorial_01/conversation_retrieval_chain.jsNote: The recommended approach is to use
npm startand select tutorials from the menu.
langchain-js-tutorial/
βββ app/
β βββ index.ts # Main application entry point
β βββ tutorial_01/
β βββ output_parser.ts # Output parsing examples
β βββ retrieval_chain.ts # Retrieval chain examples
β βββ conversation_retrieval_chain.ts # Conversation with retrieval
βββ dist/ # Compiled JavaScript files
βββ .env # Environment variables
βββ .env.sample # Environment variables template
βββ package.json # Project dependencies
βββ tsconfig.json # TypeScript configuration
βββ README.md # This file
Learn how to parse and structure outputs from language models using LangChain's output parsing capabilities.
File: app/tutorial_01/output_parser.ts
Topics covered:
- Basic output parsing
- Structured output formats
- Error handling and validation
- Using Zod for schema validation
Run command:
npx tsc && node dist/app/tutorial_01/output_parser.jsExplore retrieval-augmented generation (RAG) patterns and how to build effective retrieval chains using real professional data.
File: app/tutorial_01/retrieval_chain.ts
Topics covered:
- Document retrieval from web sources
- Web scraping with CheerioWebBaseLoader
- Document splitting and chunking
- Vector databases and similarity search
- Chain composition and context integration
- Real-world data processing (Vishwas Gopinath's professional profile)
Run command:
npx tsc && node dist/app/tutorial_01/retrieval_chain.jsBuild advanced conversational AI with memory and context-aware retrieval using professional profile data.
File: app/tutorial_01/conversation_retrieval_chain.ts
Topics covered:
- Conversation history management
- History-aware retrieval
- Vector store creation and management
- Memory integration with chains
- LangSmith tracing and debugging
- Context-aware responses about professional backgrounds
Run command:
npx tsc && node dist/app/tutorial_01/conversation_retrieval_chain.jsFeatures:
- β LangSmith tracing integration
- π§ Memory-aware conversations
- π Context-aware document retrieval
- π Detailed logging and debugging
- π¨βπ» Real professional data (Vishwas Gopinath's profile)
# Compile TypeScript
npx tsc
# Watch mode for development
npx tsc --watchnpm start- Compile and run the main applicationnpm run build- Compile TypeScript to JavaScriptnpm run dev- Run in development mode with watch
npm test@langchain/core- Core LangChain functionality@langchain/google-genai- Google Generative AI integration@langchain/community- Community loaders and tools@langchain/textsplitters- Text splitting utilitieslangchain- Main LangChain librarydotenv- Environment variable managementzod- Schema validationlangsmith- LangSmith tracing and debugging
typescript- TypeScript compiler@types/node- Node.js type definitions
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.
-
API Key Error
GoogleGenerativeAIFetchError: API key not validSolution: Ensure your Google API key is valid and correctly set in the
.envfile. -
TypeScript Compilation Error
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"Solution: Run
npx tscto compile TypeScript files before execution. -
Module Not Found
Cannot find module './tutorial_01/output_parser.js'Solution: Ensure TypeScript compilation is complete and files exist in the
distdirectory.
- Check the LangChain Documentation
- Review the Google AI Studio Documentation
- Open an issue in this repository for project-specific problems
- LangChain for the amazing framework
- Google AI for the Gemini API
- The open-source community for continuous inspiration
Happy Learning! π