Skip to content

Commit 72f6793

Browse files
committed
Reduce log printing for expected exceptions
1 parent d20ac1f commit 72f6793

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

graphrag/query/llm/text_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def chunk_text(
5050
yield from (token_encoder.decode(list(chunk)) for chunk in chunk_iterator)
5151

5252

53-
def try_parse_json_object(input: str) -> tuple[str, dict]:
53+
def try_parse_json_object(input: str, verbose: bool = True) -> tuple[str, dict]:
5454
"""JSON cleaning and formatting utilities."""
5555
# Sometimes, the LLM returns a json string with some extra description, this function will clean it up.
5656

@@ -59,7 +59,8 @@ def try_parse_json_object(input: str) -> tuple[str, dict]:
5959
# Try parse first
6060
result = json.loads(input)
6161
except json.JSONDecodeError:
62-
log.info("Warning: Error decoding faulty json, attempting repair")
62+
if verbose:
63+
log.info("Warning: Error decoding faulty json, attempting repair")
6364

6465
if result:
6566
return input, result
@@ -97,11 +98,13 @@ def try_parse_json_object(input: str) -> tuple[str, dict]:
9798
try:
9899
result = json.loads(input)
99100
except json.JSONDecodeError:
100-
log.exception("error loading json, json=%s", input)
101+
if verbose:
102+
log.exception("error loading json, json=%s", input)
101103
return input, {}
102104
else:
103105
if not isinstance(result, dict):
104-
log.exception("not expected dict type. type=%s:", type(result))
106+
if verbose:
107+
log.exception("not expected dict type. type=%s:", type(result))
105108
return input, {}
106109
return input, result
107110
else:

graphrag/query/structured_search/drift_search/action.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import logging
88
from typing import Any
99

10+
from graphrag.query.llm.text_utils import try_parse_json_object
11+
1012
log = logging.getLogger(__name__)
1113

1214

@@ -72,17 +74,12 @@ async def asearch(self, search_engine: Any, global_query: str, scorer: Any = Non
7274
drift_query=global_query, query=self.query
7375
)
7476

75-
try:
76-
response = json.loads(search_result.response)
77-
except json.JSONDecodeError:
78-
error_message = "Failed to parse search response"
79-
log.exception("%s: %s", error_message, search_result.response)
80-
# Do not launch exception as it will roll up with other steps
81-
# Instead return an empty response and let score -inf handle it
82-
response = {}
77+
# Do not launch exception as it will roll up with other steps
78+
# Instead return an empty response and let score -inf handle it
79+
_, response = try_parse_json_object(search_result.response, verbose=False)
8380

8481
self.answer = response.pop("response", None)
85-
self.score = response.pop("score", float("-inf"))
82+
self.score = float(response.pop("score", "-inf"))
8683
self.metadata.update({"context_data": search_result.context_data})
8784

8885
if self.answer is None:

0 commit comments

Comments
 (0)