pub struct Conversation {
pub past_user_inputs: Vec<String>,
pub generated_responses: Vec<String>,
pub new_user_input: Option<String>,
pub history: Vec<Vec<i64>>,
}
Expand description
Data structure keeping track of a conversation in the system. It contains past user inputs and generated answers, a history of the tokens generated and a placeholder for new user inputs to be processed by the system if submitted for prediction
Fields§
§past_user_inputs: Vec<String>
Past user inputs that have already been processed
generated_responses: Vec<String>
Past system generated responses
new_user_input: Option<String>
New user input that needs to be processed
history: Vec<Vec<i64>>
History of the tokens passed as an input and generated so far used as context for next turn generation
Implementations§
Source§impl Conversation
impl Conversation
Sourcepub fn new(text: &str) -> Conversation
pub fn new(text: &str) -> Conversation
Sourcepub fn new_empty() -> Conversation
pub fn new_empty() -> Conversation
Build a new Conversation
placeholder without user input
§Example
use rust_bert::pipelines::conversation::Conversation;
let conversation = Conversation::new_empty();
Sourcepub fn add_user_input(&mut self, text: &str) -> Result<(), RustBertError>
pub fn add_user_input(&mut self, text: &str) -> Result<(), RustBertError>
Adds a new user input to the conversation. This method returns an error if an unprocessed user input already exists
§Arguments
text
-&str
with the additional user input to continue a conversation
§Example
use rust_bert::pipelines::conversation::Conversation;
let mut conversation = Conversation::new_empty();
conversation.add_user_input("Hi there!").unwrap();
Sourcepub fn add_user_input_with_overwrite(&mut self, text: &str) -> Option<String>
pub fn add_user_input_with_overwrite(&mut self, text: &str) -> Option<String>
Adds a new user input to the conversation. If an unprocessed user input already exists, its contents are overwritten by the new value provided.
§Arguments
text
-&str
with the additional user input to continue a conversation
§Returns
Option<String>
containing overwritten string if applicable
§Example
use rust_bert::pipelines::conversation::Conversation;
let mut conversation = Conversation::new_empty();
conversation
.add_user_input("This input will not be used")
.unwrap();
let unused_string = conversation.add_user_input_with_overwrite("Hi there!");
Sourcepub fn contains_new_input(&self) -> bool
pub fn contains_new_input(&self) -> bool
Returns true
if the conversation contains new user inputs to process
§Returns
bool
flag indicating if the conversation contains new inputs to process
§Example
use rust_bert::pipelines::conversation::Conversation;
let mut conversation = Conversation::new_empty();
let false_value = conversation.contains_new_input();
conversation
.add_user_input("This input will not be used")
.unwrap();
let true_value = conversation.contains_new_input();
Sourcepub fn mark_processed(&mut self)
pub fn mark_processed(&mut self)
Marks the conversation as processed and moves the user input that was up for processing to the past user inputs.
§Example
use rust_bert::pipelines::conversation::Conversation;
let mut conversation = Conversation::new_empty();
let false_value = conversation.contains_new_input();
conversation
.add_user_input("This input will not be used")
.unwrap();
let true_value = conversation.contains_new_input();
conversation.mark_processed();
let false_value = conversation.contains_new_input();
assert_eq!(conversation.past_user_inputs.len(), 1usize);
Sourcepub fn get_last_input(&self) -> Option<&str>
pub fn get_last_input(&self) -> Option<&str>
Returns the last user input provided (including non-processed inputs).
§Returns
Option<&str>
representation of the last user input provided
§Example
use rust_bert::pipelines::conversation::Conversation;
let mut conversation = Conversation::new_empty();
let none_value = conversation.get_last_input();
conversation
.add_user_input("This input will not be used")
.unwrap();
let last_provided_input = conversation.get_last_input();
assert_eq!(last_provided_input, Some("This input will not be used"));
Sourcepub fn get_last_response(&self) -> Option<&str>
pub fn get_last_response(&self) -> Option<&str>
Returns the last response generated by the system.
§Returns
Option<&str>
representation of the last response generated by the system.
§Example
use rust_bert::pipelines::conversation::Conversation;
let mut conversation = Conversation::new("Hi There");
let non_value = conversation.get_last_response();
Sourcepub fn load_from_history<S, SI>(&mut self, texts: &[S], ids: &[SI])
pub fn load_from_history<S, SI>(&mut self, texts: &[S], ids: &[SI])
Initializes a conversation form a prior state. It is assumed that a conversation always start from a user interaction.
§Arguments
- texts: sequence of strings, alternating between past user inputs and past generated responses.
- ids: sequence of sequence of ids, alternating between past user inputs and past generated responses.
These can be generated via a ConversationModel
’s encode_prompts
.
§Example:
use rust_bert::pipelines::conversation::{ConversationManager, ConversationModel};
use rust_bert::pipelines::generation_utils::LanguageGenerator;
let model = ConversationModel::new(Default::default())?;
let mut conversation_manager = ConversationManager::new();
let history = [
"Going to the movies tonight - any suggestions?",
"The Big Lebowski",
"Is it an action movie?",
];
let encoded_history = model.encode_prompts(&history);
let conversation_1_id = conversation_manager.create_empty();
let _ = conversation_manager
.get(&conversation_1_id)
.unwrap()
.load_from_history(&history, &encoded_history);
Trait Implementations§
Source§impl Clone for Conversation
impl Clone for Conversation
Source§fn clone(&self) -> Conversation
fn clone(&self) -> Conversation
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for Conversation
impl RefUnwindSafe for Conversation
impl Send for Conversation
impl Sync for Conversation
impl Unpin for Conversation
impl UnwindSafe for Conversation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more