-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_app.py
76 lines (59 loc) · 3.53 KB
/
streamlit_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
import os
import streamlit as st
from streamlit_chat import message
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.prompts import (
SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder
)
st.title('Spiritual Chat bot')
openai_api_key = os.getenv('OPENAI_API_KEY')
if 'responses' not in st.session_state:
st.session_state['responses'] = ["Welcome my child. I am an avatar of God. Ask me your concerns."]
if 'requests' not in st.session_state:
st.session_state['requests'] = []
if 'buffer_memory' not in st.session_state:
st.session_state.buffer_memory = ConversationBufferMemory(return_messages=True)
system_msg_template = SystemMessagePromptTemplate.from_template(template="""The following is a conversation between a helpful, kind and polite AI chatbot who is an avatar of Lord Vishnu and have profound knowledge and insight on the hindu vedic text Bhagawad Gita and a human user.
The role of the AI Chatbot is to converse with the user and answer his or her questions. The AI chatbot should first understand the user's question then give it's response.
The AI chatbot's response is formatted in the following way-
```
Step 1: Mention a relavant sanskrit quote from Bhagawad Gita related to the answer.
Step 2: Write the english translation of the above quote.
Step 3: Offer a helpful answer and spiritual guidance based on Bhagawad Gita in response to the user's questions within 60 words.
Step 4: Explain the meaning of the quote you said in Step 1.
Step 5: End your response on a positive note giving hope to your user about his or her future.
```
First, the AI chatbot should introduce itself saying it is an avatar of Lord Vishnu in the form of an AI chatbot. Then ask the user what it can do for him or her.
""")
human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}")
prompt_template = ChatPromptTemplate.from_messages([system_msg_template,
MessagesPlaceholder(variable_name="history"),
human_msg_template])
llm = ChatOpenAI(openai_api_key=openai_api_key, temperature=0)
# prompt = PromptTemplate(input_variables=["input"], template=st.session_state["template"])
chain = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True)
if "my_text" not in st.session_state:
st.session_state.my_text = ""
def clear_text():
st.session_state.my_text = st.session_state.input
st.session_state.input = ""
# container for chat history
response_container = st.container()
# container for text box
text_container = st.container()
with text_container:
st.text_input("Query: ", key="input", on_change=clear_text)
query = st.session_state.get('my_text', None)
if query:
with st.spinner("typing..."):
response = chain.predict(input=query)
st.session_state.requests.append(query)
st.session_state.responses.append(response)
with response_container:
if st.session_state['responses']:
for i in range(len(st.session_state['responses'])):
message(st.session_state['responses'][i], key=str(i))
if i < len(st.session_state['requests']):
message(st.session_state["requests"][i], is_user=True, key=str(i) + '_user')