mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-04 21:06:03 +00:00
* style: remove necessary imports * style: fix F841 * style: fix F401 * style: fix F811 * style: fix E402 * style: fix E721 * style: fix E722 * style: fix E722 * style: fix F541 * style: ruff format * style: all passed * style: add ruff in deps * style: more ignores in ruff.toml * style: add pre-commit
31 lines
799 B
Python
31 lines
799 B
Python
from __future__ import annotations
|
|
|
|
from .. import truncator
|
|
from ....core import entities as core_entities
|
|
|
|
|
|
@truncator.truncator_class('round')
|
|
class RoundTruncator(truncator.Truncator):
|
|
"""前文回合数阶段器"""
|
|
|
|
async def truncate(self, query: core_entities.Query) -> core_entities.Query:
|
|
"""截断"""
|
|
max_round = query.pipeline_config['ai']['local-agent']['max-round']
|
|
|
|
temp_messages = []
|
|
|
|
current_round = 0
|
|
|
|
# 从后往前遍历
|
|
for msg in query.messages[::-1]:
|
|
if current_round < max_round:
|
|
temp_messages.append(msg)
|
|
if msg.role == 'user':
|
|
current_round += 1
|
|
else:
|
|
break
|
|
|
|
query.messages = temp_messages[::-1]
|
|
|
|
return query
|