Building a Simple LLM Chatbot that works on User Data in minutes ๐Ÿค–

Building a Simple LLM Chatbot that works on User Data in minutes ๐Ÿค–

ยท

2 min read

In this article, Iโ€™ll guide you in creating a chatbot that utilizes the power of user data . Lets transform your data into interactive conversations!

๐๐จ๐ฐ๐ž๐ซ ๐จ๐Ÿ ๐ฒ๐จ๐ฎ๐ซ ๐จ๐ฐ๐ง ๐๐š๐ญ๐š
Imagine having a chatbot that understands your documents and your content. Thatโ€™s exactly what weโ€™re building today. By leveraging your own user data, like PDFs, this chatbot will provide tailored answers and insights

๐๐š๐ซ๐ญ ๐Ÿ: ๐’๐ž๐ญ๐ญ๐ข๐ง๐  ๐”๐ฉ ๐ญ๐ก๐ž ๐„๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐ž๐ง๐ญ
Before we delve into the magic, ensure you have these files ready:
1) app.py: Here goes the code for the actual app
2) embeddings.py: To create vector embeddings of the textual data
3) โ€œdataโ€ directory: Add your PDF files here
4) .env file: Add your Hugging Face API token here

5) requirements.txt : for all the libraries required

๐ˆ๐ฆ๐ฉ๐จ๐ซ๐ญ๐ฌ

After setting up these files

Type ๐˜ฑ๐˜ช๐˜ฑ ๐˜ช๐˜ฏ๐˜ด๐˜ต๐˜ข๐˜ญ๐˜ญ -๐˜ณ ๐˜ณ๐˜ฆ๐˜ฒ๐˜ถ๐˜ช๐˜ณ๐˜ฆ๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต๐˜ด.๐˜ต๐˜น๐˜ต in your terminal and hit Enter , before proceeding to the next step

๐๐š๐ซ๐ญ ๐Ÿ: ๐‚๐ซ๐ž๐š๐ญ๐ข๐ง๐  ๐•๐ž๐œ๐ญ๐จ๐ซ ๐„๐ฆ๐›๐ž๐๐๐ข๐ง๐ ๐ฌ
Lets kick things off with embeddings.py This script is where the magic begins. The embeddings will be generated using the sentence transformers model and saved locally in a vector store DB.

๐๐š๐ซ๐ญ ๐Ÿ‘: ๐’๐ž๐ญ๐ญ๐ข๐ง๐  ๐”๐ฉ ๐ญ๐ก๐ž ๐‚๐ก๐š๐ญ๐›๐จ๐ญ
Now, letโ€™s dive into app.py, where the good stuff happens

๐ฅ๐จ๐š๐_๐ฅ๐ฅ๐ฆ(): This function loads the language model via the HF API for the chatbot. I am using Mistralโ€™s Instruct model for this case

๐œ๐ซ๐ž๐š๐ญ๐ž_๐ฉ๐ซ๐จ๐ฆ๐ฉ๐ญ_๐ญ๐ž๐ฆ๐ฉ() : This creates the custom prompt template that adds a personal touch to your chatbotโ€™s responses . Fill the blank with however you want your agent to behave ex โ€” An expert on History , Medicine etcโ€ฆ

๐‚๐ซ๐ž๐š๐ญ๐ข๐ง๐  ๐ซ๐ž๐ญ๐ซ๐ข๐ž๐ฏ๐š๐ฅ ๐œ๐ก๐š๐ข๐ง : This step will create a retrieval-based question answering chain using Langchain.

๐‚๐ก๐š๐ข๐ง๐ฅ๐ข๐ญ ๐’๐ž๐ญ๐ฎ๐ฉ : This process will setup the server for chanlit to handle messages and send the message to the chain in order to generate the response . Here we set the empty context , and handle userโ€™s message and add formatting to the generated response.

๐‘๐ฎ๐ง ๐ญ๐ก๐ž ๐œ๐ก๐š๐ญ๐›๐จ๐ญ
1) Type ๐˜ฑ๐˜บ๐˜ต๐˜ฉ๐˜ฐ๐˜ฏ ๐˜ช๐˜ฏ๐˜จ๐˜ฆ๐˜ด๐˜ต.๐˜ฑ๐˜บ in the terminal and hit enter. This step needs to be done only once to create the embeddings .
2) Once the above step is complete , run the app by executing
๐˜ค๐˜ฉ๐˜ข๐˜ช๐˜ฏ๐˜ญ๐˜ช๐˜ต ๐˜ณ๐˜ถ๐˜ฏ ๐˜ข๐˜ฑ๐˜ฑ.๐˜ฑ๐˜บ -๐˜ธ

And finally the chatbot is up and running at localhost:8000

Now you can tinker around with it and explore its maximum capabilities , play around with different prompt templates , fine tune it , try multimodal input, use different LLMs and do lots of cool stuff with it

ย