1
1
import ujson
2
2
import os
3
3
4
+
4
5
def get_all_sessions ():
5
6
source_dir = f'./data'
6
-
7
+
7
8
all_sessions = []
8
9
context = []
9
10
counter = 0
11
+ # demo data
12
+ # for idx in range(56):
13
+
10
14
for idx in range (56032 ):
11
15
try :
12
16
with open (f'{ source_dir } /{ idx } .json' , 'r' , encoding = 'utf-8' ) as f1 :
@@ -32,8 +36,9 @@ def get_all_sessions():
32
36
print ('done' )
33
37
return all_sessions
34
38
39
+
35
40
def get_dialogue_history (dialogue_history_list : list ):
36
-
41
+
37
42
dialogue_history_tmp = []
38
43
for item in dialogue_history_list :
39
44
if item ['role' ] == 'counselor' :
@@ -43,10 +48,10 @@ def get_dialogue_history(dialogue_history_list: list):
43
48
dialogue_history_tmp .append (text )
44
49
45
50
dialogue_history = '\n ' .join (dialogue_history_tmp )
46
-
51
+
47
52
return dialogue_history + '\n ' + '咨询师:'
48
-
49
-
53
+
54
+
50
55
51
56
def get_instruction_data (dialogue_history ):
52
57
instruction = f'''现在你扮演一位专业的心理咨询师,你具备丰富的心理学和心理健康知识。你擅长运用多种心理咨询技巧,例如认知行为疗法原则、动机访谈技巧和解决问题导向的短期疗法。以温暖亲切的语气,展现出共情和对来访者感受的深刻理解。以自然的方式与来访者进行对话,避免过长或过短的回应,确保回应流畅且类似人类的对话。提供深层次的指导和洞察,使用具体的心理概念和例子帮助来访者更深入地探索思想和感受。避免教导式的回应,更注重共情和尊重来访者的感受。根据来访者的反馈调整回应,确保回应贴合来访者的情境和需求。请为以下的对话生成一个回复。
@@ -61,7 +66,7 @@ def get_instruction_data(dialogue_history):
61
66
all_sessions = get_all_sessions ()
62
67
train_dir = './train_dir'
63
68
os .makedirs (train_dir , exist_ok = True )
64
-
69
+
65
70
with open (f'{ train_dir } /train.json' , 'w' , encoding = 'utf-8' ) as f :
66
71
for item in all_sessions :
67
72
dialogue_history_list = item ['dialogue_history_list' ]
@@ -74,6 +79,5 @@ def get_instruction_data(dialogue_history):
74
79
)
75
80
text = ujson .dumps ({'instruction' : instruction , 'output' : output }, ensure_ascii = False )
76
81
f .write (text + '\n ' )
77
-
82
+
78
83
print ('********' )
79
-
0 commit comments