Skip to main content
This example shows how to build a retrieval-augmented generation (RAG) API using LangChain, Express.js, and OpenInference instrumentation.

Prerequisites

  • Node.js 18+
  • OpenAI API key
  • Phoenix or another OpenTelemetry collector

Installation

1

Install dependencies

2

Set environment variables

Project Structure

Instrumentation Setup

Create instrumentation.ts:

Express Server Setup

Create index.ts:

Chat Controller with RAG

Create src/controllers/chat.controller.ts:

Vector Store Initialization

Create src/vector_store/store.ts:

Chat Route

Create src/routes/chat.route.ts:

Run the Server

Test with cURL

Key Features

Automatic Chain Tracing

LangChain instrumentation captures:
  • Retrieval chains: Document retrieval and ranking
  • LLM calls: All language model interactions
  • Prompt templates: Template rendering with variables
  • Vector store queries: Similarity search operations

Manual Instrumentation

LangChain requires manual instrumentation due to its module structure:

Production Considerations

  • Use environment-based CORS configuration
  • Implement proper error handling
  • Add rate limiting and authentication
  • Use persistent vector stores (Pinecone, Weaviate, etc.)

Next Steps