alexkueck commited on
Commit
eb160c2
·
1 Parent(s): 8e9395a

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +11 -10
utils.py CHANGED
@@ -21,6 +21,7 @@ import transformers
21
  from transformers import AutoTokenizer, AutoModelForCausalLM
22
  import datasets
23
  from datasets import load_dataset
 
24
 
25
 
26
  def is_stop_word_or_prefix(s: str, stop_words: list) -> bool:
@@ -32,19 +33,19 @@ def is_stop_word_or_prefix(s: str, stop_words: list) -> bool:
32
  return True
33
  return False
34
 
35
- def generate_prompt_with_history(text, tokenizer, max_length=2048):
36
  #prompt = "The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n[|Human|]Hello!\n[|AI|]Hi!"
37
  prompt = "Das folgende ist eine Unterhaltung zwischen einem Menschen und einem KI-Assistenten, der Baize genannt wird. Baize ist ein open-source KI-Assistent, der von UCSD entwickelt wurde. Der Mensch und der KI-Assistent chatten abwechselnd miteinander in deutsch. Die Antworten des KI Assistenten sind immer so ausführlich wie möglich und in Markdown Schreibweise und in deutscher Sprache. Wenn nötig übersetzt er sie ins Deutsche. Die Antworten des KI-Assistenten vermeiden Themen und Antworten zu unethischen, kontroversen oder sensiblen Themen. Die Antworten sind immer sehr höflich formuliert..\n[|Human|]Hallo!\n[|AI|]Hi!"
38
- #history =[]
39
- #history = ["\n[|Human|]{}\n[|AI|]{}".format(x[0],x[1]) for x in history]
40
- #history.append("\n[|Human|]{}\n[|AI|]".format(text))
41
  history_text = ""
42
- #flag = False
43
- #for x in history[::-1]:
44
- #if tokenizer(prompt+history_text+x, return_tensors="pt")['input_ids'].size(-1) <= max_length:
45
- history_text = prompt + history_text
46
- flag = True
47
-
 
48
  if flag:
49
  return prompt+history_text,tokenizer(prompt+history_text, return_tensors="pt")
50
  else:
 
21
  from transformers import AutoTokenizer, AutoModelForCausalLM
22
  import datasets
23
  from datasets import load_dataset
24
+ import evaluate
25
 
26
 
27
  def is_stop_word_or_prefix(s: str, stop_words: list) -> bool:
 
33
  return True
34
  return False
35
 
36
+ def generate_prompt_with_history(text, history, tokenizer, max_length=2048):
37
  #prompt = "The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. Human statements start with [|Human|] and AI assistant statements start with [|AI|]. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format.\n[|Human|]Hello!\n[|AI|]Hi!"
38
  prompt = "Das folgende ist eine Unterhaltung zwischen einem Menschen und einem KI-Assistenten, der Baize genannt wird. Baize ist ein open-source KI-Assistent, der von UCSD entwickelt wurde. Der Mensch und der KI-Assistent chatten abwechselnd miteinander in deutsch. Die Antworten des KI Assistenten sind immer so ausführlich wie möglich und in Markdown Schreibweise und in deutscher Sprache. Wenn nötig übersetzt er sie ins Deutsche. Die Antworten des KI-Assistenten vermeiden Themen und Antworten zu unethischen, kontroversen oder sensiblen Themen. Die Antworten sind immer sehr höflich formuliert..\n[|Human|]Hallo!\n[|AI|]Hi!"
39
+ history = ["\n[|Human|]{}\n[|AI|]{}".format(x[0],x[1]) for x in history]
40
+ history.append("\n[|Human|]{}\n[|AI|]".format(text))
 
41
  history_text = ""
42
+ flag = False
43
+ for x in history[::-1]:
44
+ if tokenizer(prompt+history_text+x, return_tensors="pt")['input_ids'].size(-1) <= max_length:
45
+ history_text = x + history_text
46
+ flag = True
47
+ else:
48
+ break
49
  if flag:
50
  return prompt+history_text,tokenizer(prompt+history_text, return_tensors="pt")
51
  else: