Package: pocbot
PocBotDescriptionSimple 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 thee-commerce chatbotInstallationInstalling is as easy as runningpip install pocbotin your terminal. Make sure you have your virtual environment activated!UsageHere'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.TheChatBotUITemplateis 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 theSingleModelChatBotUITemplateclass below.fromtypingimportAny,Dictfrompocbot.ui_templatesimportChatBotUITemplatefrompocbot.chain_templatesimportChatBotChainfromrandomimportrandint# LLM SimulationclassPikachu(ChatBotChain):"""This is a test chain model"""def__init__(self):passdefinvoke(self,input_dict:Dict[str,Any])->str:"""Method to invoke the simulated chain model"""return" ".join(["pika"]*randint(1,10))+"!"# UIpocbot=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>.pyThat's it! You should see a UI in port 8501.Brief ExplanationTheChatBotUITemplateclass 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.TheChatBotChainclass 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 aninvokemethod (see example above) defined.Single Model ChatBotIf you are using a single chain or model, you might benefit from using theSingleModelChatBotUITemplateclass. It is essentially the same as theChatBotUITemplateclass, 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, theinvokemethod is still required, but it takes two more parameters:modelandtemperature.Here's an example on how to use it:fromtypingimportAny,Listfrompocbot.ui_templatesimportSingleModelChatBotUITemplatefrompocbot.chain_templatesimportSingleModelChatBotChainfromrandomimportrandint# LLM SimulationclassPikachu(SingleModelChatBotChain):"""This is a test chain model"""def__init__(self):passdefinvoke(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])returnreturn_string.capitalize()# UIpocbot=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.ContributingIf you would like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
You can find the repositoryhereReferencesStreamlitQubika's e-commerce chatbotRoom 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 throughst.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.