|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import re
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
from CommonUtil import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -108,35 +110,36 @@ class MarkdownToJsonConverter:
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
async def generate_descriptions_for_json_batch(self, json_data):
|
|
|
|
|
def generate_descriptions_for_json_batch(self, json_data):
|
|
|
|
|
"""
|
|
|
|
|
批量生成描述语句,并替换 JSON 中的 text 属性
|
|
|
|
|
"""
|
|
|
|
|
for item in json_data:
|
|
|
|
|
if "data" in item and "title" in item["data"]:
|
|
|
|
|
title = item["data"]["title"]
|
|
|
|
|
description = await self.generate_description(title)
|
|
|
|
|
description = self.generate_description(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 = await self.generate_description(title)
|
|
|
|
|
description = self.generate_description(title) # 同步调用
|
|
|
|
|
sub_item["text"] = description
|
|
|
|
|
yield json.dumps(item, ensure_ascii=False)
|
|
|
|
|
await asyncio.sleep(0.5) # 控制逐行输出的速度
|
|
|
|
|
time.sleep(0.5) # 控制逐行输出的速度
|
|
|
|
|
|
|
|
|
|
async def generate_description(self, title):
|
|
|
|
|
def generate_description(self, title):
|
|
|
|
|
"""
|
|
|
|
|
调用 AI 接口,生成描述语句(限制在 20 个字以内)
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = await self.client.chat.completions.create(
|
|
|
|
|
response = client.chat.completions.create( # 同步调用
|
|
|
|
|
model=MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": "你是一个专业的助手,能够根据上下文生成简洁的描述信息。"},
|
|
|
|
|
{"role": "user", "content": f"请为以下标题生成一句话的描述信息,描述信息应简洁明了,且与标题内容相关,不要使用与标题内容相同的语句,不要包含任何序号(如 1.、2. 等)或 Markdown 语法(如 #、- 等),且描述长度不超过 20 个字:\n- {title}"}
|
|
|
|
|
{"role": "user",
|
|
|
|
|
"content": f"请为以下标题生成一句话的描述信息,描述信息应简洁明了,且与标题内容相关,不要使用与标题内容相同的语句,不要包含任何序号(如 1.、2. 等)或 Markdown 语法(如 #、- 等),且描述长度不超过 20 个字:\n- {title}"}
|
|
|
|
|
],
|
|
|
|
|
max_tokens=20
|
|
|
|
|
)
|
|
|
|
@ -153,7 +156,7 @@ class MarkdownToJsonConverter:
|
|
|
|
|
print(f"调用 AI 生成描述信息时出错:{e}")
|
|
|
|
|
return title
|
|
|
|
|
|
|
|
|
|
async def convert_markdown_to_json(self, markdown_content):
|
|
|
|
|
def convert_markdown_to_json(self, markdown_content):
|
|
|
|
|
"""
|
|
|
|
|
将 Markdown 内容转换为 JSON 格式
|
|
|
|
|
"""
|
|
|
|
@ -176,5 +179,5 @@ class MarkdownToJsonConverter:
|
|
|
|
|
listAll.append(item)
|
|
|
|
|
|
|
|
|
|
# 生成描述
|
|
|
|
|
async for item in self.generate_descriptions_for_json_batch(listAll):
|
|
|
|
|
for item in self.generate_descriptions_for_json_batch(listAll): # 使用普通 for 循环
|
|
|
|
|
yield item
|
|
|
|
|