Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import transformers
3import torch
4
5model_id = "BSC-LT/salamandra7b_rag_prompt_ca-en-es"
6
7prompt = "Here is a question that you should answer based on the given context. Write a response that answers the question using only information provided in the context. Provide the answer in Spanish."
8
9context = """Water boils at 100°C (212°F) at standard atmospheric pressure, which is at sea level.
10However, this boiling point can vary depending on altitude and atmospheric pressure.
11At higher altitudes, where atmospheric pressure is lower, water boils at a lower temperature.
12For example, at 2,000 meters (about 6,600 feet) above sea level, water boils at around 93°C (199°F).
13"""
14instruction = "At what temperature does water boil?"
15
16tokenizer = AutoTokenizer.from_pretrained(model_id)
17model = AutoModelForCausalLM.from_pretrained(
18 model_id,
19 device_map="cuda",
20 torch_dtype=torch.bfloat16
21 )
22
23content = f"{prompt}\n\nContext:\n{context}\n\nQuestion:\n{instruction}"
24chat = [ { "role": "user", "content": content } ]
25
26prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
27
28eos_tokens = [
29 tokenizer.eos_token_id,
30 tokenizer.convert_tokens_to_ids("<|im_end|>"),
31 ]
32
33inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
34outputs = model.generate(input_ids=inputs.to(model.device), eos_token_id=eos_tokens, max_new_tokens=200)
35