fix: when two user messages were sent

This commit is contained in:
Javier Martinez 2024-07-29 08:46:35 +02:00
parent 05a986231c
commit 0eb24a54ec
No known key found for this signature in database

View file

@ -1,6 +1,4 @@
"""This file should be imported if and only if you want to run the UI locally.""" """This file should be imported if and only if you want to run the UI locally."""
import itertools
import logging import logging
import time import time
from collections.abc import Iterable from collections.abc import Iterable
@ -113,21 +111,20 @@ class PrivateGptUi:
yield full_response yield full_response
def build_history() -> list[ChatMessage]: def build_history() -> list[ChatMessage]:
history_messages: list[ChatMessage] = list( history_messages: list[ChatMessage] = []
itertools.chain(
*[ for interaction in history:
[ history_messages.append(
ChatMessage(content=interaction[0], role=MessageRole.USER), ChatMessage(content=interaction[0], role=MessageRole.USER)
ChatMessage(
# Remove from history content the Sources information
content=interaction[1].split(SOURCES_SEPARATOR)[0],
role=MessageRole.ASSISTANT,
),
]
for interaction in history
]
) )
) if len(interaction) > 1 and interaction[1] is not None:
history_messages.append(
ChatMessage(
# Remove from history content the Sources information
content=interaction[1].split(SOURCES_SEPARATOR)[0],
role=MessageRole.ASSISTANT,
)
)
# max 20 messages to try to avoid context overflow # max 20 messages to try to avoid context overflow
return history_messages[:20] return history_messages[:20]