Skip to content

Commit 6979387

Browse files
committed
feat: enhance question rephrasing prompt with detailed instructions and context usage
1 parent e2c5a04 commit 6979387

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

libs/rag-core-api/src/rag_core_api/impl/graph/chat_graph.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ async def _determine_language_node(self, state: dict, config: Optional[RunnableC
205205
return {"language": question_language}
206206

207207
async def _rephrase_node(self, state: dict, config: Optional[RunnableConfig] = None) -> dict:
208+
if not state.get("history"):
209+
return {"rephrased_question": state["question"]}
208210
rephrased_question = await self._rephrasing_chain.ainvoke(chain_input=state, config=config)
209211
# Ensure rephrased_question is a string
210-
if hasattr(rephrased_question, "content"):
211-
rephrased_question = rephrased_question.content
212-
elif not isinstance(rephrased_question, str):
213-
rephrased_question = str(rephrased_question)
212+
rephrased_question = getattr(rephrased_question, "content", rephrased_question)
213+
rephrased_question = rephrased_question.strip() if isinstance(rephrased_question, str) else str(rephrased_question).strip()
214+
if not rephrased_question:
215+
rephrased_question = state["question"]
214216
return {"rephrased_question": rephrased_question}
215217

216218
async def _generate_node(self, state: dict, config: Optional[RunnableConfig] = None) -> dict:
@@ -228,7 +230,8 @@ async def _generate_node(self, state: dict, config: Optional[RunnableConfig] = N
228230

229231
async def _retrieve_node(self, state: dict) -> dict:
230232
try:
231-
retrieved_documents = await self._composite_retriever.ainvoke(retriever_input=state["rephrased_question"])
233+
question = state.get("rephrased_question") or state["question"]
234+
retrieved_documents = await self._composite_retriever.ainvoke(retriever_input=question)
232235
except NoOrEmptyCollectionError:
233236
logger.warning("No or empty collection encountered.")
234237
return {
@@ -280,11 +283,9 @@ def _add_nodes(self):
280283
self._state_graph.add_node(GraphNodeNames.ERROR_NODE, self._error_node)
281284

282285
def _wire_graph(self):
283-
self._state_graph.add_edge(START, GraphNodeNames.REPHRASE)
284286
self._state_graph.add_edge(START, GraphNodeNames.DETERMINE_LANGUAGE)
285-
self._state_graph.add_edge(
286-
[GraphNodeNames.REPHRASE, GraphNodeNames.DETERMINE_LANGUAGE], GraphNodeNames.RETRIEVE
287-
)
287+
self._state_graph.add_edge(GraphNodeNames.DETERMINE_LANGUAGE, GraphNodeNames.REPHRASE)
288+
self._state_graph.add_edge(GraphNodeNames.REPHRASE, GraphNodeNames.RETRIEVE)
288289
self._state_graph.add_conditional_edges(
289290
GraphNodeNames.RETRIEVE, self._docs_retrieved_edge, [GraphNodeNames.GENERATE, GraphNodeNames.ERROR_NODE]
290291
)

libs/rag-core-api/src/rag_core_api/prompt_templates/question_rephrasing_prompt.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
QUESTION_REPHRASING_PROMPT = ChatPromptTemplate.from_messages(
55
[
66
SystemMessagePromptTemplate.from_template(
7-
"Rephrase the question. Use relevant context from the history for this."
7+
"""You rewrite the user's latest message into a SINGLE, standalone search query for retrieval.
8+
9+
Rules:
10+
- Use relevant details from ChatHistory to resolve pronouns and ellipses.
11+
- Preserve the user's intent exactly; do not answer the question.
12+
- Keep the output in {language}.
13+
- Do not introduce facts not present in the Question or ChatHistory.
14+
- If the original question is already standalone, return it unchanged.
15+
- Return ONLY the rewritten question text. No preamble, no quotes."""
816
),
917
HumanMessagePromptTemplate.from_template(
1018
"""Question: {question}
11-
ChatHistory: {history}"""
19+
ChatHistory: {history}
20+
language: {language}"""
1221
),
1322
]
1423
)

0 commit comments

Comments
 (0)