Easy Streamlit UI template for ChatBots
Project description
PocBot
Description
Simple streamlit UI to implement chatbots.
- Objective: Develop a pip installable package to create easy ChatBot interfaces.
- Scope: First iteration - Simple UI. It should work out of the box.
To see it used in a real project, check out the e-commerce chatbot
Installation
Installing is as easy as running pip install pocbot in your terminal. Make sure you have your virtual environment activated!
Usage
Here's a basic example on how to use pocbot. Let's assume we are chatting with Pikachu, who only knows how to say "pika" a random number of times.
The ChatBotUITemplate is the simplest approach to create a UI for a chatbot. If you are using a single chain or model, you might want to check out the SingleModelChatBotUITemplate class below.
from typing import Any, Dict
from pocbot.ui_templates import ChatBotUITemplate
from pocbot.chain_templates import ChatBotChain
from random import randint
# LLM Simulation
class Pikachu(ChatBotChain):
"""This is a test chain model"""
def __init__(self):
pass
def invoke(self, input_dict: Dict[str, Any]) -> str:
"""Method to invoke the simulated chain model"""
return " ".join(["pika"] * randint(1, 10)) + "!"
# UI
pocbot = ChatBotUITemplate(name="PikaBot", chain=Pikachu())
if __name__ == "__main__":
pocbot()
To get started, copy and paste this code in a file in your Python project. Then, one can run it as any other Streamlit app: streamlit run <file_name>.py
That's it! You should see a UI in port 8501.
Brief Explanation
The ChatBotUITemplate class is whre the magic happens. It is thought out to be a simple class that helps one create a UI for a chatbot in a few lines of code. It has the following parameters:
name: Name of the chatbot. It will be displayed in the UI.chain:ChatBotChainobject. This is the chain that will be used to generate the responses. It is explained in the next section.
The ChatBotChain class is designed to act as an interface between the UI and the logic of the chatbot. It si pretty simple. The only constraint is that it must have an invoke method (see example above) defined.
Single Model ChatBot
If you are using a single chain or model, you might benefit from using the SingleModelChatBotUITemplate class. It is essentially the same as the ChatBotUITemplate class, but it allows you to set model parameters from the UI. Right now, it only allows to choose a model and the model's temperature.
More parameters will be added in the future.
The approach is the same as before, the invoke method is still required, but it takes two more parameters: model and temperature.
Here's an example on how to use it:
from typing import Any, List
from pocbot.ui_templates import SingleModelChatBotUITemplate
from pocbot.chain_templates import SingleModelChatBotChain
from random import randint
# LLM Simulation
class Pikachu(SingleModelChatBotChain):
"""This is a test chain model"""
def __init__(self):
pass
def invoke(self, input_dict: Dict[str, Any]) -> str:
"""Method to invoke the simulated chain model"""
pika_string = " ".join(["pika"] * randint(1, 10)) + "!"
params = f"model: {input_dict.get('model')}, temperature: {input_dict.get('temperature')}"
return_string = "\n\n".join([pika_string, params])
return return_string.capitalize()
# UI
pocbot = SingleModelChatBotUITemplate(name="SM PikaBot", chain=Pikachu())
if __name__ == "__main__":
pocbot()
This is the approach used in Qubika's e-commerce chatbot. See the link to the repository in the References section.
Contributing
If you would like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome. You can find the repository here
References
Room for improvement:
- UI templates could share a common base class. There is some improvement to be done in terms of inheritance.
SingleModelChatBotChainandChatBotChainare essentially the same class... There is room for improvement here...
- ChatBot memory handled as a list of messages through
st.state. This is not ideal. Maybe it could be pickled and cached in a file... - Add Unit Tests to ensure that future changes don't break working code.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pocbot-0.3.0.tar.gz.
File metadata
- Download URL: pocbot-0.3.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.12.0 Darwin/23.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ec4d2a80684bf42e7e2cffe5a0f421ca86eb08dc5885036a3effb2446482b59
|
|
| MD5 |
e55b15aa4a52ca288c426ff438fbf6f5
|
|
| BLAKE2b-256 |
1f5d00644d6d39de0f613cff74481d603060d610e8e948b9888b3ef48858817c
|
File details
Details for the file pocbot-0.3.0-py3-none-any.whl.
File metadata
- Download URL: pocbot-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.12.0 Darwin/23.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6674e3c93d10ca3a2def7ffae236703d9b1b390ffadc9bf6ec18d543a8dd8c58
|
|
| MD5 |
2100a10671560f587670c1fc52fec8fa
|
|
| BLAKE2b-256 |
89a47de9b538f8e8f8333d9a9ceb7119068aa7db5f76df569174e6457b337370
|