File size: 2,586 Bytes
f3b5192
 
 
 
 
 
 
 
 
 
 
 
18cb72e
f3b5192
 
 
 
444dc2c
18cb72e
59b9bf8
 
f3b5192
 
 
 
 
 
 
 
 
 
 
748323a
f3b5192
 
 
59b9bf8
f3b5192
 
 
 
8870fe7
 
3e47a11
5b8e566
8870fe7
f3b5192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import time
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS, Chroma
from langchain.chat_models.openai import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferWindowMemory

# RetrievalQAWithSourcesChain.from_llm()
# ConversationalRetrievalChain()

persist_direcory = "openai_db_100chunksize"

embeddings = OpenAIEmbeddings()
# db = FAISS.load_local(persist_directory, embeddings)
chroma = Chroma(embedding_function=embeddings, persist_directory=persist_direcory)
# retriever = chroma.as_retriever(search_type="mmr", search_kwargs={"k": 10})
retriever = chroma.as_retriever(search_kwargs={"k": 60})
query = "what were the net sales of aws in the first quarter of 2023?"
print(retriever.get_relevant_documents(query))

memory = ConversationBufferWindowMemory(
    memory_key="chat_history", return_messages=False
)

qa = ConversationalRetrievalChain.from_llm(
    llm=ChatOpenAI(model_name="gpt-4", temperature=0, streaming=True),
    chain_type="stuff",
    retriever=retriever,
    memory=memory,
    get_chat_history=lambda h: h,
    verbose=False,
    # return_source_documents=True,
)


# res = qa({"question": "what is AD790?", "chat_history": []})
# print(res["answer"])

with gr.Blocks() as demo:
    with gr.Row():
        gr.HTML("Chat on Earning calls using GPT")
    with gr.Row():
        gr.HTML("Contact: mohamzaman@deloitte.com  ngopidi@deloitte.com")

    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("clear")

    def user(user_message, history):
        return "", history + [[user_message, None]]

    def bot(history):
        bot_message = qa.run({"question": history[-1][0], "chat_history": history[:-1]})
        history[-1][1] = ""
        for character in bot_message:
            history[-1][1] += character
            time.sleep(0.05)
            yield history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

demo.queue()
demo.launch(share=False, server_name="0.0.0.0")
"""
Total net revenues of approximately $14.6 billion was up 6.1% on an
operational basis

what was the net revenue during the quarter?
The net revenue during the quarter was $14.6 billion.
how much did it increase percentage on opeational basis?
The percentage increase in net revenue on an operational basis during the quarter was 6.1%.
what was the 2022 adjusted earnings per share guidance ?
"""