-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (50 loc) · 1.97 KB
/
app.py
File metadata and controls
71 lines (50 loc) · 1.97 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
import os
import streamlit as st
from bot import Bot
if "do_search" not in st.session_state:
st.session_state["do_search"] = False
@st.cache_resource
def load_bot(
openai_key: str | None, dataset_path: str | None = None, temperature: float = 0.2
):
return Bot(
st,
openai_key=openai_key,
temperature=temperature,
debug=os.environ.get("DEBUG", False),
)
### Sidebar ###
openai_key = st.sidebar.text_input("OpenAI API Key", type="password")
dataset_path = st.sidebar.text_input("Dataset path")
activeloop_token = st.sidebar.text_input("Activeloop Token", type="password")
st.sidebar.divider()
temperature = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, value=0.2)
if activeloop_token:
os.environ["ACTIVELOOP_TOKEN"] = activeloop_token
### Main Content ###
bot = load_bot(openai_key, dataset_path, temperature)
st.title("All Your Questions Answered")
st.write(
"Hello, I'm your friendly help bot. Ask me a question! If I need more "
"knowledge to give you a good answer, I'll search the internet for "
"information."
)
question = st.text_input("Your question...")
if question:
n_docs = bot.check_vectordb(question)
if n_docs < 4 or st.session_state.do_search:
if st.session_state.do_search:
bot.add_human_message(
"That's not quite what I was looking for. Refine the search phrase to find more specific resources you can use to answer my question."
)
with st.spinner("Searching the internet for more resources..."):
search_phrase = bot.generate_search_phrase(question)
bot.fetch_resources(search_phrase)
st.session_state.do_search = False
with st.spinner("Searching the knowledge base, one second..."):
answer = bot.qa_run(question)
st.divider()
st.markdown(answer)
def force_search():
st.session_state.do_search = True
search_button = st.button("Search for more resources", on_click=force_search)