Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/app/endpoints/streaming_query.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Handler for REST API call to provide answer to streaming query."""

import ast
import json
import logging
import re
from json import JSONDecodeError
import logging
from typing import Any, AsyncIterator, Iterator

from cachetools import TTLCache # type: ignore
Expand Down Expand Up @@ -362,12 +362,12 @@ def _handle_tool_execution_event(
summary = summary[:newline_pos]
for match in METADATA_PATTERN.findall(text_content_item.text):
try:
meta = json.loads(match.replace("'", '"'))
meta = ast.literal_eval(match)
if "document_id" in meta:
metadata_map[meta["document_id"]] = meta
except JSONDecodeError:
except Exception: # pylint: disable=broad-except
logger.debug(
"JSONDecodeError was thrown in processing %s",
"An exception was thrown in processing %s",
match,
)

Expand Down
9 changes: 6 additions & 3 deletions tests/unit/app/endpoints/test_streaming_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@
""",
"""Result 1
Content: ABC
Metadata: {'docs_url': 'https://example.com/doc1', 'title': 'Doc1', 'document_id': 'doc-1'}
Metadata: {'docs_url': 'https://example.com/doc1', 'title': 'Doc1', 'document_id': 'doc-1', \
'source': None}
""",
"""Result 2
Content: ABC
Metadata: {'docs_url': 'https://example.com/doc2', 'title': 'Doc2', 'document_id': 'doc-2'}
Metadata: {'docs_url': 'https://example.com/doc2', 'title': 'Doc2', 'document_id': 'doc-2', \
'source': None}
""",
"""END of knowledge_search tool results.
""",
# Following metadata contains an intentionally incorrect keyword "Title" (instead of "title")
# and it is not picked as a referenced document.
"""Result 3
Content: ABC
Metadata: {'docs_url': 'https://example.com/doc3', 'Title': 'Doc3', 'document_id': 'doc-3'}
Metadata: {'docs_url': 'https://example.com/doc3', 'Title': 'Doc3', 'document_id': 'doc-3', \
'source': None}
""",
"""The above results were retrieved to help answer the user\'s query: "Sample Query".
Use them as supporting information only in answering this query.
Expand Down