chatbot that uses only the information in the retriever and nothing more

43 Views Asked by At

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
)       
1

There are 1 best solutions below

2
j3ffyang On

You can try the following LangChain code, based up yours, which uses:

  • pipecone vectorDB (you need to setup your project/ index, then get the API key as pre-requisite)
  • VectorStoreRetrieverMemory: retrieval with memory
  • sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 as embedding model
  • ConversationChain with memory
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
    model_name='gpt-3.5-turbo',
    temperature=0.0
)

from langchain_community.embeddings import HuggingFaceEmbeddings
embedding = HuggingFaceEmbeddings(
    model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")

from langchain_pinecone import PineconeVectorStore
from langchain.memory import VectorStoreRetrieverMemory
vectorstore = PineconeVectorStore.from_texts(
    ["Harry Potter's owl is in the castle."], embedding=embedding,
    index_name="langchain-test-index")
retriever = vectorstore.as_retriever(search_kwargs=dict(k=2))
memory = VectorStoreRetrieverMemory(retriever=retriever)

from langchain.prompts import PromptTemplate
TEMPLATE = """You're a helpful assistant, aiming at solving the problem.

Relevant pieces of previous conversation:
{history}

(You do not need to use these pieces of information if not relevant)

Answer my question: {input}
"""
PROMPT = PromptTemplate(
   input_variables=["history", "input"], template=TEMPLATE
)

from langchain.chains import ConversationChain
conversation_with_summary = ConversationChain(
    llm=llm,
    prompt=PROMPT,
    memory=memory,
    verbose=True
)

while True:
    user_input = input("Enter your question: ")
    if user_input == "exit":
        break
    else:
        result = conversation_with_summary.predict(input=user_input)
        print(result)