Skip to main content

SAP HANA Cloud Vector Engine

SAP HANA Cloud Vector Engine is a vector store fully integrated into the SAP HANA Cloud database.

Setup

You'll first need to install either the @sap/hana-client or the hdb package, and the @langchain/community package:

npm install -S @langchain/community @sap/hana-client
# or
npm install -S @langchain/community hdb

You'll also need to have database connection to a HANA Cloud instance.

OPENAI_API_KEY = "Your OpenAI API key"
HANA_HOST = "HANA_DB_ADDRESS"
HANA_PORT = "HANA_DB_PORT"
HANA_UID = "HANA_DB_USER"
HANA_PWD = "HANA_DB_PASSWORD"

API Reference:

    Create a new index from texts

    import { OpenAIEmbeddings } from "@langchain/openai";
    import hanaClient from "hdb";
    import {
    HanaDB,
    HanaDBArgs,
    } from "@langchain/community/vectorstores/hanavector";

    const connectionParams = {
    host: process.env.HANA_HOST,
    port: process.env.HANA_PORT,
    user: process.env.HANA_UID,
    password: process.env.HANA_PWD,
    // useCesu8 : false
    };
    const client = hanaClient.createClient(connectionParams);
    // connet to hanaDB
    await new Promise<void>((resolve, reject) => {
    client.connect((err: Error) => {
    // Use arrow function here
    if (err) {
    reject(err);
    } else {
    console.log("Connected to SAP HANA successfully.");
    resolve();
    }
    });
    });
    const embeddings = new OpenAIEmbeddings();
    const args: HanaDBArgs = {
    connection: client,
    tableName: "test_fromTexts",
    };
    // This function will create a table "test_fromTexts" if not exist, if exists,
    // then the value will be appended to the table.
    const vectorStore = await HanaDB.fromTexts(
    ["Bye bye", "Hello world", "hello nice world"],
    [
    { id: 2, name: "2" },
    { id: 1, name: "1" },
    { id: 3, name: "3" },
    ],
    embeddings,
    args
    );

    const response = await vectorStore.similaritySearch("hello world", 2);

    console.log(response);

    /* This result is based on no table "test_fromTexts" existing in the database.
    [
    { pageContent: 'Hello world', metadata: { id: 1, name: '1' } },
    { pageContent: 'hello nice world', metadata: { id: 3, name: '3' } }
    ]
    */
    client.disconnect();

    API Reference:

    Create a new index from a loader and perform similarity searches

    import hanaClient from "hdb";
    import {
    HanaDB,
    HanaDBArgs,
    } from "@langchain/community/vectorstores/hanavector";
    import { OpenAIEmbeddings } from "@langchain/openai";
    import { TextLoader } from "langchain/document_loaders/fs/text";
    import { CharacterTextSplitter } from "langchain/text_splitter";

    const connectionParams = {
    host: process.env.HANA_HOST,
    port: process.env.HANA_PORT,
    user: process.env.HANA_UID,
    password: process.env.HANA_PWD,
    // useCesu8 : false
    };
    const client = hanaClient.createClient(connectionParams);
    // connet to hanaDB
    await new Promise<void>((resolve, reject) => {
    client.connect((err: Error) => {
    // Use arrow function here
    if (err) {
    reject(err);
    } else {
    console.log("Connected to SAP HANA successfully.");
    resolve();
    }
    });
    });
    const embeddings = new OpenAIEmbeddings();
    const args: HanaDBArgs = {
    connection: client,
    tableName: "test_fromDocs",
    };
    // Load documents from file
    const loader = new TextLoader("./state_of_the_union.txt");
    const rawDocuments = await loader.load();
    const splitter = new CharacterTextSplitter({
    chunkSize: 500,
    chunkOverlap: 0,
    });
    const documents = await splitter.splitDocuments(rawDocuments);
    // Create a LangChain VectorStore interface for the HANA database and specify the table (collection) to use in args.
    const vectorStore = new HanaDB(embeddings, args);
    await vectorStore.initialize();
    // Delete already existing documents from the table
    await vectorStore.delete({ filter: {} });
    // add the loaded document chunks
    await vectorStore.addDocuments(documents);

    // similarity search (default:“Cosine Similarity”, options:["euclidean", "cosine"])
    const query = "What did the president say about Ketanji Brown Jackson";
    const docs = await vectorStore.similaritySearch(query, 2);
    docs.forEach((doc) => {
    console.log("-".repeat(80));
    console.log(doc.pageContent);
    });
    /*
    --------------------------------------------------------------------------------
    One of the most serious constitutional responsibilities a President has is nominating
    someone to serve on the United States Supreme Court.

    And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson.
    One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
    --------------------------------------------------------------------------------
    As I said last year, especially to our younger transgender Americans, I will always have your back as your President,
    so you can be yourself and reach your God-given potential.

    While it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year.
    From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice
    */

    // similiarity search using euclidean distance method
    const argsL2d: HanaDBArgs = {
    connection: client,
    tableName: "test_fromDocs",
    distanceStrategy: "euclidean",
    };
    const vectorStoreL2d = new HanaDB(embeddings, argsL2d);
    const docsL2d = await vectorStoreL2d.similaritySearch(query, 2);
    docsL2d.forEach((docsL2d) => {
    console.log("-".repeat(80));
    console.log(docsL2d.pageContent);
    });

    // Output should be the same as the cosine similarity search method.

    // Maximal Marginal Relevance Search (MMR)
    const docsMMR = await vectorStore.maxMarginalRelevanceSearch(query, {
    k: 2,
    fetchK: 20,
    });
    docsMMR.forEach((docsMMR) => {
    console.log("-".repeat(80));
    console.log(docsMMR.pageContent);
    });
    /*
    --------------------------------------------------------------------------------
    One of the most serious constitutional responsibilities a President has is nominating someone
    to serve on the United States Supreme Court.

    And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson.
    One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
    --------------------------------------------------------------------------------
    Groups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned
    soldiers defending their homeland.

    In this struggle as President Zelenskyy said in his speech to the European Parliament “Light will win over darkness.”
    The Ukrainian Ambassador to the United States is here tonight.

    Let each of us here tonight in this Chamber send an unmistakable signal to Ukraine and to the world.
    */
    client.disconnect();

    API Reference:

    Basic Vectorstore Operations

    import { OpenAIEmbeddings } from "@langchain/openai";
    import hanaClient from "hdb";
    // or import another node.js driver
    // import hanaClient from "@sap/haha-client";
    import { Document } from "@langchain/core/documents";
    import {
    HanaDB,
    HanaDBArgs,
    } from "@langchain/community/vectorstores/hanavector";

    const connectionParams = {
    host: process.env.HANA_HOST,
    port: process.env.HANA_PORT,
    user: process.env.HANA_UID,
    password: process.env.HANA_PWD,
    // useCesu8 : false
    };
    const client = hanaClient.createClient(connectionParams);
    // connet to hanaDB
    await new Promise<void>((resolve, reject) => {
    client.connect((err: Error) => {
    // Use arrow function here
    if (err) {
    reject(err);
    } else {
    console.log("Connected to SAP HANA successfully.");
    resolve();
    }
    });
    });
    const embeddings = new OpenAIEmbeddings();
    // define instance args
    const args: HanaDBArgs = {
    connection: client,
    tableName: "testBasics",
    };

    // Add documents with metadata.
    const docs: Document[] = [
    {
    pageContent: "foo",
    metadata: { start: 100, end: 150, docName: "foo.txt", quality: "bad" },
    },
    {
    pageContent: "bar",
    metadata: { start: 200, end: 250, docName: "bar.txt", quality: "good" },
    },
    ];

    // Create a LangChain VectorStore interface for the HANA database and specify the table (collection) to use in args.
    const vectorStore = new HanaDB(embeddings, args);
    // need to initialize once an instance is created.
    await vectorStore.initialize();
    // Delete already existing documents from the table
    await vectorStore.delete({ filter: {} });
    await vectorStore.addDocuments(docs);
    // Query documents with specific metadata.
    const filterMeta = { quality: "bad" };
    const query = "foobar";
    // With filtering on {"quality": "bad"}, only one document should be returned
    const results = await vectorStore.similaritySearch(query, 1, filterMeta);
    console.log(results);
    /*
    [ {
    pageContent: "foo",
    metadata: { start: 100, end: 150, docName: "foo.txt", quality: "bad" }
    }
    ]
    */
    // Delete documents with specific metadata.
    await vectorStore.delete({ filter: filterMeta });
    // Now the similarity search with the same filter will return no results
    const resultsAfterFilter = await vectorStore.similaritySearch(
    query,
    1,
    filterMeta
    );
    console.log(resultsAfterFilter);
    /*
    []
    */
    client.disconnect();

    API Reference:

    Using a VectorStore as a retriever in chains for retrieval augmented generation (RAG)

    import { ChatPromptTemplate } from "@langchain/core/prompts";
    import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
    import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
    import { createRetrievalChain } from "langchain/chains/retrieval";
    import hanaClient from "hdb";
    import {
    HanaDB,
    HanaDBArgs,
    } from "@langchain/community/vectorstores/hanavector";

    // Connection parameters
    const connectionParams = {
    host: process.env.HANA_HOST,
    port: process.env.HANA_PORT,
    user: process.env.HANA_UID,
    password: process.env.HANA_PWD,
    // useCesu8 : false
    };
    const client = hanaClient.createClient(connectionParams);
    // connet to hanaDB
    await new Promise<void>((resolve, reject) => {
    client.connect((err: Error) => {
    // Use arrow function here
    if (err) {
    reject(err);
    } else {
    console.log("Connected to SAP HANA successfully.");
    resolve();
    }
    });
    });
    const embeddings = new OpenAIEmbeddings();

    const args: HanaDBArgs = {
    connection: client,
    tableName: "test_fromDocs",
    };
    const vectorStore = new HanaDB(embeddings, args);
    await vectorStore.initialize();
    // Use the store as part of a chain, under the premise that "test_fromDocs" exists and contains the chunked docs.
    const model = new ChatOpenAI({ modelName: "gpt-3.5-turbo-1106" });
    const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
    [
    "system",
    "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.\n\n{context}",
    ],
    ["human", "{input}"],
    ]);

    const combineDocsChain = await createStuffDocumentsChain({
    llm: model,
    prompt: questionAnsweringPrompt,
    });

    const chain = await createRetrievalChain({
    retriever: vectorStore.asRetriever(),
    combineDocsChain,
    });

    // Ask the first question (and verify how many text chunks have been used).
    const response = await chain.invoke({
    input: "What about Mexico and Guatemala?",
    });

    console.log("Chain response:");
    console.log(response.answer);
    console.log(
    `Number of used source document chunks: ${response.context.length}`
    );
    /*
    The United States has set up joint patrols with Mexico and Guatemala to catch more human traffickers.
    Number of used source document chunks: 4
    */
    const responseOther = await chain.invoke({
    input: "What about other countries?",
    });
    console.log("Chain response:");
    console.log(responseOther.answer);
    /* Ask another question on the same conversational chain. The answer should relate to the previous answer given.
    ....including members of NATO, the European Union, and other allies such as Canada....
    */
    client.disconnect();

    API Reference:


    Help us out by providing feedback on this documentation page: