Files
LangBot/pkg/pipeline/msgtrun/truncators/round.py

31 lines
896 B
Python
Raw Normal View History

2024-05-29 20:34:49 +08:00
from __future__ import annotations
from .. import truncator
import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query
2024-05-29 20:34:49 +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
async def truncate(self, query: pipeline_query.Query) -> pipeline_query.Query:
"""截断"""
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
2024-05-29 20:34:49 +08:00
query.messages = temp_messages[::-1]
return query