diff --git a/dsRag/StartEs.py b/dsRag/StartEs.py
index a7e877b3..f59350d2 100644
--- a/dsRag/StartEs.py
+++ b/dsRag/StartEs.py
@@ -41,8 +41,6 @@ async def lifespan(app: FastAPI):
# 初始化阿里云大模型工具
app.state.aliyun_util = ALiYunUtil()
yield
- # 清理资源
- await app.state.aliyun_util.close()
app = FastAPI(lifespan=lifespan)
# 挂载静态文件目录
@@ -124,7 +122,17 @@ async def rag_stream(request: Request):
es_conn = es_search_util.es_pool.get_connection()
try:
# 向量搜索
+ logger.info(f"\n=== 开始执行查询 ===")
+ logger.info(f"原始查询文本: {query}")
+ logger.info(f"查询标签: {query_tags}")
+
+ logger.info("\n=== 向量搜索阶段 ===")
+ logger.info("1. 文本分词和向量化处理中...")
query_embedding = es_search_util.text_to_embedding(query)
+ logger.info(f"2. 生成的查询向量维度: {len(query_embedding)}")
+ logger.info(f"3. 前3维向量值: {query_embedding[:3]}")
+
+ logger.info("4. 正在执行Elasticsearch向量搜索...")
vector_results = es_conn.search(
index=ES_CONFIG['index_name'],
body={
@@ -151,8 +159,11 @@ async def rag_stream(request: Request):
"size": 3
}
)
+ logger.info(f"5. 向量搜索结果数量: {len(vector_results['hits']['hits'])}")
# 文本精确搜索
+ logger.info("\n=== 文本精确搜索阶段 ===")
+ logger.info("1. 正在执行Elasticsearch文本精确搜索...")
text_results = es_conn.search(
index=ES_CONFIG['index_name'],
body={
@@ -175,15 +186,66 @@ async def rag_stream(request: Request):
"size": 3
}
)
+ logger.info(f"2. 文本搜索结果数量: {len(text_results['hits']['hits'])}")
# 合并结果
- results = {
+ logger.info("\n=== 最终搜索结果 ===")
+ logger.info(f"向量搜索结果: {len(vector_results['hits']['hits'])}条")
+ for i, hit in enumerate(vector_results['hits']['hits'], 1):
+ logger.info(f" {i}. 文档ID: {hit['_id']}, 相似度分数: {hit['_score']:.2f}")
+ logger.info(f" 内容: {hit['_source']['user_input']}")
+
+ logger.info("文本精确搜索结果:")
+ for i, hit in enumerate(text_results['hits']['hits']):
+ logger.info(f" {i+1}. 文档ID: {hit['_id']}, 匹配分数: {hit['_score']:.2f}")
+ logger.info(f" 内容: {hit['_source']['user_input']}")
+
+ search_results = {
"vector_results": [hit['_source'] for hit in vector_results['hits']['hits']],
"text_results": [hit['_source'] for hit in text_results['hits']['hits']]
}
- return results
+ # 调用阿里云大模型整合结果
+ aliyun_util = request.app.state.aliyun_util
+
+ # 构建提示词
+ context = "\n".join([
+ f"结果{i+1}: {res['tags']['full_content']}"
+ for i, res in enumerate(search_results['vector_results'] + search_results['text_results'])
+ ])
+
+ prompt = f"""
+ 信息检索与回答助手
+ 根据以下关于'{query}'的相关信息:
+
+ 基本信息
+ - 语言: 中文
+ - 描述: 根据提供的材料检索信息并回答问题
+ - 特点: 快速准确提取关键信息,清晰简洁地回答
+
+ 相关信息
+ {context}
+
+ 回答要求
+ 1. 依托给定的资料,快速准确地回答问题,可以添加一些额外的信息,但请勿重复内容。
+ 2. 如果未提供相关信息,请不要回答。
+ 3. 如果发现相关信息与原来的问题契合度低,也不要回答
+ 4. 使用HTML格式返回,包含适当的段落、列表和标题标签
+ 5. 确保内容结构清晰,便于前端展示
+ """
+
+ # 调用阿里云大模型
+ if len(context) > 0:
+ # 调用大模型生成回答
+ html_content = aliyun_util.chat(prompt)
+ return {"data": html_content}
+ else:
+ logger.warning(f"未找到查询'{query}'的相关数据,tags: {query_tags}")
+ return {"data": "没有在知识库中找到相关的信息,无法回答此问题。", "debug": {"query": query, "tags": query_tags}}
+ except Exception as e:
+ return {"data": f"生成报告时出错: {str(e)}"}
+
finally:
es_search_util.es_pool.release_connection(es_conn)
diff --git a/dsRag/static/ai.html b/dsRag/static/ai.html
index 0a821ef2..ef252c53 100644
--- a/dsRag/static/ai.html
+++ b/dsRag/static/ai.html
@@ -219,15 +219,15 @@
知识库范围
@@ -254,7 +254,7 @@
function submitQuestion() {
const question = document.getElementById('questionInput').value.trim();
- const checkboxes = document.querySelectorAll('input[name="documents"]:checked');
+ const checkboxes = document.querySelectorAll('input[name="tags"]:checked');
if (!question) {
alert('请输入问题!');
@@ -278,7 +278,7 @@
},
body: JSON.stringify({
query: question,
- documents: selectedDocs
+ tags: selectedDocs // 使用selectedDocs作为tags参数
})
})
.then(response => response.json()) // 改回使用json()
@@ -300,12 +300,12 @@
function clearAll() {
document.getElementById('questionInput').value = '';
document.getElementById('answerArea').innerHTML = '';
- document.querySelectorAll('input[name="documents"]').forEach(cb => cb.checked = false);
+ document.querySelectorAll('input[name="tags"]').forEach(cb => cb.checked = false);
}
function saveToWord() {
const htmlContent = document.getElementById('answerArea').innerHTML;
- const checkboxes = document.querySelectorAll('input[name="documents"]:checked');
+ const checkboxes = document.querySelectorAll('input[name="tags"]:checked');
if (!htmlContent) {
alert('没有内容可保存!');
@@ -317,7 +317,7 @@
return;
}
- const selectedDocs = Array.from(checkboxes).map(cb => cb.value);
+ const selectTags = Array.from(checkboxes).map(cb => cb.value);
const loader = document.getElementById('loader');
loader.style.display = 'block';
@@ -329,7 +329,7 @@
},
body: JSON.stringify({
html_content: htmlContent,
- documents: selectedDocs
+ tags: selectTags
})
})
.then(response => response.blob())
diff --git a/dsRag/错误日志.txt b/dsRag/错误日志.txt
new file mode 100644
index 00000000..124dc0c6
--- /dev/null
+++ b/dsRag/错误日志.txt
@@ -0,0 +1,68 @@
+D:\anaconda3\envs\rag\python.exe D:\dsWork\dsProject\dsRag\StartEs.py
+INFO: Started server process [28828]
+INFO: Waiting for application startup.
+INFO: Application startup complete.
+INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
+INFO: 10.10.21.22:50333 - "GET /static/ai.html HTTP/1.1" 304 Not Modified
+INFO: 10.10.21.22:50333 - "POST /api/rag HTTP/1.1" 500 Internal Server Error
+ERROR: Exception in ASGI application
+Traceback (most recent call last):
+ File "D:\anaconda3\envs\rag\lib\site-packages\fastapi\encoders.py", line 324, in jsonable_encoder
+ data = dict(obj)
+TypeError: 'async_generator' object is not iterable
+
+During handling of the above exception, another exception occurred:
+
+Traceback (most recent call last):
+ File "D:\anaconda3\envs\rag\lib\site-packages\fastapi\encoders.py", line 329, in jsonable_encoder
+ data = vars(obj)
+TypeError: vars() argument must have __dict__ attribute
+
+The above exception was the direct cause of the following exception:
+
+Traceback (most recent call last):
+ File "D:\anaconda3\envs\rag\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 403, in run_asgi
+ result = await app( # type: ignore[func-returns-value]
+ File "D:\anaconda3\envs\rag\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
+ return await self.app(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\fastapi\applications.py", line 1054, in __call__
+ await super().__call__(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\applications.py", line 112, in __call__
+ await self.middleware_stack(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
+ raise exc
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
+ await self.app(scope, receive, _send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
+ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
+ raise exc
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
+ await app(scope, receive, sender)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\routing.py", line 714, in __call__
+ await self.middleware_stack(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\routing.py", line 734, in app
+ await route.handle(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\routing.py", line 288, in handle
+ await self.app(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\routing.py", line 76, in app
+ await wrap_app_handling_exceptions(app, request)(scope, receive, send)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
+ raise exc
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
+ await app(scope, receive, sender)
+ File "D:\anaconda3\envs\rag\lib\site-packages\starlette\routing.py", line 73, in app
+ response = await f(request)
+ File "D:\anaconda3\envs\rag\lib\site-packages\fastapi\routing.py", line 327, in app
+ content = await serialize_response(
+ File "D:\anaconda3\envs\rag\lib\site-packages\fastapi\routing.py", line 201, in serialize_response
+ return jsonable_encoder(response_content)
+ File "D:\anaconda3\envs\rag\lib\site-packages\fastapi\encoders.py", line 332, in jsonable_encoder
+ raise ValueError(errors) from e
+ValueError: [TypeError("'async_generator' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
+INFO: Shutting down
+INFO: Waiting for application shutdown.
+INFO: Application shutdown complete.
+INFO: Finished server process [28828]
+
+进程已结束,退出代码为 0