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§

Build a new Conversation with an initial user input

Arguments
  • text - String with the initial user input to start a conversation
Example
use rust_bert::pipelines::conversation::Conversation;

let conversation = Conversation::new("Hi there!");

Build a new Conversation placeholder without user input

Example
use rust_bert::pipelines::conversation::Conversation;

let conversation = Conversation::new_empty();

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();

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!");

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();

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);

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"));

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();

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more