Skip to content

SNOWTEAM2023/Cognivia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

87 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’» This is the official implementation of paper Cognivia: An AI Therapist for Evidence-Based Cognitive Behavioral Therapy.

Cognivia (or β€œε―θ–‡β€ in Chinese) is an evidence-based artificial intelligence therapist for cognitive behavioral therapy (CBT) that integrates automatic cognitive distortion identification and rational response generation.

Authors

Qi Chen, Siria Xiyueyao Luo, Xuejiao Zhao*

** Southwest Petroleum University Β  | Β  University of Groningen Β  |Β  Nanyang Technological University**

* Corresponding author

Stargazers repo roster for @SNOWTEAM2023/Cognivia

πŸ”₯ News

  • [2026.02.01] We release github repository of Cognivia. πŸ’ͺ Have a try!

🧭 Framework Overview

Figure 1: The overall framework of Cognivia.

The pipeline of our model is shown in Fig. 1, which consists of three stages:

  • (1) CBT Expert Seed Curation: Curate CBT literatures to form high quality CBT Cognitive Triplet Dataset as reference seed.
  • (2) CBT Cognitive Triplet Dataset Augmentation: Multi-stage prompting and structured generation to augment mental health questions from PsyQA dataset to generate Augmented CBT Cognitive Triplet Dataset.
  • (3) Task-oriented LoRA Fine-tuning: Fine-tuning large language models by Augmented CBT Cognitive Triplet Dataset to obtain Cognivia for cognitive distortion identification and rational response generation.

πŸ“Š Dataset Preparation

  1. CBT Cognitive Triplet Dataset: Our work is based on authoritative texts that are widely regarded as core paradigms and standard references in CBT. Using these resources, we curate a high-quality seed cognitive triplet dataset of representative CBT question–answer pairs and corresponding rational response. The seed dataset is constructed based on established theoretical frameworks and clinical guidelines, which is why we refer to our approach as evidence-based. The form of this CBT Cognitive Triplet Dataset:
{"Thought": "...", "Cognitive Distortion": "...", "Rational Response": "..."}
  1. Augmented CBT Cognitive Triplet Dataset: This dataset is based on PsyQA, a large-scale psychological question-answering dataset collected from publicly accessible online mental health forums. PsyQA consists of anonymized user-generated question–answer pairs related to psychological concerns as the following form:
{"Question": "...", "Answer": "..."}

⚠️ Note that, the original PsyQA dataset cannot be directly redistributed due to its usage policy. To ensure compliance, we provide the complete pipeline for constructing the Augmented CBT Cognitive Triplet Dataset from the original data. Users need to apply for the official certificate and downloading the PsyQA dataset from its original source before running our preprocessing scripts.

For each question π‘žπ‘– in PsyQA, we employ the DeepSeek and GPT-5 Mini with two-stage data preprocessing to construct the Augmented CBT Cognitive Triplet Dataset with 9,437 samples:

  • Stage 1: Cognitive Distortion Identification

From the original PsyQA dataset (~22K samples), we selected 9,437 question samples exhibiting cognitive distortions through a designed prompt-based filtering process. We use an expert-designed prompt_1 to identify the corresponding cognitive distortion 𝑑𝑖.

Model: DeepSeek

Prompt: filter_prompt.txt (Prompt1 Version 2)​

Script: identify_distortion.py

  • Stage 2: Rational Response Generation

We use GPT-5 Mini with prompt_2 to generate a corresponding rational response π‘Ÿπ‘–.

Model: GPT-5 Mini

Prompt: response_prompt.txt (Prompt2 Version 3)

Script: generate_response.py

The form of this Augmented CBT Cognitive Triplet Dataset:

{"Thought": "...", "Cognitive Distortion": "...", "Rational Response": "..."}

✨ Code Structure

The code structure and corresponding comments of this repository are as follows:

