Skip to main content

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 following 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 Message

Declare the 3 possible states of our FSM:

def send_greeting(ctx: dict):
    yield Message("Hello!")
    yield Message("How are you?")

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


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

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


def send_goodbye(ctx: dict):
    yield Message("Byeeeeeeee!")

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"]["content"] == "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()

Metadata

Release files for chatfaq_sdk 0.1.57

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chatfaq_sdk 0.1.57
File Size Uploaded Provenance Actions
chatfaq_sdk-0.1.57.tar.gz 18.0 kB Without provenance Details for chatfaq_sdk-0.1.57.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chatfaq_sdk 0.1.57
File Interpreter ABI Platform Actions
chatfaq_sdk-0.1.57-py3-none-any.whl Python 3 none any Details for chatfaq_sdk-0.1.57-py3-none-any.whl

Total release size: 40.4 kB

Release files / chatfaq_sdk-0.1.57.tar.gz

Download URL chatfaq_sdk-0.1.57.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e3959ebf54f0e1f0059d0bf0d347fad5be9c1a070c55586f3e66a8504bcf6f7c
BLAKE2b-256 checksum
How to use checksums
85c45046e0ed9a5b9e5d3844370013f5ce9b544423ab7f114ee01ba8e2e157e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.10.13 Linux/6.8.0-79-generic

Release files / chatfaq_sdk-0.1.57-py3-none-any.whl

Download URL chatfaq_sdk-0.1.57-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
371695a6d54875858ddf84c11c8ee9c6ba523152c6be8401347ffea568a4ff8f
BLAKE2b-256 checksum
How to use checksums
0e2439605057aa5d742f4c0b75019b6336241ce36533cae2dd3e8bad22cf91ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.10.13 Linux/6.8.0-79-generic

Security for chatfaq_sdk 0.1.57

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.57 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page