Struct rust_bert::pipelines::conversation::Conversation
source · 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>
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
’sencode_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 more