Spaces:
Runtime error
Runtime error
Haofei Yu
commited on
Commit
•
1880ac6
1
Parent(s):
47a5c6c
support pre commit (#30)
Browse files* fix imgs and typos
* add gitignore
* support pre-commit
- .github/workflows/main.yaml +1 -1
- .gitignore +1 -1
- .pre-commit-config.yaml +27 -0
- app.py +62 -45
- example.json +1 -1
- peft_config.json +1 -1
- requirements.txt +1 -1
- utils.py +16 -10
.github/workflows/main.yaml
CHANGED
@@ -16,4 +16,4 @@ jobs:
|
|
16 |
- name: Push to hub
|
17 |
env:
|
18 |
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
19 |
-
run: git push https://wdplx:[email protected]/spaces/wdplx/Sotopia-demo main
|
|
|
16 |
- name: Push to hub
|
17 |
env:
|
18 |
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
19 |
+
run: git push https://wdplx:[email protected]/spaces/wdplx/Sotopia-demo main
|
.gitignore
CHANGED
@@ -1 +1 @@
|
|
1 |
-
__pycache__/
|
|
|
1 |
+
__pycache__/
|
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v3.2.0
|
4 |
+
hooks:
|
5 |
+
- id: trailing-whitespace
|
6 |
+
- id: end-of-file-fixer
|
7 |
+
- id: check-yaml
|
8 |
+
- id: check-added-large-files
|
9 |
+
- repo: https://github.com/pre-commit/mirrors-prettier
|
10 |
+
rev: v3.0.1 # Use the sha / tag you want to point at
|
11 |
+
hooks:
|
12 |
+
- id: prettier
|
13 |
+
types_or: [html]
|
14 |
+
- repo: https://github.com/psf/black
|
15 |
+
rev: 22.12.0
|
16 |
+
hooks:
|
17 |
+
- id: black
|
18 |
+
args: [--line-length=79]
|
19 |
+
- repo: https://github.com/pycqa/isort
|
20 |
+
rev: 5.12.0
|
21 |
+
hooks:
|
22 |
+
- id: isort
|
23 |
+
args: ["--profile", "black", --line-length=72]
|
24 |
+
- repo: https://github.com/kynan/nbstripout
|
25 |
+
rev: 0.6.0
|
26 |
+
hooks:
|
27 |
+
- id: nbstripout
|
app.py
CHANGED
@@ -1,15 +1,20 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from dataclasses import dataclass
|
3 |
import os
|
|
|
|
|
|
|
|
|
4 |
import torch
|
5 |
import transformers
|
6 |
-
from
|
7 |
-
from
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
from utils import Agent,
|
11 |
|
12 |
-
DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"
|
13 |
|
14 |
|
15 |
def prepare_sotopia_info():
|
@@ -18,52 +23,60 @@ def prepare_sotopia_info():
|
|
18 |
background="Ethan Johnson is a 34-year-old male chef. He/him pronouns. Ethan Johnson is famous for cooking Italian food.",
|
19 |
goal="Uknown",
|
20 |
secrets="Uknown",
|
21 |
-
personality="Ethan Johnson, a creative yet somewhat reserved individual, values power and fairness. He likes to analyse situations before deciding.",
|
|
|
22 |
|
23 |
machine_agent = Agent(
|
24 |
name="Benjamin Jackson",
|
25 |
background="Benjamin Jackson is a 24-year-old male environmental activist. He/him pronouns. Benjamin Jackson is well-known for his impassioned speeches.",
|
26 |
goal="Figure out why they estranged you recently, and maintain the existing friendship (Extra information: you notice that your friend has been intentionally avoiding you, you would like to figure out why. You value your friendship with the friend and don't want to lose it.)",
|
27 |
secrets="Descendant of a wealthy oil tycoon, rejects family fortune",
|
28 |
-
personality="Benjamin Jackson, expressive and imaginative, leans towards self-direction and liberty. His decisions aim for societal betterment.",
|
|
|
29 |
|
30 |
-
scenario =
|
|
|
|
|
31 |
instructions = get_starter_prompt(machine_agent, human_agent, scenario)
|
32 |
return human_agent, machine_agent, scenario, instructions
|
33 |
|
34 |
|
35 |
-
|
36 |
-
|
37 |
def prepare():
|
38 |
model_name = "cmu-lti/sotopia-pi-mistral-7b-BC_SR"
|
39 |
compute_type = torch.float16
|
40 |
config_dict = PeftConfig.from_json_file("peft_config.json")
|
41 |
config = PeftConfig.from_peft_type(**config_dict)
|
42 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
return model, tokenizer
|
46 |
|
47 |
|
48 |
-
|
49 |
def introduction():
|
50 |
with gr.Column(scale=2):
|
51 |
-
gr.Image(
|
|
|
|
|
52 |
with gr.Column(scale=5):
|
53 |
gr.Markdown(
|
54 |
"""# Sotopia-Pi Demo
|
55 |
**Chat with [Sotopia-Pi](https://github.com/sotopia-lab/sotopia-pi), brainstorm ideas, discuss your holiday plans, and more!**
|
56 |
-
|
57 |
➡️️ **Intended Use**: this demo is intended to showcase an early finetuning of [sotopia-pi-mistral-7b-BC_SR](https://huggingface.co/cmu-lti/sotopia-pi-mistral-7b-BC_SR)/
|
58 |
-
|
59 |
⚠️ **Limitations**: the model can and will produce factually incorrect information, hallucinating facts and actions. As it has not undergone any advanced tuning/alignment, it can produce problematic outputs, especially if prompted to do so. Finally, this demo is limited to a session length of about 1,000 words.
|
60 |
-
|
61 |
🗄️ **Disclaimer**: User prompts and generated replies from the model may be collected by TII solely for the purpose of enhancing and refining our models. TII will not store any personally identifiable information associated with your inputs. By using this demo, users implicitly agree to these terms.
|
62 |
"""
|
63 |
)
|
64 |
|
65 |
|
66 |
-
|
67 |
def param_accordion(according_visible=True):
|
68 |
with gr.Accordion("Parameters", open=False, visible=according_visible):
|
69 |
temperature = gr.Slider(
|
@@ -91,8 +104,12 @@ def param_accordion(according_visible=True):
|
|
91 |
return temperature, session_id, max_tokens
|
92 |
|
93 |
|
94 |
-
def sotopia_info_accordion(
|
95 |
-
|
|
|
|
|
|
|
|
|
96 |
with gr.Row():
|
97 |
with gr.Column():
|
98 |
user_name = gr.Textbox(
|
@@ -150,16 +167,14 @@ def run_chat(
|
|
150 |
bot_name: str,
|
151 |
temperature: float,
|
152 |
top_p: float,
|
153 |
-
max_tokens: int
|
154 |
):
|
155 |
prompt = format_sotopia_prompt(
|
156 |
-
message,
|
157 |
-
history,
|
158 |
-
instructions,
|
159 |
-
user_name,
|
160 |
-
bot_name
|
161 |
)
|
162 |
-
input_tokens = tokenizer(
|
|
|
|
|
163 |
input_length = input_tokens.shape[-1]
|
164 |
output_tokens = model.generate(
|
165 |
input_tokens,
|
@@ -167,16 +182,17 @@ def run_chat(
|
|
167 |
top_p=top_p,
|
168 |
max_length=max_tokens,
|
169 |
pad_token_id=tokenizer.eos_token_id,
|
170 |
-
num_return_sequences=1
|
171 |
)
|
172 |
output_tokens = output_tokens[:, input_length:]
|
173 |
text_output = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
174 |
return text_output
|
175 |
|
176 |
-
|
177 |
def chat_tab():
|
178 |
model, tokenizer = prepare()
|
179 |
human_agent, machine_agent, scenario, instructions = prepare_sotopia_info()
|
|
|
180 |
# history are input output pairs
|
181 |
def run_chat(
|
182 |
message: str,
|
@@ -189,13 +205,11 @@ def chat_tab():
|
|
189 |
max_tokens: int,
|
190 |
):
|
191 |
prompt = format_sotopia_prompt(
|
192 |
-
message,
|
193 |
-
history,
|
194 |
-
instructions,
|
195 |
-
user_name,
|
196 |
-
bot_name
|
197 |
)
|
198 |
-
input_tokens = tokenizer(
|
|
|
|
|
199 |
input_length = input_tokens.shape[-1]
|
200 |
output_tokens = model.generate(
|
201 |
input_tokens,
|
@@ -203,19 +217,22 @@ def chat_tab():
|
|
203 |
top_p=top_p,
|
204 |
max_length=max_tokens,
|
205 |
pad_token_id=tokenizer.eos_token_id,
|
206 |
-
num_return_sequences=1
|
207 |
)
|
208 |
output_tokens = output_tokens[:, input_length:]
|
209 |
-
text_output = tokenizer.decode(
|
|
|
|
|
210 |
return text_output
|
211 |
|
212 |
-
|
213 |
with gr.Column():
|
214 |
with gr.Row():
|
215 |
temperature, session_id, max_tokens = param_accordion()
|
216 |
-
user_name, bot_name, scenario = sotopia_info_accordion(
|
|
|
|
|
217 |
instructions = instructions_accordion(instructions)
|
218 |
-
|
219 |
with gr.Column():
|
220 |
with gr.Blocks():
|
221 |
gr.ChatInterface(
|
@@ -226,8 +243,8 @@ def chat_tab():
|
|
226 |
show_label=False,
|
227 |
rtl=False,
|
228 |
avatar_images=(
|
229 |
-
"images/profile1.jpg",
|
230 |
-
"images/profile2.jpg"
|
231 |
),
|
232 |
),
|
233 |
textbox=gr.Textbox(
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from dataclasses import dataclass
|
3 |
+
from uuid import uuid4
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
import torch
|
7 |
import transformers
|
8 |
+
from peft import PeftConfig, PeftModel
|
9 |
+
from transformers import (
|
10 |
+
AutoModelForCausalLM,
|
11 |
+
AutoTokenizer,
|
12 |
+
BitsAndBytesConfig,
|
13 |
+
)
|
14 |
|
15 |
+
from utils import Agent, format_sotopia_prompt, get_starter_prompt
|
16 |
|
17 |
+
DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"
|
18 |
|
19 |
|
20 |
def prepare_sotopia_info():
|
|
|
23 |
background="Ethan Johnson is a 34-year-old male chef. He/him pronouns. Ethan Johnson is famous for cooking Italian food.",
|
24 |
goal="Uknown",
|
25 |
secrets="Uknown",
|
26 |
+
personality="Ethan Johnson, a creative yet somewhat reserved individual, values power and fairness. He likes to analyse situations before deciding.",
|
27 |
+
)
|
28 |
|
29 |
machine_agent = Agent(
|
30 |
name="Benjamin Jackson",
|
31 |
background="Benjamin Jackson is a 24-year-old male environmental activist. He/him pronouns. Benjamin Jackson is well-known for his impassioned speeches.",
|
32 |
goal="Figure out why they estranged you recently, and maintain the existing friendship (Extra information: you notice that your friend has been intentionally avoiding you, you would like to figure out why. You value your friendship with the friend and don't want to lose it.)",
|
33 |
secrets="Descendant of a wealthy oil tycoon, rejects family fortune",
|
34 |
+
personality="Benjamin Jackson, expressive and imaginative, leans towards self-direction and liberty. His decisions aim for societal betterment.",
|
35 |
+
)
|
36 |
|
37 |
+
scenario = (
|
38 |
+
"Conversation between two friends, where one is upset and crying"
|
39 |
+
)
|
40 |
instructions = get_starter_prompt(machine_agent, human_agent, scenario)
|
41 |
return human_agent, machine_agent, scenario, instructions
|
42 |
|
43 |
|
|
|
|
|
44 |
def prepare():
|
45 |
model_name = "cmu-lti/sotopia-pi-mistral-7b-BC_SR"
|
46 |
compute_type = torch.float16
|
47 |
config_dict = PeftConfig.from_json_file("peft_config.json")
|
48 |
config = PeftConfig.from_peft_type(**config_dict)
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
50 |
+
"mistralai/Mistral-7B-Instruct-v0.1"
|
51 |
+
)
|
52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
53 |
+
"mistralai/Mistral-7B-Instruct-v0.1"
|
54 |
+
).to("cuda")
|
55 |
+
model = PeftModel.from_pretrained(model, model_name, config=config).to(
|
56 |
+
"cuda"
|
57 |
+
)
|
58 |
return model, tokenizer
|
59 |
|
60 |
|
|
|
61 |
def introduction():
|
62 |
with gr.Column(scale=2):
|
63 |
+
gr.Image(
|
64 |
+
"images/sotopia.jpg", elem_id="banner-image", show_label=False
|
65 |
+
)
|
66 |
with gr.Column(scale=5):
|
67 |
gr.Markdown(
|
68 |
"""# Sotopia-Pi Demo
|
69 |
**Chat with [Sotopia-Pi](https://github.com/sotopia-lab/sotopia-pi), brainstorm ideas, discuss your holiday plans, and more!**
|
70 |
+
|
71 |
➡️️ **Intended Use**: this demo is intended to showcase an early finetuning of [sotopia-pi-mistral-7b-BC_SR](https://huggingface.co/cmu-lti/sotopia-pi-mistral-7b-BC_SR)/
|
72 |
+
|
73 |
⚠️ **Limitations**: the model can and will produce factually incorrect information, hallucinating facts and actions. As it has not undergone any advanced tuning/alignment, it can produce problematic outputs, especially if prompted to do so. Finally, this demo is limited to a session length of about 1,000 words.
|
74 |
+
|
75 |
🗄️ **Disclaimer**: User prompts and generated replies from the model may be collected by TII solely for the purpose of enhancing and refining our models. TII will not store any personally identifiable information associated with your inputs. By using this demo, users implicitly agree to these terms.
|
76 |
"""
|
77 |
)
|
78 |
|
79 |
|
|
|
80 |
def param_accordion(according_visible=True):
|
81 |
with gr.Accordion("Parameters", open=False, visible=according_visible):
|
82 |
temperature = gr.Slider(
|
|
|
104 |
return temperature, session_id, max_tokens
|
105 |
|
106 |
|
107 |
+
def sotopia_info_accordion(
|
108 |
+
human_agent, machine_agent, scenario, according_visible=True
|
109 |
+
):
|
110 |
+
with gr.Accordion(
|
111 |
+
"Sotopia Information", open=False, visible=according_visible
|
112 |
+
):
|
113 |
with gr.Row():
|
114 |
with gr.Column():
|
115 |
user_name = gr.Textbox(
|
|
|
167 |
bot_name: str,
|
168 |
temperature: float,
|
169 |
top_p: float,
|
170 |
+
max_tokens: int,
|
171 |
):
|
172 |
prompt = format_sotopia_prompt(
|
173 |
+
message, history, instructions, user_name, bot_name
|
|
|
|
|
|
|
|
|
174 |
)
|
175 |
+
input_tokens = tokenizer(
|
176 |
+
prompt, return_tensors="pt", padding="do_not_pad"
|
177 |
+
).input_ids.to("cuda")
|
178 |
input_length = input_tokens.shape[-1]
|
179 |
output_tokens = model.generate(
|
180 |
input_tokens,
|
|
|
182 |
top_p=top_p,
|
183 |
max_length=max_tokens,
|
184 |
pad_token_id=tokenizer.eos_token_id,
|
185 |
+
num_return_sequences=1,
|
186 |
)
|
187 |
output_tokens = output_tokens[:, input_length:]
|
188 |
text_output = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
189 |
return text_output
|
190 |
|
191 |
+
|
192 |
def chat_tab():
|
193 |
model, tokenizer = prepare()
|
194 |
human_agent, machine_agent, scenario, instructions = prepare_sotopia_info()
|
195 |
+
|
196 |
# history are input output pairs
|
197 |
def run_chat(
|
198 |
message: str,
|
|
|
205 |
max_tokens: int,
|
206 |
):
|
207 |
prompt = format_sotopia_prompt(
|
208 |
+
message, history, instructions, user_name, bot_name
|
|
|
|
|
|
|
|
|
209 |
)
|
210 |
+
input_tokens = tokenizer(
|
211 |
+
prompt, return_tensors="pt", padding="do_not_pad"
|
212 |
+
).input_ids.to("cuda")
|
213 |
input_length = input_tokens.shape[-1]
|
214 |
output_tokens = model.generate(
|
215 |
input_tokens,
|
|
|
217 |
top_p=top_p,
|
218 |
max_length=max_tokens,
|
219 |
pad_token_id=tokenizer.eos_token_id,
|
220 |
+
num_return_sequences=1,
|
221 |
)
|
222 |
output_tokens = output_tokens[:, input_length:]
|
223 |
+
text_output = tokenizer.decode(
|
224 |
+
output_tokens[0], skip_special_tokens=True
|
225 |
+
)
|
226 |
return text_output
|
227 |
|
|
|
228 |
with gr.Column():
|
229 |
with gr.Row():
|
230 |
temperature, session_id, max_tokens = param_accordion()
|
231 |
+
user_name, bot_name, scenario = sotopia_info_accordion(
|
232 |
+
human_agent, machine_agent, scenario
|
233 |
+
)
|
234 |
instructions = instructions_accordion(instructions)
|
235 |
+
|
236 |
with gr.Column():
|
237 |
with gr.Blocks():
|
238 |
gr.ChatInterface(
|
|
|
243 |
show_label=False,
|
244 |
rtl=False,
|
245 |
avatar_images=(
|
246 |
+
"images/profile1.jpg",
|
247 |
+
"images/profile2.jpg",
|
248 |
),
|
249 |
),
|
250 |
textbox=gr.Textbox(
|
example.json
CHANGED
@@ -2,4 +2,4 @@
|
|
2 |
"model": "gpt-4",
|
3 |
"prompt": "Prompt after formatting:\nImagine you are Benjamin Jackson, your task is to act/speak as Benjamin Jackson would, keeping in mind Benjamin Jackson's social goal.\nYou can find Benjamin Jackson's background and goal in the 'Here is the context of the interaction' field.\nNote that Benjamin Jackson's secret and goal is only visible to you.\nYou should try your best to achieve Benjamin Jackson's goal in a way that align with their character traits.\nAdditionally, maintaining the conversation's naturalness and realism is essential (e.g., do not repeat what other people has already said before).\n\nHere is the context of this interaction:\nScenario: Conversation between two friends, where one is upset and crying\nParticipants: Ethan Johnson and Benjamin Jackson\nEthan Johnson's background: Ethan Johnson is a 34-year-old male chef. He/him pronouns. Ethan Johnson is famous for cooking Italian food. Personality and values description: Ethan Johnson, a creative yet somewhat reserved individual, values power and fairness. He likes to analyse situations before deciding. \nBenjamin Jackson's background: Benjamin Jackson is a 24-year-old male environmental activist. He/him pronouns. Benjamin Jackson is well-known for his impassioned speeches. Personality and values description: Benjamin Jackson, expressive and imaginative, leans towards self-direction and liberty. His decisions aim for societal betterment. Benjamin's secrets: Descendant of a wealthy oil tycoon, rejects family fortune\nEthan Johnson's goal: Unknown\nBenjamin Jackson's goal: Figure out why they estranged you recently, and maintain the existing friendship (Extra information: you notice that your friend has been intentionally avoiding you, you would like to figure out why. You value your friendship with the friend and don't want to lose it.)\nConversation Starts:\n\nTurn #0: Ethan Johnson said: \"Hey Benjamin, I feel so sorry to see you like this. Do you want to talk about what's bothering you?\"\n.\nYou are at Turn #1.",
|
4 |
"result": "{'action_type': 'speak', 'argument': \"Hey Ethan, appreciate your concern, man. Actually, I've noticed we haven't been hanging out as much recently. Is everything okay? You've seemed distant...\"}"
|
5 |
-
}
|
|
|
2 |
"model": "gpt-4",
|
3 |
"prompt": "Prompt after formatting:\nImagine you are Benjamin Jackson, your task is to act/speak as Benjamin Jackson would, keeping in mind Benjamin Jackson's social goal.\nYou can find Benjamin Jackson's background and goal in the 'Here is the context of the interaction' field.\nNote that Benjamin Jackson's secret and goal is only visible to you.\nYou should try your best to achieve Benjamin Jackson's goal in a way that align with their character traits.\nAdditionally, maintaining the conversation's naturalness and realism is essential (e.g., do not repeat what other people has already said before).\n\nHere is the context of this interaction:\nScenario: Conversation between two friends, where one is upset and crying\nParticipants: Ethan Johnson and Benjamin Jackson\nEthan Johnson's background: Ethan Johnson is a 34-year-old male chef. He/him pronouns. Ethan Johnson is famous for cooking Italian food. Personality and values description: Ethan Johnson, a creative yet somewhat reserved individual, values power and fairness. He likes to analyse situations before deciding. \nBenjamin Jackson's background: Benjamin Jackson is a 24-year-old male environmental activist. He/him pronouns. Benjamin Jackson is well-known for his impassioned speeches. Personality and values description: Benjamin Jackson, expressive and imaginative, leans towards self-direction and liberty. His decisions aim for societal betterment. Benjamin's secrets: Descendant of a wealthy oil tycoon, rejects family fortune\nEthan Johnson's goal: Unknown\nBenjamin Jackson's goal: Figure out why they estranged you recently, and maintain the existing friendship (Extra information: you notice that your friend has been intentionally avoiding you, you would like to figure out why. You value your friendship with the friend and don't want to lose it.)\nConversation Starts:\n\nTurn #0: Ethan Johnson said: \"Hey Benjamin, I feel so sorry to see you like this. Do you want to talk about what's bothering you?\"\n.\nYou are at Turn #1.",
|
4 |
"result": "{'action_type': 'speak', 'argument': \"Hey Ethan, appreciate your concern, man. Actually, I've noticed we haven't been hanging out as much recently. Is everything okay? You've seemed distant...\"}"
|
5 |
+
}
|
peft_config.json
CHANGED
@@ -18,4 +18,4 @@
|
|
18 |
"v_proj"
|
19 |
],
|
20 |
"task_type": "CAUSAL_LM"
|
21 |
-
}
|
|
|
18 |
"v_proj"
|
19 |
],
|
20 |
"task_type": "CAUSAL_LM"
|
21 |
+
}
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
gradio
|
2 |
transformers
|
3 |
torch
|
4 |
-
peft
|
|
|
1 |
gradio
|
2 |
transformers
|
3 |
torch
|
4 |
+
peft
|
utils.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
-
from typing import
|
|
|
2 |
|
3 |
class Agent:
|
4 |
def __init__(self, name, background, goal, secrets, personality):
|
@@ -8,15 +9,18 @@ class Agent:
|
|
8 |
self.secrets = secrets
|
9 |
self.personality = personality
|
10 |
|
|
|
11 |
def get_starter_prompt(machine_agent, human_agent, scenario):
|
12 |
return f"Prompt after formatting:\nImagine you are {machine_agent.name}, your task is to act/speak as {machine_agent.name} would, keeping in mind {machine_agent.name}'s social goal.\nYou can find {machine_agent.name}'s background and goal in the 'Here is the context of the interaction' field.\nNote that {machine_agent.name}'s secret and goal is only visible to you.\nYou should try your best to achieve {machine_agent.name}'s goal in a way that align with their character traits.\nAdditionally, maintaining the conversation's naturalness and realism is essential (e.g., do not repeat what other people has already said before).\n\nHere is the context of this interaction:\n Scenario: {scenario}\nParticipants: {human_agent.name} and {machine_agent.name}\n{human_agent.name}'s background: {human_agent.background} Personality and values description: {human_agent.personality} \n{machine_agent.name}'s background: {machine_agent.background} Personality and values description: {machine_agent.personality} {machine_agent.name}'s secrets: {machine_agent.secrets}\n{human_agent.name}'s goal: Unknown\n{machine_agent.name}'s goal: {machine_agent.goal}\nConversation Starts:"
|
13 |
|
14 |
-
|
|
|
15 |
# [(user_message, bot_message), (user_message, bot_message)]
|
16 |
|
17 |
-
# we define dialogue history as
|
18 |
# user_name: user_message\nbot_name: bot_message\nuser_name: user_message\nbot_name: bot_message\n
|
19 |
|
|
|
20 |
def dialogue_history_length_check(string, max_token, tokenizer):
|
21 |
prompt_tokens = len(tokenizer(string)["input_ids"])
|
22 |
return max(prompt_tokens - max_token, 0)
|
@@ -46,9 +50,13 @@ def dialogue_history_creation(history, user_name, bot_name):
|
|
46 |
|
47 |
|
48 |
def dialogue_history_truncation(dialogue_history, max_token_num, tokenizer):
|
49 |
-
surpass_num = dialogue_history_length_check(
|
|
|
|
|
50 |
if surpass_num > 0:
|
51 |
-
dialogue_history = truncate_dialogue_history_to_length(
|
|
|
|
|
52 |
return dialogue_history
|
53 |
|
54 |
|
@@ -59,14 +67,12 @@ def format_sotopia_prompt(
|
|
59 |
user_name: str,
|
60 |
bot_name: str,
|
61 |
include_all_chat_history: bool = True,
|
62 |
-
index
|
63 |
) -> str:
|
64 |
prompt = instructions.strip()
|
65 |
dialogue_history, last_turn_idx = dialogue_history_creation(
|
66 |
-
history,
|
67 |
-
user_name,
|
68 |
-
bot_name
|
69 |
)
|
70 |
prompt = f"{prompt}\n{dialogue_history}"
|
71 |
prompt = f"{prompt}\n\nTurn #{last_turn_idx+1}: {user_name}: {message}\n.\nYou are at Turn #{last_turn_idx+2}."
|
72 |
-
return prompt
|
|
|
1 |
+
from typing import List, Tuple
|
2 |
+
|
3 |
|
4 |
class Agent:
|
5 |
def __init__(self, name, background, goal, secrets, personality):
|
|
|
9 |
self.secrets = secrets
|
10 |
self.personality = personality
|
11 |
|
12 |
+
|
13 |
def get_starter_prompt(machine_agent, human_agent, scenario):
|
14 |
return f"Prompt after formatting:\nImagine you are {machine_agent.name}, your task is to act/speak as {machine_agent.name} would, keeping in mind {machine_agent.name}'s social goal.\nYou can find {machine_agent.name}'s background and goal in the 'Here is the context of the interaction' field.\nNote that {machine_agent.name}'s secret and goal is only visible to you.\nYou should try your best to achieve {machine_agent.name}'s goal in a way that align with their character traits.\nAdditionally, maintaining the conversation's naturalness and realism is essential (e.g., do not repeat what other people has already said before).\n\nHere is the context of this interaction:\n Scenario: {scenario}\nParticipants: {human_agent.name} and {machine_agent.name}\n{human_agent.name}'s background: {human_agent.background} Personality and values description: {human_agent.personality} \n{machine_agent.name}'s background: {machine_agent.background} Personality and values description: {machine_agent.personality} {machine_agent.name}'s secrets: {machine_agent.secrets}\n{human_agent.name}'s goal: Unknown\n{machine_agent.name}'s goal: {machine_agent.goal}\nConversation Starts:"
|
15 |
|
16 |
+
|
17 |
+
# we define history as
|
18 |
# [(user_message, bot_message), (user_message, bot_message)]
|
19 |
|
20 |
+
# we define dialogue history as
|
21 |
# user_name: user_message\nbot_name: bot_message\nuser_name: user_message\nbot_name: bot_message\n
|
22 |
|
23 |
+
|
24 |
def dialogue_history_length_check(string, max_token, tokenizer):
|
25 |
prompt_tokens = len(tokenizer(string)["input_ids"])
|
26 |
return max(prompt_tokens - max_token, 0)
|
|
|
50 |
|
51 |
|
52 |
def dialogue_history_truncation(dialogue_history, max_token_num, tokenizer):
|
53 |
+
surpass_num = dialogue_history_length_check(
|
54 |
+
dialogue_history, max_token_num, tokenizer
|
55 |
+
)
|
56 |
if surpass_num > 0:
|
57 |
+
dialogue_history = truncate_dialogue_history_to_length(
|
58 |
+
dialogue_history, surpass_num, tokenizer
|
59 |
+
)
|
60 |
return dialogue_history
|
61 |
|
62 |
|
|
|
67 |
user_name: str,
|
68 |
bot_name: str,
|
69 |
include_all_chat_history: bool = True,
|
70 |
+
index: int = 1,
|
71 |
) -> str:
|
72 |
prompt = instructions.strip()
|
73 |
dialogue_history, last_turn_idx = dialogue_history_creation(
|
74 |
+
history, user_name, bot_name
|
|
|
|
|
75 |
)
|
76 |
prompt = f"{prompt}\n{dialogue_history}"
|
77 |
prompt = f"{prompt}\n\nTurn #{last_turn_idx+1}: {user_name}: {message}\n.\nYou are at Turn #{last_turn_idx+2}."
|
78 |
+
return prompt
|