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_exit(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()


@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.3.tar.gz (42.3 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.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for chatmachine-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e58e41a91023a8d68ef5d041a579fc4584df78a1305b1c873b6fb2f61549fe53
MD5 8c7fdb384d7fbf438f9de9cdf6063049
BLAKE2b-256 cd29dc2d0fd28ad39b2e26dd2661c69ee91dee0f0c54ec384b45f5f1c09327ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for chatmachine-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5974d2271c1ca2f5b906728026eff48f902f1f7f1198bb9bee3c677fa698f257
MD5 63bcba49d7c6c8cf3a82d9d40d5ba526
BLAKE2b-256 3bcb528613b498876abc8e32b398550cd8be84a32f5166b83310cefd34369d62

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