|
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 |
|
|
|
|
|
|
|
|
|
persist_direcory = "openai_db_100chunksize" |
|
|
|
embeddings = OpenAIEmbeddings() |
|
|
|
chroma = Chroma(embedding_function=embeddings, persist_directory=persist_direcory) |
|
|
|
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, |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
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 ? |
|
""" |
|
|