Unverified Commit 57632bf9 authored by William Horton's avatar William Horton Committed by GitHub
Browse files

Fix backward compatibility of Conversation (#26741)

* Fix backward compatibility of Conversation

I ran into a case where an external library was depending on the `new_user_input` field of Conversation. https://github.com/SeldonIO/MLServer/blob/release/1.4.x/runtimes/huggingface/mlserver_huggingface/codecs/utils.py#L37

 

This field was deprecated as part of the refactor, but if `transformers` wants to maintain backwards compatibility for now (which is mentioned in a few comments) then there's a good argument for supporting it. Some comments referred to it as an "internal" property, but it didn't start with `_` as is Python convention, so I think it's reasonable that other libraries were referencing it directly.

It's not difficult to add it to the other supported backwards-compatible properties. In addition, the implementation of `past_user_inputs` didn't actually match the past behavior (it would contain the most recent message as well) so I updated that as well.

* make style

---------
Co-authored-by: default avatarMatt <rocketknight1@gmail.com>
parent db5e0c32
...@@ -155,17 +155,29 @@ class Conversation: ...@@ -155,17 +155,29 @@ class Conversation:
yield message["role"] == "user", message["content"] yield message["role"] == "user", message["content"]
@property @property
def past_user_inputs(self): def _user_messages(self):
# This is a legacy property for backwards compatibility. It is recommended to just directly access # This is a legacy property for backwards compatibility. It is recommended to just directly access
# conversation.messages instead. # conversation.messages instead.
return [message["content"] for message in self.messages if message["role"] == "user"] return [message["content"] for message in self.messages if message["role"] == "user"]
@property
def past_user_inputs(self):
# This is a legacy property for backwards compatibility. It is recommended to just directly access
# conversation.messages instead.
return self._user_messages[:-1]
@property @property
def generated_responses(self): def generated_responses(self):
# This is a legacy property for backwards compatibility. It is recommended to just directly access # This is a legacy property for backwards compatibility. It is recommended to just directly access
# conversation.messages instead. # conversation.messages instead.
return [message["content"] for message in self.messages if message["role"] == "assistant"] return [message["content"] for message in self.messages if message["role"] == "assistant"]
@property
def new_user_input(self):
# This is a legacy property for backwards compatibility. It is recommended to just directly access
# conversation.messages instead.
return self._user_messages[-1]
@add_end_docstrings( @add_end_docstrings(
PIPELINE_INIT_ARGS, PIPELINE_INIT_ARGS,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment