Quickstart¶
In this example we will explore the basic features of DKubeX. We will start by ingesting a dataset from our workspace, deploying a base LLM model, building a RAG chat application, finetuning the LLM model with a custom dataset, deploying the finetuned LLM model, and building a RAG chat application with the finetuned LLM model. The steps are as follows:
Prerequisites¶
You must have the current version of DKubeX installed into your system. For detailed instructions regarding installation and logging in to DKubeX, please refer to Installation.
For this example, ideally you need a a10 GPU (g5.4xlarge) node attached to your cluster.
Attention
In case of a RKE2 setup, please make sure that you have labeled the node as “a10”. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
Make sure you have access and the secret token (only if required) for that particular model which you are going to deploy (in this example, Llama2-7B).
Note
In the case the model you want to deploy is not in the DKubeX LLMs registry, you can also deploy LLMs directly from the HuggingFace model hub using the configuration file of that particular model. Make sure you have access to that particular model (in case the model is a private model).
For instructions on how to deploy the model from HuggingFace repository, please refer to Deploying Base LLMs.
Export the following variables to your workspace by running the following commands on your DKubeX Terminal.
Replace the
<username>
part with your DKubeX username.export NAMESPACE="<username>" export HOMEDIR=/home/${NAMESPACE}
This example uses the ContractNLI dataset throughout where required. You need to download the dataset on your DKubeX workspace.
Attention
Although the ContractsNLI dataset is in accordance with the terms and conditions of the Creative Commons Attribution 4.0 International Public License, it is recommended to go through the terms and conditions of the dataset before using it. You can read the terms and conditions here- https://stanfordnlp.github.io/contract-nli/#download
To download the dataset, open the Terminal application from the DKubeX UI and run the following command:
cd && wget https://stanfordnlp.github.io/contract-nli/resources/contract-nli.zip
Unzip the downloaded file using the following command. A folder called contract-nli will be created which will contain the entire dataset. Also at this point, remove the unnecessary files from the dataset folder.
cd && unzip contract-nli.zip rm -rf contract-nli/dev.json contract-nli/LICENSE contract-nli/README.md contract-nli/TERMS contract-nli/test.json contract-nli/train.json
Ingesting Data¶
Note
For detailed information regarding this section, please refer to Data ingestion and creating dataset.
Important
This example uses the BAAI/bge-large-en-v1.5
embeddings model for data ingestion.
Configuring .yaml
files for ingestion¶
You need to provide a few .yaml files to be used during the ingestion process.
On the Terminal application in DKubeX UI, run the following commands:
cd && git clone -b v0.8.3 https://github.com/dkubeio/dkubex-examples.git cp ${HOMEDIR}/dkubex-examples/rag/ingestion/ingest.yaml ${HOMEDIR}/ingest.yaml && cp ${HOMEDIR}/dkubex-examples/rag/ingestion/custom-pyloaders/custom_sdr.py ${HOMEDIR}/custom_sdr.py
You need to provide proper details on the ingest.yaml file. Run
vim ${HOMEDIR}/ingest.yaml
and make the following changes.On the
reader:inputs:loader_args:input_dir:
section, provide the absolute path to your dataset folder, i.e. in this case,"/home/<your username>/contract-nli/"
(Provide your DKubeX username in place of<your username>
).On the
reader:pyloader:
section, provide the absolute path to thecustom_sdr.py
file, i.e. in this case,/home/<your username>/custom_sdr.py
(Provide your DKubeX username in place of<your username>
).
You can also modify and customize several other options in the ingest.yaml file according to your needs, including the splitter class, chunk size, embedding model to be used, etc.
Triggering ingestion and creating dataset¶
Open the Terminal application in DKubeX UI.
Use the following command to perform data ingestion and create the dataset. A dataset named contracts will be created.
d3x dataset ingest -d contracts --config ${HOMEDIR}/ingest.yaml
Note
A few documents from the ContractsNLI dataset may show errors during the ingestion process. This is expected behaviour ar those documents’ format are not suitable for ingestion.
The time taken for the ingestion process to complete depends on the size of the dataset. The ContractsNLI dataset contains 605 documents and the ingestion process may take around 30 minutes to complete. Please wait patiently for the process to complete.
In case the terminal shows a timed-out error, that means the ingestion is still in progress, and you will need to run the command provided on the CLI after the error message to continue to get the ingestion logs.
The record of the ingestion and related artifacts are also stored in the MLFlow application on DKubeX UI.
To check if the dataset has been created, stored and are ready to use, use the following command:
d3x dataset list
To check the list of documents that has been ingested in the dataset, use the following command:
d3x dataset show -d contracts
Deploying LLMs from Model Catalog¶
Note
For detailed information regarding this section, please refer to Deploying LLMs in DKubeX.
Here we will deploy the base Llama2-7B model, which is already pre-registered with DKubeX.
Note
This workflow requires an a10 GPU node. Make sure your cluster is equipped with such. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
To list all LLM models registered with DKubeX, use the following command.
d3x llms list
Export the access token for the Llama2-7B model. Replace the
<Huggingface token for Llama2-7B>
part with the token for the Llama2-7B model.export HF_TOKEN="<Huggingface token for Llama2-7B>"
Deploy the base Llama2-7B model using the following command.
d3x llms deploy --name=llama27bbase --model=meta-llama/Llama-2-7b-chat-hf --token ${HF_TOKEN} --type=a10 --publish
Note
In case you are using a EKS setup, please change the value of the flag --type from a10 to g5.4xlarge in the following command. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
You can check the status of the deployment from the Deployments page in DKubeX or by running the following command.
d3x serve list
Wait until the deployment is in running state.
Building your first RAG chat application¶
Note
For detailed information regarding this section, please refer to Creating and accessing the chatbot application.
From the DKubeX UI, open and log into the SecureLLM application. Once open, click on the Admin Login button and log in using the admin credentials provided during installation.
Hint
In case you do not have the credentials for logging in to SecureLLM, please contact your administrator.
On the left sidebar, click on the Keys menu and go to the Application Keys tab on that page.
To create a new key for your application, use the following steps:
A pop-up window will show up on your screen containing the application key for your new application. Alternatively, you can also access your application key from the list of keys in the Application Key tab.
Copy this application key for further use, as it will be required to create the chatbot application. Also make sure that you are copying the entire key including the sk- part.
From the DKubeX UI, go to the Terminal application.
You will need to configure and use the
query.yaml
file from the dkubex-examples repo to be used in the query process in the Securechat application.Run the following command to put the
query.yaml
file on your workspace.cd && cp dkubex-examples/rag/query/query.yaml ${HOMEDIR}/query.yaml
Run
vim ${HOMEDIR}/query.yaml
and provide the following details on thequery.yaml
file. Once provided, save the file.On the
chat_engine:url:
section, provide the endpoint URL of the deployed model to be used. The endpoint URL can be found on the Deployments page of DKubeX UI.On the
chat_engine:llmkey:
section, provide the serving token for the deployed model to be used. To find the serving token, go to the Deployments page of DKubeX UI and click on the deployed model name. The serving token will be available on the model details page.On the
securellm:appkey:
section, provide the application key that you created earlier on the SecureLLM application.On the
securellm:dkubex_url:
section, provide the URL to access DKubeX.On the
tracking:experiment:
section, provide a name for the experiment under which the query records and artifacts will be stored in MLFlow.
You will need to configure and use the
securechat.yaml
file from the dkubex-examples repo to configure and create the chatbot application.Run the following command to put the
securechat.yaml
file on your workspace.cd && cp dkubex-examples/rag/securechat/securechat.yaml ${HOMEDIR}/securechat.yaml
Run
vim ${HOMEDIR}/securechat.yaml
and provide the following details on thesecurechat.yaml
file. Once provided, save the file.On the
name:
field, provide a unique name to be used for the chatbot application. In this example, we will use the namendabase
.On the
env:SECUREAPP_ACCESS_KEY:
field, provide a password which will be used to access the chatbot application.On the
env:FMQUERY_ARGS:
field, provide the following details:Use the argument
llm
to specify that the chatbot application will use the LLM model deployment (llama27bbase
) in DKubeX.Provide the name of the dataset, i.e.
contracts
after the--dataset
flag.Provide the absolute path of the
query.yaml
file that you created earlier after the--config
flag. It should be/home/<your username>/query.yaml
for this example. Replace the<your username>
part with your DKubeX username.
On the
ingressprefix:
field, provide a unique prefix to be used for the chatbot application. In this example, we will use the prefix/ndabase
. This will be used in the application URL. e.g.https://123.45.67.890:32443/ndabase
Launch the app deployment with the following command:
d3x apps create -c ${HOMEDIR}/securechat.yaml
To check the status of the app deployment, use the following command:
d3x apps list
Once the app deployment status becomes running, you can access the application from the Apps page of DKubeX UI. Provide the application key that you set in the
SECUREAPP_ACCESS_KEY
field earlier to start using the chat application.
Hint
You can ask the following questions to the chatbot when using the ContractsNLI dataset:
How do I frame a confidential information clause?
What is the difference between a unilateral and mutual NDA?
What are some common exceptions to confidential information clauses?
Finetuning LLM with custom dataset¶
Note
For detailed information regarding this section, please refer to Finetuning Open Source LLMs.
You will need to use a custom python script for getting the chunks to finetune your LLM model. Run the following command to pull the script to your workspace.
cd && cp dkubex-examples/rag/finetuning/extract_chunks.py ${HOMEDIR}/extract_chunks.py
Generate the chunks using the following command. Replace the
<ingestion run ID on MLFlow>
part with the run ID of the ingestion run for your dataset on the MLFlow application.python3 ${HOMEDIR}/extract_chunks.py --experiment_name chunk-generation --run_id <ingestion run ID on MLFlow> -d ${HOMEDIR}/chunks_for_finetuning/
Train the LLM with the chunks using the following command:
Note
In case you are using a EKS setup, please change the value of the flag -t from a10 to g5.4xlarge. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
d3x fm tune model finetune -j llama27bfinetune -e 1 -b 20 -l ${HOMEDIR}/chunks_for_finetuning -o ${HOMEDIR}/ft-output/ -c 8 -m 64 -g 1 -t a10 -n meta-llama/Llama-2-7b-chat-hf --ctx-len 512
Attention
The time taken by the finetuning process depends on the size of the dataset. The ContractsNLI dataset contains 605 documents and the finetuning process may take around one hour to complete. Please wait patiently for the process to complete.
In case the terminal shows a timed-out error, that means the finetuning is still in progress, and you will need to run the command provided on the CLI after the error message to continue to get the finetuning logs.
You will need the absolute path to the finetuned model checkpoint to merge the finetuned model with the base model. Use the following command to get the absolute path to the finetuned model checkpoint:
echo ${HOMEDIR}/ft-output/meta-llama/Llama-2-7b-chat-hf/TorchTrainer_*/TorchTrainer_*/checkpoint*/
Export the absolute path to the finetuned model checkpoint to be used during the merge process with the following command. Export the
<checkpoint absolute path>
part with the absolute path to the finetuned model checkpoint you got in the previous step.export CHECKPOINT="<checkpoint absolute path>"
Merge the finetuned model checkpoint with the base model to create the final finetuned model using the following command:
Note
In case you are using a EKS setup, please change the value of the flag -t from a10 to g5.4xlarge. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
d3x fm tune model merge -j llama27bmerge -n meta-llama/Llama-2-7b-chat-hf -cp ${CHECKPOINT} -o ${HOMEDIR}/merge_output -t a10
To quantize the finetuned model, use the following command:
Note
In case you are using a EKS setup, please change the value of the flag -t from a10 to g5.4xlarge. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
d3x fm tune model quantize -j llama27bquantize -p ${HOMEDIR}/merge_output/ -o ${HOMEDIR}/quantize_result -t a10
Attention
The time taken by the quantization process depends on the size of the dataset. The ContractsNLI dataset contains 605 documents and the quantization process may take around 30 minutes to complete. Please wait patiently for the process to complete.
In case the terminal shows a timed-out error, that means the quantization is still in progress, and you will need to run the command provided on the CLI after the error message to continue to get the quantization logs.
Importing the finetuned LLM to MLFlow¶
Note
For detailed information regarding this section, please refer to Deploying LLMs in DKubeX.
To import the finetuned LLM model to MLFlow, use the following command:
d3x models import llama27bft custom_model ${HOMEDIR}/quantize_result
Deploying the finetuned LLM¶
Note
For detailed information regarding this section, please refer to Deploying LLMs in DKubeX.
Deploy the finetuned LLM model using the following command:
Note
In case you are using a EKS setup, please change the value of the flag --type from a10 to g5.4xlarge in the following command. Also, in case you are using any other type of GPU node, make sure to use the label for that node which you have put during DKubeX installation process.
d3x llms deploy -n llama27bft --mlflow llama27bft:1 --type a10 --base_model meta-llama/Llama-2-7b-chat-hf --token ${HF_TOKEN}
Check the status of the deployment from the Deployment page in DKubeX or by running the following command. Wait until the deployment is in Running state.
d3x serve list
Building a RAG chat application with your finetuned LLM¶
Note
For detailed information regarding this section, please refer to Creating and accessing the chatbot application.
From the DKubeX UI, open and log into the SecureLLM application. Once open, click on the Admin Login button and log in using the admin credentials provided during installation.
Hint
In case you do not have the credentials for logging in to SecureLLM, please contact your administrator.
On the left sidebar, click on the Keys menu and go to the Application Keys tab on that page.
To create a new key for your application, use the following steps:
A pop-up window will show up on your screen containing the application key for your new application. Alternatively, you can also access your application key from the list of keys in the Application Key tab.
Copy this application key for further use, as it will be required to create the chatbot application. Also make sure that you are copying the entire key including the sk- part.
From the DKubeX UI, go to the Terminal application.
You will need to configure and use the
query.yaml
file from the dkubex-examples repo to be used in the query process in the Securechat application.Run the following command to put the
query.yaml
file on your workspace and rename it toquery_ft.yaml
(as one instance ofquery.yaml
is already available on your workspace from earlier).cd && cp dkubex-examples/rag/query/query.yaml ${HOMEDIR}/query_ft.yaml
Run
vim ${HOMEDIR}/query_ft.yaml
and provide the following details on thequery_ft.yaml
file. Once provided, save the file.On the
chat_engine:url:
section, provide the endpoint URL of the deployed finetuned model to be used. The endpoint URL can be found on the Deployments page of DKubeX UI.On the
chat_engine:llmkey:
section, provide the serving token for the deployed finetuned model to be used. To find the serving token, go to the Deployments page of DKubeX UI and click on the deployed model name. The serving token will be available on the model details page.On the
securellm:appkey:
section, provide the application key that you created earlier on the SecureLLM application.On the
securellm:dkubex_url:
section, provide the URL to access DKubeX.On the
tracking:experiment:
section, provide a name for the experiment under which the query records and artifacts will be stored in MLFlow.
You will need to configure and use the
securechat.yaml
file from the dkubex-examples repo to configure and create the chatbot application.Run the following command to put the
securechat.yaml
file on your workspace and rename it tosecurechat_ft.yaml
(as one instance ofsecurechat.yaml
is already available on your workspace from earlier).cd && cp dkubex-examples/rag/securechat/securechat.yaml ${HOMEDIR}/securechat_ft.yaml
Run
vim ${HOMEDIR}/securechat_ft.yaml
and provide the following details on thesecurechat_ft.yaml
file. Once provided, save the file.On the
name:
field, provide a unique name to be used for the chatbot application. In this example, we will use the namendaft
.On the
env:SECUREAPP_ACCESS_KEY:
field, provide a password which will be used to access the chatbot application.On the
env:FMQUERY_ARGS:
field, provide the following details:Use the argument
llm
to specify that the chatbot application will use the LLM model deployment (llama27bft
) in DKubeX.Provide the name of the dataset, i.e.
contracts
after the--dataset
flag.Provide the absolute path of the
query_ft.yaml
file that you created earlier after the--config
flag. It should be/home/<your username>/query_ft.yaml
for this example. Replace the<your username>
part with your DKubeX username.
On the
ingressprefix:
field, provide a unique prefix to be used for the chatbot application. In this example, we will use the prefix/ndaft
. This will be used in the application URL. e.g.https://123.45.67.890:32443/ndaft
Launch the app deployment with the following command:
d3x apps create -c ${HOMEDIR}/securechat_ft.yaml
To check the status of the app deployment, use the following command:
d3x apps list
Once the app deployment status becomes running, you can access the application from the Apps page of DKubeX UI. Provide the application key that you set in the SECUREAPP_ACCESS_KEY field earlier to start using the chat application.
Hint
You can ask the following questions to the chatbot when using the ContractsNLI dataset:
How do I frame a confidential information clause?
What is the difference between a unilateral and mutual NDA?
What are some common exceptions to confidential information clauses?
Tutorials and More Information¶
For more examples including how to train and register models and deploy user applications, please visit the following pages and go through the table provided:
Training Fashion MNIST model in DKubeX
Finetuning open-source LLMs
Deploying models registered in MLFlow in DKubeX
Deploying models from Huggingface repo
Deploying LLM registered in DKubeX
Creating a Securechat App using MPNET-v2 Embeddings and Llama2-7b Summarisation Models
Creating a Securechat App using OpenAI Embeddings and OpenAI Summarisation Models
Wine Model Finetuning using Skypilot
Llama2 Finetuning using SkyPilot