Skip to content

Commit

Permalink
➕ Further refined negation handling, and added incorrect explanations
Browse files Browse the repository at this point in the history
  • Loading branch information
beverage committed Nov 19, 2024
1 parent a1eb13c commit ef5dafe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
10 changes: 3 additions & 7 deletions src/dbtest/sentences/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ async def create_sentence(verb_infinitive: str,
sentence.indirect_pronoun = response_json["indirect_pronoun"]
sentence.reflexive_pronoun = "none" # Temporarily set this to none to not break things before removed, unless kept.

# These can happen if an incorrect answer is on the negation, which is fine, but will break the negation enum.
if is_correct is False and sentence.negation not in Negation.__members__:
sentence.negation = str(Negation.none)

# If a sentence is supposed to be correct, double check it, as the prompts to generate it are overly complicated right now.
if is_correct:

Expand Down Expand Up @@ -116,13 +112,13 @@ async def create_random_sentence(is_correct: bool=True, openai_client: AsyncChat
return await create_sentence("", "", "", # Verb, pronoun, and tense remain fully random for now.
direct_object = DirectObject.random,
indirect_pronoun = IndirectPronoun.random,
negation = Negation.random if random.randint(0, 1) == 1 else Negation.none,
negation = Negation.none if random.randint(0, 2) == 0 else Negation.random,
is_correct = is_correct,
openai_client = openai_client)

async def create_random_problem_with_delay(openai_client: AsyncChatGPTClient=AsyncChatGPTClient(), display=True):
await create_random_problem(openai_client=openai_client, display=display)
await sleep(random.uniform(0.5, 2.0))
await sleep(random.uniform(1.5, 2.0))

async def create_random_problem(openai_client: AsyncChatGPTClient=AsyncChatGPTClient(), display=False):

Expand All @@ -138,7 +134,7 @@ async def create_random_problem(openai_client: AsyncChatGPTClient=AsyncChatGPTCl
responses.append(await create_sentence("", "", "",
direct_object = DirectObject.random,
indirect_pronoun = IndirectPronoun.random,
negation = Negation.random,
negation = Negation.none if random.randint(0, 2) == 0 else Negation.random,
is_correct = i is answer))

except Exception as ex:
Expand Down
18 changes: 9 additions & 9 deletions src/dbtest/sentences/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def __pronoun_ordering(self):
return "If the sentence has a COD (direct object) and a COI (indirect pronoun), put them in the right order. Switch them if necessary."

def __negatedness(self, sentence):
if sentence.negation != "none":
if sentence.negation == "random":
return f"The sentance may randomly contain a negation from the list {' '.join([n.name for n in Negation])}, or no negation at all. 'Ne' must always come before any direct objects or indirect pronouns. The negation must come directly after the object."
else:
return f"The sentence must contain the negation {sentence.negation}. The sentence must always include 'ne'." if sentence.negation != "none" else "The sentence must not contain any negation. 'Ne' must always come before any direct objects or indirect pronouns. The negation must come directly after the object."
if sentence.negation != Negation.none:
return " ".join([
f"The sentence must contain the negation {sentence.negation}. " if sentence.negation != Negation.random else f"The sentance may randomly contain a negation from the list {' '.join([n.name for n in Negation if n != Negation.random])}, or no negation at all."
"'Ne' must always come before any direct objects or indirect pronouns. The negation must come directly after the object."
])
else:
return "The sentence must not contain a negation."

Expand All @@ -47,12 +47,12 @@ def __sentence_correctness(self, sentence):
if sentence.direct_object is DirectObject.none and sentence.indirect_pronoun is IndirectPronoun.none and sentence.negation is Negation.none:
return "The sentence must contain an error in its pronoun or verb conjugation."
else:
return "The sentence must contain an error in any of its direct objects, indirect pronouns, or negations."
return "The sentence must contain an error in any of its direct objects, indirect pronouns, negations, or prepositions, ordering."
else:
return "The sentence should be correctly formed."

def __translation(self, sentence):
return "The response should include an English translation." if sentence.is_correct else "The response should not include a translation."
return "The response should include an English translation." if sentence.is_correct else f"The 'translation' field should be a short reason why '{sentence.content}' is incorrect. Do not repeat '{sentence.content}' in the output. Return only the reason."

def __detect_negations(self):
# TODO: This needs to be smarter, and plug in supported negations directly.
Expand All @@ -71,13 +71,13 @@ def __json_format(self):
"""

def __set_negation_field(self, sentence):
return f"If the sentence contains a negation, set the negation field to that negation without the 'ne' prefix, or an n' prefix. If it is two words, only use the first. Otherwise set it to none. If {sentence.is_correct} it must contain 'ne' or 'n''."
return f"If the sentence contains a negation, set the 'negation' field in the response to that negation, without any 'ne', or 'n'' prefix. If the negation is two words, use only the first word. If the sentence is not negated, or 'False' is returned, set it to 'none'"

def __set_object_type_field(self, object_type, object_name):
return f"If the generated sentence has a {object_type}, set {object_name} to 'masculine if it is masculine', 'feminine' if it is feminine, or 'plural' if it is plural. Set it to 'none' if it does not have an {object_name}."

def __correct_elisions(self):
return "The sentence should have correct French elisions. This includes que and qui connectors."
return "The sentence should have correct French elisions. This includes que, and qui connectors. This includes 'n'' negations."

def __extra_rules(self):
# TODO: we should not have an oddly specific rule around the word 'random'. This is a hack to get around poor enum handling for now.
Expand Down
2 changes: 1 addition & 1 deletion src/dbtest/sentences/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def problem_formatter(sentences) -> str:
f"{Color.LIGHT_GRAY}{"COI" if sentence.indirect_pronoun != IndirectPronoun.none.name else "---"}{Style.RESET}",
f"{Color.LIGHT_GRAY}{"NEG" if sentence.negation != Negation.none.name else "---"}{Style.RESET}",
sentence.content,
f"{Color.BRIGHT_BLUE}({sentence.translation}){Style.RESET}" if sentence.is_correct else "",
f"{Color.BRIGHT_BLUE}({sentence.translation}){Style.RESET}" if sentence.is_correct else f"{Color.LIGHT_RED}({sentence.translation}){Style.RESET}",
'\n'])

return output
Expand Down
1 change: 1 addition & 0 deletions src/dbtest/utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Color(StrEnum):
BOLD_WHITE = f"\033[37m{Style.BOLD}"
BRIGHT_BLUE = '\033[94m'
LIGHT_GRAY = '\033[90m'
LIGHT_RED = '\033[91m'
STRONG_GREEN = '\033[38;5;46m'
STRONG_RED = '\033[38;5;196m'
WHITE = '\033[37m'
Expand Down

0 comments on commit ef5dafe

Please sign in to comment.