|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI, Body
|
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
@ -18,6 +20,7 @@ client = OpenAI(
|
|
|
|
|
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def markdown_to_dict(markdown_content):
|
|
|
|
|
"""
|
|
|
|
|
将 Markdown 内容转换为 Python 字典
|
|
|
|
@ -29,6 +32,7 @@ def markdown_to_dict(markdown_content):
|
|
|
|
|
# 将 JSON 字符串转换为字典
|
|
|
|
|
return json.loads(json_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_level1(json_dict):
|
|
|
|
|
"""
|
|
|
|
|
提取一级目录,生成指定格式的 JSON 对象列表
|
|
|
|
@ -39,6 +43,7 @@ def extract_level1(json_dict):
|
|
|
|
|
return [{"type": "cover", "data": {"title": level1_title, "text": ""}}]
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_level2_and_level3(json_dict, level1_title=None):
|
|
|
|
|
"""
|
|
|
|
|
提取指定一级目录下的二级目录及其三级目录内容,生成指定格式的 JSON 对象列表
|
|
|
|
@ -81,6 +86,7 @@ def extract_level2_and_level3(json_dict, level1_title=None):
|
|
|
|
|
return result
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_contents(json_dict, level1_title=None):
|
|
|
|
|
"""
|
|
|
|
|
提取所有二级目录名称,生成目录部分的 JSON 对象
|
|
|
|
@ -95,6 +101,48 @@ def extract_contents(json_dict, level1_title=None):
|
|
|
|
|
return {"type": "contents", "data": {"items": level2_titles}}
|
|
|
|
|
return {"type": "contents", "data": {"items": []}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def expand_text_with_ai(json_dict):
|
|
|
|
|
"""
|
|
|
|
|
调用 AI 扩写每个有 title 属性的节点,生成一句话描述并放到 text 属性上
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# 递归处理字典
|
|
|
|
|
def process_dict(d):
|
|
|
|
|
if isinstance(d, dict):
|
|
|
|
|
for key, value in d.items():
|
|
|
|
|
if key == "title" and isinstance(value, str):
|
|
|
|
|
# 基于全文和当前 title 生成一句话描述
|
|
|
|
|
prompt = f"基于以下内容,为标题 '{value}' 生成一句话描述,言简意赅,不要使用标点符号:\n{json.dumps(json_dict, ensure_ascii=False)}"
|
|
|
|
|
# 调用 AI 生成描述
|
|
|
|
|
response = client.chat.completions.create(
|
|
|
|
|
model=MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": "你是一个教学经验丰富的基础教育教师"},
|
|
|
|
|
{"role": "user", "content": prompt}
|
|
|
|
|
],
|
|
|
|
|
temperature=0.7
|
|
|
|
|
)
|
|
|
|
|
# 提取生成的描述
|
|
|
|
|
description = response.choices[0].message.content.strip()
|
|
|
|
|
# 去掉标点符号
|
|
|
|
|
description = re.sub(r'[^\w\s]', '', description)
|
|
|
|
|
# 将描述赋值给 text 属性
|
|
|
|
|
d["text"] = description
|
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
|
process_dict(value)
|
|
|
|
|
elif isinstance(value, list):
|
|
|
|
|
for item in value:
|
|
|
|
|
process_dict(item)
|
|
|
|
|
elif isinstance(d, list):
|
|
|
|
|
for item in d:
|
|
|
|
|
process_dict(item)
|
|
|
|
|
|
|
|
|
|
# 处理整个字典
|
|
|
|
|
process_dict(json_dict)
|
|
|
|
|
return json_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def ConvertMarkdownToJson(markdown_content):
|
|
|
|
|
"""
|
|
|
|
|
生成一个 AsyncIterable,逐行返回 JSON 字符串
|
|
|
|
@ -102,6 +150,9 @@ async def ConvertMarkdownToJson(markdown_content):
|
|
|
|
|
# 将 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)
|
|
|
|
@ -125,6 +176,7 @@ async def ConvertMarkdownToJson(markdown_content):
|
|
|
|
|
# 添加结束标记
|
|
|
|
|
yield '{"type": "end" }'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 获取本机所有 IPv4 地址
|
|
|
|
|
def get_local_ips():
|
|
|
|
|
ips = []
|
|
|
|
@ -140,6 +192,7 @@ def get_local_ips():
|
|
|
|
|
print(f"获取 IP 地址失败: {e}")
|
|
|
|
|
return ips
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 流式生成数据的函数
|
|
|
|
|
async def generate_stream_markdown(course_name: str):
|
|
|
|
|
"""
|
|
|
|
@ -150,7 +203,8 @@ async def generate_stream_markdown(course_name: str):
|
|
|
|
|
model=MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{'role': 'system', 'content': '你是一个教学经验丰富的基础教育教师'},
|
|
|
|
|
{'role': 'user', 'content': '帮我设计一下' + course_name + '的课件提纲,用markdown格式返回。强调1、标签只能返回 #,##,###,-,其它标签一率不可以返回,这个非常重要!2、不要返回 ```markdown 或者 ``` 这样的内容! 3、每部分都有生成完整的一、二、三级内容,不能省略。'}
|
|
|
|
|
{'role': 'user',
|
|
|
|
|
'content': '帮我设计一下' + course_name + '的课件提纲,用markdown格式返回。强调1、标签只能返回 #,##,###,-,其它标签一率不可以返回,这个非常重要!2、不要返回 ```markdown 或者 ``` 这样的内容! 3、每部分都有生成完整的一、二、三级内容,不能省略。'}
|
|
|
|
|
],
|
|
|
|
|
stream=True, # 启用流式响应
|
|
|
|
|
timeout=6000,
|
|
|
|
@ -172,6 +226,7 @@ async def generate_stream_markdown(course_name: str):
|
|
|
|
|
print("\n完整的 Markdown 内容:")
|
|
|
|
|
print(full_markdown)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
# 添加 CORS 中间件
|
|
|
|
@ -183,14 +238,16 @@ app.add_middleware(
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 根路由,返回提示信息
|
|
|
|
|
@app.get("/")
|
|
|
|
|
def root():
|
|
|
|
|
return PlainTextResponse("Hello ApiStream")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/tools/aippt_outline") # 仅支持 POST 方法
|
|
|
|
|
async def aippt_outline(
|
|
|
|
|
course_name: str = Body(..., embed=True, description="课程名称") # 从请求体中获取 course_name
|
|
|
|
|
course_name: str = Body(..., embed=True, description="课程名称") # 从请求体中获取 course_name
|
|
|
|
|
):
|
|
|
|
|
# 返回流式响应
|
|
|
|
|
return StreamingResponse(
|
|
|
|
@ -204,6 +261,7 @@ async def aippt_outline(
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/tools/aippt") # 修改为 POST 方法
|
|
|
|
|
async def aippt(content: str = Body(..., embed=True, description="Markdown 内容")): # 使用 Body 接收请求体参数
|
|
|
|
|
return StreamingResponse(
|
|
|
|
@ -218,18 +276,29 @@ 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)
|
|
|
|
|
# # 获取本机所有 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)
|
|
|
|
|