forked from run-llama/rags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_🏠_Home.py
134 lines (109 loc) · 4.45 KB
/
1_🏠_Home.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import streamlit as st
from streamlit_pills import pills
from st_utils import add_sidebar, get_current_state
####################
#### STREAMLIT #####
####################
st.set_page_config(
page_title="Build a RAGs bot, powered by LlamaIndex",
page_icon="🦙",
layout="centered",
initial_sidebar_state="auto",
menu_items=None,
)
st.title("Build a RAGs bot, powered by LlamaIndex 💬🦙")
st.info(
"Use this page to build your RAG bot over your data! "
"Once the agent is finished creating, check out the `RAG Config` and "
"`Generated RAG Agent` pages.\n"
"To build a new agent, please make sure that 'Create a new agent' is selected.",
icon="ℹ️",
)
if "metaphor_key" in st.secrets:
st.info("**NOTE**: The ability to add web search is enabled.")
current_state = get_current_state()
add_sidebar()
st.info(f"Currently building/editing agent: {current_state.cache.agent_id}", icon="ℹ️")
# add pills
selected = pills(
"Outline your task!",
[
"I want to analyze this PDF file (data/invoices.pdf)",
"I want to search over my CSV documents.",
],
clearable=True,
index=None,
)
if "messages" not in st.session_state.keys(): # Initialize the chat messages history
st.session_state.messages = [
{"role": "assistant", "content": "What RAG bot do you want to build?"}
]
def add_to_message_history(role: str, content: str) -> None:
message = {"role": role, "content": str(content)}
st.session_state.messages.append(message) # Add response to message history
for message in st.session_state.messages: # Display the prior chat messages
with st.chat_message(message["role"]):
st.write(message["content"])
# def handle_user_input() -> None:
# """Handle user input."""
# prompt = st.session_state.user_question_st
# print(f"USER PROMPT: {prompt}")
# add_to_message_history("user", prompt)
# with st.chat_message("user"):
# st.write(prompt)
# # If last message is not from assistant, generate a new response
# if st.session_state.messages[-1]["role"] != "assistant":
# with st.chat_message("assistant"):
# with st.spinner("Thinking..."):
# response = current_state.builder_agent.chat(prompt)
# st.write(str(response))
# add_to_message_history("assistant", str(response))
# else:
# pass
# # check agent_ids again
# # if it doesn't match, add to directory and refresh
# agent_ids = current_state.agent_registry.get_agent_ids()
# # check diff between agent_ids and cur agent ids
# diff_ids = list(set(agent_ids) - set(st.session_state.cur_agent_ids))
# if len(diff_ids) > 0:
# # # clear streamlit cache, to allow you to generate a new agent
# # st.cache_resource.clear()
# st.rerun()
# handle user input
# st.chat_input(
# "Your question", key="user_question_st", on_submit=handle_user_input
# ) # Prompt for user input and save to chat history
# TODO: this is really hacky, only because st.rerun is jank
if prompt := st.chat_input(
"Your question",
): # Prompt for user input and save to chat history
# TODO: hacky
if "has_rerun" in st.session_state.keys() and st.session_state.has_rerun:
# if this is true, skip the user input
st.session_state.has_rerun = False
else:
add_to_message_history("user", prompt)
with st.chat_message("user"):
st.write(prompt)
# If last message is not from assistant, generate a new response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = current_state.builder_agent.chat(prompt)
st.write(str(response))
add_to_message_history("assistant", str(response))
else:
pass
# check agent_ids again
# if it doesn't match, add to directory and refresh
agent_ids = current_state.agent_registry.get_agent_ids()
# check diff between agent_ids and cur agent ids
diff_ids = list(set(agent_ids) - set(st.session_state.cur_agent_ids))
if len(diff_ids) > 0:
# # clear streamlit cache, to allow you to generate a new agent
# st.cache_resource.clear()
st.session_state.has_rerun = True
st.rerun()
else:
# TODO: set has_rerun to False
st.session_state.has_rerun = False