> ## Documentation Index
> Fetch the complete documentation index at: https://docs.memvid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LlamaIndex

> Build LlamaIndex applications with Memvid as the vector store

Integrate Memvid with LlamaIndex to build powerful RAG applications. The `llamaindex` adapter provides native LlamaIndex components for seamless integration.

<Tabs>
  <Tab title="Node.js">
    ## Installation

    ```bash theme={null}
    npm install @memvid/sdk llamaindex @llamaindex/openai
    ```

    ## Quick Start

    ```typescript theme={null}
    import { use } from '@memvid/sdk';

    // Open with LlamaIndex adapter
    const mem = await use('llamaindex', 'knowledge.mv2');

    // Access LlamaIndex tools
    const tools = mem.tools;       // FunctionTool array
    const functions = mem.functions; // Raw function schemas

    // Use query engine
    const queryEngine = mem.asQueryEngine();
    const response = await queryEngine.query({ query: 'What is Memvid?' });
    console.log(response.response);
    ```
  </Tab>

  <Tab title="Python">
    ## Installation

    ```bash theme={null}
    pip install memvid-sdk llama-index llama-index-llms-openai
    ```

    ## Quick Start

    ```python theme={null}
    from memvid_sdk import use

    # Open with LlamaIndex adapter
    mem = use('llamaindex', 'knowledge.mv2')

    # Access LlamaIndex tools and query engine
    tools = mem.tools
    query_engine = mem.as_query_engine()
    ```
  </Tab>
</Tabs>

## Available Tools

The LlamaIndex adapter provides three tools:

| Tool          | Description                                           |
| ------------- | ----------------------------------------------------- |
| `memvid_put`  | Store documents in memory with title, label, and text |
| `memvid_find` | Search for relevant documents by query                |
| `memvid_ask`  | Ask questions with RAG-style answer synthesis         |

## Using with Agents

<Tabs>
  <Tab title="Node.js">
    ```typescript theme={null}
    import { use } from '@memvid/sdk';

    // Get Memvid tools
    const mem = await use('llamaindex', 'knowledge.mv2');
    const tools = mem.tools;

    // Tools can be used directly
    for (const tool of tools) {
      console.log(`Tool: ${tool.metadata.name}`);
      console.log(`Description: ${tool.metadata.description}`);
    }

    // Or use with LlamaIndex agents (when available)
    // Note: LlamaIndex.TS agent API is evolving
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    from memvid_sdk import use
    from llama_index.llms.openai import OpenAI
    from llama_index.core.agent import ReActAgent
    import asyncio

    # Get Memvid tools
    mem = use('llamaindex', 'knowledge.mv2')
    tools = mem.tools

    # Create ReAct agent
    llm = OpenAI(model="gpt-4o")
    agent = ReActAgent(
        name="MemvidResearcher",
        tools=tools,
        llm=llm,
        verbose=True
    )

    # Run agent
    async def run():
        response = await agent.run("Search for information about vector stores")
        print(response)

    asyncio.run(run())
    ```
  </Tab>
</Tabs>

## Using as a Query Engine

<Tabs>
  <Tab title="Node.js">
    ```typescript theme={null}
    import { use } from '@memvid/sdk';

    // Initialize
    const mem = await use('llamaindex', 'knowledge.mv2');

    // Get query engine factory
    const queryEngine = mem.asQueryEngine();

    // Query
    const response = await queryEngine.query({ query: 'What is Memvid?' });
    console.log(`Answer: ${response.response}`);

    // Access sources
    if (response.sourceNodes) {
      for (const node of response.sourceNodes) {
        console.log(`Source: ${node.node.metadata?.title}`);
      }
    }
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    from memvid_sdk import use

    # Initialize
    mem = use('llamaindex', 'knowledge.mv2', read_only=True)

    # Get query engine
    query_engine = mem.as_query_engine()

    # Query
    response = query_engine.query("What are the best practices?")
    print(response.response)

    # Access sources
    for source in response.source_nodes:
        print(f"Source: {source.node.metadata.get('title')}")
    ```
  </Tab>
</Tabs>

## Using as a Vector Store (Python)

```python theme={null}
from memvid_sdk import use
from llama_index.core import VectorStoreIndex
from llama_index.llms.openai import OpenAI

# Initialize with llamaindex adapter
mem = use('llamaindex', 'knowledge.mv2', read_only=True)

# Get the vector store
vector_store = mem.as_vector_store()

# Build index from vector store
index = VectorStoreIndex.from_vector_store(vector_store)

# Create query engine
query_engine = index.as_query_engine(
    llm=OpenAI(model="gpt-4o")
)

# Query
response = query_engine.query("Explain the architecture")
print(response)
```

## Chat Engine (Python)

```python theme={null}
from memvid_sdk import use
from llama_index.core import VectorStoreIndex
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.llms.openai import OpenAI

# Initialize
mem = use('llamaindex', 'knowledge.mv2', read_only=True)
vector_store = mem.as_vector_store()

# Build index
index = VectorStoreIndex.from_vector_store(vector_store)

# Create chat engine with memory
chat_engine = index.as_chat_engine(
    chat_mode="context",
    llm=OpenAI(model="gpt-4o"),
    memory=ChatMemoryBuffer.from_defaults(token_limit=3000)
)

# Chat
response = chat_engine.chat("What is Memvid?")
print(response)

# Follow-up (maintains context)
response = chat_engine.chat("How does search work?")
print(response)
```

## Custom Search Options

```python theme={null}
from memvid_sdk import use

mem = use('llamaindex', 'knowledge.mv2')

# Search with specific mode
results = mem.find('authentication', mode='lex', k=10)  # Lexical only
results = mem.find('user login flow', mode='sem', k=10)  # Semantic only
results = mem.find('auth best practices', mode='auto', k=10)  # Hybrid

# With scope filtering
results = mem.find('API', scope='mv2://docs/', k=5)
```

## Best Practices

1. **Use read-only mode** for retrieval-only applications
2. **Set appropriate k values** based on your context window
3. **Use hybrid mode** for best recall
4. **Close the memory** when done

```python theme={null}
mem = use('llamaindex', 'knowledge.mv2', read_only=True)
try:
    # Do work
    retriever = mem.as_retriever(k=10)
    # ... use retriever
finally:
    mem.seal()
```

## Next Steps

<CardGroup cols={2}>
  <Card title="LangChain" icon="link" href="/frameworks/langchain">
    LangChain integration
  </Card>

  <Card title="Vercel AI SDK" icon="bolt" href="/frameworks/vercel-ai">
    Vercel AI SDK integration
  </Card>
</CardGroup>
