Vivek K. Singh
Vivek K. Singh

Reputation: 170

make Langchain Chatbot access multiple knowledge sources

I am building a Chatbot using langchain I want the chatbot to have a knowledgebase in a vectorstore and also have access to wikipedia. I have created the part where chatbot has a vectorstore to get data from. Please guide me on how can I go about building the feature where chatbot can access wikipedia.

def get_answer(query):
    client = get_weaviate_client()
    db = Weaviate(client=client, index_name=INDEX_NAME, text_key="text")
    llm = ChatOpenAI(temperature=0)
    
    chain = RetrievalQA.from_chain_type(
        retriever=db.as_retriever(),
        llm=llm, chain_type="stuff",
        memory=ConversationBufferWindowMemory(k=3)
    )
    response = chain(
        {"query": query},
        return_only_outputs=True,
    )
    return response['result']

Upvotes: 0

Views: 413

Answers (2)

use an ai agent to access wikipedia using a ChatOpenapi agent

from langchain_openai import OpenAI,ChatOpenAI
from langchain.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.agents import tool, AgentType, Tool,initialize_agent,load_tools

search = GoogleSerperAPIWrapper(serper_api_key=serpapi_key)
llm=ChatOpenAI(model="gpt-3.5-turbo",temperature=0, openai_api_key=key)
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
llm_math = LLMMathChain.from_llm(llm)
tools = [
     Tool(
        name="Math LLM",
        func=llm_math.run,
        description="provides answers to math-related questions",
    ),
     Tool(
        name="Wikipedia",
        func=wikipedia.run,
        description="useful for wikipedia searches",
    ),
    
    
]
agent=initialize_agent(tools,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,handle_parsing_errors=True,memory=memory)
agent.agent.llm_chain.prompt.template
agent.run("find Cerebras Sparse Large Language Model (LLM) training performance then provide a 160 word summary. extract only numbers and calculate the increase as a percentage")

Upvotes: 0

ZKS
ZKS

Reputation: 2836

you need to pip install wikipedia

 pip install wikipedia

Code sample to make call to API

from langchain.tools import WikipediaQueryRun
from langchain.utilities import WikipediaAPIWrapper

wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
print(wikipedia.run("HUNTER X HUNTER"))

Upvotes: 0

Related Questions