Skip to content

Commit aff026b

Browse files
committed
[DERCBOT-919] fixes
1 parent 4a56b28 commit aff026b

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/models/rag/rag_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class RagDebugData(QADebugData):
142142
description='The prompt of the question rephrased with the history of the conversation.',
143143
examples=['Given the following conversation, rephrase the follow up question to be a standalone question.'],
144144
)
145+
question_condensing_history: list[ChatMessage] = Field(
146+
description="Conversation history, used to reformulate the user's question.")
145147
condensed_question: Optional[str] = Field(
146148
description='The question rephrased with the history of the conversation.',
147149
examples=['Hello, how to plan a trip to Morocco ?'],

gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/services/langchain/callbacks/rag_callback_handler.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
class RAGCallbackHandler(BaseCallbackHandler):
2828
"""Customized RAG callback handler that retrieves data from the chain execution."""
2929

30-
records: Dict[str, Any] = {
31-
'chat_prompt': None,
32-
'chat_chain_output': None,
33-
'rag_prompt': None,
34-
'rag_chain_output': None,
35-
'documents': None,
36-
}
30+
def __init__(self):
31+
self.records: Dict[str, Any] = {
32+
'chat_prompt': None,
33+
'chat_chain_output': None,
34+
'rag_prompt': None,
35+
'rag_chain_output': None,
36+
'documents': None,
37+
}
3738

3839
def on_chain_start(
3940
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any

gen-ai/orchestrator-server/src/main/python/server/src/gen_ai_orchestrator/services/langchain/rag_chain.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,21 @@ def rag_guard(inputs, response, documents_required):
348348
chain_reply_no_answer = response['answer'] == inputs['no_answer']
349349

350350
if no_docs_but_required:
351-
if chain_can_give_no_answer_reply and chain_reply_no_answer: # We expect the chain to use it's no answer value and it did, it's the expected behavior
351+
if chain_can_give_no_answer_reply and chain_reply_no_answer:
352+
# We expect the chain to use its non-response value, and it has done so, which is the expected behavior.
352353
return
353354
# Everything else isn't expected
354355
message = 'The RAG system cannot provide an answer when no documents are found and documents are required'
355356
rag_log(level=ERROR, message=message, inputs=inputs, response=response)
356357
raise GenAIGuardCheckException(ErrorInfo(cause=message))
357358

358-
return
359+
if chain_reply_no_answer and not no_docs_retrieved:
360+
# If the chain responds with its non-response value and the documents are retrieved,
361+
# so we remove them from the RAG response.
362+
message = 'The RAG gives no answer for user question, but some documents has been found!'
363+
rag_log(level=WARNING, message=message, inputs=inputs, response=response)
364+
response['documents'] = []
365+
359366

360367
def rag_log(level, message, inputs, response):
361368
"""
@@ -386,7 +393,7 @@ def get_rag_documents(handler: RAGCallbackHandler) -> List[RagDocument]:
386393
Get documents used on RAG context
387394
388395
Args:
389-
response: the rag answer
396+
handler: the RAG Callback Handler
390397
"""
391398

392399
return [
@@ -404,9 +411,14 @@ def get_rag_debug_data(
404411
) -> RagDebugData:
405412
"""RAG debug data assembly"""
406413

414+
history = []
415+
if query.dialog:
416+
history = query.dialog.history
417+
407418
return RagDebugData(
408419
user_question=query.question_answering_prompt.inputs['question'],
409420
question_condensing_prompt=records_callback_handler.records['chat_prompt'],
421+
question_condensing_history=history,
410422
condensed_question=records_callback_handler.records['chat_chain_output'],
411423
question_answering_prompt=records_callback_handler.records['rag_prompt'],
412424
documents=get_rag_documents(records_callback_handler),

0 commit comments

Comments
 (0)