Skip to content

Commit

Permalink
Make sure that the first message in the history has a user role when …
Browse files Browse the repository at this point in the history
…trimming the history
  • Loading branch information
efunneko committed Aug 12, 2024
1 parent fb8143a commit 8ba3185
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/solace_ai_connector/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def import_module(name, base_path=None, component_package=None):
"solace_ai_connector.components.general",
"solace_ai_connector.components.general.for_testing",
"solace_ai_connector.components.general.langchain",
"solace_ai_connector.components.general.openai",
"solace_ai_connector.components.inputs_outputs",
"solace_ai_connector.transforms",
"solace_ai_connector.common",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,17 @@ def prune_history(self, session_id, history):
def clear_history_but_keep_depth(self, session_id: str, depth: int, history):
if session_id in history:
messages = history[session_id]["messages"]
if depth > 0:
kept_messages = []
user_message_count = 0
for message in reversed(messages):
kept_messages.append(message)
if message["role"] == "user":
user_message_count += 1
if user_message_count == depth:
break
history[session_id]["messages"] = list(reversed(kept_messages))
else:
# Check if the history is already shorter than the depth
if depth == 0 or len(messages) <= depth:
history[session_id]["messages"] = []

# If the message at depth is not a user message, then
# increment the depth until a user message is found
else:
while depth < len(messages) and messages[-depth]["role"] != "user":
depth += 1
history[session_id]["messages"] = messages[-depth:]

history[session_id]["last_accessed"] = time.time()

def handle_timer_event(self, timer_data):
Expand Down

0 comments on commit 8ba3185

Please sign in to comment.