Giter VIP home page Giter VIP logo

cbt-agent-project-'s Introduction

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Recommendations
  5. Team

About The Project

Cognitive Behavioral Therapy (CBT) is a form of psychotherapy that aims to improve mental health by changing negative patterns of thought and behavior. It involves identifying and challenging fallecies and learning practical self-help strategies. CBT is commonly used to treat anxiety, depression, and other mental health conditions. This project aims develop a chatbot that simulates a CBT session.

The project is developed with the BESSER Bot Framework (BBF). BBF facilitate the definition and deployment of chatbots by defining the conversation logic using states, intents and transitions. The following figure shows the bot states, intents and transitions implemented.

bot start

  • The state is used to customize the interaction needed with the user. The blue ellipses are the states implemented in our proposal. There is an special state named “fallback_state” which is triggered when the bot intent is not understood by the bot, it is represented with the green ellipse.
  • The intent is used to define the goals between the interaction of the bot and the user. The intents are recognised using a NER model with predefined words, and these are represented as orange lines.
  • The transitions are used to trigger the next state in the conversation, and when an intent is matched the transition is executed to the next state. In our implementation we used the “Custom Event Transitions”, these transitions uses a customized function with a boolean result to move to the next state of the conversation. In our proposal these are represented with the purple lines.

In three states of the CBT chatbot: question_state, incomplete_state, and recommendation_state, we have implemented the use of an LLM. The project uses Langchain to establish a connection with a LLM, use chains with customized prompts, and information extraction tools.

  • Connection with LLM: Our proposal can work with the OpenAI or AzureOpenAI APIs by customizing the .env configuration file. Langchain creates the connection with the API and the chatbot sends customized prompts to request a response from the LLM.
  • Customized prompts: The prompts are customized with a “System prompt” that provide instructions to the LLM about the text generation purpose. In some cases the prompt is also customized with the memory of the conversation to generate text related to the context of the chat conversation.
  • Information Extraction (IE) Tools: The IE tools are used to extract structured information from the text provided by the user, in the case of CBT, our interest is to structure the data with the ABC model: Activating Events (A), Beliefs (B), and Consequences (C). The structured ABC data of situations that has affected negatively to the user is an input to provide recommendations about the fallacies in the way of thinking.

To manage the workflow of the CBT session, the following states are implementedß:

  • Initial state: Greet the user with a brief information of the tool purpose and confirms if a recent situation has had a negative effect on their feelings. If so, the bad situation state is activated. If not, the conversation is finished.
  • Bad Situation state: Ask the user to describe the situation that had a negative effect on him.
  • Question state: Initiates the extraction of structured ABC model data using the LLM to collect evidence about the beliefs of the user. Additionally, the LLM is also used to generate follow-up questions about their beliefs. If the information described by the user is detailed enough, a Recommendation state will follow. If not, the Incomplete state follows.
  • Incomplete state: The LLM is used to extract structured information of the ABC model and combines it with previous information extracted. The LLM generate new questions based on the chat history and the structured data missing to understand the situation of the user. If the information described by the user is complete the Recommendation state is activated. If not, the Question state is executed again.
  • Recommendation state: In this state, the ABC model structured information is the main input to generate recommendations to change the way of thinking of the person in a positive and logical way. If the recommendation is useful for the user, the conversation is finished. If not, the Question state is executed again to deep dive on the concerns of the user.
  • End CBT state: The conversation is finished, and the Initial state is activated to restart a new conversation. Fallback state: When the bot has not understood the user message, it will activate this state to generate a message that reinforce the purpose of the tool and to share more about the situation that generated bad feelings.

(back to top)

Built With

(back to top)

Getting Started

Prerequisites

Request OpenAI or Azure keys to have access to an LLM API. Instructions are in the following links:

Create the .env file with the following environment variables:

  • In case of OpenAI LLM
OPENAI_API_TYPE = "openai"
OPENAI_API_BASE = <OpenAI_API_endpoint>
OPENAI_API_KEY = <OpenAI_API_key>
  • In case of Azure LLM:
