I need some help,i'm building a chatbot with langchain and Pinecone ; i have tried to use the different chain that i found but none of them actually works fine.What is the best way to create a chain with a memory , a retriver, map_reduce and the prompt that specify that the bot should answear ONLY with the knowledge in the retriever? I have also an additional question , does the retriever found the chunks/docs that have an high similarity with the question ? how many?
llm = ChatOpenAI(
openai_api_key=OPENAI_API_KEY,
model_name='gpt-3.5-turbo',
temperature=0.0
)
# conversational memory
conversational_memory = ConversationSummaryBufferMemory(
llm=llm,
memory_key='chat_history',
max_token_limit=1000,
return_messages=True
)
# retrieval qa chain
from langchain.chains import RetrievalQAWithSourcesChain
qa_with_sources = RetrievalQA.from_chain_type(
llm=llm,
chain_type="map_reduce",
retriever=vectorstore.as_retriever()
)
query = input("Ask me anything: ")
from langchain.agents import Tool
tools = [
Tool(
name='Knowledge Base',
func=qa_with_sources,
description=(
'use this tool to answer with the text retrieved only'
)
)
]
from langchain.agents import initialize_agent
agent = initialize_agent(
agent='chat-conversational-react-description',
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
early_stopping_method='generate',
memory=conversational_memory
)
You can try the following LangChain code, based up yours, which uses:
pipeconevectorDB (you need to setup your project/ index, then get the API key as pre-requisite)VectorStoreRetrieverMemory: retrieval with memorysentence-transformers/paraphrase-multilingual-MiniLM-L12-v2as embedding modelConversationChainwith memory