Could somebody please assist me in fixing the code and letting me know where I am missing out? Despite the fact that the data from earlier chats is preserved in buffermemory, it is not taken into account when responding.
Code -
PDF File Import
from langchain.document_loaders.pdf import PyPDFLoader
loader = PyPDFLoader(“titanic.pdf”)
pages = loader.load()
Splitting
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size = 1500,
chunk_overlap = 150
)
data = text_splitter.split_documents(pages)
Embeddings
from langchain.embeddings.openai import OpenAIEmbeddings
embedding = OpenAIEmbeddings(api_key=api_key)
Vector Storage
from langchain.vectorstores import Chroma
persist_directory = ‘database/’
vectordb = Chroma.from_documents(
documents=pages,
embedding=embedding,
persist_directory=persist_directory
)
Create Model
from langchain.chat_models import ChatOpenAI
llm_model = ChatOpenAI(api_key = api_key, temperature=0.2, model=“gpt-3.5-turbo”)
Memory
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key=“chat_history”,
return_messages=True
)
Conversational Retrieval Chain
from langchain.chains import ConversationalRetrievalChain
retriever=vectordb.as_retriever()
qa = ConversationalRetrievalChain.from_llm(
llm=llm_model,
retriever=retriever,
memory=memory
)
question = “can you prepare 5 questions, make sure the questions can be answered using one word”
result1 = qa({“question”: question})
print(result1[‘answer’])
print(‘Next Question’)
question = “provide 4 options to each of the above questions”
result2 = qa({“question”: question})
print(result2[‘answer’])
Output -
Response1 -
- Who were the victims of the implosion?
- Will OceanGate face criminal charges?
- What caused the Titan to fall apart?
- How much did the Titan submersible search operation cost?
- How did the pressure at depth lead to implosion?
Response2
I’m sorry, I don’t have the capability to generate multiple-choice questions. If you have any other questions or need information, feel free to ask.
memory.buffer
[HumanMessage(content=‘can you prepare 5 questions, make sure the questions can be answered using one word’),
AIMessage(content=‘1. Who were the victims of the implosion?\n2. Will OceanGate face criminal charges?\n3. What caused the Titan to fall apart?\n4. How much did the Titan submersible search operation cost?\n5. How did the pressure at depth lead to implosion?’),
HumanMessage(content=‘provide 4 options to each of the above questions’),
AIMessage(content=“I’m sorry, I don’t have the capability to generate multiple-choice questions. If you have any other questions or need information, feel free to ask.”)]