Commit ·
3340974
1
Parent(s): 3d93cfe
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Conversational Q&A Chatbot
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
from langchain.schema import HumanMessage,SystemMessage,AIMessage
|
| 5 |
+
from langchain.chat_models import ChatOpenAI
|
| 6 |
+
|
| 7 |
+
## Streamlit UI
|
| 8 |
+
st.set_page_config(page_title="Conversational Q&A Chatbot")
|
| 9 |
+
st.header("Hey, Let's Chat")
|
| 10 |
+
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
chat=ChatOpenAI(temperature=0.6)
|
| 16 |
+
|
| 17 |
+
'''
|
| 18 |
+
This will create a session state if not present under the key name of flowmessgaes
|
| 19 |
+
By default we are setting it to SystemMessage with the condition
|
| 20 |
+
'''
|
| 21 |
+
if 'flowmessages' not in st.session_state:
|
| 22 |
+
st.session_state['flowmessages']=[
|
| 23 |
+
SystemMessage(content="Yor are a comedian AI assitant")
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
## Function to load OpenAI model and get respones
|
| 27 |
+
'''
|
| 28 |
+
Once the function executes it will append the questions in 'flowmessages' key and send it to chatllm model
|
| 29 |
+
for doing predicts. Once the answer is generated the output (AIMessage) gets appended into flowmessages and return the content
|
| 30 |
+
'''
|
| 31 |
+
def get_chatmodel_response(question):
|
| 32 |
+
st.session_state['flowmessages'].append(HumanMessage(content=question))
|
| 33 |
+
answer=chat(st.session_state['flowmessages'])
|
| 34 |
+
st.session_state['flowmessages'].append(AIMessage(content=answer.content))
|
| 35 |
+
return answer.content
|
| 36 |
+
|
| 37 |
+
input=st.text_input("Input: ",key="input")
|
| 38 |
+
response=get_chatmodel_response(input)
|
| 39 |
+
submit=st.button("Ask the question")
|
| 40 |
+
|
| 41 |
+
## If ask button is clicked
|
| 42 |
+
if submit:
|
| 43 |
+
st.subheader("The Response is")
|
| 44 |
+
st.write(response)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|