Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
黄南光
/
wiki_knowledge
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit cc153b69
authored
Jul 21, 2026
by
黄南光
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
修复rag呆板问题
1 parent
82018760
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
10 deletions
src/rag/retrieval/answer.py
src/rag/retrieval/pipeline.py
src/rag/retrieval/answer.py
View file @
cc153b6
...
@@ -163,6 +163,15 @@ FALLBACK_ANSWER = (
...
@@ -163,6 +163,15 @@ FALLBACK_ANSWER = (
"您可以补充一下品牌或更具体的问题,或者请管理员补充相关文档,我再帮您查。"
"您可以补充一下品牌或更具体的问题,或者请管理员补充相关文档,我再帮您查。"
)
)
# 闲聊/无需检索问题的对话提示词:让模型真正读用户这句话作答,而不是回固定问候语。
# 刻意不带知识库上下文——闲聊不检索,但要能自然回应"你是谁/你能干嘛/你效率怎么样"这类问题。
_CHITCHAT_SYSTEM_PROMPT
=
"""你是一名汽车后市场服务领域知识库的智能助手,友好、自然、有温度,像一位耐心的资深同事。
用户当前这句话被判定为闲聊或与知识库检索无关的日常对话。请像真实助手一样,直接、自然地回应用户说的这句话,而不是套用固定问候语。
- 可以正常聊天,也可以回答关于你自己的问题(身份、能做什么、擅长什么等)。
- 不要编造知识库里的专业数据(价格、条款、参数、政策、话术等)——这类内容只在真正检索到资料时才回答。
- 如果用户其实想查专业问题但说得太笼统,温和地引导他把问题说具体(比如补充品牌、车型或具体场景)。
- 简洁口语化,不要套用带序号的模板,不要标注来源。只输出最终回复。"""
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# 回答生成(共享系统提示词,语气温和、按意图组织、信息不足时主动追问)
# 回答生成(共享系统提示词,语气温和、按意图组织、信息不足时主动追问)
...
@@ -224,6 +233,28 @@ async def generate_answer_stream(query: str, intent: IntentResult, chunks: list[
...
@@ -224,6 +233,28 @@ async def generate_answer_stream(query: str, intent: IntentResult, chunks: list[
yield
item
yield
item
def
_format_chitchat_history
(
history
:
list
[
dict
[
str
,
Any
]]
|
None
)
->
str
:
"""把会话历史压成规整的多轮文本,供闲聊回复保持上下文连贯。"""
if
not
history
:
return
"(无)"
role_cn
=
{
"user"
:
"用户"
,
"assistant"
:
"助手"
,
"system"
:
"系统"
}
lines
=
[
f
"{role_cn.get(h.get('role'), h.get('role') or '')}:{h.get('content') or ''}"
for
h
in
history
]
return
"
\n
"
.
join
(
lines
)
async
def
generate_chitchat_stream
(
query
:
str
,
history
:
list
[
dict
[
str
,
Any
]]
|
None
=
None
):
"""闲聊/无关问题也让模型真正读问题作答(不检索知识库),流式逐步 yield。"""
prompt
=
f
"""{_CHITCHAT_SYSTEM_PROMPT}
【历史对话】
{_format_chitchat_history(history)}
用户:{query}
助手:"""
async
for
item
in
chat_stream
(
RAG_ANSWER_MODEL
,
prompt
,
purpose
=
"RAG_CHITCHAT"
):
yield
item
def
_format_chunk
(
idx
:
int
,
chunk
:
RagChunk
)
->
str
:
def
_format_chunk
(
idx
:
int
,
chunk
:
RagChunk
)
->
str
:
"""把片段渲染成带来源定位(页码/章节/表头)的上下文条目,供回答标注来源。"""
"""把片段渲染成带来源定位(页码/章节/表头)的上下文条目,供回答标注来源。"""
loc
:
list
[
str
]
=
[]
loc
:
list
[
str
]
=
[]
...
...
src/rag/retrieval/pipeline.py
View file @
cc153b6
...
@@ -26,7 +26,12 @@ from src.rag.core.config import (
...
@@ -26,7 +26,12 @@ from src.rag.core.config import (
)
)
from
src.rag.core.utils
import
safe_json_dumps
from
src.rag.core.utils
import
safe_json_dumps
from
src.rag.infra.serializers
import
_chunk_to_dict
,
_json_or_empty
from
src.rag.infra.serializers
import
_chunk_to_dict
,
_json_or_empty
from
src.rag.retrieval.answer
import
FALLBACK_ANSWER
,
generate_answer
,
generate_answer_stream
from
src.rag.retrieval.answer
import
(
FALLBACK_ANSWER
,
generate_answer
,
generate_answer_stream
,
generate_chitchat_stream
,
)
from
src.rag.retrieval.intent
import
classify_intent
from
src.rag.retrieval.intent
import
classify_intent
from
src.rag.retrieval.retriever
import
retrieve
from
src.rag.retrieval.retriever
import
retrieve
from
src.utils.llm_tracker
import
print_thinking
,
token_counter_context
from
src.utils.llm_tracker
import
print_thinking
,
token_counter_context
...
@@ -344,19 +349,33 @@ async def stream_query(payload):
...
@@ -344,19 +349,33 @@ async def stream_query(payload):
yield
_reasoning_step
(
"intent"
,
"意图识别"
,
"done"
,
step_start
,
intent
.
__dict__
)
yield
_reasoning_step
(
"intent"
,
"意图识别"
,
"done"
,
step_start
,
intent
.
__dict__
)
if
intent
.
intent
in
{
"chitchat"
,
"unsupported"
}:
if
intent
.
intent
in
{
"chitchat"
,
"unsupported"
}:
if
intent
.
intent
==
"chitchat"
:
answer
=
"您好!我是您的智能助手,请问有什么可以帮您的吗?"
else
:
answer
=
intent
.
unsupported_reason
or
"当前 RAG 知识库仅支持知识问答,暂不处理课程推荐或任务检索。"
yield
{
"type"
:
"retrieval_done"
,
"data"
:
{}}
yield
{
"type"
:
"retrieval_done"
,
"data"
:
{}}
step_start
=
time
.
time
()
step_start
=
time
.
time
()
yield
_reasoning_step
(
"answer"
,
"生成回答"
,
"running"
,
step_start
,
{
"model"
:
RAG_ANSWER_MODEL
})
yield
_reasoning_step
(
"answer"
,
"生成回答"
,
"running"
,
step_start
,
{
"model"
:
RAG_ANSWER_MODEL
})
answer
=
""
delta_count
=
0
delta_count
=
0
for
i
in
range
(
0
,
len
(
answer
),
RAG_FAKE_STREAM_CHUNK_CHARS
):
if
intent
.
intent
==
"chitchat"
:
delta
=
answer
[
i
:
i
+
8
]
# 闲聊也让模型真正读用户这句话作答(不检索知识库),而不是回固定问候语。
delta_count
+=
1
async
with
_llm_semaphore
:
yield
{
"type"
:
"answer_delta"
,
"content"
:
delta
}
token
=
print_thinking
.
set
(
False
)
await
asyncio
.
sleep
(
0
)
try
:
async
for
event
in
generate_chitchat_stream
(
payload
.
query
,
effective_history
):
if
event
[
"type"
]
==
"thinking_delta"
:
continue
if
event
[
"type"
]
==
"answer_delta"
:
answer
+=
event
[
"content"
]
delta_count
+=
1
yield
event
finally
:
print_thinking
.
reset
(
token
)
else
:
# unsupported:给出明确的不支持原因(无需模型),假流式吐出。
answer
=
intent
.
unsupported_reason
or
"当前 RAG 知识库仅支持知识问答,暂不处理课程推荐或任务检索。"
for
i
in
range
(
0
,
len
(
answer
),
RAG_FAKE_STREAM_CHUNK_CHARS
):
delta
=
answer
[
i
:
i
+
RAG_FAKE_STREAM_CHUNK_CHARS
]
delta_count
+=
1
yield
{
"type"
:
"answer_delta"
,
"content"
:
delta
}
await
asyncio
.
sleep
(
0
)
_log_answer_complete
(
answer
,
delta_count
,
step_start
)
_log_answer_complete
(
answer
,
delta_count
,
step_start
)
final_dict
=
await
_finish_query
(
payload
,
intent
,
{},
answer
,
get_tokens
(),
start
,
"Success"
)
final_dict
=
await
_finish_query
(
payload
,
intent
,
{},
answer
,
get_tokens
(),
start
,
"Success"
)
yield
_reasoning_step
(
"answer"
,
"生成回答"
,
"done"
,
step_start
,
{
"tokens"
:
get_tokens
(),
"answer_length"
:
len
(
answer
)})
yield
_reasoning_step
(
"answer"
,
"生成回答"
,
"done"
,
step_start
,
{
"tokens"
:
get_tokens
(),
"answer_length"
:
len
(
answer
)})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment