main
HuangHai 5 months ago
parent b11c7168bf
commit 2bd058ce11

@ -1,6 +1,8 @@
import asyncio import asyncio
import json import json
import re import re
import time
from CommonUtil import * from CommonUtil import *
@ -108,35 +110,36 @@ class MarkdownToJsonConverter:
return result return result
async def generate_descriptions_for_json_batch(self, json_data): def generate_descriptions_for_json_batch(self, json_data):
""" """
批量生成描述语句并替换 JSON 中的 text 属性 批量生成描述语句并替换 JSON 中的 text 属性
""" """
for item in json_data: for item in json_data:
if "data" in item and "title" in item["data"]: if "data" in item and "title" in item["data"]:
title = item["data"]["title"] title = item["data"]["title"]
description = await self.generate_description(title) description = self.generate_description(title) # 同步调用
item["data"]["text"] = description item["data"]["text"] = description
if "data" in item and "items" in item["data"]: if "data" in item and "items" in item["data"]:
for sub_item in item["data"]["items"]: for sub_item in item["data"]["items"]:
if "title" in sub_item: if "title" in sub_item:
title = sub_item["title"] title = sub_item["title"]
description = await self.generate_description(title) description = self.generate_description(title) # 同步调用
sub_item["text"] = description sub_item["text"] = description
yield json.dumps(item, ensure_ascii=False) 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 个字以内 调用 AI 接口生成描述语句限制在 20 个字以内
""" """
try: try:
response = await self.client.chat.completions.create( response = client.chat.completions.create( # 同步调用
model=MODEL_NAME, model=MODEL_NAME,
messages=[ messages=[
{"role": "system", "content": "你是一个专业的助手,能够根据上下文生成简洁的描述信息。"}, {"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 max_tokens=20
) )
@ -153,7 +156,7 @@ class MarkdownToJsonConverter:
print(f"调用 AI 生成描述信息时出错:{e}") print(f"调用 AI 生成描述信息时出错:{e}")
return title return title
async def convert_markdown_to_json(self, markdown_content): def convert_markdown_to_json(self, markdown_content):
""" """
Markdown 内容转换为 JSON 格式 Markdown 内容转换为 JSON 格式
""" """
@ -176,5 +179,5 @@ class MarkdownToJsonConverter:
listAll.append(item) 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 yield item

@ -145,13 +145,13 @@ def expand_text_with_ai(json_dict):
return json_dict return json_dict
async def convertMarkdownToJson(markdown_content): def convertMarkdownToJson(markdown_content):
# 创建转换器实例 # 创建转换器实例
converter = MarkdownToJsonConverter(client) converter = MarkdownToJsonConverter(client)
# 转换 Markdown 为 JSON # 转换 Markdown 为 JSON
async for item in converter.convert_markdown_to_json(markdown_content): for item in converter.convert_markdown_to_json(markdown_content): # 使用普通 for 循环
yield item yield item
# 添加结束标记 # 添加结束标记
yield '{"type": "end" }' yield '{"type": "end" }'
@ -241,7 +241,7 @@ async def aippt_outline(
@app.post("/api/tools/aippt") # 修改为 POST 方法 @app.post("/api/tools/aippt") # 修改为 POST 方法
async def aippt(content: str = Body(..., embed=True, description="Markdown 内容")): # 使用 Body 接收请求体参数 def aippt(content: str = Body(..., embed=True, description="Markdown 内容")): # 使用 Body 接收请求体参数
return StreamingResponse( return StreamingResponse(
convertMarkdownToJson(content), # 传入 content convertMarkdownToJson(content), # 传入 content
media_type="text/plain", # 使用 text/plain 格式 media_type="text/plain", # 使用 text/plain 格式

Loading…
Cancel
Save