forked from run-llama/rags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_utils.py
58 lines (49 loc) · 1.7 KB
/
st_utils.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
"""Streamlit utils."""
from agent_utils import (
load_agent_ids_from_directory,
load_cache_from_directory,
)
from constants import (
AGENT_CACHE_DIR,
)
from typing import Optional
import streamlit as st
def update_selected_agent_with_id(selected_id: Optional[str] = None) -> None:
"""Update selected agent with id."""
# set session state
st.session_state.selected_id = (
selected_id if selected_id != "Create a new agent" else None
)
if st.session_state.selected_id is None:
st.session_state.selected_cache = None
else:
# load agent from directory
agent_cache = load_cache_from_directory(
str(AGENT_CACHE_DIR), st.session_state.selected_id
)
st.session_state.selected_cache = agent_cache
## handler for sidebar specifically
def update_selected_agent() -> None:
"""Update selected agent."""
selected_id = st.session_state.agent_selector
update_selected_agent_with_id(selected_id)
def add_sidebar() -> None:
"""Add sidebar."""
with st.sidebar:
st.session_state.cur_agent_ids = load_agent_ids_from_directory(
str(AGENT_CACHE_DIR)
)
choices = ["Create a new agent"] + st.session_state.cur_agent_ids
# by default, set index to 0. if value is in selected_id, set index to that
index = 0
if "selected_id" in st.session_state.keys():
if st.session_state.selected_id is not None:
index = choices.index(st.session_state.selected_id)
# display buttons
st.radio(
"Agents",
choices,
index=index,
on_change=update_selected_agent,
key="agent_selector",
)