Skip to main content

ChatFAQ SDK

Project description

ChatFAQ's SDK

For those chatbots with complex Finite State Machine (FSM) behaviours, you will probably want to run them on a separate process, that is what for the SDK is made for. Its primary function is to execute the FSM's computations (transition's conditions and states) by running Remote Procedure Call (RPC) server that listen to the back-end requests.

Prerequisites

Make sure the next list of packages are installed on your system:

  • Python 3.10
  • python3.10-dev
  • python3.10-distutils
  • poetry

Installation

PYPI

poetry add chatfaq-sdk

Local build

Set Up:

Install project dependencies:

poetry install

Run

First of all, create a .env file with the needed variables set. You can see an example of those on .env_example file. Next you can see the explanation of each variable:

CHATFAQ_RETRIEVAL_HTTP: The address for the HTTP of the back-end server.

CHATFAQ_BACKEND_WS: The address for the WS of the back-end server.

CHATFAQ_TOKEN: The token to authenticate with the back-end server. You can retrieve the auth token from the backend server:

curl -X POST -u username:password http://localhost:8000/back/api/login/

Run the example:

make run_example

This will run the example FSM that is located in ./examples/model_example/init.py file. You can modify this file to test your own FSMs.

Docker

Alternatively you can simply run the server using docker.

Build

docker build -t chatfaq-sdk .

Run

docker run chatfaq-sdk

Usage

Simple example

This is just a dummy example that displays the basic usage of the library.

We are going to build the next FSM:

fsm

Import basic modules to build your first FMS:

import os
import random
from chatfaq_sdk import ChatFAQSDK
from chatfaq_sdk.fsm import FSMDefinition, State, Transition
from chatfaq_sdk.conditions import Condition
from chatfaq_sdk.layers import Text

Declare the 3 possible states of our FSM:

def send_greeting(ctx: dict):
    yield Text("Hello!")
    yield Text("How are you?", allow_feedback=False)

greeting_state = State(name="Greeting", events=[send_greeting], initial=True)


def send_answer(ctx: dict):
    last_payload = ctx["conv_mml"][-1]["stack"][0]["payload"]
    yield Text(
        f'My answer to your message: "{last_payload}" is: {random.randint(0, 999)}'
    )
    yield Text(f"Tell me more")

answering_state = State(
    name="Answering",
    events=[send_answer],
)


def send_goodbye(ctx: dict):
    yield Text("Byeeeeeeee!", allow_feedback=False)

goodbye_state = State(
    name="Goodbye",
    events=[send_goodbye],
)

Declare the only computable condition for the transitions of our FSM:

def is_saying_goodbye(ctx: dict):
    if ctx["conv_mml"][-1]["stack"][0]["payload"] == "goodbye":
        return Condition(1)
    return Condition(0)

Now lets glue everything together:

Declare our transitions

any_to_goodbye = Transition(dest=goodbye_state, conditions=[is_saying_goodbye])

greeting_to_answer = Transition(
    source=greeting_state,
    dest=answering_state,
    unless=[is_saying_goodbye],
)
answer_to_answer = Transition(
    source=answering_state, dest=answering_state, unless=[is_saying_goodbye]
)

Build the final instance of our FSM:

fsm_definition = FSMDefinition(
    states=[greeting_state, answering_state, goodbye_state],
    transitions=[greeting_to_answer, any_to_goodbye, answer_to_answer],
)

Finally, run the RPC server loop with the previously built FSM:

import os

sdk = ChatFAQSDK(
    chatfaq_retrieval_http="http://localhost:8000",
    chatfaq_ws="ws://localhost:8000",
    token=os.getenv("CHATFAQ_TOKEN"),
    fsm_name="my_first_fsm",
    fsm_definition=fsm_definition,
)
sdk.connect()

The resulting FSM looks like this:

Other useful info

Build the docs

go inside the doc directory and run:

poetry run make html

Publish package

PYPI test

add repository to poetry config

poetry config repositories.chatfaq-sdk https://test.pypi.org/legacy/

get token from https://test.pypi.org/manage/account/token/

store token using

poetry config pypi-token.chatfaq-sdk pypi-YYYYYYYY

PYPI production

get token from https://pypi.org/manage/account/token/

store token using

poetry config pypi-token.chatfaq-sdk pypi-XXXXXXXX

Each time you need to publish

Bump version

poetry version prerelease

or

poetry version patch

Poetry Build

poetry build

Poetry Publish

To TestPyPi

poetry publish -r chatfaq-sdk

To PyPi

poetry publish

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

chatfaq_sdk-0.1.20.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

chatfaq_sdk-0.1.20-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file chatfaq_sdk-0.1.20.tar.gz.

File metadata

  • Download URL: chatfaq_sdk-0.1.20.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.13 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for chatfaq_sdk-0.1.20.tar.gz
Algorithm Hash digest
SHA256 7ab33e0e19dd3d9beb811f2ce8f10755b74705f01fdbd357828d66932a7c0812
MD5 72e25096c6e50d808c815923439dfe76
BLAKE2b-256 18f253667ec06c5282869e5de68c51b820b6e08ae69969581ff0ad34ff8d8c7f

See more details on using hashes here.

File details

Details for the file chatfaq_sdk-0.1.20-py3-none-any.whl.

File metadata

  • Download URL: chatfaq_sdk-0.1.20-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.13 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for chatfaq_sdk-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 704b313ddb4f2c05657beb28361cac2a63b6e94e0041a5572502031a69969b57
MD5 5f6b00f5f18409f96e95b113047beafc
BLAKE2b-256 272f91fcc82e2f003bae8b50ba5b4fafaf34e1a2a03cc59b9b28810d31c930b4

See more details on using hashes here.

Supported by

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