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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: gpl
|
| 3 |
+
---
|
| 4 |
+
# Counseling with CAMEL
|
| 5 |
+
|
| 6 |
+
### Setup
|
| 7 |
+
```
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import multiprocessing
|
| 11 |
+
import re
|
| 12 |
+
import traceback
|
| 13 |
+
from abc import ABC, abstractmethod
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
|
| 16 |
+
import requests
|
| 17 |
+
from langchain.prompts import PromptTemplate
|
| 18 |
+
from langchain_openai import OpenAI
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
### Define Agents
|
| 22 |
+
```
|
| 23 |
+
class Agent():
|
| 24 |
+
def __init__(self, vLLM_server, model_id):
|
| 25 |
+
self.llm = OpenAI(
|
| 26 |
+
temperature=0.0,
|
| 27 |
+
openai_api_key='EMPTY',
|
| 28 |
+
openai_api_base=vLLM_server,
|
| 29 |
+
max_tokens=512,
|
| 30 |
+
model=model_id
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def generate(self):
|
| 34 |
+
pass
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
```
|
| 38 |
+
class CBTAgent(Agent):
|
| 39 |
+
def __init__(self, prompt, vLLM_server, model_id):
|
| 40 |
+
super().__init__(vLLM_server, model_id)
|
| 41 |
+
self.prompt_template = PromptTemplate(
|
| 42 |
+
input_variables=[
|
| 43 |
+
"client_information",
|
| 44 |
+
"reason_counseling",
|
| 45 |
+
'history',
|
| 46 |
+
],
|
| 47 |
+
template=prompt
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
def generate(self, client_information, reason, history):
|
| 51 |
+
history_text = '\n'.join(
|
| 52 |
+
[
|
| 53 |
+
f"{message['role'].capitalize()}: {message['message']}"
|
| 54 |
+
for message in history
|
| 55 |
+
]
|
| 56 |
+
)
|
| 57 |
+
prompt = self.prompt_template.format(
|
| 58 |
+
client_information=client_information,
|
| 59 |
+
reason_counseling=reason,
|
| 60 |
+
history= history_text
|
| 61 |
+
)
|
| 62 |
+
response = self.llm.invoke(prompt)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
cbt_technique = response.split("Counseling")[0].replace("\n", "")
|
| 66 |
+
except:
|
| 67 |
+
cbt_technique = None
|
| 68 |
+
try:
|
| 69 |
+
cbt_plan = response.split("Counseling planning:\n")[1].split("\nCBT")[0]
|
| 70 |
+
except:
|
| 71 |
+
cbt_plan = None
|
| 72 |
+
|
| 73 |
+
return cbt_technique, cbt_plan
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
```
|
| 77 |
+
class CounsleorAgent(Agent):
|
| 78 |
+
def __init__(self, prompt, vLLM_server, model_id, cbt_plan):
|
| 79 |
+
super().__init__(vLLM_server, model_id)
|
| 80 |
+
self.cbt_plan = cbt_plan
|
| 81 |
+
self.prompt_template = PromptTemplate(
|
| 82 |
+
input_variables=[
|
| 83 |
+
"client_information",
|
| 84 |
+
"reason_counseling",
|
| 85 |
+
"cbt_plan",
|
| 86 |
+
"history"
|
| 87 |
+
],
|
| 88 |
+
template=prompt
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
def generate(self, client_information, reason, history):
|
| 92 |
+
history_text = '\n'.join(
|
| 93 |
+
[
|
| 94 |
+
f"{message['role'].capitalize()}: {message['message']}"
|
| 95 |
+
for message in history
|
| 96 |
+
]
|
| 97 |
+
)
|
| 98 |
+
prompt = self.prompt_template.format(
|
| 99 |
+
client_information=client_information,
|
| 100 |
+
reason_counseling=reason,
|
| 101 |
+
cbt_plan=self.cbt_plan,
|
| 102 |
+
history=history_text,
|
| 103 |
+
)
|
| 104 |
+
# print(prompt)
|
| 105 |
+
response = self.llm.invoke(prompt)
|
| 106 |
+
# print(f"Response: {response}")
|
| 107 |
+
|
| 108 |
+
if "'message':" in response:
|
| 109 |
+
response = response.split("'message':")[1].split(", {")[0].replace("\"","").replace("]", "").replace("}", "")
|
| 110 |
+
return response.split("Counselor:")[-1].replace("\n", "").replace("\\", "").replace("\"","").strip()
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
### Define prompt templates
|
| 114 |
+
```
|
| 115 |
+
RESPONSE_PROMPT="""<|start_header_id|>system<|end_header_id|>
|
| 116 |
+
|
| 117 |
+
You are playing the role of a counselor in a psychological counseling session. Your task is to use the provided client information and counseling planning to generate the next counselor utterance in the dialogue. The goal is to create a natural and engaging response that builds on the previous conversation and aligns with the counseling plan.<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 118 |
+
|
| 119 |
+
Client Information:
|
| 120 |
+
{client_information}
|
| 121 |
+
|
| 122 |
+
Reason for seeking counseling:
|
| 123 |
+
{reason_counseling}
|
| 124 |
+
|
| 125 |
+
Counseling planning:
|
| 126 |
+
{cbt_plan}
|
| 127 |
+
|
| 128 |
+
Counseling Dialogue:
|
| 129 |
+
{history}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
| 130 |
+
|
| 131 |
+
"""
|
| 132 |
+
```
|
| 133 |
+
|
| 134 |
+
```
|
| 135 |
+
CBT_PLAN_PROMPT="""<|start_header_id|>system<|end_header_id|>
|
| 136 |
+
|
| 137 |
+
You are a counselor specializing in CBT techniques. Your task is to use the provided client information, and dialogue to generate an appropriate CBT technique and a detailed counseling plan.<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 138 |
+
|
| 139 |
+
Types of CBT Techniques:
|
| 140 |
+
Efficiency Evaluation, Pie Chart Technique, Alternative Perspective, Decatastrophizing, Pros and Cons Analysis, Evidence-Based Questioning, Reality Testing, Continuum Technique, Changing Rules to Wishes, Behavior Experiment, Problem-Solving Skills Training, Systematic Exposure
|
| 141 |
+
|
| 142 |
+
Client Information:
|
| 143 |
+
{client_information}
|
| 144 |
+
|
| 145 |
+
Reason for seeking counseling:
|
| 146 |
+
{reason_counseling}
|
| 147 |
+
|
| 148 |
+
Counseling Dialogue:
|
| 149 |
+
{history}
|
| 150 |
+
|
| 151 |
+
Choose an appropriate CBT technique and create a counseling plan based on that technique.<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
### Start!
|
| 155 |
+
```
|
| 156 |
+
def collect_info(name, age, gender, occupation, education, matrital_status, family_details, reason):
|
| 157 |
+
CLINET_INFO = f"""Name: {name}
|
| 158 |
+
Age: {age}
|
| 159 |
+
Gender: {gender}
|
| 160 |
+
Occupation: {occupation}
|
| 161 |
+
Education: {education}
|
| 162 |
+
Marital Status: {matrital_status}
|
| 163 |
+
Family Details: {family_details}"""
|
| 164 |
+
|
| 165 |
+
REASON_FOR_COUNSELING = reason
|
| 166 |
+
HISTORY_INIT = f"Counselor: Hi {name}, it's nice to meet you. How can I assist you today?\nClient: "
|
| 167 |
+
|
| 168 |
+
return CLINET_INFO, REASON_FOR_COUNSELING, HISTORY_INIT
|
| 169 |
+
|
| 170 |
+
def start_demo(intake_form, reason, history_init):
|
| 171 |
+
model_id = "DLI-Lab/camel"
|
| 172 |
+
vLLM_server = ```YOUR vLLM SERVER```
|
| 173 |
+
max_turns = 20
|
| 174 |
+
|
| 175 |
+
print("Welcome to the Multi-Turn ClientAgent Demo!\n")
|
| 176 |
+
print(f"[Intake Form]")
|
| 177 |
+
print(intake_form)
|
| 178 |
+
print("Type 'exit' to quit the demo.\n")
|
| 179 |
+
|
| 180 |
+
print("====== Counseling Session ======\n")
|
| 181 |
+
first_response = history_init.split('Counselor: ')[-1].split('\nClient')[0]
|
| 182 |
+
print(f"Counselor: {first_response}")
|
| 183 |
+
|
| 184 |
+
num_turn = 0
|
| 185 |
+
while num_turn < max_turns:
|
| 186 |
+
if num_turn == 0:
|
| 187 |
+
user_input = input("You (Client): ")
|
| 188 |
+
# print(f"You (Client): {user_input}")
|
| 189 |
+
history_init = history_init + user_input
|
| 190 |
+
history = [
|
| 191 |
+
{"role": "Counselor", "message": history_init.split("Counselor: ")[-1].split("\nClient")[0]},
|
| 192 |
+
{"role": "Client", "message": history_init.split("Client: ")[-1]}
|
| 193 |
+
]
|
| 194 |
+
# print("CBT Planning")
|
| 195 |
+
CBT_Planner = CBTAgent(CBT_PLAN_PROMPT, vLLM_server, model_id)
|
| 196 |
+
cbt_technique, cbt_plan = CBT_Planner.generate(intake_form, reason, history)
|
| 197 |
+
# print(f"CBT Technique: {cbt_technique}")
|
| 198 |
+
# print(f"CBT Plan: {cbt_plan}")
|
| 199 |
+
|
| 200 |
+
num_turn+=1
|
| 201 |
+
else:
|
| 202 |
+
counselor = CounsleorAgent(RESPONSE_PROMPT, vLLM_server, model_id, cbt_plan)
|
| 203 |
+
counselor_response = counselor.generate(intake_form, reason, history)
|
| 204 |
+
print(f"Counselor: {counselor_response}")
|
| 205 |
+
|
| 206 |
+
history.append({"role": "Counselor", "message": counselor_response})
|
| 207 |
+
|
| 208 |
+
user_input = input("You (Client): ")
|
| 209 |
+
|
| 210 |
+
if user_input.lower() == 'exit':
|
| 211 |
+
print("\n====== Exiting the demo. Goodbye! ======\n")
|
| 212 |
+
break
|
| 213 |
+
|
| 214 |
+
print(f"You (Client): {user_input}")
|
| 215 |
+
history.append({"role": "Client", "message": user_input})
|
| 216 |
+
|
| 217 |
+
num_turn+=1
|
| 218 |
+
|
| 219 |
+
print("Demo completed.")
|
| 220 |
+
return cbt_plan, history
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
## Example
|
| 224 |
+
# name = "Laura"
|
| 225 |
+
# age = "45"
|
| 226 |
+
# gender = "female"
|
| 227 |
+
# occupation = "Office Job"
|
| 228 |
+
# education = "College Graduate"
|
| 229 |
+
# matrital_status = "Single"
|
| 230 |
+
# family_details = "Lives alone"
|
| 231 |
+
|
| 232 |
+
name = input("Let's begin the pre-counseling session. What is your name? ")
|
| 233 |
+
age = input("How old are you? ")
|
| 234 |
+
gender = input("What is your gender? (e.g., Male, Female)")
|
| 235 |
+
occupation = input("What is your occupation? ")
|
| 236 |
+
education = input("What is your highest level of education? (e.g., College Graduate)")
|
| 237 |
+
marital_status = input("What is your marital status? (e.g., Single, Married)")
|
| 238 |
+
family_details = input("Can you briefly describe your family situation? (e.g., Lives alone)")
|
| 239 |
+
reason = input("What brings you here for counseling? Please explain briefly. ")
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
CLINET_INFO, REASON_FOR_COUNSELING, HISTORY_INIT = collect_info(name, age, gender, occupation, education, matrital_status, family_details, reason)
|
| 243 |
+
cbt_plan, history = start_demo(CLINET_INFO, REASON_FOR_COUNSELING, HISTORY_INIT)
|
| 244 |
+
|
| 245 |
+
print(f"CBT Plan: {cbt_plan}\n\n")
|
| 246 |
+
|
| 247 |
+
for message in history:
|
| 248 |
+
print(f"{message['role']}: {message['message']}")
|
| 249 |
+
```
|