Skip to content

Commit

Permalink
🐛 fix pydantic error in rag_basic
Browse files Browse the repository at this point in the history
  • Loading branch information
baptiste-pasquier committed Apr 17, 2024
1 parent 7868535 commit d4585ae
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions backend/rag_components/chain_links/rag_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This chain answers the provided question based on documents it retreives."""

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.retrievers import BaseRetriever
from langchain_core.runnables import RunnablePassthrough
Expand All @@ -7,13 +8,12 @@
from backend.rag_components.chain_links.documented_runnable import DocumentedRunnable
from backend.rag_components.chain_links.retrieve_and_format_docs import fetch_docs_chain


prompt = """
As a chatbot assistant, your mission is to respond to user inquiries in a precise and concise manner based on the documents provided as input. It is essential to respond in the same language in which the question was asked. Responses must be written in a professional style and must demonstrate great attention to detail. Do not invent information. You must sift through various sources of information, disregarding any data that is not relevant to the query's context. Your response should integrate knowledge from the valid sources you have identified. Additionally, the question might include hypothetical or counterfactual statements. You need to recognize these and adjust your response to provide accurate, relevant information without being misled by the counterfactuals. Respond to the question only taking into account the following context. If no context is provided, do not answer. You may provide an answer if the user explicitely asked for a general answer. You may ask the user to rephrase their question, or their permission to answer without specific context from your own knowledge.
Context: {relevant_documents}
Question: {question}
""" # noqa: E501
""" # noqa: E501


class Question(BaseModel):
Expand All @@ -26,9 +26,17 @@ class Response(BaseModel):

def rag_basic(llm, retriever: BaseRetriever) -> DocumentedRunnable:
chain = (
{"relevant_documents": fetch_docs_chain(retriever), "question": RunnablePassthrough(Question)}
{
"relevant_documents": fetch_docs_chain(retriever),
"question": RunnablePassthrough(input_type=Question),
}
| ChatPromptTemplate.from_template(prompt)
| llm
)
typed_chain = chain.with_types(input_type=str, output_type=Response)
return DocumentedRunnable(typed_chain, chain_name="Answer questions from documents stored in a vector store", prompt=prompt, user_doc=__doc__)
return DocumentedRunnable(
typed_chain,
chain_name="Answer questions from documents stored in a vector store",
prompt=prompt,
user_doc=__doc__,
)

0 comments on commit d4585ae

Please sign in to comment.