In this use case, we will embark on a journey to explore the openness and interoperability between the SAP HANA Cloud vector engine, SAP Generative AI Hub, and focusing on the LangChain (Python) Framework for developing LLM applications. The goal is to equip you with the knowledge and skills to handle unstructured and semi-structured data and build efficient applications.
Harness the power of Large Language Models (LLMs) from SAP Generative AI Hub to embed structured and semi-structured data. Once embedded, this data is stored in SAP HANA Cloud, enabling seamless storage and querying of vector embeddings.
This section utilizes Jupyter notebooks deployed in SAP Business Application Studio (BAS) to demonstrate leveraging a vector-based semantic search to implement a simple Q&A application using the Retrieval Augmented Generation (RAG) pattern. The integration of SAP Generative AI, SAP HANA Cloud vector, and LangChain framework showcases their combined strengths, emphasizing the ease and effectiveness of building advanced LLM applications with these interoperable tools.

Documents to be included in vector analysis are fed into the model.
The contents of the files are split into smaller chunks.
Embedding functions are used to create embeddings from the file/document chunks.
The embeddings are then stored as vectors in the SAP HANA Cloud Database.
When a query or prompt is submitted, the query itself is then embedded into vector form.
The query vector is compared to the values stored as vectors in SAP HANA Cloud via a similarity/semantic search.
The most appropriate results are forwarded, along with the original query, to a large language model such as Chat GPT.
The LLM uses the results of the HANA vector search to augment its own searching capabilities, and the final answer is returned to the user.
Jupyter Notebook Extension in Business Application Studio
This exercise uses the Jupyter Notebooks extension in SAP Business Application Studio (BAS). This enables the use of python coding to leverage large language models in addition to the SAP HANA Cloud vector engine.
For the Dev Space name, use _VectorDB and select SAP HANA Native Application as the application type. On the right hand side, under the list of Additional SAP Extensions, choose Python Tools and then select Create Dev Space to start the process. This will create a dedicated development environment with all the tools required to run the notebook.

It will take a few minutes for the Dev Space to start up. The status can be seen beside the name.

Once the status changes to Running, select the name (_VectorDB) to open it.


The Cloud Foundry Sign In panel will appear. Overwrite the Cloud Foundry Endpoint with the following value, and then choose SSO as the authentication method:
| Login Option | Selection |
|---|---|
| Cloud Foundry Endpoint | {placeholder|cfapi_coreai} |
| Authentication method | SSO Passcode |
Select the Open a new browser page to generate your SSO passcode.

Enter academy-platform as the identity provider and then select the Sign in with alternative provider option.

Copy the generated passcode to the clipboard.

Paste the copied passcode in the Enter your SSO passcode field.

Sign into the Cloud Foundry Target by clicking the Sign In button.

Proceed by selecting the desired Cloud Foundry Target and Space as seen below.
| Cloud Foundry Target | Selection |
|---|---|
| Cloud Foundry Organization | {placeholder|cforg_coreai} |
| Space | dev |
Select Apply

Observe all the services associated with the target:

The following steps are to setup the required notebook.
On the Get Started page in Business Application Studio, select the option Clone from Git to import a repository from Github.

A prompt will appear on top asking for a URL to the repository. Copy and paste in the following URL and then press Enter.
1https://github.com/SAP-samples/sap-genai-hub-with-sap-hana-cloud-vector-engine.git
The following message will appear. Select Open to see the imported files in the Explorer window.

The files should now be visible in the Explorer pane on the left hand side. Select the Jupyter Notebook genAI_vectordb.ipynb to open it.


Open a new terminal in BAS by following the numbered sequence of steps.

Change to the bin folder where the cf_create_bas_sso.py script is located.
To change folder use the following shell command as seen below:
1cd bin
Create the SAP AI Core service key by executing the following command from the bin folder.
1python3 cf_create_bas_sso.py --service "default_aicore" --service_key {placeholder|userid}_key
The script will indicate if the creation was successful or not.

Once the key has been successfully created, the terminal window can be closed. Select the ‘X’ button to close it and return to the jupyter notebook.

The notebook, with executable code cells, will be displayed on the screen.

In the next section the python environment will be configured so that the code cells can be executed from within the Jupyter notebook.
Run the first code cell to install the required python modules.

When the first cell is executed, a prompt asking to choose a kernel source will appear. Select Python Environments.

On the next screen, select the recommended Python kernel (3.11.2).


Once the libraries have been successfully installed, the kernel must be restarted. Do this by clicking on the Restart option in the menu bar at the top.

On the following pop-up message box, click on Restart again to complete the process.

Please follow the instructions in the Jupyter Notebook in BAS to continue with the rest of the lesson.
