1
+ import json
1
2
from typing import Literal , List
2
- from langchain_aws import ChatBedrock
3
3
from langchain_core .messages import HumanMessage , ToolMessage
4
4
from agent .tools import aggregate , discover_fields , search , retrieve_documents
5
5
from langchain_core .messages .base import BaseMessage
9
9
from langgraph .graph import END , START , StateGraph , MessagesState
10
10
from langgraph .prebuilt import ToolNode
11
11
from langgraph .errors import GraphRecursionError
12
+ from core .document import minimize_documents
12
13
from core .setup import checkpoint_saver
13
14
from agent .callbacks .socket import SocketCallbackHandler
14
15
from typing import Optional
18
19
Please provide a brief answer to the question using the tools provided. Include specific details from multiple documents that
19
20
support your answer. Answer in raw markdown, but not within a code block. When citing source documents, construct Markdown
20
21
links using the document's canonical_link field. Do not include intermediate messages explaining your process. If the user's
21
- question is unclear, ask for clarification.
22
+ question is unclear, ask for clarification. Use no more than 6 tool calls. If you still cannot answer the question after 6
23
+ tool calls, summarize the information you have gathered so far and suggest ways in which the user might narrow the scope
24
+ of their question to make it more answerable.
22
25
"""
23
26
24
27
MAX_RECURSION_LIMIT = 16
@@ -27,7 +30,6 @@ class SearchWorkflow:
27
30
def __init__ (self , model : BaseModel , system_message : str , metrics = None ):
28
31
self .metrics = metrics
29
32
self .model = model
30
- self .summarization_model = ChatBedrock (model = "us.anthropic.claude-3-5-sonnet-20241022-v2:0" , streaming = False )
31
33
self .system_message = system_message
32
34
33
35
def should_continue (self , state : MessagesState ) -> Literal ["tools" , END ]:
@@ -41,44 +43,18 @@ def should_continue(self, state: MessagesState) -> Literal["tools", END]:
41
43
42
44
def summarize (self , state : MessagesState ):
43
45
messages = state ["messages" ]
44
- question = messages [0 ].content
45
46
last_message = messages [- 1 ]
46
47
if last_message .name not in ["search" , "retrieve_documents" ]:
47
48
return {"messages" : messages }
48
49
49
- summary_prompt = f"""
50
- Summarize the following content. Return ONLY a valid JSON list where each
51
- document is replaced with a new dict with the `id`, `title`, `canonical_link`,
52
- and `api_link` fields, as well as any other information from the original that
53
- might be useful in answering questions. Flatten any nested structures to retain
54
- only semantically useful information (e.g., [{{'id': id1, 'label': label1}},
55
- {{'id': id2, 'label': label2}}, {{'id': id3, 'label': label3}}] becomes
56
- [label1, label2, label3]). Be judicious about what information is retained,
57
- but keep enough to answer the question "{ question } " and any likely followups.
58
-
59
- It is extremely important that you return only the valid, parsable summarized
60
- JSON with no additional text or explanation, no markdown code fencing, and all
61
- unnecessary whitespace removed.
62
-
63
- Prioritize speed over comprehensiveness.
64
-
65
- { last_message .content }
66
- """
67
-
68
- config = {
69
- "callbacks" : [self .metrics ] if self .metrics else [],
70
- "metadata" : {"source" : "summarize" }
71
- }
72
-
73
50
start_time = time .time ()
74
-
75
- summary = self .summarization_model .invoke ([HumanMessage (content = summary_prompt )], config = config )
76
-
51
+ content = minimize_documents (json .loads (last_message .content ))
52
+ content = json .dumps (content , separators = (',' , ':' ))
77
53
end_time = time .time ()
78
54
elapsed_time = end_time - start_time
79
- print (f'Condensed { len (last_message .content )} bytes to { len (summary . content )} bytes in { elapsed_time :.2f} seconds' )
55
+ print (f'Condensed { len (last_message .content )} bytes to { len (content )} bytes in { elapsed_time :.2f} seconds. Savings: { 100 * ( 1 - len ( content ) / len ( last_message . content )):.2f } % ' )
80
56
81
- last_message .content = summary . content
57
+ last_message .content = content
82
58
83
59
return {"messages" : messages }
84
60
0 commit comments