mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-28 02:07:05 -04:00
refac
This commit is contained in:
@@ -66,31 +66,81 @@ async def compact_messages_for_request(
|
||||
if not compacted_messages or not recent_messages:
|
||||
return messages, previous_summary, False
|
||||
|
||||
summary = await _generate_summary(
|
||||
request,
|
||||
user,
|
||||
model_id,
|
||||
models,
|
||||
compacted_messages,
|
||||
recent_messages,
|
||||
previous_summary,
|
||||
config['prompt_template'],
|
||||
)
|
||||
event_emitter = None
|
||||
if metadata.get('chat_id') and metadata.get('message_id'):
|
||||
from open_webui.socket.main import get_event_emitter
|
||||
|
||||
event_emitter = await get_event_emitter(metadata)
|
||||
|
||||
if event_emitter:
|
||||
await event_emitter(
|
||||
{
|
||||
'type': 'context_compaction',
|
||||
'data': {
|
||||
'action': 'context_compaction',
|
||||
'description': 'Compacting context',
|
||||
'done': False,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
try:
|
||||
summary = await _generate_summary(
|
||||
request,
|
||||
user,
|
||||
model_id,
|
||||
models,
|
||||
compacted_messages,
|
||||
recent_messages,
|
||||
previous_summary,
|
||||
config['prompt_template'],
|
||||
)
|
||||
except Exception:
|
||||
if event_emitter:
|
||||
await event_emitter(
|
||||
{
|
||||
'type': 'context_compaction',
|
||||
'data': {
|
||||
'action': 'context_compaction',
|
||||
'description': 'Context compaction failed',
|
||||
'done': True,
|
||||
'error': True,
|
||||
},
|
||||
}
|
||||
)
|
||||
raise
|
||||
|
||||
chat_id = metadata.get('chat_id')
|
||||
message_id = metadata.get('message_id')
|
||||
if chat_id and message_id and not chat_id.startswith(('local:', 'channel:')):
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(chat_id, message_id, {'contextSummary': summary})
|
||||
checkpoint_message_id = metadata.get('user_message_id') or metadata.get('message_id')
|
||||
if chat_id and checkpoint_message_id and not chat_id.startswith(('local:', 'channel:')):
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
chat_id,
|
||||
checkpoint_message_id,
|
||||
{'contextSummary': summary},
|
||||
)
|
||||
|
||||
log.info(
|
||||
'Compacted chat context for chat=%s message=%s dropped=%d kept=%d summary_chars=%d',
|
||||
'Compacted chat context for chat=%s checkpoint=%s response=%s dropped=%d kept=%d summary_chars=%d',
|
||||
chat_id,
|
||||
message_id,
|
||||
checkpoint_message_id,
|
||||
metadata.get('message_id'),
|
||||
len(compacted_messages),
|
||||
len(recent_messages),
|
||||
len(summary),
|
||||
)
|
||||
|
||||
if event_emitter:
|
||||
await event_emitter(
|
||||
{
|
||||
'type': 'context_compaction',
|
||||
'data': {
|
||||
'action': 'context_compaction',
|
||||
'description': 'Context compacted',
|
||||
'done': True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
return recent_messages, summary, True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user