Chainlit / docs

9 stars 59 forks source link

Issue on Callback Handler: LangChainDeprecationWarning #123

Open princechoudhary16 opened 2 months ago

princechoudhary16 commented 2 months ago

Path: /api-reference/integrations/langchain This is my code: from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader from langchain.prompts import PromptTemplate from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import FAISS from langchain_community.llms import CTransformers from langchain.chains import RetrievalQA

from langchain_community.chat_loaders.whatsapp import WhatsAppChatLoader

from langchain_openai import AzureChatOpenAI

from langchain_community.chat_loaders.base import ChatSession

from langchain_community.chat_loaders.utils import (

map_ai_messages,

merge_chat_runs,

)

import chainlit as cl from db import handle_db_queries

DB_FAISS_PATH = 'C://Users//PRINCE CHOUDHARY//Downloads//Llama2-Medical-Chatbot-main//vectorstore//db_faiss'

custom_prompt_template = """Use the following pieces of information to answer the user's question. If you don't know the answer, just say that you don't know, don't try to make up an answer.

Context: {context} Question: {question}

Only return the helpful answer below and nothing else. Helpful answer: """

def set_custom_prompt(): """ Prompt template for QA retrieval for each vectorstore """ prompt = PromptTemplate(template=custom_prompt_template, input_variables=['context', 'question']) return prompt

Retrieval QA Chain

def retrieval_qa_chain(llm, prompt, db): qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=db.as_retriever(search_kwargs={'k': 2}), return_source_documents=True, chain_type_kwargs={'prompt': prompt} ) return qa_chain

Loading the model

def load_llm():

Load the locally downloaded model here

llm = CTransformers(
    #model = "TheBloke/Llama-2-7B-Chat-GGML",
    model="C://Users//PRINCE CHOUDHARY//Downloads//Llama2-Medical-Chatbot-main//llama-2-7b-chat.ggmlv3.q8_0.bin",
    model_type="llama",
    max_new_tokens = 512,
    temperature = 0.5
)
return llm

QA Model Function

def qa_bot(): embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': 'cpu'}) db = FAISS.load_local(DB_FAISS_PATH, embeddings, allow_dangerous_deserialization=True) llm = load_llm() qa_prompt = set_custom_prompt() qa = retrieval_qa_chain(llm, qa_prompt, db)

return qa

output function

def final_result(query): qa_result = qa_bot() response = qa_result({'query': query}) return response

chainlit code

@cl.on_chat_start async def start(): chain = qa_bot() if chain is None: print("Failed to initialize chain object") else: msg = cl.Message(content="Starting the bot...") await msg.send() msg.content = "Hi, Welcome to BotVerse. What is your query?" await msg.update()

    cl.user_session.set("chain", chain)

@cl.on_message async def main(message: cl.Message): chain = cl.user_session.get("chain") cb = cl.AsyncLangchainCallbackHandler( stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"] ) cb.answer_reached = True if chain is None: print("Error: The chain is not initialized.") else:

Determine if the message is a database query

    if "Chinook DB" in message.content:  # This is a simple example condition
        response = handle_db_queries(message.content)
    else:
        # Use the existing chain for other types of queries
        response = await chain.acall(message.content, callbacks=[cb])

    answer = response.get("result") if isinstance(response, dict) else response
    sources = response.get("source_documents") if isinstance(response, dict) else None

    if sources:
        answer += f"\nSources:" + str(sources)
    else:
        answer += "\nNo sources found"
    await cl.Message(content=answer).send()

On running the application This issue is coming up: C:\Users\PRINCE CHOUDHARY\Downloads\Llama2-Medical-Chatbot-main\venv\lib\site-packages\langchain_core_api\deprecation.py:117: LangChainDeprecationWarning: The function acall was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use ainvoke instead. warn_deprecated(

Can anyone help with thr issue?

amoudiri commented 2 weeks ago

replace acall in "response = await chain.acall(message.content, callbacks=[cb])" by ainvoke