update
This commit is contained in:
parent
0f40e33a1d
commit
ec63f50a94
|
|
@ -9,6 +9,7 @@ from tqdm import tqdm
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pickle
|
import pickle
|
||||||
# from utils import get_llama_activations_bau, tokenized_tqa, tokenized_tqa_gen, tokenized_tqa_gen_end_q
|
# from utils import get_llama_activations_bau, tokenized_tqa, tokenized_tqa_gen, tokenized_tqa_gen_end_q
|
||||||
|
from utils import get_hal_prompt, get_qa_prompt
|
||||||
import llama_iti
|
import llama_iti
|
||||||
import pickle
|
import pickle
|
||||||
import argparse
|
import argparse
|
||||||
|
|
@ -59,8 +60,9 @@ def main():
|
||||||
parser.add_argument('--dataset_name', type=str, default='triviaqa')
|
parser.add_argument('--dataset_name', type=str, default='triviaqa')
|
||||||
parser.add_argument('--num_gene', type=int, default=1)
|
parser.add_argument('--num_gene', type=int, default=1)
|
||||||
parser.add_argument('--use_api', type=bool, default=False)
|
parser.add_argument('--use_api', type=bool, default=False)
|
||||||
parser.add_argument('--sample', type=bool, default=False)
|
parser.add_argument('--most_likely', type=bool, default=False)
|
||||||
parser.add_argument("--model_dir", type=str, default=None, help='local directory with model data')
|
parser.add_argument("--model_dir", type=str, default=None, help='local directory with model data')
|
||||||
|
parser.add_argument("--instruction", type=str, default=None, help='local directory of instruction file.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
MODEL = HF_NAMES[args.model_name] if not args.model_dir else args.model_dir
|
MODEL = HF_NAMES[args.model_name] if not args.model_dir else args.model_dir
|
||||||
|
|
@ -156,6 +158,8 @@ def main():
|
||||||
dataset = get_dataset(llama_iti.LlamaTokenizer.from_pretrained(MODEL, trust_remote_code=True))
|
dataset = get_dataset(llama_iti.LlamaTokenizer.from_pretrained(MODEL, trust_remote_code=True))
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid dataset name")
|
raise ValueError("Invalid dataset name")
|
||||||
|
f = open(args.instruction, 'r', encoding="utf-8")
|
||||||
|
instruction = f.read()
|
||||||
|
|
||||||
if not args.use_api:
|
if not args.use_api:
|
||||||
|
|
||||||
|
|
@ -173,44 +177,43 @@ def main():
|
||||||
if not os.path.exists(f'./save_for_eval/{args.dataset_name}/{args.model_name}_hal_det/answers'):
|
if not os.path.exists(f'./save_for_eval/{args.dataset_name}/{args.model_name}_hal_det/answers'):
|
||||||
os.mkdir(f'./save_for_eval/{args.dataset_name}/{args.model_name}_hal_det/answers')
|
os.mkdir(f'./save_for_eval/{args.dataset_name}/{args.model_name}_hal_det/answers')
|
||||||
|
|
||||||
# period_token_id = [tokenizer(_)['input_ids'][-1] for _ in ['\n']]
|
if not os.path.exists(f'./save_for_eval/{args.dataset_name}/{args.model_name}_hal_det/hallucinations'):
|
||||||
# period_token_id += [tokenizer.eos_token_id]
|
os.mkdir(f'./save_for_eval/{args.dataset_name}/{args.model_name}_hal_det/hallucinations')
|
||||||
|
|
||||||
|
|
||||||
for i in range(begin_index, end_index):
|
for i in range(begin_index, end_index):
|
||||||
answers = [None] * args.num_gene
|
answers = [None] * args.num_gene
|
||||||
|
hallucinations= [None] * args.num_gene
|
||||||
if args.dataset_name == 'tydiqa':
|
if args.dataset_name == 'tydiqa':
|
||||||
question = dataset[int(used_indices[i])]['question']
|
question = dataset[int(used_indices[i])]['question']
|
||||||
prompt = "Concisely answer the following question based on the information in the given passage: \n" + \
|
prompt = get_qa_prompt(dataset[int(used_indices[i])]['context'],question)
|
||||||
" Passage: " + dataset[int(used_indices[i])]['context'] + " \n Q: " + question + " \n A:"
|
hallucination_prompt=get_hal_prompt(dataset[int(used_indices[i])]['context'],question,instruction)
|
||||||
# prompt = tokenizer(
|
|
||||||
# "Concisely answer the following question based on the information in the given passage: \n" + \
|
|
||||||
# " Passage: " + dataset[int(used_indices[i])]['context'] + " \n Q: " + question + " \n A:",
|
|
||||||
# return_tensors='pt').input_ids.cuda()
|
|
||||||
elif args.dataset_name == 'coqa':
|
elif args.dataset_name == 'coqa':
|
||||||
prompt = dataset[i]['prompt']
|
prompt = get_qa_prompt("None",dataset[i]['prompt'])
|
||||||
# prompt = tokenizer(
|
hallucination_prompt=get_hal_prompt("None",dataset[i]['prompt'],instruction)
|
||||||
# dataset[i]['prompt'], return_tensors='pt').input_ids.cuda()
|
|
||||||
else:
|
else:
|
||||||
question = dataset[i]['question']
|
question = dataset[i]['question']
|
||||||
prompt = f"Answer the question concisely. Q: {question}" + " A:"
|
prompt = get_qa_prompt("None",question)
|
||||||
# prompt = tokenizer(f"Answer the question concisely. Q: {question}" + " A:", return_tensors='pt').input_ids.cuda()
|
hallucination_prompt=get_hal_prompt("None",question,instruction)
|
||||||
|
|
||||||
for gen_iter in range(args.num_gene):
|
for gen_iter in range(args.num_gene):
|
||||||
if args.sample:
|
if args.most_likely:
|
||||||
response = openai.Completion.create(engine=args.model_name,
|
response = openai.ChatCompletion.create(
|
||||||
prompt=prompt,
|
model=args.model_name,
|
||||||
max_tokens=50,
|
messages=prompt,
|
||||||
n=1,
|
temperature=1,
|
||||||
stop=None,
|
max_tokens=256,
|
||||||
top_p=1,
|
top_p=1
|
||||||
temperature=0.9,)
|
)
|
||||||
|
hallucination_response = openai.ChatCompletion.create(
|
||||||
|
model=args.model_name,
|
||||||
|
messages=hallucination_prompt,
|
||||||
|
temperature=1,
|
||||||
|
max_tokens=256,
|
||||||
|
top_p=1
|
||||||
|
)
|
||||||
decoded=response.choices[0].text
|
decoded=response.choices[0].text
|
||||||
# generated = model.generate(prompt,
|
hallucination_decoded=hallucination_response.choices[0].text
|
||||||
# num_beams=5,
|
|
||||||
# num_return_sequences=1,
|
|
||||||
# do_sample=False,
|
|
||||||
# max_new_tokens=64,
|
|
||||||
# )
|
|
||||||
else:
|
else:
|
||||||
response = openai.Completion.create(engine=args.model_name,
|
response = openai.Completion.create(engine=args.model_name,
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
|
|
|
||||||
31
utils.py
31
utils.py
|
|
@ -897,3 +897,34 @@ def get_com_directions(num_layers, num_heads, train_set_idxs, val_set_idxs, sepa
|
||||||
com_directions = np.array(com_directions)
|
com_directions = np.array(com_directions)
|
||||||
|
|
||||||
return com_directions
|
return com_directions
|
||||||
|
|
||||||
|
def get_hal_prompt(knowledge, question, instruction):
|
||||||
|
if isinstance(instruction, str):
|
||||||
|
message = [
|
||||||
|
{"role": "user", "content": instruction +
|
||||||
|
"\n\n#Knowledge#: " + knowledge +
|
||||||
|
"\n#Question#: " + question +
|
||||||
|
# "\n#Right Answer#: " + answer +
|
||||||
|
"\n#Hallucinated Answer#: "}
|
||||||
|
]
|
||||||
|
elif isinstance(instruction, list):
|
||||||
|
mes = [{"role": "user",
|
||||||
|
"content": "You are now a mature hallucination generator. Please generate hallucinated answer for the following question. You can use any method you have learned that is suitable for the given question." +
|
||||||
|
"\n\n#Knowledge#: " + knowledge +
|
||||||
|
"\n#Question#: " + question +
|
||||||
|
# "\n#Right Answer#: " + answer +
|
||||||
|
"\n#Hallucinated Answer#: "}]
|
||||||
|
message = instruction + mes
|
||||||
|
else:
|
||||||
|
raise TypeError("The instruction must be str or list!")
|
||||||
|
return message
|
||||||
|
|
||||||
|
def get_qa_prompt(knowledge, question):
|
||||||
|
return [
|
||||||
|
{"role": "user", "content": "Answer the question concisely." +
|
||||||
|
"\n\n#Knowledge#: " + knowledge +
|
||||||
|
"\n#Question#: " + question +
|
||||||
|
# "\n#Right Answer#: " + answer +
|
||||||
|
"\n#Answer#: "}
|
||||||
|
]
|
||||||
|
|
||||||
Loading…
Reference in New Issue