Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Avoid null reference to last_user_message in ChatHistory #580

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/steamship/agents/functional/functions_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ def build_chat_history_for_tool(self, context: AgentContext) -> List[Block]:

messages_from_memory = []
# get prior conversations
if context.chat_history.is_searchable():
_last_user_message = context.chat_history.last_user_message
if (
context.chat_history.is_searchable()
and _last_user_message
and _last_user_message.text
and len(_last_user_message.text.strip())
):
messages_from_memory.extend(
context.chat_history.search(context.chat_history.last_user_message.text, k=3)
.wait()
.to_ranked_blocks()
context.chat_history.search(_last_user_message, k=3).wait().to_ranked_blocks()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context.chat_history.search(_last_user_message, k=3).wait().to_ranked_blocks()
context.chat_history.search(_last_user_message.text, k=3).wait().to_ranked_blocks()

)
# TODO(dougreid): we need a way to threshold message inclusion, especially for small contexts

Expand All @@ -61,8 +65,12 @@ def build_chat_history_for_tool(self, context: AgentContext) -> List[Block]:
# de-dupe the messages from memory
ids = [
sys_msg.id,
context.chat_history.last_user_message.id,
] # filter out last user message, it is appended afterwards
]

if _last_user_message:
# filter out last user message, it is appended afterwards
ids.append(_last_user_message.id)

for msg in messages_from_memory:
if msg.id not in ids:
messages.append(msg)
Expand All @@ -72,7 +80,8 @@ def build_chat_history_for_tool(self, context: AgentContext) -> List[Block]:

# put the user prompt in the appropriate message location
# this should happen BEFORE any agent/assistant messages related to tool selection
messages.append(context.chat_history.last_user_message)
if _last_user_message:
messages.append(context.chat_history.last_user_message)

# get working history (completed actions)
messages.extend(self._function_calls_since_last_user_message(context))
Expand Down
Loading