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