I have a function to generate a llama response, but when called, it consistently returns ValueError: text input must be of type str
(single example), List[str]
(batch or single pretokenized example) or List[List[str]]
(batch of pretokenized examples). Please help!
model_id = “meta-llama/Meta-Llama-3-8B-Instruct”
tokenizer = AutoTokenizer.from_pretrained(model_id, padding_side=’left’)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map=”auto”)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
def generate_llama_completion(prompt, model=model, tokenizer=tokenizer, temperature=0.7, max_tokens=200):
# tokenize input
inputs = tokenizer(prompt, return_tensors=”pt”, padding=True, truncation=True, max_length=512)
input_ids = inputs.input_ids
attention_mask = inputs.attention_mask
# generate response
outputs = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
max_length=max_tokens,
temperature=temperature,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
do_sample=True
)
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response_text
I’ve tried using to_list() with input_ids() but this isn’t working. I’m not sure what’s wrong with the function.
Irini Kanaris Miyashiro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.