Team Ai
Apppublic

itismouad/pythonic-raqa-langchain-pinecone

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
0likes
app.py94 linesDownload Raw Back to root
1from operator import itemgetter2import chainlit as cl3from langchain.schema.runnable import RunnablePassthrough4from langchain.vectorstores import FAISS5from langchain.chains import RetrievalQA6from langchain.chat_models import ChatOpenAI7from langchain.prompts.chat import (8    ChatPromptTemplate,9    SystemMessagePromptTemplate,10    HumanMessagePromptTemplate,11)12 13from utils import ArxivLoader, PineconeIndexer14 15system_template = """16Use the provided context to answer the user's query.17 18You may not answer the user's query unless there is specific context in the following text.19 20If you do not know the answer, or cannot answer, please respond with "I don't know".21 22Context:23{context}24"""25 26messages = [27    SystemMessagePromptTemplate.from_template(system_template),28    HumanMessagePromptTemplate.from_template("{question}"),29]30 31prompt = ChatPromptTemplate(messages=messages)32chain_type_kwargs = {"prompt": prompt}33 34@cl.author_rename35def rename(orig_author: str):36    rename_dict = {"RetrievalQA": "Learning about Nuclear Fission"}37    return rename_dict.get(orig_author, orig_author)38 39@cl.on_chat_start  # marks a function that will be executed at the start of a user session40async def start_chat():41 42    msg = cl.Message(content=f"Initializing the Application...")43    await msg.send()44 45    # load documents from Arxiv46    axloader = ArxivLoader()47    axloader.main()48 49    # load embedder and the retriever50    pi = PineconeIndexer()51    pi.load_embedder()52    retriever=pi.get_vectorstore().as_retriever()53    print(pi.index.describe_index_stats())54 55    # build llm56    llm = ChatOpenAI(57        model="gpt-3.5-turbo",58        temperature=059    )60 61    msg = cl.Message(content=f"Application is ready !")62    await msg.send()63 64    cl.user_session.set("llm", llm)65    cl.user_session.set("retriever", retriever)66 67@cl.on_message  # marks a function that should be run each time the chatbot receives a message from a user68async def main(message: cl.Message):69 70    llm = cl.user_session.get("llm")71    retriever = cl.user_session.get("retriever")72 73    retrieval_augmented_qa_chain = (74        {"context": itemgetter("question") | retriever,75        "question": itemgetter("question")76        }77        | RunnablePassthrough.assign(78            context=itemgetter("context")79        )80        | {81            "response": prompt  | llm,82            "context": itemgetter("context"),83        }84    )85 86    answer = retrieval_augmented_qa_chain.invoke({"question" : message.content})87    88    await cl.Message(content=answer["response"].content).send()89 90 91 92 93 94