Gen AI Chat Bot Python Project using Sentence transformer model all-MiniLM-L6-v2
- Get link
- X
- Other Apps
Step 1 –
Import libraries needed for text encoding, similarity calculation, and chatbot interface.
SentenceTransformer –
Converts sentences into numerical vectors so machines can
understand the meaning of text.
scikit-learn (cosine_similarity) –
Calculates similarity between two text vectors to find how closely related they are.
NumPy –
Used for numerical computing, arrays, matrices, and fast mathematical operations.
Pandas –
Used for handling datasets in table format such as reading data, filtering,
cleaning, and manipulating rows and columns.
ipywidgets –
Creates interactive UI elements like text boxes, buttons,
sliders, and dropdowns in notebook environments.
IPython.display –
Displays widgets, HTML, images, and formatted output inside notebooks.
Step 2 –
Loading the model – SentenceTransformer loads the model "all-MiniLM-L6-v2" which converts sentences into numerical embeddings.
Step 3 – Creating product data –
A dictionary is created with product names and product descriptions
where each product has a corresponding description explaining its features.
Step 4 –
Converting dictionary to table –
Pandas converts the dictionary into a DataFrame table with columns and rows
where each row represents a product and its description.
Step 5 –
Combining product and description – The product column and description column
are merged into one combined text column so the model understands the
product name and description together.
Step 6 –
Converting text to vectors – The combined text is passed into the SentenceTransformer
model which converts each sentence into numerical embeddings
representing semantic meaning.
Step 7 –
Creating conversational message lists – Lists are created for greetings,
courtesy messages, goodbye messages, and “how are you” type messages
so the chatbot can respond naturally.
Step 8 –
Welcome message – A welcome message is created using HTML
formatting that introduces the chatbot as an AI product specialist
when the chat interface loads.
Step 9 –
Creating chat box – An HTML chat area is created using ipywidgets which
acts as the conversation window between the user and the chatbot.
Step 10 –
Creating input box – A text input box is created where the user types product
questions and it displays a placeholder prompt.
Step 11 –
Creating send button – A button is created so the user can send their message to
the chatbot.
Step 12 –
Chatbot response logic – A function processes the user message, converts it
to embeddings, compares similarity with product vectors using cosine similarity,
finds the best match, and returns the product recommendation or conversational response.
Step 13 –
Chat message handling function – Another function reads the user message
from the input box, sends it to the chatbot response function, stores
both the user message and bot reply in chat history, updates the chat interface,
and clears the input box.
Step 14 –
send_button.on_click(send_message) – Clicking the send button triggers
the function that processes and sends the message.
Step 15 –
input_box.on_submit(send_message) – Pressing Enter in
the input box also triggers the same message sending function.
Step 16 –
Display interface –
The chat box, input box, and send button are displayed together to
create the interactive chatbot UI.
Final Output –
A simple Small Language Model AI chatbot that understands
product questions, compares semantic meaning using embeddings, recommends
relevant products, and responds conversationally to greetings and common messages.
Step 1 –
Import libraries needed for text encoding, similarity calculation, and chatbot interface.
SentenceTransformer –
Converts sentences into numerical vectors so machines can
understand the meaning of text.
scikit-learn (cosine_similarity) –
Calculates similarity between two text vectors to find how closely related they are.
NumPy –
Used for numerical computing, arrays, matrices, and fast mathematical operations.
Pandas –
Used for handling datasets in table format such as reading data, filtering,
cleaning, and manipulating rows and columns.
ipywidgets –
Creates interactive UI elements like text boxes, buttons,
sliders, and dropdowns in notebook environments.
IPython.display –
Displays widgets, HTML, images, and formatted output inside notebooks.
Step 2 –
Loading the model – SentenceTransformer loads the model "all-MiniLM-L6-v2" which converts sentences into numerical embeddings.
Step 3 – Creating product data –
A dictionary is created with product names and product descriptions
where each product has a corresponding description explaining its features.
Step 4 –
Converting dictionary to table –
Pandas converts the dictionary into a DataFrame table with columns and rows
where each row represents a product and its description.
Step 5 –
Combining product and description – The product column and description column
are merged into one combined text column so the model understands the
product name and description together.
Step 6 –
Converting text to vectors – The combined text is passed into the SentenceTransformer
model which converts each sentence into numerical embeddings
representing semantic meaning.
Step 7 –
Creating conversational message lists – Lists are created for greetings,
courtesy messages, goodbye messages, and “how are you” type messages
so the chatbot can respond naturally.
Step 8 –
Welcome message – A welcome message is created using HTML
formatting that introduces the chatbot as an AI product specialist
when the chat interface loads.
Step 9 –
Creating chat box – An HTML chat area is created using ipywidgets which
acts as the conversation window between the user and the chatbot.
Step 10 –
Creating input box – A text input box is created where the user types product
questions and it displays a placeholder prompt.
Step 11 –
Creating send button – A button is created so the user can send their message to
the chatbot.
Step 12 –
Chatbot response logic – A function processes the user message, converts it
to embeddings, compares similarity with product vectors using cosine similarity,
finds the best match, and returns the product recommendation or conversational response.
Step 13 –
Chat message handling function – Another function reads the user message
from the input box, sends it to the chatbot response function, stores
both the user message and bot reply in chat history, updates the chat interface,
and clears the input box.
Step 14 –
send_button.on_click(send_message) – Clicking the send button triggers
the function that processes and sends the message.
Step 15 –
input_box.on_submit(send_message) – Pressing Enter in
the input box also triggers the same message sending function.
Step 16 –
Display interface –
The chat box, input box, and send button are displayed together to
create the interactive chatbot UI.
Final Output –
A simple Small Language Model AI chatbot that understands
product questions, compares semantic meaning using embeddings, recommends
relevant products, and responds conversationally to greetings and common messages.
- Get link
- X
- Other Apps
Comments
Post a Comment