-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: 严照东 <[email protected]>
- Loading branch information
严照东
committed
Sep 26, 2023
1 parent
4986a94
commit 2fb5318
Showing
5 changed files
with
261 additions
and
262 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import random | ||
import numpy as np | ||
import torch | ||
from fastchat.conversation import get_conv_template | ||
|
||
def set_random_seed(seed): | ||
"""Set random seed for reproducability.""" | ||
if seed is not None and seed > 0: | ||
random.seed(seed) | ||
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
|
||
|
||
|
||
def covert_prompt_to_input_ids_with_history(text, history, tokenizer, max_token, convo_template="aquila-chat"): | ||
# aquila-chat as default | ||
conv = get_conv_template(convo_template) | ||
|
||
conv.append_message(conv.roles[1], None) | ||
conv.append_message(conv.roles[0], text) | ||
|
||
example = tokenizer.encode_plus(f"{conv.get_prompt()} ", None, max_length=None)['input_ids'] | ||
|
||
while(len(history) > 0 and (len(example) < max_token)): | ||
tmp = history.pop() | ||
if tmp[0] == 'ASSISTANT': | ||
conv.append_message(conv.roles[1], tmp[1]) | ||
else: | ||
conv.append_message(conv.roles[0], tmp[1]) | ||
example = tokenizer.encode_plus(f"{conv.get_prompt()} ", None, max_length=None)['input_ids'] | ||
|
||
if len(example) >= max_token: | ||
conv.messages.pop() | ||
conv.messages = conv.messages[::-1] | ||
print('model in:', conv.get_prompt()) | ||
example = tokenizer.encode_plus(f"{conv.get_prompt()} ", None, max_length=None)['input_ids'] | ||
|
||
return example |
Oops, something went wrong.