diff --git a/AI/AiService/MarkdownToJsonConverter.py b/AI/AiService/MarkdownToJsonConverter.py index 35d02520..c0e31d7f 100644 --- a/AI/AiService/MarkdownToJsonConverter.py +++ b/AI/AiService/MarkdownToJsonConverter.py @@ -1,3 +1,4 @@ +import asyncio import json import re from CommonUtil import * @@ -107,30 +108,31 @@ class MarkdownToJsonConverter: return result - def generate_descriptions_for_json_batch(self, json_data): + async 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 = self.generate_description(title) + description = await 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 = self.generate_description(title) + description = await self.generate_description(title) sub_item["text"] = description - print(item) + yield json.dumps(item, ensure_ascii=False) + await asyncio.sleep(0.5) # 控制逐行输出的速度 - def generate_description(self, title): + async def generate_description(self, title): """ 调用 AI 接口,生成描述语句(限制在 20 个字以内) """ try: - response = self.client.chat.completions.create( + response = await self.client.chat.completions.create( model=MODEL_NAME, messages=[ {"role": "system", "content": "你是一个专业的助手,能够根据上下文生成简洁的描述信息。"}, @@ -151,7 +153,7 @@ class MarkdownToJsonConverter: print(f"调用 AI 生成描述信息时出错:{e}") return title - def convert_markdown_to_json(self, markdown_content): + async def convert_markdown_to_json(self, markdown_content): """ 将 Markdown 内容转换为 JSON 格式 """ @@ -174,17 +176,5 @@ class MarkdownToJsonConverter: listAll.append(item) # 生成描述 - self.generate_descriptions_for_json_batch(listAll) - - -if __name__ == "__main__": - # 读取 Sample.md 内容 - with open("Sample.md", "r", encoding="utf-8") as file: - markdown_content = file.read() - - # 创建转换器实例 - converter = MarkdownToJsonConverter(client) - - # 转换 Markdown 为 JSON - converter.convert_markdown_to_json(markdown_content) - + async for item in self.generate_descriptions_for_json_batch(listAll): + yield item diff --git a/AI/AiService/CallDeepSeekStream.py b/AI/AiService/Start.py similarity index 80% rename from AI/AiService/CallDeepSeekStream.py rename to AI/AiService/Start.py index 6cf7c091..302ee8f0 100644 --- a/AI/AiService/CallDeepSeekStream.py +++ b/AI/AiService/Start.py @@ -10,6 +10,8 @@ import markdown_to_json import json import asyncio +from AiService.MarkdownToJsonConverter import MarkdownToJsonConverter + # 阿里云中用来调用 deepseek v3 的密钥 MODEL_API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc" MODEL_NAME = "qwen-plus" @@ -143,37 +145,13 @@ def expand_text_with_ai(json_dict): return json_dict -async def ConvertMarkdownToJson(markdown_content): - """ - 生成一个 AsyncIterable,逐行返回 JSON 字符串 - """ - # 将 Markdown 转换为字典 - json_dict = markdown_to_dict(markdown_content) - - # 调用 AI 扩写每个有 title 属性的节点 - json_dict = expand_text_with_ai(json_dict) - - # 提取一级目录 - level1_json = extract_level1(json_dict) - print(level1_json) - for item in level1_json: - yield json.dumps(item, ensure_ascii=False) - await asyncio.sleep(0.5) # 控制逐行输出的速度 - - # 生成目录部分 - contents_json = extract_contents(json_dict) - print(contents_json) - yield json.dumps(contents_json, ensure_ascii=False) - await asyncio.sleep(0.5) - - # 提取二级目录及其三级目录内容 - level2_and_level3_json = extract_level2_and_level3(json_dict) - print(level2_and_level3_json) - for item in level2_and_level3_json: - yield json.dumps(item, ensure_ascii=False) - await asyncio.sleep(0.5) - - # 添加结束标记 +async def convertMarkdownToJson(markdown_content): + # 创建转换器实例 + converter = MarkdownToJsonConverter(client) + # 转换 Markdown 为 JSON + async for item in converter.convert_markdown_to_json(markdown_content): + yield item + # 添加结束标记 yield '{"type": "end" }' @@ -265,7 +243,7 @@ async def aippt_outline( @app.post("/api/tools/aippt") # 修改为 POST 方法 async def aippt(content: str = Body(..., embed=True, description="Markdown 内容")): # 使用 Body 接收请求体参数 return StreamingResponse( - ConvertMarkdownToJson(content), # 传入 content + convertMarkdownToJson(content), # 传入 content media_type="text/plain", # 使用 text/plain 格式 headers={ "Cache-Control": "no-cache", @@ -279,26 +257,16 @@ async def aippt(content: str = Body(..., embed=True, description="Markdown 内 # 运行应用 if __name__ == "__main__": - # # 获取本机所有 IPv4 地址 - # ips = get_local_ips() - # if not ips: - # print("无法获取本机 IP 地址,使用默认地址 127.0.0.1") - # ips = ["127.0.0.1"] - # - # # 打印所有 IP 地址 - # print("服务将在以下 IP 地址上运行:") - # for ip in ips: - # print(f"http://{ip}:5173") - # - # # 启动 FastAPI 应用,绑定到所有 IP 地址 - # uvicorn.run(app, host="0.0.0.0", port=5173) - - # 读取Sample.md - with open("Sample.md", "r", encoding="utf-8") as f: - markdown_content = f.read() - json_dict = markdown_to_dict(markdown_content) - - # 调用 AI 扩写每个有 title 属性的节点 - json_dict = expand_text_with_ai(json_dict) - - print(json_dict) + # 获取本机所有 IPv4 地址 + ips = get_local_ips() + if not ips: + print("无法获取本机 IP 地址,使用默认地址 127.0.0.1") + ips = ["127.0.0.1"] + + # 打印所有 IP 地址 + print("服务将在以下 IP 地址上运行:") + for ip in ips: + print(f"http://{ip}:5173") + + # 启动 FastAPI 应用,绑定到所有 IP 地址 + uvicorn.run(app, host="0.0.0.0", port=5173) \ No newline at end of file diff --git a/AI/AiService/__pycache__/MarkdownToJsonConverter.cpython-310.pyc b/AI/AiService/__pycache__/MarkdownToJsonConverter.cpython-310.pyc new file mode 100644 index 00000000..9ab76b3e Binary files /dev/null and b/AI/AiService/__pycache__/MarkdownToJsonConverter.cpython-310.pyc differ