-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_bfi.py
137 lines (115 loc) · 5.24 KB
/
run_bfi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import random
import json
import openai
import os
import pandas as pd
import sys
from tqdm import tqdm
import re
from pathlib import Path
import json
import numpy as np
import itertools
from gpt import is_answer_in_valid_form
from tenacity import retry, stop_after_attempt, wait_random_exponential
import multiprocessing
import replicate
openai.organization = ""
openai.api_key = ""
def construct_big_five_words(persona_type):
"""Construct the list of personality traits
e.g., introverted + antagonistic + conscientious + emotionally stable + open to experience
"""
options = list(persona_type)
last_item = "and " + options[-1]
options[-1] = last_item
return ", ".join(options)
def run_gpt_query(model_name, temperature, system_prompt, user_prompt):
response = openai.ChatCompletion.create(
model=model_name,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=temperature,
)
return response
def run_llama2_query(temperature, system_prompt, user_prompt):
response = replicate.run(
"meta/llama-2-70b-chat:2d19859030ff705a87c746f7e96eea03aefb71f166725aee39692f1476566d48",
input={"prompt": user_prompt,
"system_prompt": system_prompt,
"max_new_tokens": 500,
"temperature": temperature,
"top_p": 1.0,
}
)
return response
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(15))
def generate_bfi_response(model_name, temperature, persona_type, prompt_file):
"""Generate a response using the specified model, temperature, persona type, and prompt file.
Args:
model_name (str): The name of the model to use for generating the response.
temperature (float): The temperature parameter for controlling the randomness of the generated response.
persona_type (str): The type of persona to use for generating the response.
prompt_file (str): The file containing the prompt to use for generating the response.
Returns:
str: bfi response
str: user prompt
"""
# Read prompt template
big_five_trait_str = construct_big_five_words(persona_type)
system_prompt = "You are a character who is {}.".format(big_five_trait_str)
user_prompt = open(prompt_file).read()
user_prompt = user_prompt.strip("\n").strip()
user_prompt = user_prompt + "\n\n"
while True:
if model_name.lower().startswith("gpt"):
response = run_gpt_query(model_name, temperature, system_prompt, user_prompt)
response = response["choices"][0]['message']['content'].strip("\n")
elif model_name.lower().startswith("llama"):
# print("llama-2 generating")
# print(system_prompt, user_prompt)
response = run_llama2_query(temperature, system_prompt, user_prompt)
response = "".join([each for each in response]).strip("\n").strip()
# print("YOUR RESPONSE:")
# print(response)
if is_answer_in_valid_form(response, 44):
return response, user_prompt
else:
print("====>>> Answer not right, re-submitting request...")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--prompt_file", type=str, default="./prompts/bfi_prompt.txt")
parser.add_argument("--model", default="gpt-3.5-turbo-0613", type=str)
parser.add_argument("--temperature", default=0.7, type=float)
args = parser.parse_args()
# create if not exits
args.output_folder = os.path.join("./outputs/", args.model, "temp{}".format(args.temperature), "bfi")
Path(args.output_folder).mkdir(parents=True, exist_ok=True)
# query for each persona type
personality_types = [["extroverted", "introverted"],
["agreeable", "antagonistic"],
["conscientious", "unconscientious"],
["neurotic", "emotionally stable"],
["open to experience", "closed to experience"]]
pool = multiprocessing.Pool()
responses = []
for persona_type in tqdm(list(itertools.product(*personality_types))):
print("Processing persona --> {}".format(" + ".join(persona_type)))
for iteration in range(1, 11):
# json_output = generate_bfi_response(args.model, args.temperature, persona_type, prompt_file=args.prompt_file)
response = pool.apply_async(generate_bfi_response, args=(args.model, args.temperature, persona_type, args.prompt_file))
persona_encoding = "_".join([trait[:4] for trait in persona_type])
responses.append([persona_encoding, iteration, response])
# print(json_output)
for persona_encoding, iteration, response in tqdm(responses):
json_obj = {"persona_encoding": persona_encoding, "iteration": iteration}
json_obj["annotation"] = response.get()[0]
json_obj["user_prompt"] = response.get()[1]
json_obj = json.dumps(json_obj, indent=4)
with open(os.path.join(args.output_folder, "{}_p{}.json".format(persona_encoding, iteration)), "w", encoding='UTF-8') as out:
out.write(json_obj)
if __name__ == "__main__":
main()