Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from langchain.schema import (
|
6 |
+
AIMessage,
|
7 |
+
HumanMessage,
|
8 |
+
SystemMessage
|
9 |
+
)
|
10 |
+
|
11 |
+
st.set_page_config(page_title="LangChain ChatBot", page_icon=":robot:")
|
12 |
+
st.header("Hey, let's chat")
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
if "sessionMessages" not in st.session_state:
|
17 |
+
st.session_state.sessionMessages = [
|
18 |
+
SystemMessage(content="You are a helpful assistant that likes to chat and answer questions.")
|
19 |
+
]
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
def load_answer(question):
|
24 |
+
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
25 |
+
assistant_answer = chat(st.session_state.sessionMessages )
|
26 |
+
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
|
27 |
+
|
28 |
+
return assistant_answer.content
|
29 |
+
|
30 |
+
|
31 |
+
def get_text():
|
32 |
+
input_text = st.text_input("You: ", key= input)
|
33 |
+
return input_text
|
34 |
+
|
35 |
+
|
36 |
+
chat = ChatOpenAI(temperature=0.3)
|
37 |
+
user_input=get_text()
|
38 |
+
submit = st.button('Ask')
|
39 |
+
|
40 |
+
if submit:
|
41 |
+
|
42 |
+
response = load_answer(user_input)
|
43 |
+
st.subheader("Bot:")
|
44 |
+
st.write(response,key= 1)
|
45 |
+
|