File size: 7,911 Bytes
116bc23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a64f5a9
 
 
116bc23
a64f5a9
 
cee7002
116bc23
a64f5a9
 
 
116bc23
a64f5a9
 
 
116bc23
a64f5a9
 
 
116bc23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
    )