Skip to content

Commit

Permalink
chore: Some code comments and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
anirbanbasu committed Jun 11, 2024
1 parent 8559770 commit 6e42138
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 165 deletions.
24 changes: 18 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@

from pathlib import Path

# CWD = Current Working Directory
CWD = Path(__file__).parent
# Load the external CSS file to be used as global styles for the page.
extern_style = (CWD / "styles.css").read_text(encoding=constants.CHAR_ENCODING_UTF8)

page_step: solara.Reactive[int] = solara.reactive(1)


@solara.component
def CustomLayout(children: Any = []):
"""Define a custom layout for the app."""
sm.set_theme_colours()
# It is necessary to initialise the default settings before the page is rendered.
sm.initialise_default_settings()

with solara.AppLayout(
children=children,
color=None, # sm.corrective_background_colour.value,
color=None,
navigation=True,
sidebar_open=False,
) as app_layout:
Expand Down Expand Up @@ -70,12 +74,14 @@ def CustomLayout(children: Any = []):

@solara.component
def Page():
# Remove the "This website runs on Solara" message
solara.Style(constants.UI_SOLARA_NOTICE_REMOVE)
"""Define the main page."""
# Apply the external CSS file as global styles for the page.
solara.Style(extern_style)

# Numeric labels for the steps
step_labels = [1, 2, 3, 4]

