Building A ChatGPT Wizard with MistralAI Using Taipy
Let's learn how to build a simple chatbot using the Taipy GUI library and the Mistral-7B-Instruct-v0.1-GGUF language model from the transformers library.
- Copied!
TL; DR
Let's learn how to build a simple chatbot using the Taipy GUI library and the Mistral-7B-Instruct-v0.1-GGUF language model from the transformers library.
The walkthrough
- loading the language model
- generating responses to user prompts
- updating & clearing conversation history
- application styling
By the end of this article, we will have a basic understanding of how to build a chatbot using these tools.
1. Loading the Mistral-7B-Instruct-v0.1-GGUF model
Mistral 7B is a super-smart language model with 7 billion parameters! It beats the best 13B model, Llama 2, in all tests and even outperforms the powerful 34B model, Llama 1, in reasoning, math, and code generation. How? Mistral 7B uses smart tricks like grouped-query attention (GQA) for quick thinking and sliding window attention (SWA) to handle all sorts of text lengths without slowing down.
Source: Mistral.AI Docs
And there's more! Mistral AI Team fine-tuned Mistral 7B for specific tasks with Mistral 7B – Instruct. It outshines Llama 2 13B in chat and rocks both human and automated tests. The best part? Mistral 7B – was released under the Apache 2.0 license.
Download GGUF files using ctransformers
Install ctransformers
With no GPU acceleration
or install with ctransformers with CUDA GPU acceleration
or install with ctransformers with AMD ROCm GPU acceleration (Linux only)
or install with ctransformers with Metal GPU acceleration for macOS systems only
Load the model
All set? Let's run the code below to download and send a prompt to the model. Make sure to free up space on your computer and connect to a good internet connection.
The model will continue the statement as follows,
2. Installing Taipy
Taipy is an open-source Python library that makes it simple to create data-driven web applications. It oversees the visible part (Frontend) and the behind-the-scenes (Backend) operations. Its goal is to speed up the process of developing applications, from the early design stages to having a fully functional product ready for use.
⭐⭐⭐ Star us on GitHub ⭐⭐⭐
Source: Taipy Docs
Requirement: Python 3.8 or later on Linux, Windows, and Mac.
Open up a terminal and run the following command to install Taipy with all its dependencies.
We're set!
Let's say hello to Taipy!
Save the code as a Python file: e.g., hi_taipy.py
.
Run the code and wait for the client link http://127.0.0.1:5000
to display and pop up in your browser.
You can change the port if you want to run multiple servers at the same time with Gui(...).run(port=xxxx)
.
3. Create a chat interface with Taipy
Now we are familiar with Taipy, let's get our hands dirty and build our chat interface.
Step 1: Import the AutoModelForCausalLM class from the ctransformers library
This will generate text using pre-trained language models.
Step 2: Import the Taipy GUI library
Used to build the chatbot user interface
Step 3: Load the Mistral-7B-Instruct-v0.1-GGUF model
Step 4: Initialize the prompt and response variables as empty strings.
Step 5: Define the chat function
Called when the user clicks the "Chat" button in the user interface. This function takes the current state of the GUI as input, generates text using the pre-trained language model based on the user's prompt, and updates the response variable in the state.
Step 6: Define the user interface with Taipy GUI
Time to define the user interface for our chatbot using the Taipy GUI library. The user interface consists of an input field where the user can enter a prompt, a "Chat" button that triggers the chat function, and a display area where the chatbot's response is shown.
Step 7: Run!
Now, let's run the Taipy GUI application using the run method.
Full Code
Results
Here it is, a simple chat interface!
Let's level up our application to become a chatbot, as we imagine.
4. Set the Mistral AI Chatbot
Step 1:
In this step, we initialize the prompt and response and the conversation
Step 2:
Update the chat function
Step 3:
Add clear_conversation function to clear the conversation history.
The function sets the state.conversation object to a new dictionary with a single key-value pair, where the key is Conversation, and the value is an empty list. This effectively clears the conversation history, as the state.conversation object is now an empty dictionary with a single key-value pair containing an empty list. The updated state.conversation object will be reflected in the chatbot UI, showing an empty conversation history.
Step 4:
Let's define the layout of the user interface for the Chatbot. Let's add a logo by downloading and saving it in the same directory as the script. Then attach clear_conversation to the New chat button.
5. Styling the chatbot
Now, let's style our chat UI by floating the response to the left and the prompt to the right-hand side with some CSS.
Step 1:
Create a CSS file with the same title as the python file and save it in the same directory.
Step 2:
Create the style_conv function: The style_conv function is a callback function used to apply styles to the conversation history table in the Taipy GUI. It takes three arguments: state, idx, and row.
The state argument is a dictionary containing the GUI's current state, including the conversation history. The idx argument is the index of the current row in the table, and the row argument is the index of the current column in the table.
The function checks the value of the idx argument to determine which style to apply to the current row. If idx is None, the function returns None, indicating no style should be applied.
If idx is an even number, the function returns the string user_mssg, corresponding to the CSS class for the user's prompts. If idx is an odd number, the function returns the string mistral_mssg, corresponding to the CSS class for the chatbot's responses.
Here is the code for the style_conv function:
To use the style_conv function in the Taipy GUI, we need to pass it as the value of the style attribute in the table element. For example:
Step 3:
Add a sidebar: Redefine the page to add the sidebar.
Conclusion
To conclude with, this article demonstrated how to build a simple chatbot using the Taipy GUI library and the Mistral-7B-Instruct-v0.1-GGUF language model from the ctransformers library. The code provided shows how to load the language model, generate responses to user prompts, update the conversation history, and clear the conversation history. The chatbot's UI, built using the Taipy GUI library, provides a user-friendly interface for interacting with the chatbot. Overall, this article provides a useful starting point for building more sophisticated chatbots using these Taipy.
Resources:
- HuggingFace: Mistral-7B-Instruct-v0.1-GGUF
- Taipy: Taipy Docs
- Copied!