-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.py
404 lines (344 loc) · 13.2 KB
/
mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import json
import threading
import concurrent.futures
import numpy as np
import os
import logging
import base64
import random
from tqdm.auto import tqdm
from PIL import Image
from src.llm_backend import LLMBackend
from src.utils import *
from src.prompts import summary_prompt, profile_prompt, knowledge_prompt, deduce_prompt
#默认的warning级别,只输出warning以上的
#使用basicConfig()来指定日志级别和相关信息
logging.basicConfig(
level=logging.DEBUG, #设置日志输出格式
filename="logs/mock.log", #log日志输出的文件位置和文件名
filemode="a", #文件的写入格式,w为重新写入文件,默认是追加
format="%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s", #日志输出的格式
datefmt="%Y-%m-%d %H:%M:%S", #时间输出的格式
)
max_workers = 30
MAX_RETRY_TIMES = 5
data_path = "data"
logs_path = "logs"
preview_path = os.path.join(data_path, "preview")
posts_path = os.path.join(data_path, "id_post_map")
user_info_path = os.path.join(data_path, "user_info_map")
user_posts_path = os.path.join(data_path, "user_posts_map")
user_scores_path = os.path.join(data_path, "user_avg_scores")
user_traits_path = os.path.join(data_path, "user_traits")
user_profiles_path = os.path.join(data_path, "user_profiles")
# dataset_path = os.path.join(data_path, "dataset")
deduce_queries_path = os.path.join(data_path, "deduce_queries")
user_avg_scores_path = os.path.join(preview_path, "user_avg_scores.json")
user_explanations_path = os.path.join(preview_path, "user_explanations.json")
media_path = os.path.join(data_path, "media")
media_images_path = os.path.join(media_path, "images")
media_videos_path = os.path.join(media_path, "videos")
media_images_caption_path = os.path.join(data_path, "captions")
user_conversations_path = os.path.join(data_path, "user_conversations")
post_knowledges_path = os.path.join(data_path, "knowledges")
glm_public = LLMBackend(
platform="zhipuai",
base_url="https://open.bigmodel.cn/api/paas/v4",
api_key="120985c00e389dac93ae62522ab5ae7a.lX6mrF4YEcSw4fmq",
model="glm-4-flash"
)
# llama = LLMBackend(
# platform="openai",
# base_url="http://172.16.64.188:8000/v1",
# api_key="-",
# model="llama3.1-70b"
# )
llm = glm_public
llm.test()
user_avg_scores = read_user_avg_scores(user_avg_scores_path)
user_explanations = read_user_explanations(user_explanations_path)
users_info = read_users_info(user_info_path)
user_posts = read_user_posts(user_posts_path)
user_conversations = read_user_conversations(user_conversations_path)
post_ids = read_posts_id_map(posts_path)
check_dirs(user_traits_path)
check_dirs(user_profiles_path)
check_dirs(post_knowledges_path)
check_dirs(deduce_queries_path)
print(list(user_conversations.keys())[0])
print(len(list(user_explanations.keys())))
## Process Summaries
summaries = []
# 遍历 user_scores 文件夹内的文件
already_summaried = []
for filename in tqdm(os.listdir(user_traits_path)):
# 检查文件是否为JSON文件
if filename.endswith('.json'):
file_path = os.path.join(user_traits_path, filename)
# with open(file_path, 'r', encoding="utf8") as f:
# score_info = json.load(f)
base_name, extension = os.path.splitext(filename)
already_summaried.append(base_name)
print(f"already_summaried numbers:\t{len(already_summaried)}")
for user, explanation in user_explanations.items():
userName = user.split("-", 2)[-1]
nickName = users_info[userName]["name"]
# if userName == "ClickHouseDB":
# continue
summary_query = summary_prompt.format(user=nickName, evaluation=explanation)
summary_dic = {
"id": user,
"content": summary_query
}
summaries.append(summary_dic)
def summary(query, phar):
q_id = query["id"]
if q_id in already_summaried:
phar.update(1)
return
q_content = query["content"]
try_time = 0
while try_time < MAX_RETRY_TIMES:
try:
res = llm.request(q_content)
file_path = os.path.join(user_traits_path, f"{q_id}.json")
sj = {
"id": q_id,
"trait": res
}
save_dic2json(file_path, sj)
# phar.update(1)
break
except Exception as e:
print(e)
file_path = os.path.join(logs_path, f"error-{try_time}-{q_id}.json")
try_time += 1
phar.update(1)
print(summaries[0])
print("testing...")
print(llm.request(summaries[0]["content"]))
# 多线程请求
toal_summary_num = len(summaries)
phar_summary = tqdm(total=toal_summary_num)
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
for i in summaries:
future = executor.submit(summary, i, phar_summary)
## Process Profiles
user_traits = read_user_traits(user_traits_path)
# 遍历 user_scores 文件夹内的文件
already_profiled = []
for filename in tqdm(os.listdir(user_profiles_path)):
# 检查文件是否为JSON文件
if filename.endswith('.json'):
file_path = os.path.join(user_profiles_path, filename)
# with open(file_path, 'r', encoding="utf8") as f:
# score_info = json.load(f)
base_name, extension = os.path.splitext(filename)
already_profiled.append(base_name)
print(f"already_profiled numbers:\t{len(already_profiled)}")
profiles = []
for user, trait in user_traits.items():
userName = user.split("-", 2)[-1]
nickName = users_info[userName]["name"]
conv = user_conversations[user]
sample_conv = random.sample(conv, 10)
profile_query = profile_prompt.format(user=nickName, posts=sample_conv)
profile_dic = {
"id": user,
"content": profile_query
}
profiles.append(profile_dic)
print(profiles[0])
print(llm.request(profiles[0]["content"]))
def profile(query, phar):
q_id = query["id"]
if q_id in already_profiled:
phar.update(1)
return
q_content = query["content"]
try_time = 0
while try_time < MAX_RETRY_TIMES:
try:
res = llm.request(q_content)
file_path = os.path.join(user_profiles_path, f"{q_id}.json")
sj = {
"id": q_id,
"profile": res
}
save_dic2json(file_path, sj)
# phar.update(1)
break
except Exception as e:
print(e)
file_path = os.path.join(logs_path, f"error-{try_time}-{q_id}.json")
try_time += 1
phar.update(1)
# 多线程请求
toal_profile_num = len(profiles)
phar_profile = tqdm(total=toal_profile_num)
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
for i in profiles:
future = executor.submit(profile, i, phar_profile)
## Potential Knowledge
user_profiles = read_user_profiles(user_profiles_path)
# 遍历 user_scores 文件夹内的文件
already_knowledged = []
for filename in tqdm(os.listdir(post_knowledges_path)):
# 检查文件是否为JSON文件
if filename.endswith('.json'):
file_path = os.path.join(post_knowledges_path, filename)
# with open(file_path, 'r', encoding="utf8") as f:
# score_info = json.load(f)
base_name, extension = os.path.splitext(filename)
already_knowledged.append(base_name)
print(f"already_knowledged numbers:\t{len(already_knowledged)}")
# 定义接收的数据格式
from pydantic import BaseModel, Field
class PotentialInfo(BaseModel):
ContainKnowledge: bool = Field(description="Whether the content of the conversation clearly implies knowledge.")
DetailedKnowledge: str = Field(description="Simulated the potential knowledge behind the conversation in detail.")
# 创建输出解析器
from langchain.output_parsers import PydanticOutputParser
potential_output_parser = PydanticOutputParser(pydantic_object=PotentialInfo)
# 获取输出格式指示
potential_format_instructions = potential_output_parser.get_format_instructions()
# 打印提示
print("输出格式:", potential_format_instructions)
# # 解析模型的输出
# potential_parsed_output = potential_output_parser.parse(output)
# # 将Pydantic格式转换为字典
# potential_parsed_output_dict = potential_parsed_output.dict()
# # 打印字典
# print("输出的数据:", potential_parsed_output_dict)
knowledge_queries = []
for user, conversations in user_conversations.items():
userName = user.split("-", 2)[-1]
user_info = users_info[userName]
nickName = user_info["name"]
for conversation in conversations:
meta_data = conversation["meta_data"]
sample_id = meta_data["conve_id"]
conversation_str = str(conversation["conversation"]).replace("Post", "Content")
knowledge_query = knowledge_prompt.format(
potential_format_instructions=potential_format_instructions,
conversation=conversation_str
)
knowledge_dic = {
"id": sample_id,
"content": knowledge_query
}
knowledge_queries.append(knowledge_dic)
res = llm.request(knowledge_queries[9000]["content"])
# 解析模型的输出
potential_parsed_output = potential_output_parser.parse(res)
# 将Pydantic格式转换为字典
potential_parsed_output_dict = potential_parsed_output.dict()
# 打印字典
print("输出的数据:", potential_parsed_output_dict)
def perform_knowledge(query, phar):
q_id = query["id"]
if q_id in already_knowledged:
phar.update(1)
return
q_content = query["content"]
try_time = 0
while try_time < MAX_RETRY_TIMES:
try:
file_path = os.path.join(post_knowledges_path, f"{q_id}.json")
if os.path.exists(file_path):
break
res = llm.request(q_content)
# 解析模型的输出
potential_parsed_output = potential_output_parser.parse(res)
# 将 Pydantic 格式转换为字典
potential_parsed_output_dict = potential_parsed_output.dict()
knowledge_result = {
"id": q_id,
"knowledge": potential_parsed_output_dict
}
save_dic2json(file_path, knowledge_result)
break
except Exception as e:
print(res)
print(e)
try_time += 1
phar.update(1)
# 多线程请求
toal_knowledge_num = len(knowledge_queries)
phar_knowledge = tqdm(total=toal_knowledge_num)
perform_knowledge(knowledge_queries[0], phar_knowledge)
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
for query in knowledge_queries:
future = executor.submit(perform_knowledge, query, phar_knowledge)
## Deduce
post_knowledges = read_post_knowledges(post_knowledges_path)
# 定义接收的数据格式
from pydantic import BaseModel, Field
class DeduceInfo(BaseModel):
rmr: bool = Field(description="Whether my Post shows the content of my profile.")
ept: bool = Field(description="Whether my Post provides explicit evidence of my personality traits.")
bpa: str = Field(description="A brief related psychological activities when I post my Post.")
# 创建输出解析器
from langchain.output_parsers import PydanticOutputParser
output_parser = PydanticOutputParser(pydantic_object=DeduceInfo)
# 获取输出格式指示
format_instructions = output_parser.get_format_instructions()
# 打印提示
print("输出格式:", format_instructions)
# # 解析模型的输出
# parsed_output = output_parser.parse(output)
# # 将Pydantic格式转换为字典
# parsed_output_dict = parsed_output.dict()
# # 打印字典
# print("输出的数据:", parsed_output_dict)
print(len(list(post_knowledges.keys())))
deduces = []
for user, explanation in tqdm(user_explanations.items()):
userName = user.split("-", 2)[-1]
nickName = users_info[userName]["name"]
user_trait = user_traits[user]["trait"]
user_profile = user_profiles[user]["profile"]
if user_trait == "":
continue
for conversation in user_conversations[user]:
meta_data = conversation["meta_data"]
if meta_data["isQuote"]:
continue
sample_id = meta_data["conve_id"]
try:
knowledge = post_knowledges[sample_id]["knowledge"]
except Exception as e:
print(e)
continue
if not knowledge["ContainKnowledge"]:
continue
deduce_query = deduce_prompt.format(
user=nickName,
profile=user_profile,
traits=user_trait,
pk=knowledge["DetailedKnowledge"],
conversation=conversation["conversation"],
format_instructions=format_instructions
)
deduce_dic = {
"id": sample_id,
"fid": user,
"uid": userName,
"content": deduce_query
}
deduces.append(deduce_dic)
print(len(deduces))
content = deduces[0]["content"]
print(content)
res = llm.request(content)
print(res)
# 解析模型的输出
parsed_output = output_parser.parse(res)
# 将Pydantic格式转换为字典
parsed_output_dict = parsed_output.dict()
# 打印字典
print("输出的数据:", parsed_output_dict)
for item in tqdm(deduces):
q_id = item["id"]
file_path = os.path.join(deduce_queries_path, f"{q_id}.json")
save_dic2json(file_path, item)