-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa67d5f
commit b0b445d
Showing
14 changed files
with
298 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""This chain condenses the chat history and the question into one standalone question.""" | ||
|
||
from langchain_core.prompts import PromptTemplate | ||
from langchain_core.output_parsers import StrOutputParser | ||
from pydantic import BaseModel | ||
|
||
|
||
from langchain_core.runnables import RunnableSequence | ||
|
||
|
||
class QuestionWithChatHistory(BaseModel): | ||
question: str | ||
chat_history: str | ||
|
||
|
||
class StandaloneQuestion(BaseModel): | ||
standalone_question: str | ||
|
||
|
||
prompt = """\ | ||
Given the conversation history and the following question, can you rephrase the user's \ | ||
question in its original language so that it is self-sufficient. You are presented \ | ||
with a conversation that may contain some spelling mistakes and grammatical errors, \ | ||
but your goal is to understand the underlying question. Make sure to avoid the use of \ | ||
unclear pronouns. | ||
If the question is already self-sufficient, return the original question. If it seem \ | ||
the user is authorizing the chatbot to answer without specific context, make sure to \ | ||
reflect that in the rephrased question. | ||
Chat history: {chat_history} | ||
Question: {question} | ||
""" # noqa: E501 | ||
|
||
|
||
def condense_question(llm) -> RunnableSequence: | ||
condense_question_prompt = PromptTemplate.from_template( | ||
prompt | ||
) # chat_history, question | ||
|
||
standalone_question = condense_question_prompt | llm | StrOutputParser() | ||
|
||
typed_chain = standalone_question.with_types( | ||
input_type=QuestionWithChatHistory, output_type=StandaloneQuestion | ||
) | ||
return typed_chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from langchain_core.retrievers import BaseRetriever | ||
from langchain_core.runnables.history import RunnableWithMessageHistory | ||
from backend.config import RagConfig | ||
|
||
from backend.rag_components.chain_links.answer_question_from_docs_and_history import ( | ||
answer_question_from_docs_and_history_chain, | ||
) | ||
from backend.rag_components.chain_links.condense_question import condense_question | ||
from backend.rag_components.chat_message_history import get_chat_message_history | ||
|
||
from backend.utils.llm import get_text_llm | ||
from pydantic import BaseModel | ||
from langchain_core.runnables.base import RunnableSequence | ||
|
||
|
||
class QuestionWithHistory(BaseModel): | ||
"""Question with chat history.""" | ||
|
||
question: str | ||
chat_history: str | ||
|
||
|
||
class Response(BaseModel): | ||
"""Response to the question.""" | ||
|
||
response: str | ||
|
||
|
||
def construct_rag_with_history( | ||
base_chain: RunnableSequence, | ||
config: RagConfig, | ||
) -> RunnableWithMessageHistory: | ||
"""Constructs a RAG pipeline with memory. | ||
Args: | ||
config (DictConfig): Configuration object. | ||
Returns: | ||
RunnableWithMessageHistory: RAG pipeline with memory. | ||
""" | ||
text_llm = get_text_llm(config) | ||
|
||
reformulate_question = condense_question(text_llm) | ||
|
||
chain = reformulate_question | base_chain | ||
typed_chain = chain.with_types(input_type=QuestionWithHistory, output_type=Response) | ||
|
||
chain_with_mem = RunnableWithMessageHistory( | ||
typed_chain, | ||
lambda session_id: get_chat_message_history(config, session_id), | ||
input_messages_key="question", | ||
history_messages_key="chat_history", | ||
) | ||
|
||
return chain_with_mem |
Oops, something went wrong.