main
HuangHai 2 weeks ago
parent bbc2c9a636
commit 6647e49033

@ -0,0 +1,83 @@
import json
import fastapi
import uvicorn
from fastapi import FastAPI
from lightrag import QueryParam
from sse_starlette import EventSourceResponse
from starlette.staticfiles import StaticFiles
from Util.LightRagUtil import *
# 在程序开始时添加以下配置
logging.basicConfig(
level=logging.INFO, # 设置日志级别为INFO
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# 或者如果你想更详细地控制日志输出
logger = logging.getLogger('lightrag')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
async def lifespan(app: FastAPI):
yield
async def print_stream(stream):
async for chunk in stream:
if chunk:
print(chunk, end="", flush=True)
app = FastAPI(lifespan=lifespan)
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="Static"), name="static")
@app.post("/api/rag")
async def rag(request: fastapi.Request):
data = await request.json()
topic = data.get("topic") # Chinese, Math
# 拼接路径
WORKING_PATH= "./Topic/" + topic
# 查询的问题
query = data.get("query")
# 关闭参考资料
user_prompt="\n 1、不要输出参考资料 或者 References "
user_prompt = user_prompt + "\n 2、如果问题与提供的知识库内容不符则明确告诉未在知识库范围内提到"
async def generate_response_stream(query: str):
try:
rag = LightRAG(
working_dir=WORKING_PATH,
llm_model_func=create_llm_model_func(),
embedding_func=create_embedding_func()
)
await rag.initialize_storages()
await initialize_pipeline_status()
resp = await rag.aquery(
query=query,
param=QueryParam(mode="hybrid", stream=True))
async for chunk in resp:
if not chunk:
continue
yield f"data: {json.dumps({'reply': chunk})}\n\n"
print(chunk, end='', flush=True)
except Exception as e:
yield f"data: {json.dumps({'error': str(e)})}\n\n"
finally:
# 清理资源
await rag.finalize_storages()
return EventSourceResponse(generate_response_stream(query=query))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

@ -1,87 +0,0 @@
import json
import logging
import os
import warnings
from logging.handlers import RotatingFileHandler
import fastapi
import uvicorn
from fastapi import FastAPI
from sse_starlette import EventSourceResponse
from starlette.staticfiles import StaticFiles
from Util.LightRagUtil import initialize_rag
from lightrag import QueryParam
# 初始化日志
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# 配置日志处理器
log_file = os.path.join(os.path.dirname(__file__), 'Logs', 'app.log')
os.makedirs(os.path.dirname(log_file), exist_ok=True)
# 文件处理器
file_handler = RotatingFileHandler(
log_file, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
# 控制台处理器
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)
logger.addHandler(console_handler)
async def lifespan(app: FastAPI):
# 抑制HTTPS相关警告
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
# 初始化RAG
app.state.rag = await initialize_rag(working_dir="./Test/Math")
yield
# 清理资源
await app.state.rag.finalize_storages()
async def print_stream(stream):
async for chunk in stream:
if chunk:
print(chunk, end="", flush=True)
app = FastAPI(lifespan=lifespan)
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="Static"), name="static")
@app.post("/api/rag")
async def rag(request: fastapi.Request):
data = await request.json()
query = data.get("query")
async def generate_response_stream(query: str):
try:
resp = await request.app.state.rag.aquery(
query=query,
param=QueryParam(mode="hybrid", stream=True))
async for chunk in resp:
if not chunk:
continue
yield f"data: {json.dumps({'reply': chunk})}\n\n"
print(chunk, end='', flush=True)
except Exception as e:
yield f"data: {json.dumps({'error': str(e)})}\n\n"
return EventSourceResponse(generate_response_stream(query=query))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

@ -1,87 +0,0 @@
import json
import logging
import os
import warnings
from logging.handlers import RotatingFileHandler
import fastapi
import uvicorn
from fastapi import FastAPI
from sse_starlette import EventSourceResponse
from starlette.staticfiles import StaticFiles
from Util.LightRagUtil import initialize_rag
from lightrag import QueryParam
# 初始化日志
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# 配置日志处理器
log_file = os.path.join(os.path.dirname(__file__), 'Logs', 'app.log')
os.makedirs(os.path.dirname(log_file), exist_ok=True)
# 文件处理器
file_handler = RotatingFileHandler(
log_file, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
# 控制台处理器
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)
logger.addHandler(console_handler)
async def lifespan(app: FastAPI):
# 抑制HTTPS相关警告
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
# 初始化RAG
app.state.rag = await initialize_rag(working_dir="./Tools/Topic/Chinese")
yield
# 清理资源
await app.state.rag.finalize_storages()
async def print_stream(stream):
async for chunk in stream:
if chunk:
print(chunk, end="", flush=True)
app = FastAPI(lifespan=lifespan)
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="Static"), name="static")
@app.post("/api/rag")
async def rag(request: fastapi.Request):
data = await request.json()
query = data.get("query")
async def generate_response_stream(query: str):
try:
resp = await request.app.state.rag.aquery(
query=query,
param=QueryParam(mode="hybrid", stream=True))
async for chunk in resp:
if not chunk:
continue
yield f"data: {json.dumps({'reply': chunk})}\n\n"
print(chunk, end='', flush=True)
except Exception as e:
yield f"data: {json.dumps({'error': str(e)})}\n\n"
return EventSourceResponse(generate_response_stream(query=query))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

