Skip to main content

Add your description here

Project description

Python Chat Machines

CI Coverage pypi versions license

Small, simple, and typed Python library for building chatbots using state machines, that can be integrated with REST APIs, discord, whatsapp, telegram, etc.

Installation

Install using pip install chatmachine or uv add chatmachine.

Usage

import uvicorn
from fastapi import FastAPI

from chatmachine import ChatMachine, SessionState, State

app = FastAPI()


class MENU(State):
    def on_enter(session: SessionState):
        session.add_output("""Welcome! Select an option:                  
                            1. Log in
                            2. Exit""")

    def on_update(session: SessionState):
        if session.input == "1":
            session.change_state(LOGIN)
        elif session.input == "2":
            session.add_output("Thank you for using me")
            session.end()
        else:
            session.add_output("Invalid option. Please try again.")
            return


class LOGIN(State):
    def on_enter(session: SessionState):
        session.add_output("Please enter your token:")

    def on_update(session: SessionState):
        token = session.input
        if token != "1234":
            session.add_output("Invalid token. Please try again.")
            return
        else:
            session.add_output("Logged in successfully.")
            session.change_state(MENU)


chat_machine = ChatMachine(start_state=MENU)


@app.post("/chat")
def get_chatbot_response(user_input: str, session_id: str):
    output = chat_machine.run(user_input, session_id)
    return {"response": output}


if __name__ == "__main__":
    uvicorn.run("fastapi_example:app", host="0.0.0.0", port=8000, reload=True)

Explanation

The first State defined will automatically be selected as the initial state. The on_enter method is called when the state is entered, and the on_exit method is called when the state is exited. The on_update method is called on every input received in the state after it has been entered once.

The SessionState object is passed to the state methods, which contains the input and output attributes. The input attribute contains the user input. The add_output method is used to append a message to the output. A custom data pydantic object can be set to session.data to store any data you want to keep through states.

The change_state method is used to change the state of the session. It will automatically trigger the new state's on_enter method. The end method is used to end the session.

Planned

  • Database integrations for storing sessions.
  • More examples for different platforms.
  • More personalization options.

License

This project is licensed under the terms of the MIT license. See the LICENSE file for details.

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

chatmachine-0.1.5.tar.gz (73.8 kB view details)

Uploaded Source

Built Distribution

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

chatmachine-0.1.5-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file chatmachine-0.1.5.tar.gz.

File metadata

  • Download URL: chatmachine-0.1.5.tar.gz
  • Upload date:
  • Size: 73.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.16

File hashes

Hashes for chatmachine-0.1.5.tar.gz
Algorithm Hash digest
SHA256 667d5f98742ad7195cec07c24f15a08d25e509a8fc74bfb1145fc521ba8b54ec
MD5 82e5b7298aad8a4f0c02abc20b462697
BLAKE2b-256 6e1f50d239462e54902a53035271a63950a7fc23a9818b7a5f2041db33228bde

See more details on using hashes here.

File details

Details for the file chatmachine-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for chatmachine-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b8ca8f6c6d09691d62652901f88f15cb5511cf0c81856f365f17cf47296b9512
MD5 9727fc3b3ed844db82cb192aa596ee19
BLAKE2b-256 a2f1953aa700287535cf5506a4b0b0c800a2c5d74121d8ebe9307ce8ae0a183e

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