Skip to content

HabitualCoder/langchainjs-examples

Repository files navigation

πŸš€ LangChain.js Complete AI Application

A comprehensive Next.js application demonstrating all major LangChain.js capabilities with Google Gemini integration. This project showcases text generation, image analysis, speech processing, transcription, multimodal processing, and more.

πŸ“‹ Table of Contents

✨ Features

🎯 Core AI Capabilities

  • Text Generation - Basic chat and structured output with Gemini
  • Image Analysis - Upload images for OCR, content analysis, and visual Q&A
  • Speech Processing - Generate speech scripts and analyze speech patterns
  • Transcript Analysis - Process audio transcripts and extract insights
  • Multimodal Processing - Handle multiple content types simultaneously
  • Feature Comparison - AI-powered comparison between LangChain.js and Vercel AI SDK

πŸ”§ Advanced Features

  • Streaming Support - Real-time text and structured output streaming
  • Tool Calling - Function calling and external tool integration
  • File Upload - Support for image files (PNG, JPEG)
  • Schema Validation - Zod-based structured output validation
  • Error Handling - Comprehensive error handling and validation
  • Responsive UI - Modern, mobile-friendly interface

πŸ› οΈ Tech Stack

  • Framework: Next.js 15 with App Router
  • AI Framework: LangChain.js
  • AI Model: Google Gemini 2.0 Flash
  • Styling: Tailwind CSS
  • Validation: Zod
  • Language: TypeScript
  • Deployment: Vercel-ready

πŸš€ Setup Instructions

Prerequisites

  • Node.js 18+
  • npm or pnpm
  • Google AI API key

1. Clone the Repository

git clone <your-repo-url>
cd langchainjs-examples

2. Install Dependencies

npm install
# or
pnpm install

3. Environment Setup

Create a .env.local file in the root directory:

GOOGLE_API_KEY=your_google_api_key_here

Getting Google API Key:

  1. Go to Google AI Studio
  2. Sign in with your Google account
  3. Click "Get API Key"
  4. Create a new API key
  5. Copy the key to your .env.local file

4. Run the Development Server

npm run dev
# or
pnpm dev

5. Open Your Browser

Navigate to http://localhost:3000 to see the application.

πŸ“ Project Structure

langchainjs-examples/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/                    # API routes
β”‚   β”‚   β”œβ”€β”€ text/route.ts       # Text generation API
β”‚   β”‚   β”œβ”€β”€ image/route.ts      # Image analysis API
β”‚   β”‚   β”œβ”€β”€ speech/route.ts     # Speech processing API
β”‚   β”‚   └── transcription/route.ts # Transcript analysis API
β”‚   β”œβ”€β”€ simple-chat-llm/        # Basic text generation examples
β”‚   β”‚   β”œβ”€β”€ chat.ts
β”‚   β”‚   β”œβ”€β”€ structured-chat.ts
β”‚   β”‚   β”œβ”€β”€ schema-examples.ts
β”‚   β”‚   └── prompt-reveal.ts
β”‚   β”œβ”€β”€ streaming/              # Streaming examples
β”‚   β”‚   β”œβ”€β”€ stream-text.ts
β”‚   β”‚   └── stream-structured.ts
β”‚   β”œβ”€β”€ tool-calling/           # Tool calling examples
β”‚   β”‚   β”œβ”€β”€ basic-tools.ts
β”‚   β”‚   └── advanced-tools.ts
β”‚   β”œβ”€β”€ image-generation/       # Image processing
β”‚   β”‚   β”œβ”€β”€ image-prompts.ts
β”‚   β”‚   β”œβ”€β”€ image-analysis.ts
β”‚   β”‚   └── page.tsx           # UI for image analysis
β”‚   β”œβ”€β”€ speech/                 # Speech processing
β”‚   β”‚   β”œβ”€β”€ speech-processing.ts
β”‚   β”‚   └── page.tsx           # UI for speech processing
β”‚   β”œβ”€β”€ transcription/          # Transcript processing
β”‚   β”‚   β”œβ”€β”€ transcript-processing.ts
β”‚   β”‚   └── page.tsx           # UI for transcript analysis
β”‚   β”œβ”€β”€ multimodal/            # Multimodal processing
β”‚   β”‚   β”œβ”€β”€ content-analysis.ts
β”‚   β”‚   └── page.tsx           # UI for multimodal processing
β”‚   β”œβ”€β”€ comparison/            # Feature comparison
β”‚   β”‚   β”œβ”€β”€ feature-comparison.ts
β”‚   β”‚   └── page.tsx           # UI for comparison
β”‚   β”œβ”€β”€ page.tsx               # Main homepage
β”‚   └── layout.tsx             # App layout
β”œβ”€β”€ public/                     # Static assets
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ tailwind.config.js
└── README.md

