Files
Fay/simulation_engine/llm_json_parser.py
xszyou 11e115b228 fay自然进化
1. 增加清除记忆功能;
2. 增加克隆性格功能;
3. 增加认知模型(专属的记忆逻辑、反思逻辑);
4. 修复自动播报bug;
5. fay_url配置响修正;
6. 修复流式输出前置换行问题;
7. 修复没有用户聊天记录前端反复添加默认用户问题;
8. 更新dockerfile;
9. 重构util.py代码。
1. Fay ai编程指南:https://qqk9ntwbcit.feishu.cn/wiki/FKFywXWaeiBH28k4Q67c3eF7njC
2.Fay认知模型:https://qqk9ntwbcit.feishu.cn/wiki/BSW3wSsMdikiHUkiCJYcSp2lnio
2025-04-02 23:31:46 +08:00

58 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import re
def extract_first_json_dict(input_str):
try:
# Replace curly quotes with standard double quotes
input_str = (input_str.replace("", "\"")
.replace("", "\"")
.replace("", "'")
.replace("", "'"))
# Find the first occurrence of '{' in the input_str
start_index = input_str.index('{')
# Initialize a count to keep track of open and close braces
count = 1
end_index = start_index + 1
# Loop to find the closing '}' for the first JSON dictionary
while count > 0 and end_index < len(input_str):
if input_str[end_index] == '{':
count += 1
elif input_str[end_index] == '}':
count -= 1
end_index += 1
# Extract the JSON substring
json_str = input_str[start_index:end_index]
# Parse the JSON string into a Python dictionary
json_dict = json.loads(json_str)
return json_dict
except ValueError:
# Handle the case where the JSON parsing fails
return None
def extract_first_json_dict_categorical(input_str):
reasoning_pattern = r'"Reasoning":\s*"([^"]+)"'
response_pattern = r'"Response":\s*"([^"]+)"'
reasonings = re.findall(reasoning_pattern, input_str)
responses = re.findall(response_pattern, input_str)
return responses, reasonings
def extract_first_json_dict_numerical(input_str):
reasoning_pattern = re.compile(r'"Reasoning":\s*"([^"]+)"')
response_pattern = re.compile(r'"Response":\s*(\d+\.?\d*)')
reasonings = reasoning_pattern.findall(input_str)
responses = response_pattern.findall(input_str)
return responses, reasonings