pub fn new(generate_config: GenerateConfig) -> Result<OpenAIGenerator, RustBertError> {
        generate_config.validate();

        //        The following allow keeping the same GenerationConfig Default for GPT, GPT2 and BART models
        let model_resource = if &generate_config.model_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2ModelResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptModelResources::GPT,
            ))
        } else {
            generate_config.model_resource.clone()
        };

        let config_resource = if &generate_config.config_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2ConfigResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptConfigResources::GPT,
            ))
        } else {
            generate_config.config_resource.clone()
        };

        let vocab_resource = if &generate_config.vocab_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2VocabResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptVocabResources::GPT,
            ))
        } else {
            generate_config.vocab_resource.clone()
        };

        let merges_resource = if &generate_config.merges_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2MergesResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptMergesResources::GPT,
            ))
        } else {
            generate_config.merges_resource.clone()
        };

        let config_path = config_resource.get_local_path()?;
        let vocab_path = vocab_resource.get_local_path()?;
        let merges_path = merges_resource.get_local_path()?;
        let weights_path = model_resource.get_local_path()?;
        let device = generate_config.device;

        let mut var_store = nn::VarStore::new(device);
        let tokenizer = OpenAiGptTokenizer::from_file(
            vocab_path.to_str().unwrap(),
            merges_path.to_str().unwrap(),
            true,
        )?;
        let config = Gpt2Config::from_file(config_path);
        let model = OpenAIGPTLMHeadModel::new(&var_store.root(), &config);
        var_store.load(weights_path)?;

        let bos_token_id = None;
        let eos_token_ids = None;
        let pad_token_id = None;
        let is_encoder_decoder = false;
        let vocab_size = config.vocab_size;
        let decoder_start_id = None;

        Ok(OpenAIGenerator {
            model,
            tokenizer,
            var_store,
            generate_config,
            bos_token_id,
            eos_token_ids,
            pad_token_id,
            is_encoder_decoder,
            vocab_size,
            decoder_start_id,
        })
    }
/// Build a new `OpenAIGenerator`
///
/// # Arguments
///
/// * `generate_config` - `GenerateConfig` object containing the resource references (model, vocabulary, configuration), generation options and device placement (CPU/GPU)
///
/// # Example
///
/// ```no_run
/// # fn main() -> anyhow::Result<()> {
/// use rust_bert::pipelines::generation::{GenerateConfig, OpenAIGenerator};
/// let generate_config = GenerateConfig {
///     max_length: 30,
///     do_sample: true,
///     num_beams: 5,
///     temperature: 1.1,
///     num_return_sequences: 3,
///     ..Default::default()
/// };
/// let gpt_generator = OpenAIGenerator::new(generate_config)?;
/// # Ok(())
/// # }
/// ```
Build a new `OpenAIGenerator`
Build a new ` OpenAIGenerator `
pub fn new(generate_config: GenerateConfig) -> Result<OpenAIGenerator, RustBertError> {
        generate_config.validate();
        //        The following allow keeping the same GenerationConfig Default for GPT, GPT2 and BART models
        let model_resource = if &generate_config.model_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2ModelResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptModelResources::GPT,
            ))
        } else {
            generate_config.model_resource.clone()
        };
        let config_resource = if &generate_config.config_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2ConfigResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptConfigResources::GPT,
            ))
        } else {
            generate_config.config_resource.clone()
        };
        let vocab_resource = if &generate_config.vocab_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2VocabResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptVocabResources::GPT,
            ))
        } else {
            generate_config.vocab_resource.clone()
        };
        let merges_resource = if &generate_config.merges_resource
            == &Resource::Remote(RemoteResource::from_pretrained(Gpt2MergesResources::GPT2))
        {
            Resource::Remote(RemoteResource::from_pretrained(
                OpenAiGptMergesResources::GPT,
            ))
        } else {
            generate_config.merges_resource.clone()
        };
        let config_path = config_resource.get_local_path()?;
        let vocab_path = vocab_resource.get_local_path()?;
        let merges_path = merges_resource.get_local_path()?;
        let weights_path = model_resource.get_local_path()?;
        let device = generate_config.device;
        let mut var_store = nn::VarStore::new(device);
        let tokenizer = OpenAiGptTokenizer::from_file(
            vocab_path.to_str().unwrap(),
            merges_path.to_str().unwrap(),
            true,
        )?;
        let config = Gpt2Config::from_file(config_path);
        let model = OpenAIGPTLMHeadModel::new(&var_store.root(), &config);
        var_store.load(weights_path)?;
        let bos_token_id = None;
        let eos_token_ids = None;
        let pad_token_id = None;
        let is_encoder_decoder = false;
        let vocab_size = config.vocab_size;
        let decoder_start_id = None;
        Ok(OpenAIGenerator {
            model,
            tokenizer,
            var_store,
            generate_config,
            bos_token_id,
            eos_token_ids,
            pad_token_id,
            is_encoder_decoder,
            vocab_size,
            decoder_start_id,
        })
    }
