Skip to content

Commit 363e6da

Browse files
authored
Fix/empty Space indexes crashes shared chat (#71)
* fix: ask error handler when one space has a corrupt index don't crash return "I don't know" * handle empty or corrupt indexes. Prevent Shared Ask from crashing if the index for one of the spaces has an issue. * fix: typo, remove trace log and unused imports * chore: fix typo in comments
1 parent 4e484ee commit 363e6da

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

source/docq/support/llm.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from llama_index.node_parser.extractors import (
2020
KeywordExtractor,
2121
MetadataExtractor,
22-
# MetadataFeatureExtractor,
2322
QuestionsAnsweredExtractor,
2423
SummaryExtractor,
2524
TitleExtractor,
@@ -105,7 +104,7 @@ def _get_node_parser() -> SimpleNodeParser:
105104

106105
node_parser = (
107106
SimpleNodeParser.from_defaults( # SimpleNodeParser is the default when calling ServiceContext.from_defaults()
108-
metadata_extractor=metadata_extractor, # adds extracted metatdata as extra_info
107+
metadata_extractor=metadata_extractor, # adds extracted metadata as extra_info
109108
)
110109
)
111110

@@ -138,29 +137,41 @@ def run_ask(input_: str, history: str, space: SpaceKey = None, spaces: list[Spac
138137
# With additional spaces likely to be combining a number of shared spaces.
139138
indices = []
140139
summaries = []
140+
output = _default_response()
141141
all_spaces = spaces + ([space] if space else [])
142142
for s_ in all_spaces:
143143
try:
144144
index_ = _load_index_from_storage(s_)
145145
summary_ = index_.as_query_engine().query(
146146
"What is the summary of all the documents?"
147147
) # note: we might not need to do this any longer because summary is added as node metadata.
148-
indices.append(index_)
149-
150-
summaries.append(summary_.response)
148+
if summary_ and summary_.response is not None:
149+
indices.append(index_)
150+
summaries.append(summary_.response)
151+
else:
152+
log.warning("The summary generated for Space '%s' was empty so skipping from the Graph index.", s_)
153+
continue
151154
except Exception as e:
152155
log.warning(
153-
"Index for space '%s' failed to load. Maybe the index isn't created yet. Error message: %s", s_, e
156+
"Index for space '%s' failed to load, skipping. Maybe the index isn't created yet. Error message: %s",
157+
s_,
158+
e,
154159
)
155160
continue
156161

157162
log.debug("number summaries: %s", len(summaries))
158-
graph = ComposableGraph.from_indices(
159-
GPTListIndex, indices, index_summaries=summaries, service_context=_get_service_context()
160-
)
161-
output = graph.as_query_engine().query(PROMPT_QUESTION.format(history=history, input=input_))
162-
163-
log.debug("(Ask combined spaces %s) Q: %s, A: %s", all_spaces, input_, output)
163+
try:
164+
graph = ComposableGraph.from_indices(
165+
GPTListIndex, indices, index_summaries=summaries, service_context=_get_service_context()
166+
)
167+
output = graph.as_query_engine().query(PROMPT_QUESTION.format(history=history, input=input_))
168+
169+
log.debug("(Ask combined spaces %s) Q: %s, A: %s", all_spaces, input_, output)
170+
except Exception as e:
171+
log.error(
172+
"Failed to create ComposableGraph. Maybe there was an issue with one of the Space indexes. Error message: %s",
173+
e,
174+
)
164175
else:
165176
# No additional spaces i.e. likely to be against a user's documents in their personal space.
166177
if space is None:

0 commit comments

Comments
 (0)