Upload 2 files
Browse files- app.py +41 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.tools import DuckDuckGoSearchRun
|
3 |
+
|
4 |
+
# import random
|
5 |
+
import time
|
6 |
+
|
7 |
+
st.title("Simple chat")
|
8 |
+
search = DuckDuckGoSearchRun()
|
9 |
+
|
10 |
+
# Initialize chat history
|
11 |
+
if "messages" not in st.session_state:
|
12 |
+
st.session_state.messages = []
|
13 |
+
|
14 |
+
# Display chat messages from history on app rerun
|
15 |
+
for message in st.session_state.messages:
|
16 |
+
with st.chat_message(message["role"]):
|
17 |
+
st.markdown(message["content"])
|
18 |
+
|
19 |
+
# Accept user input
|
20 |
+
if prompt := st.chat_input("What is up?"):
|
21 |
+
# Add user message to chat history
|
22 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
23 |
+
# Display user message in chat message container
|
24 |
+
with st.chat_message("user"):
|
25 |
+
st.markdown(prompt)
|
26 |
+
|
27 |
+
# Display assistant response in chat message container
|
28 |
+
with st.chat_message("assistant"):
|
29 |
+
message_placeholder = st.empty()
|
30 |
+
full_response = ""
|
31 |
+
# search = DuckDuckGoSearchRun()
|
32 |
+
assistant_response = search.run(prompt)
|
33 |
+
# Simulate stream of response with milliseconds delay
|
34 |
+
for chunk in assistant_response.split():
|
35 |
+
full_response += chunk + " "
|
36 |
+
time.sleep(0.05)
|
37 |
+
# Add a blinking cursor to simulate typing
|
38 |
+
message_placeholder.markdown(full_response + "▌")
|
39 |
+
message_placeholder.markdown(full_response)
|
40 |
+
# Add assistant response to chat history
|
41 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
langchain
|
3 |
+
duckduckgo-search
|