@ -4,6 +4,18 @@ from Util.DocxUtil import get_docx_content_by_pandoc
from Util.LightRagUtil import configure_logging, initialize_rag
import os
# 化学
#WORKING_DIR = "./Topic/Chemistry"
#docx_file = 'static/Txt/化学方程式.docx'
# 苏轼
#WORKING_DIR = "./Topic/Chinese"
#docx_file = 'static/Txt/苏轼.docx'
# 数学
WORKING_DIR = "./Topic/Math"
docx_file = 'static/Txt/小学数学教学中的若干问题.docx'
async def main():
# 注释掉或删除以下清理代码
files_to_delete = [
@ -15,10 +27,7 @@ async def main():
"vdb_entities.json",
"vdb_relationships.json",
]
# 创建工作目录
WORKING_DIR="./Topic/Chemistry"
# docx文件路径
docx_file = 'static/Txt/化学方程式.docx'
# 获取docx文件的内容
content = get_docx_content_by_pandoc(docx_file)

File diff suppressed because one or more lines are too long

@ -0,0 +1,14 @@
{
"hybrid": {
"0eea3a1f6f9b9c5cc3fc45c951cdff0b": {
"return": "{\"high_level_keywords\": [\"\\u5c0f\\u5b66\\u6570\\u5b66\", \"\\u6559\\u5b66\\u6a21\\u578b\", \"\\u6559\\u80b2\\u65b9\\u6cd5\"], \"low_level_keywords\": [\"\\u52a0\\u51cf\\u6cd5\\u6a21\\u578b\", \"\\u4e58\\u9664\\u6cd5\\u6a21\\u578b\", \"\\u51e0\\u4f55\\u56fe\\u5f62\", \"\\u5206\\u6570\\u6a21\\u578b\", \"\\u5e94\\u7528\\u9898\"]}",
"cache_type": "keywords",
"chunk_id": null,
"embedding": null,
"embedding_shape": null,
"embedding_min": null,
"embedding_max": null,
"original_prompt": "小学数学中有哪些常见模型?"
}
}
}

