main
HuangHai 5 months ago
parent 1b0aebdcf5
commit 547b0f5890

@ -4,68 +4,6 @@ import re
from CommonUtil import *
def update_json_with_ai(client, markdown_content, json_obj):
"""
更新 JSON 对象 title 属性生成描述信息填充到 text 属性中
"""
# 如果 json_obj 是字符串,则将其解析为字典
if isinstance(json_obj, str):
json_obj = json.loads(json_obj)
# 检查是否存在嵌套的 data 对象
if "data" in json_obj and isinstance(json_obj["data"], dict):
data = json_obj["data"]
if "title" in data:
title = data["title"]
description = generate_description_with_ai(client, markdown_content, title)
data["text"] = description # 将生成的描述信息填充到 text 属性中
elif "title" in json_obj:
# 如果 title 直接存在于 json_obj 中
title = json_obj["title"]
description = generate_description_with_ai(client, markdown_content, title)
json_obj["text"] = description
# 递归处理 items 中的子对象
if "items" in json_obj:
for item in json_obj["items"]:
update_json_with_ai(client, markdown_content, item)
return json_obj
def generate_description_with_ai(client, markdown_content, title):
"""
使用 AI 生成一句话的描述信息
"""
# 构造提示词
prompt = f"以下是 Markdown 内容:\n{markdown_content}\n\n请为以下标题生成一句话的描述信息,描述信息应简洁明了,且与标题内容相关,不要包含任何序号(如 1.、2. 等)或 Markdown 语法(如 #、- 等),也一定不要重复标题:\n{title}"
try:
# 调用 AI 模型
response = client.chat.completions.create(
model=MODEL_NAME, # 根据需要选择合适的模型
messages=[
{"role": "system", "content": "你是一个专业的助手,能够根据上下文生成简洁的描述信息。"},
{"role": "user", "content": prompt}
],
max_tokens=50 # 限制生成内容的长度
)
# 提取生成的描述信息
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() # 去除 Markdown 语法
return description
else:
print(f"AI 未返回有效描述信息,标题:{title}")
return title # 如果 AI 未返回有效描述,则返回标题本身
except Exception as e:
print(f"调用 AI 生成描述信息时出错:{e}")
return title # 如果调用失败,则返回标题本身
# 流式生成数据的函数
async def generate_stream_markdown(course_name: str):
"""
@ -100,12 +38,6 @@ async def generate_stream_markdown(course_name: str):
print(full_markdown)
# 测试函数
async def test_generate_stream_markdown():
async for chunk in generate_stream_markdown("三角形面积"):
print(chunk.decode("utf-8"), end="", flush=True)
def extract_level1_title(markdown_content):
"""
Markdown 字符串中提取一级目录的文本内容
@ -183,22 +115,42 @@ def extract_level2_and_level3(markdown_content):
return result
def add_descriptions_to_structure(client, markdown_content, structure):
def convert_structure_to_json(structure):
"""
遍历结构化数据为每个二级和三级文本生成描述信息并赋值给 text 属性
将结构化数据转换为指定的 JSON 格式
"""
result = []
for level2 in structure:
# 为二级目录生成描述信息
# 处理二级目录
level2_title = level2["title"]
level2["text"] = generate_description_with_ai(client, markdown_content, level2_title)
# 遍历三级目录
level2_json = {
"type": "transition",
"data": {
"title": level2_title,
"text": level2_title
}
}
result.append(level2_json)
# 处理三级目录
for level3 in level2["children"]:
# 为三级目录生成描述信息
level3_title = level3["title"]
level3["text"] = generate_description_with_ai(client, markdown_content, level3_title)
items = [{"title": item, "text": item} for item in level3["items"]]
level3_json = {
"type": "content",
"data": {
"title": level3_title,
"text": level3_title,
"items": items
}
}
result.append(level3_json)
return result
return structure
# 运行应用
if __name__ == "__main__":
@ -209,22 +161,23 @@ if __name__ == "__main__":
with open("Sample.md", "r", encoding="utf-8") as file:
markdown_content = file.read()
listAll = []
# 一级名称
level1_title = extract_level1_title(markdown_content)
json_obj = {"type": "cover", "data": {"title": level1_title, "text": ""}}
# print(json.dumps(json_obj, ensure_ascii=False))
# 更新 JSON 对象
updated_json = update_json_with_ai(client, markdown_content, json_obj)
print(json.dumps(updated_json, ensure_ascii=False, indent=2))
listAll.append(json_obj)
# 二级名称列表
contents = extract_level2_titles(markdown_content)
json_obj = {"type": "contents", "data": {"items": contents}}
print(json.dumps(json_obj, ensure_ascii=False))
listAll.append(json_obj)
# 二级目录和三级目录
result = extract_level2_and_level3(markdown_content)
print(json.dumps(result, ensure_ascii=False, indent=2))
json_obj = convert_structure_to_json(result)
# 遍历结果并一个对象一行打印
for item in json_obj:
listAll.append(item)
result=add_descriptions_to_structure(client, markdown_content, result)
print(json.dumps(result, ensure_ascii=False, indent=2))
print(listAll)

Loading…
Cancel
Save