work_state
(ilyass mabrouk)
9
@Apoorva_Joshi Yes exactly, and I came with some changes, and for now it’s working for me
here is the current implementation that works for me
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain_openai import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain_openai import ChatOpenAI
def vector_search_from_connection_string(db_name, collection_name):
vector_search = MongoDBAtlasVectorSearch.from_connection_string(
"mongodb_connection_string",
f"{db_name}.{collection_name}",
OpenAIEmbeddings(),
index_name="vector_index"
)
return vector_search
def perform_question_answering(query):
vector_search = vector_search_from_connection_string("langchain_db", "test")
qa_retriever = vector_search.as_retriever(
search_type = "similarity",
search_kwargs = {
"k": 10,
"score_threshold": 0.75,
}
)
prompt_template = """If you encounter a question for which you don't know the answer based on the predefined points,
please respond with 'I'm sorry, I can't provide that information\nI don't have any knowledge about {context}\n\nIs there something else you'd like to ask?' and refrain from making up an answer.
However, if the answer is not present in the predefined points, then Provide comprehensive information related to the user's query.
Remember, your goal is to assist the user in the best way possible. If the question is unclear or ambiguous, you can ask for clarification.
Maintain the same language as the follow up input message.
Chat History:
{chat_history}
User's Question: {question}
AI Answer:"""
PROMPT = PromptTemplate.from_template(prompt_template)
memory = ConversationBufferMemory(
memory_key="chat_history",
input_key="question",
return_messages=True
)
qa = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(),
verbose=True,
memory=memory,
retriever=qa_retriever,
condense_question_prompt=PROMPT,
response_if_no_docs_found="""I'm sorry, I can't provide that information.
I don't have any knowledge about it.
Is there something else you'd like to ask?"""
)
docs = qa({"question": query})
return docs["answer"]
Thank you all very much for the helpful insights
2 Likes