2024-05-29 20:34:49 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from .. import truncator
|
2025-06-15 22:04:31 +08:00
|
|
|
import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query
|
2024-05-29 20:34:49 +08:00
|
|
|
|
|
|
|
|
|
2025-04-29 17:24:07 +08:00
|
|
|
@truncator.truncator_class('round')
|
2024-05-29 20:34:49 +08:00
|
|
|
class RoundTruncator(truncator.Truncator):
|
2025-07-10 11:09:33 +08:00
|
|
|
"""Truncate the conversation message chain to adapt to the LLM message length limit."""
|
2024-05-29 20:34:49 +08:00
|
|
|
|
2025-06-15 22:04:31 +08:00
|
|
|
async def truncate(self, query: pipeline_query.Query) -> pipeline_query.Query:
|
2025-04-29 17:24:07 +08:00
|
|
|
"""截断"""
|
2025-04-03 17:57:51 +08:00
|
|
|
max_round = query.pipeline_config['ai']['local-agent']['max-round']
|
2024-05-29 20:34:49 +08:00
|
|
|
|
|
|
|
|
temp_messages = []
|
|
|
|
|
|
|
|
|
|
current_round = 0
|
|
|
|
|
|
2025-07-10 11:09:33 +08:00
|
|
|
# Traverse from back to front
|
2024-05-29 20:34:49 +08:00
|
|
|
for msg in query.messages[::-1]:
|
|
|
|
|
if current_round < max_round:
|
|
|
|
|
temp_messages.append(msg)
|
|
|
|
|
if msg.role == 'user':
|
|
|
|
|
current_round += 1
|
|
|
|
|
else:
|
|
|
|
|
break
|
2025-04-29 17:24:07 +08:00
|
|
|
|
2024-05-29 20:34:49 +08:00
|
|
|
query.messages = temp_messages[::-1]
|
|
|
|
|
|
|
|
|
|
return query
|