pub fn new ( generate_config : GenerateConfig ) -> Result < OpenAIGenerator , RustBertError > { generate_config . validate ( ) ; //        The following allow keeping the same GenerationConfig Default for GPT, GPT2 and BART models let model_resource = if & generate_config . model_resource == & Resource :: Remote ( RemoteResource :: from_pretrained ( Gpt2ModelResources :: GPT2 ) ) { Resource :: Remote ( RemoteResource :: from_pretrained ( OpenAiGptModelResources :: GPT , ) ) } else { generate_config . model_resource . clone ( ) } ; let config_resource = if & generate_config . config_resource == & Resource :: Remote ( RemoteResource :: from_pretrained ( Gpt2ConfigResources :: GPT2 ) ) { Resource :: Remote ( RemoteResource :: from_pretrained ( OpenAiGptConfigResources :: GPT , ) ) } else { generate_config . config_resource . clone ( ) } ; let vocab_resource = if & generate_config . vocab_resource == & Resource :: Remote ( RemoteResource :: from_pretrained ( Gpt2VocabResources :: GPT2 ) ) { Resource :: Remote ( RemoteResource :: from_pretrained ( OpenAiGptVocabResources :: GPT , ) ) } else { generate_config . vocab_resource . clone ( ) } ; let merges_resource = if & generate_config . merges_resource == & Resource :: Remote ( RemoteResource :: from_pretrained ( Gpt2MergesResources :: GPT2 ) ) { Resource :: Remote ( RemoteResource :: from_pretrained ( OpenAiGptMergesResources :: GPT , ) ) } else { generate_config . merges_resource . clone ( ) } ; let config_path = config_resource . get_local_path ( ) ? ; let vocab_path = vocab_resource . get_local_path ( ) ? ; let merges_path = merges_resource . get_local_path ( ) ? ; let weights_path = model_resource . get_local_path ( ) ? ; let device = generate_config . device ; let mut var_store = nn :: VarStore :: new ( device ) ; let tokenizer = OpenAiGptTokenizer :: from_file ( vocab_path . to_str ( ) . unwrap ( ) , merges_path . to_str ( ) . unwrap ( ) , true , ) ? ; let config = Gpt2Config :: from_file ( config_path ) ; let model = OpenAIGPTLMHeadModel :: new ( & var_store . root ( ) , & config ) ; var_store . load ( weights_path ) ? ; let bos_token_id = None ; let eos_token_ids = None ; let pad_token_id = None ; let is_encoder_decoder = false ; let vocab_size = config . vocab_size ; let decoder_start_id = None ; Ok ( OpenAIGenerator { model , tokenizer , var_store , generate_config , bos_token_id , eos_token_ids , pad_token_id , is_encoder_decoder , vocab_size , decoder_start_id , } ) }
Build a new `OpenAIGenerator`
Build a new ` OpenAIGenerator `
generate_config
GenerateConfig
generate_config
GenerateConfig
example
None
fn main() -> anyhow::result<()> {
use rust_bert::pipelines::generation::{GenerateConfig, OpenAIGenerator};
let generate_config = GenerateConfig {
max_length: 30,
do_sample: true,
num_beams: 5,
temperature: 1.1,
num_return_sequences: 3,
Default::default()
};
let gpt_generator = OpenAIGenerator::new(generate_config)?.
use rust_bert :: pipelines :: generation :: { GenerateConfig OpenAIGenerator } ; let generate_config = GenerateConfig { max_length : 30 do_sample : true num_beams : 5 temperature : 1 . 1 num_return_sequences : 3 Default :: default () } ; let gpt_generator = OpenAIGenerator :: new ( generate_config ) ? .
ok(())
None
}
None