from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chains import RetrievalQA from langchain.chat_models import ChatOpenAI from langchain.document_loaders import PyMuPDFLoader import os from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType import chainlit as cl @cl.on_chat_start async def start(): welcome_message1 = "Hey, Welcome to **10K-GPT**!" welcome_message2 = "**10K-GPT** is designed to provide you with an interactive, user-friendly interface that lets you pose queries and fetch answers from Form 10-K documents of some of the world's leading tech giants:\n- **Meta**\n- **Amazon**\n- **Alphabet**\n- **Apple**\n- **Microsoft**,\n\nfor the years **2022, 2021, and 2020**.\n\nPlease ask a question to begin!" sample_questions="Some of the questions you can try:\n***" elements = [ cl.Image(path="10KGPTLogo.png", name="10K-GPT", display="inline"), # cl.Text(content=welcome_message1, name="10K-GPT", display="inline"), ] await cl.Message(content=welcome_message1, elements=elements).send() await cl.Message(content=welcome_message2).send() @cl.langchain_factory(use_async=False) def load(): embeddings = OpenAIEmbeddings() llm = ChatOpenAI(temperature=0, model="gpt-4", streaming=True) apple_2022_docs_store = FAISS.load_local(r'data/datastores/apple_2022', embeddings) apple_2021_docs_store = FAISS.load_local(r'data/datastores/apple_2021', embeddings) apple_2020_docs_store = FAISS.load_local(r'data/datastores/apple_2020', embeddings) microsoft_2022_docs_store = FAISS.load_local(r'data/datastores/msft_2022', embeddings) microsoft_2021_docs_store = FAISS.load_local(r'data/datastores/msft_2021', embeddings) microsoft_2020_docs_store = FAISS.load_local(r'data/datastores/msft_2020', embeddings) amazon_2022_docs_store = FAISS.load_local(r'data/datastores/amzn_2022', embeddings) amazon_2021_docs_store = FAISS.load_local(r'data/datastores/amzn_2021', embeddings) amazon_2020_docs_store = FAISS.load_local(r'data/datastores/amzn_2020', embeddings) alphabet_2022_docs_store = FAISS.load_local(r'data/datastores/alphbt_2022', embeddings) alphabet_2021_docs_store = FAISS.load_local(r'data/datastores/alphbt_2021', embeddings) alphabet_2020_docs_store = FAISS.load_local(r'data/datastores/alphbt_2020', embeddings) meta_2022_docs_store = FAISS.load_local(r'data/datastores/meta_2022', embeddings) meta_2021_docs_store = FAISS.load_local(r'data/datastores/meta_2021', embeddings) meta_2020_docs_store = FAISS.load_local(r'data/datastores/meta_2020', embeddings) apple_2022_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=apple_2022_docs_store.as_retriever(search_kwargs={'k':5})) apple_2021_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=apple_2021_docs_store.as_retriever(search_kwargs={'k':5})) apple_2020_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=apple_2020_docs_store.as_retriever(search_kwargs={'k':5})) microsoft_2022_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=microsoft_2022_docs_store.as_retriever(search_kwargs={'k':5})) microsoft_2021_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=microsoft_2021_docs_store.as_retriever(search_kwargs={'k':5})) microsoft_2020_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=microsoft_2020_docs_store.as_retriever(search_kwargs={'k':5})) amazon_2022_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=amazon_2022_docs_store.as_retriever(search_kwargs={'k':5})) amazon_2021_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=amazon_2021_docs_store.as_retriever(search_kwargs={'k':5})) amazon_2020_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=amazon_2020_docs_store.as_retriever(search_kwargs={'k':5})) meta_2022_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=meta_2022_docs_store.as_retriever(search_kwargs={'k':5})) meta_2021_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=meta_2021_docs_store.as_retriever(search_kwargs={'k':5})) meta_2020_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=meta_2020_docs_store.as_retriever(search_kwargs={'k':5})) alphabet_2022_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=alphabet_2022_docs_store.as_retriever(search_kwargs={'k':5})) alphabet_2021_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=alphabet_2021_docs_store.as_retriever(search_kwargs={'k':5})) alphabet_2020_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=alphabet_2020_docs_store.as_retriever(search_kwargs={'k':5})) tools = [ Tool( name="Apple Form 10K 2022", func=apple_2022_qa.run, description="useful when you need to answer from Apple 2022", ), Tool( name="Apple Form 10K 2021", func=apple_2021_qa.run, description="useful when you need to answer from Apple 2021", ), Tool( name="Apple Form 10K 2020", func=apple_2020_qa.run, description="useful when you need to answer from Apple 2020", ), Tool( name="Microsoft Form 10K 2022", func=microsoft_2022_qa.run, description="useful when you need to answer from Microsoft 2022", ), Tool( name="Microsoft Form 10K 2021", func=microsoft_2021_qa.run, description="useful when you need to answer from Microsoft 2021", ), Tool( name="Microsoft Form 10K 2020", func=microsoft_2020_qa.run, description="useful when you need to answer from Microsoft 2020", ), Tool( name="Meta Form 10K 2022", func=meta_2022_qa.run, description="useful when you need to answer from Meta 2022", ), Tool( name="Meta Form 10K 2021", func=meta_2021_qa.run, description="useful when you need to answer from Meta 2021", ), Tool( name="Meta Form 10K 2020", func=meta_2020_qa.run, description="useful when you need to answer from Meta 2020", ), Tool( name="Alphabet Form 10K 2022", func=alphabet_2022_qa.run, description="useful when you need to answer from Alphabet or Google 2022", ), Tool( name="Alphabet Form 10K 2021", func=alphabet_2021_qa.run, description="useful when you need to answer from Alphabet or Google 2021", ), Tool( name="Alphabet Form 10K 2020", func=alphabet_2020_qa.run, description="useful when you need to answer from Alphabet or Google 2020", ), Tool( name="Amazon Form 10K 2022", func=amazon_2022_qa.run, description="useful when you need to answer from Amazon 2022", ), Tool( name="Amazon Form 10K 2021", func=amazon_2021_qa.run, description="useful when you need to answer from Amazon 2021", ), Tool( name="Amazon Form 10K 2020", func=amazon_2020_qa.run, description="useful when you need to answer from Amazon 2020", ), ] # Construct the agent. We will use the default agent type here. # See documentation for a full list of options. return initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True )