> [SAP HANA Cloud Vector Engine](https://www.sap.com/events/teched/news-guide/ai.html#article8) is a vector store fully integrated into the `SAP HANA Cloud` database.

## Setting up[​](#setting-up "Direct link to Setting up")

Installation of the HANA database driver.

```
# Pip install necessary package%pip install --upgrade --quiet  hdbcli
```

For `OpenAIEmbeddings` we use the OpenAI API key from the environment.

```
import os# Use OPENAI_API_KEY env variable# os.environ["OPENAI_API_KEY"] = "Your OpenAI API key"
```

Create a database connection to a HANA Cloud instance.

```
from hdbcli import dbapi# Use connection settings from the environmentconnection = dbapi.connect(    address=os.environ.get("HANA_DB_ADDRESS"),    port=os.environ.get("HANA_DB_PORT"),    user=os.environ.get("HANA_DB_USER"),    password=os.environ.get("HANA_DB_PASSWORD"),    autocommit=True,    sslValidateCertificate=False,)
```

## Example[​](#example "Direct link to Example")

Load the sample document “state\_of\_the\_union.txt” and create chunks from it.

```
from langchain_community.docstore.document import Documentfrom langchain_community.document_loaders import TextLoaderfrom langchain_community.vectorstores.hanavector import HanaDBfrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import CharacterTextSplittertext_documents = TextLoader("../../modules/state_of_the_union.txt").load()text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)text_chunks = text_splitter.split_documents(text_documents)print(f"Number of document chunks: {len(text_chunks)}")embeddings = OpenAIEmbeddings()
```

Create a LangChain VectorStore interface for the HANA database and specify the table (collection) to use for accessing the vector embeddings

```
db = HanaDB(    embedding=embeddings, connection=connection, table_name="STATE_OF_THE_UNION")
```

Add the loaded document chunks to the table. For this example, we delete any previous content from the table which might exist from previous runs.

```
# Delete already existing documents from the tabledb.delete(filter={})# add the loaded document chunksdb.add_documents(text_chunks)
```

Perform a query to get the two best-matching document chunks from the ones that were added in the previous step. By default “Cosine Similarity” is used for the search.

```
query = "What did the president say about Ketanji Brown Jackson"docs = db.similarity_search(query, k=2)for doc in docs:    print("-" * 80)    print(doc.page_content)
```

Query the same content with “Euclidian Distance”. The results shoud be the same as with “Cosine Similarity”.

```
from langchain_community.vectorstores.utils import DistanceStrategydb = HanaDB(    embedding=embeddings,    connection=connection,    distance_strategy=DistanceStrategy.EUCLIDEAN_DISTANCE,    table_name="STATE_OF_THE_UNION",)query = "What did the president say about Ketanji Brown Jackson"docs = db.similarity_search(query, k=2)for doc in docs:    print("-" * 80)    print(doc.page_content)
```

## Maximal Marginal Relevance Search (MMR)[​](#maximal-marginal-relevance-search-mmr "Direct link to Maximal Marginal Relevance Search (MMR)")

`Maximal marginal relevance` optimizes for similarity to query AND diversity among selected documents. The first 20 (fetch\_k) items will be retrieved from the DB. The MMR algorithm will then find the best 2 (k) matches.

```
docs = db.max_marginal_relevance_search(query, k=2, fetch_k=20)for doc in docs:    print("-" * 80)    print(doc.page_content)
```

## Basic Vectorstore Operations[​](#basic-vectorstore-operations "Direct link to Basic Vectorstore Operations")

```
db = HanaDB(    connection=connection, embedding=embeddings, table_name="LANGCHAIN_DEMO_BASIC")# Delete already existing documents from the tabledb.delete(filter={})
```

We can add simple text documents to the existing table.

```
docs = [Document(page_content="Some text"), Document(page_content="Other docs")]db.add_documents(docs)
```

Add documents with metadata.

```
docs = [    Document(        page_content="foo",        metadata={"start": 100, "end": 150, "doc_name": "foo.txt", "quality": "bad"},    ),    Document(        page_content="bar",        metadata={"start": 200, "end": 250, "doc_name": "bar.txt", "quality": "good"},    ),]db.add_documents(docs)
```

Query documents with specific metadata.

```
docs = db.similarity_search("foobar", k=2, filter={"quality": "bad"})# With filtering on "quality"=="bad", only one document should be returnedfor doc in docs:    print("-" * 80)    print(doc.page_content)    print(doc.metadata)
```

Delete documents with specific metadata.

```
db.delete(filter={"quality": "bad"})# Now the similarity search with the same filter will return no resultsdocs = db.similarity_search("foobar", k=2, filter={"quality": "bad"})print(len(docs))
```

## Using a VectorStore as a retriever in chains for retrieval augmented generation (RAG)[​](#using-a-vectorstore-as-a-retriever-in-chains-for-retrieval-augmented-generation-rag "Direct link to Using a VectorStore as a retriever in chains for retrieval augmented generation (RAG)")

```
from langchain.memory import ConversationBufferMemoryfrom langchain_openai import ChatOpenAI# Access the vector DB with a new tabledb = HanaDB(    connection=connection,    embedding=embeddings,    table_name="LANGCHAIN_DEMO_RETRIEVAL_CHAIN",)# Delete already existing entries from the tabledb.delete(filter={})# add the loaded document chunks from the "State Of The Union" filedb.add_documents(text_chunks)# Create a retriever instance of the vector storeretriever = db.as_retriever()
```

Define the prompt.

```
from langchain_core.prompts import PromptTemplateprompt_template = """You are an expert in state of the union topics. You are provided multiple context items that are related to the prompt you have to answer.Use the following pieces of context to answer the question at the end.```{context}```Question: {question}"""PROMPT = PromptTemplate(    template=prompt_template, input_variables=["context", "question"])chain_type_kwargs = {"prompt": PROMPT}
```

Create the ConversationalRetrievalChain, which handles the chat history and the retrieval of similar document chunks to be added to the prompt.

```
from langchain.chains import ConversationalRetrievalChainllm = ChatOpenAI(model="gpt-3.5-turbo")memory = ConversationBufferMemory(    memory_key="chat_history", output_key="answer", return_messages=True)qa_chain = ConversationalRetrievalChain.from_llm(    llm,    db.as_retriever(search_kwargs={"k": 5}),    return_source_documents=True,    memory=memory,    verbose=False,    combine_docs_chain_kwargs={"prompt": PROMPT},)
```

Ask the first question (and verify how many text chunks have been used).

```
question = "What about Mexico and Guatemala?"result = qa_chain.invoke({"question": question})print("Answer from LLM:")print("================")print(result["answer"])source_docs = result["source_documents"]print("================")print(f"Number of used source document chunks: {len(source_docs)}")
```

Examine the used chunks of the chain in detail. Check if the best ranked chunk contains info about “Mexico and Guatemala” as mentioned in the question.

```
for doc in source_docs:    print("-" * 80)    print(doc.page_content)    print(doc.metadata)
```

Ask another question on the same conversational chain. The answer should relate to the previous answer given.

```
question = "What about other countries?"result = qa_chain.invoke({"question": question})print("Answer from LLM:")print("================")print(result["answer"])
```

## Standard tables vs. “custom” tables with vector data[​](#standard-tables-vs.-custom-tables-with-vector-data "Direct link to Standard tables vs. “custom” tables with vector data")

As default behaviour, the table for the embeddings is created with 3 columns:

*   A column `VEC_TEXT`, which contains the text of the Document
*   A column `VEC_META`, which contains the metadata of the Document
*   A column `VEC_VECTOR`, which contains the embeddings-vector of the Document’s text

```
# Access the vector DB with a new tabledb = HanaDB(    connection=connection, embedding=embeddings, table_name="LANGCHAIN_DEMO_NEW_TABLE")# Delete already existing entries from the tabledb.delete(filter={})# Add a simple document with some metadatadocs = [    Document(        page_content="A simple document",        metadata={"start": 100, "end": 150, "doc_name": "simple.txt"},    )]db.add_documents(docs)
```

Show the columns in table “LANGCHAIN\_DEMO\_NEW\_TABLE”

```
cur = connection.cursor()cur.execute(    "SELECT COLUMN_NAME, DATA_TYPE_NAME FROM SYS.TABLE_COLUMNS WHERE SCHEMA_NAME = CURRENT_SCHEMA AND TABLE_NAME = 'LANGCHAIN_DEMO_NEW_TABLE'")rows = cur.fetchall()for row in rows:    print(row)cur.close()
```

Show the value of the inserted document in the three columns

```
cur = connection.cursor()cur.execute(    "SELECT VEC_TEXT, VEC_META, TO_NVARCHAR(VEC_VECTOR) FROM LANGCHAIN_DEMO_NEW_TABLE LIMIT 1")rows = cur.fetchall()print(rows[0][0])  # The textprint(rows[0][1])  # The metadataprint(rows[0][2])  # The vectorcur.close()
```

Custom tables must have at least three columns that match the semantics of a standard table

*   A column with type `NCLOB` or `NVARCHAR` for the text/context of the embeddings
*   A column with type `NCLOB` or `NVARCHAR` for the metadata
*   A column with type `REAL_VECTOR` for the embedding vector

The table can contain additional columns. When new Documents are inserted into the table, these additional columns must allow NULL values.

```
# Create a new table "MY_OWN_TABLE" with three "standard" columns and one additional columnmy_own_table_name = "MY_OWN_TABLE"cur = connection.cursor()cur.execute(    (        f"CREATE TABLE {my_own_table_name} ("        "SOME_OTHER_COLUMN NVARCHAR(42), "        "MY_TEXT NVARCHAR(2048), "        "MY_METADATA NVARCHAR(1024), "        "MY_VECTOR REAL_VECTOR )"    ))# Create a HanaDB instance with the own tabledb = HanaDB(    connection=connection,    embedding=embeddings,    table_name=my_own_table_name,    content_column="MY_TEXT",    metadata_column="MY_METADATA",    vector_column="MY_VECTOR",)# Add a simple document with some metadatadocs = [    Document(        page_content="Some other text",        metadata={"start": 400, "end": 450, "doc_name": "other.txt"},    )]db.add_documents(docs)# Check if data has been inserted into our own tablecur.execute(f"SELECT * FROM {my_own_table_name} LIMIT 1")rows = cur.fetchall()print(rows[0][0])  # Value of column "SOME_OTHER_DATA". Should be NULL/Noneprint(rows[0][1])  # The textprint(rows[0][2])  # The metadataprint(rows[0][3])  # The vectorcur.close()
```

Add another document and perform a similarity search on the custom table.

```
docs = [    Document(        page_content="Some more text",        metadata={"start": 800, "end": 950, "doc_name": "more.txt"},    )]db.add_documents(docs)query = "What's up?"docs = db.similarity_search(query, k=2)for doc in docs:    print("-" * 80)    print(doc.page_content)
```