Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_id = "avinashkongara4/llama3-ragnarok-merged"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype=torch.float16,
10 device_map="auto"
11)
12
13def ask(question, context):
14 prompt = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
15Context: {context}
16Question: {question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
17
18 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
19 with torch.no_grad():
20 out = model.generate(**inputs, max_new_tokens=200, temperature=0.1)
21 return tokenizer.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
22
23# Example
24context = "The Eiffel Tower is located in Paris, France. It was built in 1889."
25question = "Where is the Eiffel Tower located?"
26print(ask(question, context))