Package: chatfaq_sdk
ChatFAQ's SDKFor 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.PrerequisitesMake sure the next list of packages are installed on your system:Python 3.10python3.10-devpython3.10-distutilspoetryInstallationPYPIpoetry add chatfaq-sdkLocal buildSet Up:Install project dependencies:poetry installRunFirst of all, create a.envfile with the needed variables set. You can see an example of those on.env_examplefile. 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_exampleThis will run the example FSM that is located in./examples/model_example/init.pyfile. You can modify this file to test your own FSMs.DockerAlternatively you can simply run the server using docker.Builddocker build -t chatfaq-sdk .Rundocker run chatfaq-sdkUsageSimple exampleThis is just a dummy example that displays the basic usage of the library.We are going to build the next FSM:Import basic modules to build your first FMS:importosimportrandomfromchatfaq_sdkimportChatFAQSDKfromchatfaq_sdk.fsmimportFSMDefinition,State,Transitionfromchatfaq_sdk.conditionsimportConditionfromchatfaq_sdk.layersimportTextDeclare the 3 possible states of our FSM:defsend_greeting(ctx:dict):yieldText("Hello!")yieldText("How are you?",allow_feedback=False)greeting_state=State(name="Greeting",events=[send_greeting],initial=True)defsend_answer(ctx:dict):last_payload=ctx["last_mml"]["stack"][0]["payload"]yieldText(f'My answer to your message: "{last_payload}" is:{random.randint(0,999)}')yieldText(f"Tell me more")answering_state=State(name="Answering",events=[send_answer],)defsend_goodbye(ctx:dict):yieldText("Byeeeeeeee!",allow_feedback=False)goodbye_state=State(name="Goodbye",events=[send_goodbye],)Declare the only computable condition for the transitions of our FSM:defis_saying_goodbye(ctx:dict):ifctx["last_mml"]["stack"][0]["payload"]=="goodbye":returnCondition(1)returnCondition(0)Now lets glue everything together:Declare our transitionsany_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:importossdk=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 infoBuild the docsgo inside thedocdirectory and run:poetry run make htmlPublish packagePYPI testadd repository to poetry configpoetry config repositories.chatfaq-sdk https://test.pypi.org/legacy/get token fromhttps://test.pypi.org/manage/account/token/store token usingpoetry config pypi-token.chatfaq-sdk pypi-YYYYYYYYPYPI productionget token fromhttps://pypi.org/manage/account/token/store token usingpoetry config pypi-token.chatfaq-sdk pypi-XXXXXXXXEach time you need to publishBump versionpoetry version prereleaseorpoetry version patchPoetry PublishTo TestPyPipoetry publish -r chatfaq-sdkTo PyPipoetry publish