Back to Documentation

LlamaIndex Integration

Build data-augmented LLM applications with BaseGrid as your memory layer

Chat Memory

Persistent chat history for context-aware conversations

Vector Store

Semantic search over your knowledge base

Custom Retriever

Build RAG pipelines with BaseGrid retrieval

Agent Tools

Memory search as a tool for LlamaIndex agents

Installation

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

Basic Setup

Initialize BaseGrid and create an agent to manage your knowledge base and conversations.

typescript
1import { BaseGrid } from '@basegrid-io/core';
2import { OpenAI } from 'llamaindex';
3
4// Initialize BaseGrid
5const bg = new BaseGrid({
6    apiKey: process.env.BASEGRID_API_KEY
7});
8
9// Create an agent
10const agent = await bg.agents.create({
11    name: 'llamaindex-assistant',
12    description: 'LlamaIndex-powered knowledge assistant'
13});
14
15// Initialize LLM
16const llm = new OpenAI({
17    model: 'gpt-4',
18    temperature: 0.7
19});

Chat Engine with Memory

Use BaseGrid as the chat history backend for LlamaIndex's ContextChatEngine.

typescript
1import { BaseGridChatMemory } from 'basegrid/llamaindex';
2import { ContextChatEngine } from 'llamaindex';
3
4// Create BaseGrid-backed chat memory
5const chatMemory = new BaseGridChatMemory({
6    basegrid: bg,
7    agentId: agent.id
8});
9
10// Initialize chat engine with BaseGrid memory
11const chatEngine = new ContextChatEngine({
12    llm: llm,
13    chatHistory: chatMemory,
14    systemPrompt: 'You are a helpful AI assistant with persistent memory.'
15});
16
17// Chat with memory persistence
18const response = await chatEngine.chat({
19    message: 'Remember that my favorite color is blue'
20});
21
22console.log(response.response);
23
24// Later conversation - memory persists
25const response2 = await chatEngine.chat({
26    message: 'What is my favorite color?'
27});
28
29console.log(response2.response); // "Your favorite color is blue"

💡 Tip: Chat history is automatically persisted across sessions, so users can pick up conversations exactly where they left off.

Vector Store Index

Create a vector index backed by BaseGrid for semantic search over your documents.

typescript
1import { BaseGridVectorStore } from 'basegrid/llamaindex';
2import { VectorStoreIndex, Document } from 'llamaindex';
3
4// Create vector store backed by BaseGrid
5const vectorStore = new BaseGridVectorStore({
6    basegrid: bg,
7    agentId: agent.id
8});
9
10// Create documents
11const documents = [
12    new Document({ text: 'BaseGrid provides sub-200ms memory retrieval for AI agents' }),
13    new Document({ text: 'LlamaIndex is a data framework for LLM applications' }),
14    new Document({ text: 'RAG combines retrieval with generation for better responses' })
15];
16
17// Create index from documents
18const index = await VectorStoreIndex.fromDocuments(documents, {
19    vectorStore: vectorStore
20});
21
22// Query the index
23const queryEngine = index.asQueryEngine();
24const result = await queryEngine.query({
25    query: 'How fast is BaseGrid?'
26});
27
28console.log(result.response); // "BaseGrid provides sub-200ms memory retrieval"

Custom RAG Pipeline

Build a custom retrieval-augmented generation pipeline with BaseGrid as the retriever.

typescript
1import { BaseGridRetriever } from 'basegrid/llamaindex';
2import { RetrieverQueryEngine, ResponseSynthesizer } from 'llamaindex';
3
4// Create BaseGrid retriever
5const retriever = new BaseGridRetriever({
6    basegrid: bg,
7    agentId: agent.id,
8    topK: 5,
9    similarityThreshold: 0.7
10});
11
12// Create query engine with custom retriever
13const responseSynthesizer = new ResponseSynthesizer({
14    llm: llm,
15    responseMode: 'compact'
16});
17
18const queryEngine = new RetrieverQueryEngine({
19    retriever: retriever,
20    responseSynthesizer: responseSynthesizer
21});
22
23// Query with retrieval
24const answer = await queryEngine.query({
25    query: 'What are the pricing tiers?'
26});
27
28console.log(answer.response);

Agent with Memory Tool

Give your LlamaIndex agents the ability to search through stored memories.

typescript
1import { BaseGridTool } from 'basegrid/llamaindex/tools';
2import { OpenAIAgent } from 'llamaindex';
3
4// Create memory search tool
5const memoryTool = new BaseGridTool({
6    basegrid: bg,
7    agentId: agent.id,
8    name: 'search_memory',
9    description: 'Search through conversation history and stored knowledge'
10});
11
12// Create agent with tools
13const agent = new OpenAIAgent({
14    llm: llm,
15    tools: [memoryTool],
16    systemPrompt: 'You are a helpful assistant with access to persistent memory.',
17    verbose: true
18});
19
20// Use the agent
21const response = await agent.chat({
22    message: 'Search my memory for information about pricing'
23});
24
25console.log(response.response);

Document Ingestion

Load and ingest documents from your file system into BaseGrid.

typescript
1import { SimpleDirectoryReader } from 'llamaindex';
2import { BaseGridVectorStore } from 'basegrid/llamaindex';
3
4// Load documents from directory
5const reader = new SimpleDirectoryReader();
6const documents = await reader.loadData('./docs');
7
8// Create vector store
9const vectorStore = new BaseGridVectorStore({
10    basegrid: bg,
11    agentId: agent.id
12});
13
14// Ingest documents with metadata
15for (const doc of documents) {
16    await vectorStore.add([{
17        text: doc.text,
18        metadata: {
19            source: doc.metadata.file_name,
20            timestamp: new Date().toISOString()
21        }
22    }]);
23}
24
25console.log(`Ingested ${documents.length} documents`);

Next Steps