pub fn generate_responses<'a>(
        &self,
        conversation_manager: &'a mut ConversationManager,
    ) -> HashMap<&'a Uuid, &'a str> {
        let (active_uuid, active_conversations) = conversation_manager.get_active_conversations();
        if !active_uuid.is_empty() {
            let texts = active_conversations
                .iter()
                .map(|c| c.new_user_input.as_ref().unwrap().as_str())
                .collect_vec();

            let history = active_conversations
                .iter()
                .map(|c| &c.history)
                .collect_vec();

            let prompt_ids = self.encode_prompts(texts.as_slice());
            let input_tensor = self.concat_input_history(prompt_ids, history);
            let input_length = *input_tensor.size().last().unwrap() as usize;
            let mut generated = self.model.generate_from_ids_and_past(input_tensor, None);
            let removed_padding_quantities = self.clean_padding_indices(&mut generated);

            let mut output = HashMap::with_capacity(active_uuid.len());

            for (((conversation, generated_sequence), uuid), removed_padding) in
                active_conversations
                    .into_iter()
                    .zip(generated.into_iter())
                    .zip(active_uuid.into_iter())
                    .zip(removed_padding_quantities.into_iter())
            {
                conversation
                    .generated_responses
                    .push(self.model.get_tokenizer().decode(
                        generated_sequence[input_length - removed_padding.0..].to_vec(),
                        true,
                        true,
                    ));
                conversation.history = generated_sequence;
                conversation.mark_processed();
                output.insert(uuid, conversation.get_last_response().unwrap());
            }
            output
        } else {
            HashMap::new()
        }
    }
/// Perform a multi-turn conversation based on user input
///
/// # Arguments
///
/// * `conversation_manager` - `&mut ConversationManager` Conversation manager keeping track of active conversations
///
/// # Returns
/// * `HashMap<&Uuid, &str>` Responses from the model for each active conversation, referenced by Uuid
///
/// # Example
///
/// ```no_run
/// # fn main() -> anyhow::Result<()> {
/// use rust_bert::pipelines::conversation::{ConversationManager, ConversationModel};
/// use rust_bert::pipelines::generation::LanguageGenerator;
/// let model = ConversationModel::new(Default::default())?;
///
/// let mut conversation_manager = ConversationManager::new();
/// conversation_manager.create("Hello, how are you?");
///
/// let output = model.generate_responses(&mut conversation_manager);
/// # Ok(())
/// # }
/// ```
Perform a multi-turn conversation based on user input
Perform a multi - turn conversation based on user input
pub fn generate_responses<'a>(
        &self,
        conversation_manager: &'a mut ConversationManager,
    ) -> HashMap<&'a Uuid, &'a str> {
        let (active_uuid, active_conversations) = conversation_manager.get_active_conversations();
        if !active_uuid.is_empty() {
            let texts = active_conversations
                .iter()
                .map(|c| c.new_user_input.as_ref().unwrap().as_str())
                .collect_vec();
            let history = active_conversations
                .iter()
                .map(|c| &c.history)
                .collect_vec();
            let prompt_ids = self.encode_prompts(texts.as_slice());
            let input_tensor = self.concat_input_history(prompt_ids, history);
            let input_length = *input_tensor.size().last().unwrap() as usize;
            let mut generated = self.model.generate_from_ids_and_past(input_tensor, None);
            let removed_padding_quantities = self.clean_padding_indices(&mut generated);
            let mut output = HashMap::with_capacity(active_uuid.len());
            for (((conversation, generated_sequence), uuid), removed_padding) in
                active_conversations
                    .into_iter()
                    .zip(generated.into_iter())
                    .zip(active_uuid.into_iter())
                    .zip(removed_padding_quantities.into_iter())
            {
                conversation
                    .generated_responses
                    .push(self.model.get_tokenizer().decode(
                        generated_sequence[input_length - removed_padding.0..].to_vec(),
                        true,
                        true,
                    ));
                conversation.history = generated_sequence;
                conversation.mark_processed();
                output.insert(uuid, conversation.get_last_response().unwrap());
            }
            output
        } else {
            HashMap::new()
        }
    }
pub fn generate_responses < ' a > ( & self , conversation_manager : & ' a mut ConversationManager , ) -> HashMap < & ' a Uuid , & ' a str > { let ( active_uuid , active_conversations ) = conversation_manager . get_active_conversations ( ) ; if ! active_uuid . is_empty ( ) { let texts = active_conversations . iter ( ) . map ( | c | c . new_user_input . as_ref ( ) . unwrap ( ) . as_str ( ) ) . collect_vec ( ) ; let history = active_conversations . iter ( ) . map ( | c | & c . history ) . collect_vec ( ) ; let prompt_ids = self . encode_prompts ( texts . as_slice ( ) ) ; let input_tensor = self . concat_input_history ( prompt_ids , history ) ; let input_length = * input_tensor . size ( ) . last ( ) . unwrap ( ) as usize ; let mut generated = self . model . generate_from_ids_and_past ( input_tensor , None ) ; let removed_padding_quantities = self . clean_padding_indices ( & mut generated ) ; let mut output = HashMap :: with_capacity ( active_uuid . len ( ) ) ; for ( ( ( conversation , generated_sequence ) , uuid ) , removed_padding ) in active_conversations . into_iter ( ) . zip ( generated . into_iter ( ) ) . zip ( active_uuid . into_iter ( ) ) . zip ( removed_padding_quantities . into_iter ( ) ) { conversation . generated_responses . push ( self . model . get_tokenizer ( ) . decode ( generated_sequence [ input_length - removed_padding . 0 .. ] . to_vec ( ) , true , true , ) ) ; conversation . history = generated_sequence ; conversation . mark_processed ( ) ; output . insert ( uuid , conversation . get_last_response ( ) . unwrap ( ) ) ; } output } else { HashMap :: new ( ) } }
Perform a multi-turn conversation based on user input
Perform a multi - turn conversation based on user input
&self
conversation_manager
&'a mut ConversationManager
`HashMap<&Uuid, &str>` Responses from the model for each active conversation, referenced by Uuid
` HashMap<&Uuid &str > ` Responses from the model for each active conversation referenced by Uuid
&self
conversation_manager
&'a mut ConversationManager
example
None
fn main() -> anyhow::result<()> {
use rust_bert::pipelines::conversation::{ConversationManager, ConversationModel};
use rust_bert::pipelines::generation::LanguageGenerator;
let model = ConversationModel::new(Default::default())?.

let mut conversation_manager = ConversationManager::new();
conversation_manager.create("Hello, how are you?").

let output = model.generate_responses(&mut conversation_manager).
use rust_bert :: pipelines :: conversation :: { ConversationManager ConversationModel } ; use rust_bert :: pipelines :: generation :: LanguageGenerator ; let model = ConversationModel :: new ( Default :: default () ) ? . let mut conversation_manager = ConversationManager :: new () ; conversation_manager . create ( " Hello how are you? " ) . let output = model . generate_responses ( &mut conversation_manager ) .
ok(())
None
}
None