OPENAI_API_TYPE = "azure"
OPENAI_API_VERSION = <Azure_API_version>
AZURE_OPENAI_ENDPOINT = <Azure_API_endpoint>
AZURE_OPENAI_API_KEY = <Azure_API_key>
AZURE_DEPLOYMENT_NAME = <Azure_Model_Deployment_Name>
  • Streamlit and websocket config:
STREAMLIT_HOST = <Hostname_Streamlit_Server>
STREAMLIT_PORT = "5000"
WEBSOCKET_HOST = <Hostname_Websocket_Server>
WEBSOCKET_PORT = "8765"

Run the project

  1. Install Python 3.11 and create a virtual environment
  2. Install the required packages:
    pip install -r requirements.txt
  3. Run the application:
    python chat.py
  4. A browser page will open with a welcome message for the chat conversation. If the chat conversation does not start, please reload the webpage.

Demo

A deployed version of the project is available in the following link: Chat-CBT

(back to top)

Usage

Upon accessing the previous link, you will be greeted with a welcome message as shown in the figure. To the left, there is an option to follow a link for further details on CBT.

bot start

Below is an example of the interaction with the chatbot.

bot example

Choosing the second option will cause the bot to terminate the ongoing session and initiate a new one. If you select the first option because you are still feeling bad, the chatbot will continue the conversation, asking more questions and offering counterarguments aimed at altering your way of thinking and eventually make you feel better.

The following illustrates an interaction with the chatbot tailored for someone who has recently been diagnosed with diabetes. In this scenario, the chatbot continuously poses questions to uncover the central thought affecting the individual.

bot example2

(back to top)

Recommendations

As future work, some adjustments can be performed. Regarding the welcoming message, the statement outlining the project's purpose could be reformulated to enhance user engagement. The conclusion sometimes is rushed, with users quickly affirming their improved state. This could be expanded to allow users the option to continue the dialogue, suggesting a more gradual resolution. Moreover, the recommendation for users to adopt coping mechanisms could be presented as an interactive query, inviting users to delve deeper into these strategies.

Another recommendation is to use Retrieval Augmented Generation (RAG) to reduce hallucinations issues by providng recommendations from a list of predefined outputs that are related to the fallacy identified by the LLM in the user input.

Enhancing the Chatbot with a Custom Model and Dataset

To significantly improve the chatbot's ability to identify and interact with users about their cognitive fallacies, we recommend the development of a custom model trained on a specialized dataset. This dataset should encompass a broad range of cognitive distortions, derived from anonymized therapy sessions, scenarios created by mental health professionals, and existing psychological research. Fine-tuning an open-source LLM with this rich dataset will enable the chatbot to detect subtle indications of cognitive fallacies in user inputs and provide therapeutic responses that are both relevant and grounded in CBT practices. This approach aims to strike a balance between personalized interaction and adherence to psychological principles, enhancing the chatbot's utility as a therapeutic tool.

Prioritizing Ethical and Privacy Considerations

The implementation of a custom model for therapeutic purposes must be accompanied by stringent ethical and privacy safeguards. It is imperative to ensure the confidentiality and security of user data through robust encryption, secure storage practices, and adherence to privacy regulations such as GDPR and HIPAA. Ethically, the chatbot must be designed to avoid biases and harmful assumptions, incorporating guidelines to mitigate potential risks and enabling human oversight for quality control. Transparency regarding the chatbot's limitations and offering users the option to seek human support are essential ethical practices. These measures are crucial not only for user protection but also for fostering trust in the technology, thereby enhancing its effectiveness and user engagement.

Incorporating these recommendations will pave the way for a more sophisticated, ethical, and user-centric chatbot.

Team

  • Dulce Canha
  • Francesco Lodola
  • Jonathan Silva
  • Karthick Panner Selvam
  • Shuai Ren

cbt-agent-project-'s People

Contributors

jsilvam avatar dcanha avatar karthickai avatar

Watchers

 avatar

Forkers

karthickai

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.