-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_qa.py
More file actions
21 lines (15 loc) · 791 Bytes
/
Copy pathllm_qa.py
File metadata and controls
21 lines (15 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_name = "meta-llama/Llama-2-7b-chat-hf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
def format_prompt(context, query):
return f"""You are a helpful medical assistant. Answer only based on the following context. If you are unsure, say 'I don't know.'
Context:
{context}
Query: {query}
Answer:"""
def generate_answer(prompt):
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=150, do_sample=True, temperature=0.7, top_p=0.9)
return tokenizer.decode(output[0], skip_special_tokens=True).split("Answer:")[-1].strip()