Skip to content

Commit a8579b8

Browse files
committed
truncate search query if needed
... suggestions from @awilfox
1 parent 140ccc0 commit a8579b8

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

willa/chatbot/graph_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class WillaChatbotState(TypedDict):
2323
tind_metadata: NotRequired[str]
2424
documents: NotRequired[list[Any]]
2525

26+
2627
class GraphManager: # pylint: disable=too-few-public-methods
2728
"""Manages the shared LangGraph workflow for all chatbot instances."""
2829

@@ -79,6 +80,11 @@ def _prepare_search_query(self, state: WillaChatbotState) -> dict[str, str]:
7980

8081
# summarization may include a system message as well as any human or ai messages
8182
search_query = '\n'.join(str(msg.content) for msg in messages if hasattr(msg, 'content'))
83+
84+
# if summarization fails or some other issue, truncate to the last 2048 characters
85+
if len(search_query) > 2048:
86+
search_query = search_query[-2048:]
87+
8288
return {"search_query": search_query}
8389

8490
def _retrieve_context(self, state: WillaChatbotState) -> dict[str, str | list[Any]]:
@@ -94,7 +100,7 @@ def _retrieve_context(self, state: WillaChatbotState) -> dict[str, str | list[An
94100
matching_docs = retriever.invoke(search_query)
95101
formatted_documents = [
96102
{
97-
"id": f"{doc.metadata.get('tind_metadata', {}).get('tind_id', [''])[0]}_{i}",
103+
"id": f"{i}_{doc.metadata.get('tind_metadata', {}).get('tind_id', [''])[0]}",
98104
"page_content": doc.page_content,
99105
"title": doc.metadata.get('tind_metadata', {}).get('title', [''])[0],
100106
"project": doc.metadata.get('tind_metadata', {}).get('isPartOf', [''])[0],

0 commit comments

Comments
 (0)