-
Notifications
You must be signed in to change notification settings - Fork 14
/
second_brain_agent.py
72 lines (54 loc) · 1.71 KB
/
second_brain_agent.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
"""Second brain web interface
Take a question from the user and call the LLM for an answer using the
closest documents stored in the vector database as context.
"""
import re
import sys
import streamlit as st
from dotenv import load_dotenv
from htmlTemplates import bot_template, css, user_template
from lib import Agent
def clean_email(response):
"Clean the email address from the response"
return re.sub(r"<(.*@.*)>", r"<\1>", response)
def handle_userinput(user_question):
"Handle the input from the user as a question to the LLM"
response = clean_email(st.session_state.agent.html_question(user_question))
print(response, file=sys.stderr)
st.write(
user_template.replace("{{MSG}}", user_question),
bot_template.replace("{{MSG}}", response),
unsafe_allow_html=True,
)
def clear_input_box():
"Empty the input box"
handle_userinput(st.session_state["question"])
st.session_state["question"] = ""
def main():
"Entry point"
load_dotenv()
st.set_page_config(
page_title="Ask questions to your Second Brain", page_icon=":brain:"
)
st.write(css, unsafe_allow_html=True)
st.header("Ask a question to your Second Brain :brain:")
if "agent" not in st.session_state:
st.session_state.agent = Agent()
st.text_input(
"Ask a question to your second brain:",
key="question",
on_change=clear_input_box,
)
st.components.v1.html(
"""
<script>
var input = window.parent.document.querySelectorAll("input[type=text]");
for (var i = 0; i < input.length; ++i) {{
input[i].focus();
}}
</script>
""",
height=150,
)
if __name__ == "__main__":
main()