HuangHai 7 days ago
commit 2f1e09d1c5

@ -1,12 +1,80 @@
# routes/TeachingModelController.py
from fastapi import APIRouter, Request, Response, Depends
from fastapi import APIRouter, Depends
from auth.dependencies import *
from utils.PageUtil import *
from utils.ParseRequest import *
# 创建一个路由实例,需要依赖get_current_user,登录后才能访问
router = APIRouter(dependencies=[Depends(get_current_user)])
@router.get("/")
async def test(request: Request, response: Response):
return {"success": True, "message": "成功!"}
# 【TeachingModel-1】获取主题列表
@router.get("/getTrainedTheme")
async def get_trained_theme(request: Request):
# 获取参数
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
stage_id = await get_request_num_param(request, "stage_id", True, True, None)
subject_id = await get_request_num_param(request, "subject_id", True, True, None)
page_number = await get_request_num_param(request, "page_number", False, True, 1)
page_size = await get_request_num_param(request, "page_size", False, True, 10)
# 数据库查询
select_trained_theme_sql: str = f"SELECT * FROM t_ai_teaching_model_theme WHERE is_deleted = 0 and search_flag = 1 AND bureau_id = '{bureau_id}' AND stage_id = {stage_id} AND subject_id = {subject_id}"
print(select_trained_theme_sql)
page = await get_page_data_by_sql(select_trained_theme_sql, page_number, page_size)
page = await translate_person_bureau_name(page)
# 结果返回
return {"success": True, "message": "查询成功!", "data": page}
# 【TeachingModel-2】获取热门主题列表
@router.get("/getHotTheme")
async def get_hot_theme(request: Request):
# 获取参数
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
page_number = await get_request_num_param(request, "page_number", False, True, 1)
page_size = await get_request_num_param(request, "page_size", False, True, 3)
# 数据库查询
select_hot_theme_sql: str = f"SELECT * FROM t_ai_teaching_model_theme WHERE is_deleted = 0 and search_flag = 1 and bureau_id = '{bureau_id}' ORDER BY quote_count DESC"
print(select_hot_theme_sql)
page = await get_page_data_by_sql(select_hot_theme_sql, page_number, page_size)
page = await translate_person_bureau_name(page)
# 结果返回
return {"success": True, "message": "查询成功!", "data": page}
# 【TeachingModel-3】获取最新主题列表
@router.get("/getNewTheme")
async def get_new_theme(request: Request):
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
page_number = await get_request_num_param(request, "page_number", False, True, 1)
page_size = await get_request_num_param(request, "page_size", False, True, 3)
# 数据库查询
select_new_theme_sql: str = f"SELECT * FROM t_ai_teaching_model_theme WHERE is_deleted = 0 and search_flag = 1 and bureau_id = '{bureau_id}' ORDER BY create_time DESC"
print(select_new_theme_sql)
page = await get_page_data_by_sql(select_new_theme_sql, page_number, page_size)
page = await translate_person_bureau_name(page)
# 结果返回
return {"success": True, "message": "查询成功!", "data": page}
# 【TeachingModel-4】获取问题列表
@router.get("/getQuestion")
async def get_question(request: Request):
# 获取参数
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
person_id = await get_request_str_param(request, "person_id", True, True)
theme_id = await get_request_num_param(request, "theme_id", True, True, None)
question_type = await get_request_num_param(request, "question_type", True, True, None)
page_number = await get_request_num_param(request, "page_number", False, True, 1)
page_size = await get_request_num_param(request, "page_size", False, True, 10)
person_sql = ""
if question_type == 2:
person_sql = f"AND person_id = '{person_id}'"
# 数据库查询
select_question_sql: str = f"SELECT * FROM t_ai_teaching_model_question WHERE is_deleted = 0 and bureau_id = '{bureau_id}' AND theme_id = {theme_id} AND question_type = {question_type} {person_sql}"
print(select_question_sql)
page = await get_page_data_by_sql(select_question_sql, page_number, page_size)
return {"success": True, "message": "查询成功!", "data": page}

@ -80,7 +80,7 @@ async def save(request: Request):
if id == 0:
param["search_flag"] = 0
param["train_flag"] = 0
param["quote_num"] = 0
param["quote_count"] = 0
# 插入数据
id = await insert("t_ai_teaching_model_theme", param, False)
return {"success": True, "message": "保存成功!", "data": {"insert_id" : id}}

@ -216,4 +216,25 @@ async def execute_sql(sql):
except Exception as e:
logging.error(f"数据库查询错误: {e}")
logging.error(f"执行的SQL语句: {sql}")
raise Exception(f"执行SQL失败: {e}")
raise Exception(f"执行SQL失败: {e}")
async def find_person_name_by_id(person_id):
sql = f"SELECT person_name FROM t_sys_loginperson WHERE person_id = $1 and b_use = 1"
logging.debug(sql)
person_list = await find_by_sql(sql, (person_id,))
if person_list:
return person_list[0]['person_name']
else:
return None
async def find_bureau_name_by_id(org_id):
sql = f"SELECT org_name FROM t_base_organization WHERE org_id = $1 and b_use = 1"
logging.debug(sql)
org_list = await find_by_sql(sql, (org_id,))
if org_list:
return org_list[0]['org_name']
else:
return None

@ -46,3 +46,23 @@ async def get_page_data_by_sql(total_data_sql: str, page_number: int, page_size:
return {"page_number": page_number, "page_size": page_size, "total_row": total_row, "total_page": total_page, "list": page_data}
else:
return {"page_number": page_number, "page_size": page_size, "total_row": 0, "total_page": 0, "list": []}
# 翻译person_name, bureau_id
async def translate_person_bureau_name(page):
for item in page["list"]:
if item["person_id"] is not None:
person_id = str(item["person_id"])
person_name = await find_person_name_by_id(person_id)
if person_name:
item["person_name"] = person_name
else:
item["person_name"] = ""
if item["bureau_id"] is not None:
bureau_id = str(item["bureau_id"])
bureau_name = await find_bureau_name_by_id(bureau_id)
if bureau_name:
item["bureau_name"] = bureau_name
else:
item["bureau_name"] = ""
return page

@ -0,0 +1,371 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>【小学数学】教学大模型</title>
<link rel="icon" href="data:,">
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
text-align: center;
}
.data-area {
background-color: white;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.input-area {
background-color: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
textarea {
width: 100%;
height: 40px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
resize: none;
margin-bottom: 10px;
margin-right: 20px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #45a049;
}
#clearBtn {
background-color: #f44336;
}
#clearBtn:hover {
background-color: #d32f2f;
}
#saveWordBtn {
background-color: #2196F3;
}
.icon {
margin-right: 5px;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.doc-links {
display: flex;
gap: 20px;
margin: 20px 0;
align-items: center;
}
.doc-checkboxes {
display: flex;
gap: 20px;
margin-bottom: 10px;
}
.doc-checkboxes label {
display: flex;
align-items: center;
gap: 5px;
cursor: pointer;
}
.examples {
margin: 20px 0;
}
.example-category {
margin-bottom: 15px;
}
.example-category h3 {
margin-bottom: 10px;
color: #333;
}
.example-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.example-item {
padding: 8px 12px;
background-color: #e9f5ff;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.example-item:hover {
background-color: #d0e3fa;
}
.input-area textarea#questionInput {
margin-right: 10px !important;
width: calc(100% - 10px);
box-sizing: border-box;
}
#answerArea {
min-height: 240px;
height: auto;
overflow-y: auto;
}
.loading-animation {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 200px;
color: #666;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>【小学数学】教学大模型</h1>
<div class="data-area" id="answerArea">
<div style="color:#666; padding:20px; text-align:center;">
<p>请在下方输入您的问题,答案将在此处显示</p>
<p>您也可以点击"示例问题"快速体验</p>
</div>
</div>
<div class="input-area">
<label for="questionInput"></label><textarea id="questionInput" placeholder="请输入您的问题..."></textarea>
<div class="examples">
<div class="example-category">
<h3>已收集问题示例</h3>
<div class="example-list">
<div class="example-item" onclick="fillExample('小学数学中有哪些常见模型?')">
小学数学中有哪些常见模型?
</div>
<div class="example-item" onclick="fillExample('帮我写一下 如何理解点、线、面、体、角 的教学设计')">
帮我写一下 如何理解点、线、面、体、角 的教学设计
</div>
<div class="example-item" onclick="fillExample('三角形中两边之和大于第三边的证明')">
三角形中两边之和大于第三边的证明?
</div>
<div class="example-item"
onclick="fillExample('求证在三角形ABC中P为其内部任意一点。请证明∠BPC > ∠A。')">
求证在三角形ABC中P为其内部任意一点。请证明∠BPC > ∠A。
</div>
</div>
</div>
<div class="example-category">
<h3>未收集问题示例</h3>
<div class="example-list">
<div class="example-item" onclick="fillExample('微积分的基本定理是什么?')">微积分的基本定理是什么?
</div>
<div class="example-item" onclick="fillExample('线性代数在AI中的应用')">线性代数在AI中的应用</div>
</div>
</div>
</div>
</div>
<div class="doc-links">
<h3>知识库范围</h3>
<div class="doc-checkboxes">
<label>
<input type="radio" name="topic" value="Math" checked>
小学数学
</label>
<label>
<input type="radio" name="topic" value="JiHe">
几何
</label>
</div>
</div>
<button id="submitBtn" onclick="submitQuestion()"><span class="icon">💡</span>提问</button>
<button id="clearBtn" onclick="clearAll()"><span class="icon">🗑️</span>清空</button>
</div>
<script>
// 在页面加载时显示加载动画
document.addEventListener('DOMContentLoaded', function () {
const answerArea = document.getElementById('answerArea');
answerArea.innerHTML = '<div class="loading-animation"><div class="spinner"></div><div>正在加载资源...</div></div>';
// 禁用所有按钮
const buttons = document.querySelectorAll('button');
buttons.forEach(btn => btn.disabled = true);
});
MathJax = {
loader: {load: ['[tex]/mhchem']},
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
packages: {'[+]': ['mhchem']}
},
options: {
enableMenu: false,
processHtmlClass: 'tex2jax_process',
ignoreHtmlClass: 'tex2jax_ignore'
},
svg: {
fontCache: 'global'
},
startup: {
pageReady: () => {
return MathJax.startup.defaultPageReady().then(() => {
console.log('MathJax initialized');
// 移除加载动画并启用所有按钮
const answerArea = document.getElementById('answerArea');
answerArea.innerHTML = '<div style="color:#666; padding:20px; text-align:center;"><p>请在下方输入您的问题,答案将在此处显示</p><p>您也可以点击"示例问题"快速体验</p></div>';
const buttons = document.querySelectorAll('button');
buttons.forEach(btn => btn.disabled = false);
return MathJax.typesetPromise();
});
}
}
};
// marked配置
const markedOptions = {
mangle: false,
headerIds: false
};
function fillExample(question) {
document.getElementById('questionInput').value = question;
}
function submitQuestion() {
const question = document.getElementById('questionInput').value.trim();
const answerArea = document.getElementById('answerArea');
const topic = document.querySelector('input[name="topic"]:checked').value;
if (!question) {
alert('请输入问题!');
return;
}
// 添加加载动画
answerArea.innerHTML = '<div class="loading-animation"><div class="spinner"></div><div>思考中...</div></div>';
fetch('/api/rag', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify({
query: question,
topic: topic,
})
})
.then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let accumulatedContent = '';
function processChunk() {
return reader.read().then(({done, value}) => {
if (done) return;
buffer += decoder.decode(value, {stream: true});
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (line.includes('data:')) {
const jsonStr = line.replace(/^data:\s*/, '').replace(/^data:\s*/, '').trim();
if (jsonStr) {
try {
const data = JSON.parse(jsonStr);
if (data.reply) {
accumulatedContent += data.reply;
answerArea.innerHTML = marked.parse(accumulatedContent, markedOptions);
MathJax.typesetPromise();
}
} catch (e) {
console.log('忽略解析错误:', e);
}
}
}
}
return processChunk().then(() => {
// 在流处理完成后保存完整的markdown内容
localStorage.setItem('lastMarkdownContent', accumulatedContent);
});
});
}
return processChunk();
})
.catch(error => {
// 移除加载动画
answerArea.innerHTML = '';
console.error('Error:', error);
// 出错时也移除加载动画
answerArea.innerHTML = '<div style="color:red">请求出错,请重试</div>';
});
}
function clearAll() {
document.getElementById('questionInput').value = '';
document.getElementById('answerArea').innerHTML = '';
}
</script>
<script src="/static/mathjax/es5/tex-mml-chtml.js" id="MathJax-script" async></script>
<script src="/static/js/marked.min.js"></script>
</body>
</html>

@ -176,7 +176,9 @@
<body>
<div class="container">
<h1>【东师理想】教育大模型</h1>
<center><h3><a href="ChangChun.html">【网络爬虫示例】、长春市教育信息资讯库</a>
<center>
<br><a href="Math.html">【知识库示例】、小学数学</a><br>
<h3><a href="ChangChun.html">【网络爬虫示例】、长春市教育信息资讯库</a>
<br><br><a href="ShiJi.html">【关系图谱示例】、少年读史记</a>
<br><br><a href="tree.html">【进行中,知识图谱示例】、小学数学知识图谱</a>
</h3></center>

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save