@ -112,3 +112,31 @@ async def initialize_rag(working_dir):
await initialize_pipeline_status()
return rag
def create_llm_model_func():
def llm_model_func(prompt, system_prompt=None, history_messages=[], **kwargs):
return openai_complete_if_cache(
LLM_MODEL_NAME,
prompt,
system_prompt=system_prompt,
history_messages=history_messages,
api_key=LLM_API_KEY,
base_url=LLM_BASE_URL,
**kwargs,
)
return llm_model_func
def create_embedding_func():
return EmbeddingFunc(
embedding_dim=1024,
max_token_size=8192,
func=lambda texts: openai_embed(
texts,
model=EMBED_MODEL_NAME,
api_key=EMBED_API_KEY,
base_url=EMBED_BASE_URL,
),
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 460 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>东师理想教学大模型</title>
<title>东师理想教学大模型</title>
<link rel="icon" href="data:,">
<style>
body {
@ -47,7 +47,7 @@
border-radius: 4px;
resize: none;
margin-bottom: 10px;
margin-right: 20px; /* 新增右侧边距 */
margin-right: 20px;
}
button {
@ -76,10 +76,6 @@
background-color: #2196F3;
}
#saveWordBtn:hover {
background-color: #0b7dda;
}
.icon {
margin-right: 5px;
}
@ -100,18 +96,6 @@
align-items: center;
}
.doc-link {
padding: 8px 15px;
background-color: #e7f3fe;
border-radius: 4px;
color: #2196F3;
text-decoration: none;
transition: all 0.3s;
}
.doc-link:hover {
background-color: #d0e3fa;
}
.doc-checkboxes {
display: flex;
@ -164,9 +148,9 @@
}
#answerArea {
min-height: 240px; /* 12行高度 */
min-height: 240px;
height: auto;
overflow-y: auto; /* 添加滚动条 */
overflow-y: auto;
}
.loading-animation {
@ -187,15 +171,6 @@
animation: spin 1s linear infinite;
margin-bottom: 10px;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
@ -213,38 +188,54 @@
<div class="examples">
<div class="example-category">
<h3>知识库内问题示例:</h3>
<h3>已收集问题示例</h3>
<div class="example-list">
<div class="example-item" onclick="fillExample('小学数学中有哪些模型?')">小学数学中有哪些模型?</div>
<div class="example-item" onclick="fillExample('帮我写一下 “如何理解点、线、面、体、角”的教学设计')">
帮我写一下 “如何理解点、线、面、体、角”的教学设计
<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('苏轼的家人都有谁?')">苏轼的家人都有谁?</div>
<div class="example-item" onclick="fillExample('苏轼有哪些有名的诗句?')">苏轼有哪些有名的诗句?</div>
<div class="example-item" onclick="fillExample('氧化铁和硝酸的反应方程式?')">
氧化铁和硝酸的反应方程式?
</div>
</div>
</div>
<div class="example-category">
<h3>知识库外问题示例:</h3>
<h3>未收集问题示例</h3>
<div class="example-list">
<div class="example-item" onclick="fillExample('微积分的基本定理是什么?')">微积分的基本定理是什么?
<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="checkbox" name="tags" value="MATH_1" checked>
小学数学
</label>
</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="Chinese">
苏轼知识
</label>
<label>
<input type="radio" name="topic" value="Chemistry">
化学学科
</label>
</div>
<button id="submitBtn" onclick="submitQuestion()"><span class="icon">💡</span>提问</button>
<button id="clearBtn" onclick="clearAll()"><span class="icon">🗑️</span>清空</button>
</div>
<button id="submitBtn" onclick="submitQuestion()"><span class="icon">💡</span>提问</button>
<button id="clearBtn" onclick="clearAll()"><span class="icon">🗑️</span>清空</button>
</div>
</div>
<script>
@ -288,13 +279,7 @@
}
}
};
</script>
<!--国内可以访问的JS,不要使用静态的本地的,因为文件不全的话没有好的显示效果!-->
<script src="/static/mathjax/es5/tex-mml-chtml.js" id="MathJax-script" async></script>
<script src="/static/js/marked.min.js"></script>
<!--<script src="https://cdn.bootcdn.net/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js" id="MathJax-script" async></script>-->
<!--<script src="https://cdn.bootcdn.net/ajax/libs/marked/4.0.0/marked.min.js"></script>-->
<script>
// marked配置
const markedOptions = {
mangle: false,
@ -308,15 +293,16 @@
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: {
@ -324,67 +310,67 @@
'Accept': 'text/event-stream'
},
body: JSON.stringify({
query: question
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();
.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);
}
} catch (e) {
console.log('忽略解析错误:', e);
}
}
}
}
return processChunk().then(() => {
// 在流处理完成后保存完整的markdown内容
localStorage.setItem('lastMarkdownContent', accumulatedContent);
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>';
});
}
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 = '';
document.querySelectorAll('input[name="tags"]').forEach(cb => cb.checked = false);
}
</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>
</html>

@ -1,392 +0,0 @@
<!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;
}
#saveWordBtn:hover {
background-color: #0b7dda;
}
.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-link {
padding: 8px 15px;
background-color: #e7f3fe;
border-radius: 4px;
color: #2196F3;
text-decoration: none;
transition: all 0.3s;
}
.doc-link:hover {
background-color: #d0e3fa;
}
.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; /* 12行高度 */
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;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="container">
<h1>非结构化文档+自动知识图谱构建 </h1>
<center><h2>【苏轼知识】大模型问答 <a href="Txt/sushi.txt">知识库文档</a></h2></center>
<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>
</div>
<div class="example-category">
<h3>知识库外问题示例:</h3>
<div class="example-list">
<div class="example-item" onclick="fillExample('福尔摩斯是谁?')">福尔摩斯是谁?
</div>
</div>
</div>
</div>
<div class="doc-links">
<h3>知识库范围</h3>
<div class="doc-checkboxes">
<label>
<input type="checkbox" name="tags" value="MATH_1" checked>
苏轼知识库
</label>
</div>
</div>
<button id="submitBtn" onclick="submitQuestion()"><span class="icon">💡</span>提问</button>
<button id="clearBtn" onclick="clearAll()"><span class="icon">🗑️</span>清空</button>
<img src="https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/%7Byear%7D/%7Bmonth%7D/%7Bmd5%7D.%7BextName%7D/20250705112607260.png" width="500px" height="400px" />
</div>
</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();
});
}
}
};
</script>
<!--国内可以访问的JS,不要使用静态的本地的,因为文件不全的话没有好的显示效果!-->
<script src="/static/mathjax/es5/tex-mml-chtml.js" id="MathJax-script" async></script>
<script src="/static/js/marked.min.js"></script>
<!--<script src="https://cdn.bootcdn.net/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js" id="MathJax-script" async></script>-->
<!--<script src="https://cdn.bootcdn.net/ajax/libs/marked/4.0.0/marked.min.js"></script>-->
<script>
// 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');
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
})
})
.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 = '';
document.querySelectorAll('input[name="tags"]').forEach(cb => cb.checked = false);
}
</script>
</body>
</html>
Loading…
Cancel
Save