jonpreamble commited on
Commit
c7a90d5
·
1 Parent(s): bd049e0

Unit testing & prompt tuning

Browse files
Files changed (4) hide show
  1. app.py +1 -1
  2. decider_questions.py +1 -1
  3. decider_utils.py +25 -2
  4. run_unit_tests.py +42 -13
app.py CHANGED
@@ -85,7 +85,7 @@ def run_1_game_turn(s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_u
85
  elif decider_utils.yesno(QUESTION_IS_USER_ENGAGED_WITH_BANDITS, s_narr_transcript, default=NO) and decider_utils.special_case_is_running_away(s_user_input):
86
  finally_add2_both_tscripts += "Invalid entry. You cannot outrun these bandits.\n"
87
 
88
- elif decider_utils.yesno(QUESTION_IS_ACTION_MAGIC, s_user_input, default=NO):
89
  finally_add2_both_tscripts += "Invalid entry. You are not a spellcaster and have no magic items except your revolver.\n"
90
 
91
  else:
 
85
  elif decider_utils.yesno(QUESTION_IS_USER_ENGAGED_WITH_BANDITS, s_narr_transcript, default=NO) and decider_utils.special_case_is_running_away(s_user_input):
86
  finally_add2_both_tscripts += "Invalid entry. You cannot outrun these bandits.\n"
87
 
88
+ elif decider_utils.special_case_is_magic(s_user_input):
89
  finally_add2_both_tscripts += "Invalid entry. You are not a spellcaster and have no magic items except your revolver.\n"
90
 
91
  else:
decider_questions.py CHANGED
@@ -3,5 +3,5 @@ QUESTION_DOES_USER_STILL_HAVE_AT_LEAST_30_GOLD = "At the end of the above story,
3
  QUESTION_IS_USER_ENGAGED_WITH_BANDITS = "At the end of the above story, is the protagonist currently still engaged in a standoff with bandits?"
4
  QUESTION_IS_ACTION_LIKELY_LETHAL = "Is the action just described likely to result in anyone dying?"
5
  QUESTION_IS_ACTION_RUNNING_AWAY = "Does that sentence describe the act of fleeing?"
6
- QUESTION_IS_ACTION_MAGIC = "Is the action just described an example of using supernatural magical spells / potions / etc?"
7
  QUESTION_DID_PROTAGONIST_KILL = "In the story segment above, did the protagonist kill anyone?"
 
3
  QUESTION_IS_USER_ENGAGED_WITH_BANDITS = "At the end of the above story, is the protagonist currently still engaged in a standoff with bandits?"
4
  QUESTION_IS_ACTION_LIKELY_LETHAL = "Is the action just described likely to result in anyone dying?"
5
  QUESTION_IS_ACTION_RUNNING_AWAY = "Does that sentence describe the act of fleeing?"
6
+ QUESTION_IS_ACTION_MAGIC = "Yes or no: Does the previous sentence involve magic powers or items?"
7
  QUESTION_DID_PROTAGONIST_KILL = "In the story segment above, did the protagonist kill anyone?"
decider_utils.py CHANGED
@@ -4,10 +4,16 @@ import decider_questions
4
  YES = True
5
  NO = False
6
 
 
7
 
8
  def yesno(question, text, default):
 
 
9
  prompt = text + "\n\n" + question
10
- print(prompt)
 
 
 
11
  hopefully_word_yes_or_no = openai.Completion.create(
12
  engine="text-davinci-002",
13
  prompt=prompt,
@@ -17,7 +23,10 @@ def yesno(question, text, default):
17
  presence_penalty=0,
18
  n=1,
19
  )["choices"][0]["text"]
20
- print(hopefully_word_yes_or_no)
 
 
 
21
  hopefully_word_yes_or_no = hopefully_word_yes_or_no.upper().strip()
22
 
23
  result = default
@@ -42,8 +51,22 @@ def special_case_is_running_away(text):
42
  for keyword in ["run", "away", "hide", "escape", "flee", "sprint", "teleport"]:
43
  if keyword in text.lower():
44
  might_really_be_fleeing = True
 
45
 
46
  if might_really_be_fleeing:
47
  return yesno(decider_questions.QUESTION_IS_ACTION_RUNNING_AWAY, text, default=NO)
48
  else:
49
  return NO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  YES = True
5
  NO = False
6
 
7
+ g_decider_utils_dbg_printing = False
8
 
9
  def yesno(question, text, default):
10
+ global g_decider_utils_dbg_printing
11
+
12
  prompt = text + "\n\n" + question
13
+
14
+ if g_decider_utils_dbg_printing:
15
+ print(prompt)
16
+
17
  hopefully_word_yes_or_no = openai.Completion.create(
18
  engine="text-davinci-002",
19
  prompt=prompt,
 
23
  presence_penalty=0,
24
  n=1,
25
  )["choices"][0]["text"]
26
+
27
+ if g_decider_utils_dbg_printing:
28
+ print(hopefully_word_yes_or_no)
29
+
30
  hopefully_word_yes_or_no = hopefully_word_yes_or_no.upper().strip()
31
 
32
  result = default
 
51
  for keyword in ["run", "away", "hide", "escape", "flee", "sprint", "teleport"]:
52
  if keyword in text.lower():
53
  might_really_be_fleeing = True
54
+ break
55
 
56
  if might_really_be_fleeing:
57
  return yesno(decider_questions.QUESTION_IS_ACTION_RUNNING_AWAY, text, default=NO)
58
  else:
59
  return NO
60
+
61
+ def special_case_is_magic(text):
62
+ is_magic = False
63
+ for keyword in ["magic", "spell", "fly", "invisib", "levitat", "teleport", "dragon", "genie", "fairy", "demon", "devil", "angel", "griffin", "wand"]:
64
+ if keyword in text.lower():
65
+ is_magic = True
66
+ break
67
+
68
+ if is_magic:
69
+ return YES
70
+ else:
71
+ return yesno(decider_questions.QUESTION_IS_ACTION_MAGIC, text, default=NO)
72
+
run_unit_tests.py CHANGED
@@ -1,24 +1,53 @@
1
  #! /usr/bin/env python3
2
 
3
- import os
4
  import openai
 
 
 
5
 
6
  openai.organization = os.environ.get("OPENAI_ORGANIZATION")
7
  openai.api_key = os.environ.get("OPENAI_KEY")
8
 
 
9
 
10
- from decider_utils import YES, NO, yesno, special_case_is_running_away
11
- from decider_questions import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
 
 
 
 
 
13
 
14
- assert YES == special_case_is_running_away("run away")
15
- assert YES == special_case_is_running_away("I run away")
16
- assert YES == special_case_is_running_away("flee")
17
- assert YES == special_case_is_running_away("I flee.")
18
 
19
- assert NO == special_case_is_running_away("A")
20
- assert NO == special_case_is_running_away("B")
21
- assert NO == special_case_is_running_away("C")
22
- assert NO == special_case_is_running_away("ok")
23
- assert NO == special_case_is_running_away("say ok")
24
- assert NO == special_case_is_running_away("say okay")
 
1
  #! /usr/bin/env python3
2
 
3
+ import os, sys
4
  import openai
5
+ import decider_utils
6
+ from decider_utils import YES, NO, yesno
7
+ from decider_questions import *
8
 
9
  openai.organization = os.environ.get("OPENAI_ORGANIZATION")
10
  openai.api_key = os.environ.get("OPENAI_KEY")
11
 
12
+ decider_utils.g_decider_utils_dbg_printing = True
13
 
14
+ # Begin tests:
15
+
16
+ assert YES == decider_utils.special_case_is_magic("I fly straight up")
17
+ assert YES == decider_utils.special_case_is_magic("fly up")
18
+ assert YES == decider_utils.special_case_is_magic("turn invisible")
19
+ assert YES == decider_utils.special_case_is_magic("i turn invisible")
20
+ assert YES == decider_utils.special_case_is_magic("i teleport")
21
+ assert YES == decider_utils.special_case_is_magic("I summon a genie")
22
+ assert YES == decider_utils.special_case_is_magic("I summon an angel")
23
+ assert YES == decider_utils.special_case_is_magic("I throw some magic powder")
24
+ assert YES == decider_utils.special_case_is_magic("I start levitating")
25
+ assert YES == decider_utils.special_case_is_magic("I start levitating")
26
+ assert YES == decider_utils.special_case_is_magic("I say wingardium leviosa")
27
+
28
+ assert NO == decider_utils.special_case_is_magic("I hide behind a rock")
29
+ assert NO == decider_utils.special_case_is_magic("I hide behind a tree")
30
+ assert NO == decider_utils.special_case_is_magic("I hide behind a boulder")
31
+ assert NO == decider_utils.special_case_is_magic("I attempt to bargain")
32
+ assert NO == decider_utils.special_case_is_magic("I start tap dancing")
33
+ assert NO == decider_utils.special_case_is_magic("I say no")
34
+ assert NO == decider_utils.special_case_is_magic("I say ok fine")
35
+ assert NO == decider_utils.special_case_is_magic("I fire my revolver")
36
+ assert NO == decider_utils.special_case_is_magic("I fire my pistol")
37
+
38
+
39
+ assert YES == decider_utils.special_case_is_running_away("run away")
40
+ assert YES == decider_utils.special_case_is_running_away("I run away")
41
+ assert YES == decider_utils.special_case_is_running_away("flee")
42
+ assert YES == decider_utils.special_case_is_running_away("I flee.")
43
 
44
+ assert NO == decider_utils.special_case_is_running_away("A")
45
+ assert NO == decider_utils.special_case_is_running_away("B")
46
+ assert NO == decider_utils.special_case_is_running_away("C")
47
+ assert NO == decider_utils.special_case_is_running_away("ok")
48
+ assert NO == decider_utils.special_case_is_running_away("say ok")
49
+ assert NO == decider_utils.special_case_is_running_away("say okay")
50
 
 
 
 
 
51
 
52
+ print("All tests passed.")
53
+ sys.exit(0)