πŸ”Œ API Endpoints

Text Generation

  • POST /api/text
  • Body: { "prompt": "Your text prompt" }
  • Response: { "success": true, "content": "AI response" }

Image Analysis

  • POST /api/image
  • Body: FormData with image file and prompt text
  • Response: { "success": true, "content": "Image analysis" }

Speech Processing

  • POST /api/speech
  • Body: { "text": "Text to process", "processingType": "speech_script" }
  • Response: { "success": true, "content": "Processed speech" }

Transcript Analysis

  • POST /api/transcription
  • Body: { "transcript": "Audio transcript", "analysisType": "summary" }
  • Response: { "success": true, "content": "Analysis result" }

πŸ’‘ Usage Examples

1. Basic Text Generation

const response = await fetch('/api/text', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ prompt: 'Explain quantum computing' })
});
const data = await response.json();
console.log(data.content);

2. Image Analysis

const formData = new FormData();
formData.append('image', imageFile);
formData.append('prompt', 'What do you see in this image?');

const response = await fetch('/api/image', {
  method: 'POST',
  body: formData
});
const data = await response.json();
console.log(data.content);

3. Structured Output

import { z } from "zod";

const schema = z.object({
  answer: z.string().describe("The main answer"),
  confidence: z.number().min(0).max(1)
});

const structuredModel = model.withStructuredOutput(schema);
const result = await structuredModel.invoke("What is AI?");

βš–οΈ LangChain.js vs Vercel AI SDK

Feature LangChain.js Vercel AI SDK Best For
Text Generation model.invoke() generateText() LangChain: Complex workflows
Structured Output withStructuredOutput() generateObject() LangChain: Advanced schemas
Streaming model.stream() streamText() Both: Real-time apps
Tool Calling bindTools() Built-in LangChain: Advanced tools
State Management Built-in chains None LangChain: Complex workflows
Multimodal Full support Limited LangChain: Image/audio
React Components None Built-in Vercel: Quick UI
Learning Curve Steeper Gentle Vercel: Simple apps

When to Choose Each:

Choose LangChain.js if:

  • Building complex AI workflows
  • Need advanced prompt engineering
  • Want extensive tool integration
  • Building enterprise applications
  • Need multimodal capabilities

Choose Vercel AI SDK if:

  • Building simple chat applications
  • Need React components out of the box
  • Want minimal setup
  • Building Next.js applications
  • Need quick prototyping

🎯 Key Learnings

  1. LangChain.js is more powerful but has a steeper learning curve
  2. Vercel AI SDK is simpler but limited to basic text generation
  3. Both use the same underlying AI models - the difference is in the framework
  4. LangChain.js excels at complex workflows with state management and tool integration
  5. Vercel AI SDK excels at quick UI development with built-in React components

πŸ”§ Development

Running Examples

# Run specific examples
npx tsx app/simple-chat-llm/chat.ts
npx tsx app/streaming/stream-text.ts
npx tsx app/tool-calling/basic-tools.ts

Building for Production

npm run build
npm start

πŸ“š Additional Resources

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Built with ❀️ using LangChain.js, Next.js, and Google Gemini

About

A demo app built with Nextjs and Langchainjs to learn and practice

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages