> For the complete documentation index, see [llms.txt](https://docs.envector.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.envector.io/1.4.x/integrations/langchain-envector.md).

# LangChain

We provide an integration with LangChain 🔗 to use enVector as a vector store. This allows you to leverage enVector's capabilities for storing and retrieving vector embeddings within AI applications.

## Setup

You first need to install the langchain-envector package:

```bash
pip install langchain-envector
```

## Initialization

Key dataclasses live in `langchain_envector.config`:

* `ConnectionConfig`: address or host/port for EnVector.
* `KeyConfig`: key path, key ID, optional preset/eval mode.
* `IndexSettings`: index name, dimension (32–4096), query encryption mode, optional output fields and fetch parameters.
* `EnvectorConfig`: wraps the above and enables auto-creation via `create_if_missing`.

Example Usage:

```python
from langchain_envector.config import ConnectionConfig, EnvectorConfig, IndexSettings, KeyConfig
from langchain_envector.vectorstore import Envector

cfg = EnvectorConfig(
    connection=ConnectionConfig(
        address="localhost:50050", # your envector address
        # access_token="..."       # if needed
    ),
    key=KeyConfig(
        key_path="./keys",         # your key path
        key_id="my_key_id",        # your key id
    ),
    index=IndexSettings(
        index_name="my_index",     # your index name
        dim=1536,                  # your embedding vector dimension
    ),
)
```

To use the vector store, you also need to provide an embeddings model.

The following example uses OpenAI embeddings with environment variable `OPENAI_API_KEY`:

```python
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
```

Now, you can initialize the Envector vector store with the configuration and embeddings:

```python
store = Envector(config=cfg, embeddings=embeddings)
```

## Manage vector store

Once you have initialized your vector store, we can interact with it by adding items. ​

### Add items to vector store

Use the `add_documents` or `add_texts` method to add content.

```python
from langchain_core.documents import Document

docs = [
    Document(
        page_content="enVector is a vector search engine that lets you search directly on encrypted data.", 
        metadata={"source": "document.pdf", "page": 1, "chunk": 0}
    ),
    Document(
        page_content="LangChain is a open-source framework for developing applications powered by language models.", 
        metadata={"source": "document.pdf", "page": 1, "chunk": 1}
    ),
]
store.add_documents(docs)
```

## Query vector store

Once the vector store has been prepared to search, we can query it with your query or query vector.

### Similarity search

Performing a similarity search with filtering on top-k results can be done as follows:

```python
results = store.similarity_search_with_score(query, k=1)
for doc, score in results:
    print(f"* [SCORE={score:.4f}] {doc.page_content} [{doc.metadata}]")
```

The methods `similarity_search` and `similarity_search_with_vector` (with `embeddings.embed_query()`) are also available to perform vector search.

## Usage for Retrieval-Augmented Generation

In RAG, you take the query as a question that is to be answered by a LLM, but the LLM must answer the question based on the information it is seeing from the vectorstore.

The following example shows how to set up a retrieval chain using OpenAI LLM and the enVector vector store:

```python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

# llm (set OPENAI_API_KEY)
llm = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0.0,
)

# Create a prompt template
prompt = ChatPromptTemplate.from_template(
    """Answer the question using the following context:
    {context}

    Question: {question}
    """
)

# Create a retrieval chain
chain = (
    {
        "context": store.as_retriever(),
        "question": RunnablePassthrough(),
    }
    | prompt
    | llm
    | StrOutputParser()
)

# Invoke the chain
chain.invoke("What is enVector?")
```

## Troubleshooting

* Connection issues: verify EnVector address and registered keys.
* Embeddings mismatch: ensure embedding dimension equals `index.dim` when supplying vectors.
* Unexpected raw strings: confirm inserts used the JSON envelope.
* Key Issues: check key's metadata to sync with the registered key if facing any key issue.

## API reference

The main class is `langchain_envector.vectorstore.Envector`, which extends `langchain.vectorstores.VectorStore`. It provides the following methods:

* `add_texts`: Add a list of texts with optional metadata to the vector store.
* `add_documents`: Add a list of LangChain `Document` objects to the vector store.
* `similarity_search`: Perform a similarity search for the given query and return the top-k matching documents.
* `similarity_search_with_score`: Perform a similarity search for the given query and return the top-k matching documents along with their similarity scores.
* `similarity_search_with_vector`: Perform a similarity search using the provided query vector and return the top-k matching documents.

See more details in the GitHub repository: <https://github.com/CryptoLabInc/langchain-envector>.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.envector.io/1.4.x/integrations/langchain-envector.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
