Back to Documentation

LangChain Integration

Use BaseGrid as persistent memory for your LangChain applications

Drop-in Memory

Replace LangChain's default memory with BaseGrid for persistent, searchable conversations

Vector Store

Use BaseGrid as a vector store for RAG (Retrieval Augmented Generation) chains

Agent Tools

Give LangChain agents access to BaseGrid's memory search as a tool

Installation

bash
1npm install @basegrid-io/core langchain @langchain/openai
2# or
3yarn add @basegrid-io/core langchain @langchain/openai

Basic Setup

First, initialize BaseGrid and create an agent to store your conversation history.

typescript
1import { BaseGrid } from '@basegrid-io/core';
2import { ChatOpenAI } from '@langchain/openai';
3import { BufferMemory } from 'langchain/memory';
4import { ConversationChain } from 'langchain/chains';
5
6// Initialize BaseGrid
7const bg = new BaseGrid({
8    apiKey: process.env.BASEGRID_API_KEY
9});
10
11// Create an agent
12const agent = await bg.agents.create({
13    name: 'langchain-assistant',
14    description: 'LangChain-powered conversational agent'
15});

Memory Integration

Replace LangChain's default BufferMemory with BaseGrid-backed memory for persistent conversations.

typescript
1import { BaseGridMemory } from 'basegrid/langchain';
2
3// Create BaseGrid-backed memory
4const memory = new BaseGridMemory({
5    basegrid: bg,
6    agentId: agent.id,
7    memoryKey: 'chat_history',
8    returnMessages: true
9});
10
11// Initialize LangChain with BaseGrid memory
12const model = new ChatOpenAI({
13    modelName: 'gpt-4',
14    temperature: 0.7
15});
16
17const chain = new ConversationChain({
18    llm: model,
19    memory: memory
20});
21
22// Use the chain
23const response = await chain.call({
24    input: 'What did we discuss about pricing?'
25});
26
27console.log(response.response);

💡 Tip: BaseGrid automatically handles memory persistence, so conversations survive across sessions without any additional configuration.

RAG (Retrieval Augmented Generation)

Use BaseGrid as a vector store for semantic search in your RAG pipelines.

typescript
1import { RetrievalQAChain } from 'langchain/chains';
2import { BaseGridVectorStore } from 'basegrid/langchain';
3
4// Create vector store backed by BaseGrid
5const vectorStore = new BaseGridVectorStore({
6    basegrid: bg,
7    agentId: agent.id,
8    embeddingModel: 'text-embedding-ada-002'
9});
10
11// Add documents
12await vectorStore.addDocuments([
13    { pageContent: 'BaseGrid offers sub-200ms memory retrieval', metadata: { source: 'docs' } },
14    { pageContent: 'Pricing starts at $49/month for Pro Starter', metadata: { source: 'pricing' } }
15]);
16
17// Create RAG chain
18const qaChain = RetrievalQAChain.fromLLM(
19    model,
20    vectorStore.asRetriever()
21);
22
23const answer = await qaChain.call({
24    query: 'How fast is BaseGrid?'
25});
26
27console.log(answer.text); // "BaseGrid offers sub-200ms memory retrieval"

Agent with Memory Tool

Give your LangChain agents the ability to search through conversation history using BaseGrid as a tool.

typescript
1import { initializeAgentExecutorWithOptions } from 'langchain/agents';
2import { Calculator } from 'langchain/tools/calculator';
3import { BaseGridTool } from 'basegrid/langchain/tools';
4
5// Create BaseGrid tool for memory search
6const memoryTool = new BaseGridTool({
7    basegrid: bg,
8    agentId: agent.id,
9    name: 'search_memory',
10    description: 'Search through conversation history and stored facts'
11});
12
13// Initialize agent with tools
14const executor = await initializeAgentExecutorWithOptions(
15    [memoryTool, new Calculator()],
16    model,
17    {
18        agentType: 'chat-conversational-react-description',
19        memory: memory,
20        verbose: true
21    }
22);
23
24// Run agent
25const result = await executor.call({
26    input: 'What was the total from our last calculation, and multiply it by 5'
27});

Next Steps