-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
138 lines (102 loc) · 5.74 KB
/
app.py
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
import streamlit as st
import openai
import os
import json
from gptcache import cache
from gptcache.adapter import openai
from gptcache.embedding import Onnx
from gptcache.manager import CacheBase, VectorBase, get_data_manager
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation
import time
os.environ["OPENAI_API_KEY"]= "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"
openai.api_key = os.environ["OPENAI_API_KEY"]
# openai.api_key = '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'
onnx = Onnx()
data_manager = get_data_manager(CacheBase("sqlite"), VectorBase("faiss", dimension=onnx.dimension))
cache.init(
embedding_func=onnx.to_embeddings,
data_manager=data_manager,
similarity_evaluation=SearchDistanceEvaluation(),
)
cache.set_openai_key()
def new_session():
st.session_state['text']=''
del st.session_state.messages
file = 'database.json'
with open (file,'w') as f:
f.seek(0)
f.truncate()
if 'chat_history' not in st.session_state: # Creating a list -chat history in session state if not existed
st.session_state.chat_history=[]
if "messages" not in st.session_state: # Creating messages list in session state if not existed
st.session_state.messages = []
with st.sidebar:
st.header("Financial Advisor App ")
st.image("https://cdn.dribbble.com/users/1299339/screenshots/2982257/media/a0a4dcb030548cd22cc59c8214bfa1a4.gif")
st.header("About")
st.markdown("This interactive chatbot will engage users in conversations about their financial goals and provide personalized guidance on managing their finances, paying off loans, investing, planning for retirement, and addressing other financial concerns.")
st.write("For Reference")
st.write('''-[Streamlit](https://streamlit.io/)
- [OpenAI](https://platform.openai.com/docs/models)''')
def main():
st.header("Welcome to Your Personal Financial Advisor💸")
options=st.chat_input("Enter your response ")
start_time = time.time()
if options == None:
st.markdown("Hi, How can I Help You")
else:
with st.chat_message("assistant"):
chat_response=get_chat_response(options)
for message in st.session_state["messages"]:
if message["role"] == "user":
# Display a user message in the chat interface
st.write('👤:',message["content"])
elif message["role"] == "assistant":
# Display an assistant message in the chat interface
st.write("Time consuming: {:.2f}s".format(time.time() - start_time))
st.write('🤖:',message["content"])
st.button("End Chat",on_click=new_session,help="To start over")
def get_chat_response(user_message):
messages = load_messages()
messages.append({"role": "user", "content": user_message})
# Send to ChatGpt/OpenAi
gpt_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
parsed_gpt_response = gpt_response['choices'][0]['message']['content']
# Save messages
save_messages(user_message, parsed_gpt_response)
return parsed_gpt_response
def load_messages():
messages = []
options=''
name=''
file = 'database.json'
empty = os.stat(file).st_size == 0
if not empty:
with open(file) as db_file:
data = json.load(db_file)
for item in data:
messages.append(item)
else:
messages.append(
{"role": "system", "content": f"""You are a financial advisor for a Bank.When a user asks for any financial advise, guide them in best way possible.
Remember to greet the user with 'hi welcome to the your Financial Advisor App, how can I help you?' if the user asks 'hi' or 'hello.
Ask direct questions early on in the conversation to determine if the user is seeking financing personally or on behalf of a client. Follow-up questions should be framed accordingly.
Your goal is to sell some sort of financial product of the bank (loan, insurance, health plan, etc) to the user.
Adapt the 'Always Be Closing' principle carefully for chatbot use. Another theory is when selling anything, everything should be framed within the idea of YES. Always compliment. Always confirm. Always be optimistic. Always push the borrower to the closing point goals (in this case - to speak with another human, to get the deals started).
"""}
)
return messages
def save_messages(user_message, gpt_response):
file = 'database.json'
messages = load_messages()
messages.append({"role": "user", "content": user_message})
st.session_state.messages.append({"role": "user", "content": user_message})
messages.append({"role": "assistant", "content": gpt_response})
st.session_state.messages.append({"role": "assistant", "content": gpt_response})
with open(file, 'w') as f:
json.dump(messages, f)
if __name__ == '__main__':
main()