# Show all settings in the sidebar only if the user has passed the basic settings step.
with solara.Sidebar():
if page_step.value in step_labels[2:]:
settings_uic.AllSettingsCategorical()
Expand All @@ -93,7 +99,7 @@ def Page():
case 1:
solara.Markdown("Information")
case 2:
solara.Markdown("Language model (LLM)")
solara.Markdown("Basic settings")
case 3:
solara.Markdown("Data")
case 4:
Expand All @@ -102,6 +108,7 @@ def Page():
rv.Divider()
with rv.StepperItems():
with rv.StepperContent(step=1):
# Show a welcome message and the EU AI Act transparency notice and get the user to agree to it.
with rv.Card(elevation=0):
solara.Markdown(constants.MESSAGE_TLDRLC_WELCOME)
solara.Markdown(
Expand All @@ -125,10 +132,11 @@ def Page():
on_click=lambda: page_step.set(2),
)
with rv.StepperContent(step=2):
# Show the basic settings to get started.
with rv.Card(elevation=0):
solara.Markdown(
"""
### Language model settings
### Basic settings
_You can configure other settings of the language model along
with indexing and storage from the settings menu, which is available
Expand All @@ -149,6 +157,7 @@ def Page():
on_click=lambda: page_step.set(3),
)
with rv.StepperContent(step=3):
# Show the data ingestion options, ingest data before proceeding to the chat interface.
with rv.Card(elevation=0):
solara.Markdown(
"""
Expand Down Expand Up @@ -196,7 +205,7 @@ def Page():
)
with rv.CardActions():
solara.Button(
"LLM",
"Settings",
icon_name="mdi-cogs",
disabled=(
ingest_uic.ingest_webpage_data.pending
Expand All @@ -223,6 +232,7 @@ def Page():
on_click=lambda: page_step.set(4),
)
with rv.StepperContent(step=4):
# Show the chat interface once the some data has been ingested.
with rv.Card(elevation=0):
with rv.CardActions():
solara.Button(
Expand All @@ -232,9 +242,11 @@ def Page():
icon_name="mdi-page-previous",
on_click=lambda: page_step.set(3),
)
# TODO: The chat interface needs to be dynamically resized if sidebar is open.
chat_uic.ChatInterface()


routes = [
# Define the main route for the app with the custom layout.
solara.Route(path="/", component=Page, label="TLDRLC", layout=CustomLayout),
]
5 changes: 5 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Remove the "This website runs on Solara" message. */
.v-application--wrap > div:nth-child(2) > div:nth-child(2){
display: none !important;
}

.solara-autorouter-content {
display: flex;
flex-direction: column;
Expand Down
19 changes: 9 additions & 10 deletions ui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ def update_llm_system_message(callback_data: Any = None):


@solara.component
def LLMSettingsBasicComponent():
"""Component for the basic language model settings."""
def BasicSettingsComponent():
"""Component for the basic settings."""

if sm.global_settings__llm_provider_notice.value is not constants.EMPTY_STRING:
solara.Info(icon=True, label=sm.global_settings__llm_provider_notice.value)

solara.Select(
label="Language model provider",
Expand Down Expand Up @@ -390,6 +393,8 @@ def test_graphdb_connection(callback_data: Any = None):
""",
elevation=0,
):
if status.value is not None:
solara.display(status.value)
solara.Checkbox(
label="Disable Neo4j and use in-memory storage",
value=sm.global_settings__neo4j_disable,
Expand Down Expand Up @@ -422,9 +427,6 @@ def test_graphdb_connection(callback_data: Any = None):
on_value=test_graphdb_connection,
)

if status.value is not None:
solara.display(status.value)


@solara.component
def DocumentsIndexVectorStorageSettingsComponent():
Expand Down Expand Up @@ -469,6 +471,8 @@ def test_redis_connection(callback_data: Any = None):
""",
elevation=0,
):
if status.value is not None:
solara.display(status.value)
solara.Checkbox(
label="Disable Redis",
value=sm.global_settings__redis_disable,
Expand All @@ -493,11 +497,6 @@ def test_redis_connection(callback_data: Any = None):
on_value=test_redis_connection,
)

if status.value is not None:
solara.display(status.value)

# update_index_documents_storage_context()


@solara.component
def GraphVisualisationSettingsComponent():
Expand Down
5 changes: 4 additions & 1 deletion utils/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@


class TLDRLCLangfuseCallbackHandler(LlamaIndexCallbackHandler):
"""Custom Langfuse callback handler."""
"""Custom Langfuse callback handler, which does nothing more than
calling its superclass methods.
WARNING: This class could be deprecated in the future."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
6 changes: 0 additions & 6 deletions utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,6 @@
UI_STATUS_CONTAINER_HEIGHT = "300"
UI_CHAT_CONTAINER_HEIGHT = "400"

UI_SOLARA_NOTICE_REMOVE = """
.v-application--wrap > div:nth-child(2) > div:nth-child(2){
display: none !important;
}
"""

MESSAGE_TLDRLC_WELCOME = """
### Too Long, Didn't Read, Let's Chat (TLDRLC)
Expand Down
17 changes: 12 additions & 5 deletions utils/retrievers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@


class VectorKnowledgeGraphRetriever(BaseRetriever):
"""Custom retriever that performs both semantic search and knowledge graph search."""
"""Custom retriever that retrieves from a semantic search (vector) index and a knowledge graph index."""

def __init__(
self,
vector_retriever: BaseRetriever,
knowledge_graph_retriever: BaseRetriever,
mode: str = constants.BOOLEAN_OR,
) -> None:
"""Init params."""
"""Initialisation parameters."""

if vector_retriever is None:
raise ValueError("A valid vector index retriever must be specified.")
raise ValueError(
"A valid semantic search (vector) index retriever must be specified."
)
if knowledge_graph_retriever is None:
raise ValueError("A valid knowledge graph retriever must be specified.")
if mode not in (constants.BOOLEAN_AND, constants.BOOLEAN_OR):
raise ValueError(
f"Invalid retriever mode {mode}. It must be either {constants.BOOLEAN_AND} or {constants.BOOLEAN_OR}."
f"""Invalid retriever logical combination mode {mode}.
It must be either {constants.BOOLEAN_AND} (intersection) or {constants.BOOLEAN_OR} (union)."""
)
self._vector_retriever = vector_retriever
self._keyword_retriever = knowledge_graph_retriever
Expand All @@ -55,22 +58,26 @@ def __init__(
def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
"""Retrieve nodes given query."""

# Retrieve nodes from both indices
vector_nodes = self._vector_retriever.retrieve(query_bundle)
knowledge_graph_nodes = self._keyword_retriever.retrieve(query_bundle)

vector_ids = {n.node.node_id for n in vector_nodes}
knowledge_graph_ids = {n.node.node_id for n in knowledge_graph_nodes}

# Create a combined dictionary of nodes with scores
combined_dict = {n.node.node_id: n for n in vector_nodes}
combined_dict.update({n.node.node_id: n for n in knowledge_graph_nodes})

# Perform set operation
if self._mode == constants.BOOLEAN_AND:
retrieve_ids = vector_ids.intersection(knowledge_graph_ids)
elif self._mode == constants.BOOLEAN_OR:
retrieve_ids = vector_ids.union(knowledge_graph_ids)
else:
raise ValueError(
f"Set operation is not defined for invalid retriever mode {self._mode}, which must be either {constants.BOOLEAN_AND} or {constants.BOOLEAN_OR}."
f"""Set operation is not defined for invalid retriever logical combination mode {self._mode},
which must be either {constants.BOOLEAN_AND} (intersection) or {constants.BOOLEAN_OR} (union)."""
)

retrieve_nodes = [combined_dict[rid] for rid in retrieve_ids]
Expand Down
Loading

0 comments on commit 6e42138

Please sign in to comment.