forked from run-llama/rags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_⚙️_RAG_Config.py
132 lines (115 loc) · 4.46 KB
/
2_⚙️_RAG_Config.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
"""Streamlit page showing builder config."""
import streamlit as st
from typing import cast, Optional
from agent_utils import (
RAGParams,
RAGAgentBuilder,
ParamCache,
remove_agent_from_directory,
)
from st_utils import update_selected_agent_with_id
from constants import AGENT_CACHE_DIR
from st_utils import add_sidebar
####################
#### STREAMLIT #####
####################
def update_agent() -> None:
"""Update agent."""
if (
"config_agent_builder" in st.session_state.keys()
and st.session_state.config_agent_builder is not None
):
agent_builder = cast(RAGAgentBuilder, st.session_state.config_agent_builder)
### Update the agent
agent_builder.update_agent(
st.session_state.agent_id_st,
system_prompt=st.session_state.sys_prompt_st,
include_summarization=st.session_state.include_summarization_st,
top_k=st.session_state.top_k_st,
chunk_size=st.session_state.chunk_size_st,
embed_model=st.session_state.embed_model_st,
llm=st.session_state.llm_st,
)
# Update Radio Buttons: update selected agent to the new id
update_selected_agent_with_id(agent_builder.cache.agent_id)
else:
raise ValueError("Agent builder is None. Cannot update agent.")
def delete_agent() -> None:
"""Delete agent."""
if (
"config_agent_builder" in st.session_state.keys()
and st.session_state.config_agent_builder is not None
):
agent_builder = cast(RAGAgentBuilder, st.session_state.config_agent_builder)
### Delete agent
# remove saved agent from directory
remove_agent_from_directory(str(AGENT_CACHE_DIR), agent_builder.cache.agent_id)
# Update Radio Buttons: update selected agent to the new id
update_selected_agent_with_id(None)
else:
raise ValueError("Agent builder is None. Cannot delete agent.")
st.set_page_config(
page_title="RAG Pipeline Config",
page_icon="🦙",
layout="centered",
initial_sidebar_state="auto",
menu_items=None,
)
st.title("RAG Pipeline Config")
add_sidebar()
# first, pick the cache: this is preloaded from an existing agent,
# or is part of the current one being created
if (
"selected_cache" in st.session_state.keys()
and st.session_state.selected_cache is not None
):
cache = cast(ParamCache, st.session_state.selected_cache)
agent_builder: Optional[RAGAgentBuilder] = RAGAgentBuilder(cache)
elif "agent_builder" in st.session_state.keys():
agent_builder = cast(RAGAgentBuilder, st.session_state.agent_builder)
else:
agent_builder = None
# set as session state
st.session_state.config_agent_builder = agent_builder
if agent_builder is not None:
st.info(f"Viewing config for agent: {agent_builder.cache.agent_id}", icon="ℹ️")
agent_id_st = st.text_input(
"Agent ID", value=agent_builder.cache.agent_id, key="agent_id_st"
)
if agent_builder.cache.system_prompt is None:
system_prompt = ""
else:
system_prompt = agent_builder.cache.system_prompt
sys_prompt_st = st.text_area(
"System Prompt", value=system_prompt, key="sys_prompt_st"
)
rag_params = cast(RAGParams, agent_builder.cache.rag_params)
file_names = st.text_input(
"File names (not editable)",
value=",".join(agent_builder.cache.file_names),
disabled=True,
)
urls = st.text_input(
"URLs (not editable)", value=",".join(agent_builder.cache.urls), disabled=True
)
include_summarization_st = st.checkbox(
"Include Summarization (only works for GPT-4)",
value=rag_params.include_summarization,
key="include_summarization_st",
)
top_k_st = st.number_input("Top K", value=rag_params.top_k, key="top_k_st")
chunk_size_st = st.number_input(
"Chunk Size", value=rag_params.chunk_size, key="chunk_size_st"
)
embed_model_st = st.text_input(
"Embed Model", value=rag_params.embed_model, key="embed_model_st"
)
llm_st = st.text_input("LLM", value=rag_params.llm, key="llm_st")
if agent_builder.cache.agent is not None:
st.button("Update Agent", on_click=update_agent)
st.button(":red[Delete Agent]", on_click=delete_agent)
else:
# show text saying "agent not created"
st.info("Agent not created. Please create an agent in the above section.")
else:
st.info("No agent builder found. Please create an agent in the above section.")