Struct 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

Source

pub fn new(text: &str) -> Conversation

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

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

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

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

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

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

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

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

pub fn load_from_history<S, SI>(&mut self, texts: &[S], ids: &[SI])
where S: AsRef<str>, SI: AsRef<[i64]>,

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

Source§

fn clone(&self) -> Conversation

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Conversation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T