-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
117 lines (98 loc) · 5.42 KB
/
streamlit_app.py
File metadata and controls
117 lines (98 loc) · 5.42 KB
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
# streamlit_app.py
import hmac
import streamlit as st
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the password is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
if not check_password():
st.stop() # Do not continue if check_password is not True.
# Main Streamlit app starts here
import streamlit as st
from llama_index.llms import OpenAI,ChatMessage,MessageRole
import openai
from llama_index.prompts import ChatPromptTemplate
from llama_index.chat_engine import SimpleChatEngine
openai.api_key = st.secrets.openai_key
st.title("Let's Achieve your goals")
if "messages" not in st.session_state.keys(): # Initialize the chat messages history
st.session_state.messages = [{'role': 'assistant', 'content': 'How may i help you!'}]
st.session_state.custom_chat_history = [
]
for msg in st.session_state.messages:
if msg["role"] == "user":
st.session_state.custom_chat_history.append(
ChatMessage(
role=MessageRole.USER,
content=msg["content"],
)
)
else:
st.session_state.custom_chat_history.append(
ChatMessage(
role=MessageRole.ASSISTANT,
content=msg["content"],
)
)
if "chat_engine" not in st.session_state.keys():
st.session_state.chat_engine = SimpleChatEngine.from_defaults(
llm=OpenAI(temperature=0, model_name="gpt-4"),
system_prompt=(
"You are a mentor for humans who are in a confusion for finding and achieving goals in USA.Your goal is to ask user for their goal and help them in reaching their goal.you dont know anything about user's details. \n"
"Greet the user when required and be kind to the user\n"
"You should ask user some questions which are related to the profession the user wants to achieve. \n"
"The questions should not be asked in a one go, ask questions in step by step manner.Asking questions should be like a conversational manner. \n"
"You should ask the questions related to the requirements of the profession like age,marital status,gender,citizenship,required money and everything related to that profession,in step by step manner.After getting answer for one question,you should ask another question\n"
"You should ask every question which are related to the profession,minimum 10 questions or more.after asking all the questions you should think of a plan from the users answers. \n"
"you should analyze the user answers and give them a plan from next steps from their current position to achieve their goals in step by step manner. \n"
"'Your suggested plan should to include everything that user should do to achieve their goals.'\n"
"after giving the plan,you should ask user whether he/she have any doubts about the plan.\n"
"Some rules to follow:\n"
"1.don't use any hallucination\n"
"2.Be kind to the user and greet them in a nice way. \n"
"3.Don't give responses like a bot"
"4.Dont ever forget to give the user a plan to achieve their goals in point wise only."
"5.You should ask questions in a nice way,it should be not be like an interrogative question."
),
verbose=True
)
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
st.session_state.custom_chat_history.append(
ChatMessage(
role=MessageRole.USER,
content=prompt,
))
for message in st.session_state.messages: # Display the prior chat messages
with st.chat_message(message["role"]):
st.write(message["content"])
# If last message is not from assistant, generate a new response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
# print("s mess",st.session_state.messages)
# st.session_state.custom_chat_history
response = st.session_state.chat_engine.chat(prompt,st.session_state.custom_chat_history)
st.write(response.response)
message = {"role": "assistant", "content": response.response}
st.session_state.messages.append(message) # Add response to message history
st.session_state.custom_chat_history.append(
ChatMessage(
role=MessageRole.ASSISTANT,
content=response.response,
))