xintongsong commented on code in PR #332:
URL: https://github.com/apache/flink-agents/pull/332#discussion_r2621906683


##########
python/flink_agents/runtime/memory/compaction_functions.py:
##########
@@ -0,0 +1,173 @@
+################################################################################
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+# limitations under the License.
+#################################################################################
+import json
+from typing import TYPE_CHECKING, List, Type, cast
+
+from flink_agents.api.chat_message import ChatMessage, MessageRole
+from flink_agents.api.memory.long_term_memory import (
+    BaseLongTermMemory,
+    MemorySet,
+    MemorySetItem,
+    SummarizationStrategy,
+)
+from flink_agents.api.prompts.prompt import Prompt
+from flink_agents.api.resource import ResourceType
+from flink_agents.api.runner_context import RunnerContext
+
+if TYPE_CHECKING:
+    from flink_agents.api.chat_models.chat_model import BaseChatModelSetup
+
+
+DEFAULT_ANALYSIS_PROMPT = Prompt.from_text("""<role>
+Context Summarize Assistant
+</role>
+
+<primary_objective>
+Your sole objective in this task is to summarize the context above.
+</primary_objective>
+
+<objective_information>
+You're nearing the total number of input tokens you can accept, so you need 
compact the context. To achieve this objective, you must extract important 
topics first. The extracted topics
+must no more than {limit}. Afterwards, you should generate summarization for 
each topic, and and record which messages the summary was derived from.
+</objective_information>
+
+<output_example>
+You must always respond with valid json format in this format:
+{"topic1": {"summarization": "User ask what is 1 * 2, and the result is 3.", 
"messages": [0,1,2]},
+ ...
+ "topic4": {"summarization": "User ask what's the weather tomorrow, llm use 
the search_weather, and the answer is snow.", "messages": [9,10,11,12]}
+}
+</output_example>
+""")
+
+
+def summarize(
+    ltm: BaseLongTermMemory,
+    memory_set: MemorySet,
+    ctx: RunnerContext,
+    ids: List[str] | None = None,
+) -> None:
+    """Generate summarization of the items in the memory set.
+
+    Will add the summarization to memory set, and delete original items 
involved
+    in summarization.
+
+    Args:
+        ltm: The long term memory the memory set belongs to.
+        memory_set: The memory set to be summarized.
+        ctx: The runner context used to retrieve needed resources.
+        ids: The ids of items to be summarized. If not provided, all items 
will be
+        involved in summarization. Optional
+    """
+    strategy: SummarizationStrategy = cast(
+        "SummarizationStrategy", memory_set.compaction_strategy
+    )
+
+    # retrieve all items
+    items: List[MemorySetItem] = ltm.get(memory_set=memory_set, ids=ids)
+
+    response: ChatMessage = _generate_summarization(
+        items, memory_set.item_type, strategy, ctx
+    )

Review Comment:
   The amount of memory to be compacted can be large and may exceed the context 
window of the model. We should take that into consideration. Let's add a todo 
here and create a follow-up issue.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to