Cognivia/
β”œβ”€β”€ Cognivia.py                 # Main entry script for running Cognivia
β”œβ”€β”€ prompts
β”‚   β”œβ”€β”€ filter_prompt.txt       # Prompt_1 of CBT Cognitive Triplet Dataset Augmentation (Cognitive Distortion Labelling)
β”‚   └── response_prompt.txt     # Prompt_2 of CBT Cognitive Triplet Dataset Augmentation (Rational Response Generation)
β”‚
β”œβ”€β”€ data/                       
β”‚   └── CBT_Cognitive_Triplet_Dataset.xlsx # CBT Cognitive Triplet Dataset curated from CBT Literatures
β”‚
β”œβ”€β”€ src/                      
β”‚   β”œβ”€β”€ evaluation_with_8_dimensions.py    # Custom 8-dimension quality evaluation
β”‚   β”œβ”€β”€ evaluation_with_NLP.py   # NLP-based evaluation methods
β”‚   β”œβ”€β”€ fine-tuned_model_generate.py       # Generate responses using fine-tuned model
β”‚   β”œβ”€β”€ identify_distortion.py   # Identify cognitive distortions
β”‚   β”œβ”€β”€ generate_response.py     # Generate rational responses
β”‚
β”œβ”€β”€ materials/                  # Figures & assets for the paper
β”œβ”€β”€ README.md                   # Project introduction and usage
β”œβ”€β”€ LICENCE.txt                 # Licence information
└── requirements.txt            # Python dependencies

πŸš€ Quickstart

0) Install

git clone https://github.com/SNOWTEAM2023/Cognivia.git

cd Cognivia
pip install -r requirements.txt

1) Modify Tokens

To use your own OpenAI, DeepSeek, or SiliconFlow API tokens, replace the placeholders with your actual tokens. The relevant sections in the code have been left blank for this purpose.

# Replace with your OpenAI API token
api_key = "your_openai_api"
# Replace with your DeepSeek API token
api_key = "your_deepseek_api"
# Replace with your SiliconFlow API token
api_key = "your_siliconflow_api"

3) Augment Dataset

The relevant sections in the code have been left blank to ensure the correct path is used.

# Replace with your path to the preprocessed dataset.
psyqa_path = os.path.join(current_dir, "questions.xlsx")

Once the paths and tokens have been updated, run this file to get Augmented CBT Cognitive Triplet Dataset :

identify_distortion.py && generate_response.py

To meet the training requirements of the model, we transformed the data in the Augmented CBT Cognitive Triplet Dataset into the following format:

{"user": "...", "assistant": "..."}

6οΌ‰Train

We fine-tuned Qwen2.5-7B-Instruct using LoRA on SiliconFlow. The model is accessible via API with ID [ft:LoRA/Qwen/Qwen2.5-7B-Instruct:d50jhbk50mis73di8n5g:gpt5_mini:udjarjexxlodpjueztat-ckpt_step_625]. You can try it out with the code I've included below.

from openai import OpenAI

# Replace with your SiliconFlow API token
api_key = "your_siliconflow_api"

client = OpenAI(
    api_key=api_key,
    base_url="https://api.siliconflow.cn/v1"
)

FINE_TUNED_MODEL_ID = "ft:LoRA/Qwen/Qwen2.5-7B-Instruct:d50jhbk50mis73di8n5g:gpt5_mini:udjarjexxlodpjueztat-ckpt_step_62"
# Replace with your question
test_user_input = ("Is my career short-lived? Feeling lost about the future.")

response = client.chat.completions.create(
    model=FINE_TUNED_MODEL_ID,
    messages=[
        {
            "role": "system",
            "content": """You are a cognitive behavioral therapy (CBT) psychologist.
                          First, identify the type of cognitive distortion exhibited in the statement,
                          and then provide a response containing the following five paragraphs, separated by blank lines:
                          1.Empathy and Validation
                          2.Cognitive Distortion Analysis
                          3.Reflective Questions 
                          4.CBT Exercise Recommendation 
                          5.Encouragement and Next Steps.
                          If it does not contain a cognitive distortion (e.g., casual conversation, general questions, or statements without distortions),
                          switch to a natural, supportive conversation mode. Respond in a warm, counselor-like tone without analyzing distortions or following the five-paragraph structure.
                       """
        },
        {
            "role": "user",
            "content": test_user_input
        }
    ],
    temperature=0.2,
    max_tokens=300
)

print("\n--- Model Analysis Results ---\n")
print(response.choices[0].message.content)

5) Evaluation

The relevant sections in the code have been left blank to ensure the correct path is used.

# Replace with your path to test dataset
INPUT_FILE = os.path.join(current_dir, "test.xlsx")
file2_path = os.path.join(current_dir, "test.xlsx")

Our work shows that Cognivia performs particularly well on CBT tasks, you can use the following for evaluation.

fine-tuned_model_generate.py && (evaluation_with_NLP.py & evaluation_with_8_dimensions.py &)

πŸ”‘ License

This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License. Commercial use is prohibited without a separate license agreement with the author.

About

No description, website, or topics provided.

Resources

License

Stars

8 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages