Skip to main content

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, List
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: str, chat_history: List[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: ChatBotChain object. 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.

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: str, chat_history: List[Any], model: str, temperature: float) -> str:
        """Method to invoke the simulated chain model"""
        pika_string = " ".join(["pika"] * randint(1, 10)) + "!"
        params = self._build_chain(model=model, temperature=temperature)
        return_string = "\n\n".join([pika_string, params])
        return return_string.capitalize()
    
    def _build_chain(self, model: str, temperature: float) -> Any:
        """Build the chain used to generate the response"""
        return f"Model: {model}, Temperature: {temperature}"


# 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.
  • 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

pocbot-0.2.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pocbot-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pocbot-0.2.0.tar.gz.

File metadata

  • Download URL: pocbot-0.2.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Darwin/23.2.0

File hashes

Hashes for pocbot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 84bdb948826894ff45a48234c9708524b83114aa54051dc36b9b4181cdd805bb
MD5 1c525232807e614f37121cd346a69d65
BLAKE2b-256 b2d36b5a0f7a81c536ca4f6e9bd66d1358995a9bae7bab075b8853a329faed16

See more details on using hashes here.

File details

Details for the file pocbot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pocbot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 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

Hashes for pocbot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61df288233c89aa3a313576e7a467db775f9d9ed05cddf8d50100d329facbe81
MD5 cf8984b87f0c6dbad3209e92a7b205d4
BLAKE2b-256 df83bdd3f448c53f874664c6ce5a6323f5895cc746d4d8a4d390de808a792279

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page