main
HuangHai 5 months ago
parent 547b0f5890
commit 8e53a36bf6

@ -116,7 +116,6 @@ def extract_level2_and_level3(markdown_content):
return result
def convert_structure_to_json(structure):
"""
将结构化数据转换为指定的 JSON 格式
@ -150,8 +149,56 @@ def convert_structure_to_json(structure):
result.append(level3_json)
return result
def generate_descriptions_for_json_batch(client, json_data):
"""
批量生成描述语句并替换 JSON 中的 text 属性
"""
# 遍历 JSON 数据,逐项生成描述
for item in json_data:
if "data" in item and "title" in item["data"]:
# 生成一级或二级标题的描述
title = item["data"]["title"]
description = generate_description(client, title)
item["data"]["text"] = description
if "data" in item and "items" in item["data"]:
# 生成三级标题及其子项的描述
for sub_item in item["data"]["items"]:
if "title" in sub_item:
title = sub_item["title"]
description = generate_description(client, title)
sub_item["text"] = description
print(item)
def generate_description(client, title):
"""
调用 AI 接口生成描述语句限制在 16 个字以内
"""
try:
# 调用 AI 模型
response = client.chat.completions.create(
model=MODEL_NAME, # 根据需要选择合适的模型
messages=[
{"role": "system", "content": "你是一个专业的助手,能够根据上下文生成简洁的描述信息。"},
{"role": "user", "content": f"请为以下标题生成一句话的描述信息,描述信息应简洁明了,且与标题内容相关,不要包含任何序号(如 1.、2. 等)或 Markdown 语法(如 #、- 等),且描述长度不超过 20 个字:\n- {title}"}
],
max_tokens=20 # 限制生成描述的长度
)
# 提取生成的描述信息
if response.choices and response.choices[0].message.content:
description = response.choices[0].message.content.strip()
# 去除序号和 Markdown 语法(如 1.、#、- 等)
description = re.sub(r'[\d.]+', '', description).strip()
description = re.sub(r'[#-]', '', description).strip()
return description
else:
print(f"AI 未返回有效描述信息:{title}")
return title # 如果 AI 未返回有效描述,则返回标题本身
except Exception as e:
print(f"调用 AI 生成描述信息时出错:{e}")
return title # 如果调用失败,则返回标题本身
# 运行应用
if __name__ == "__main__":
# 使用 asyncio.run 运行异步函数
@ -179,5 +226,4 @@ if __name__ == "__main__":
for item in json_obj:
listAll.append(item)
print(listAll)
generate_descriptions_for_json_batch(client, listAll)

Loading…
Cancel
Save