Skip to content

Commit

Permalink
return a single chat message instead of an empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Oct 6, 2024
1 parent 7811818 commit 3f36713
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/banks/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def chat_messages(self, data: dict[str, Any] | None = None) -> list[ChatMessage]
except ValidationError:
# Ignore lines that are not a message
pass

if not messages:
# fallback, if there was no {% chat %} block in the template,
# put the text into a single user message
messages.append(ChatMessage(role="user", content=rendered))

return messages


Expand Down
6 changes: 6 additions & 0 deletions tests/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,9 @@ def test_chat_messages_cached():
p = Prompt(p_file.read_text(), render_cache=mock_cache)
p.chat_messages()
mock_cache.set.assert_called_once()


def test_chat_message_no_chat_tag():
text = "This is raw text"
p = Prompt(text=text)
assert p.chat_messages() == [ChatMessage(role="user", content=text)]

0 comments on commit 3f36713

Please sign in to comment.