Spaces:
Runtime error
Runtime error
niuyazhe
commited on
Commit
•
f18dd2a
1
Parent(s):
bf99ea0
feature(nyz): add level1 questions
Browse files- app.py +20 -4
- llmriddles/questions/executor.py +3 -3
- llmriddles/questions/level1.py +184 -40
app.py
CHANGED
@@ -12,7 +12,15 @@ _LANG = os.environ.get('QUESTION_LANG', 'cn')
|
|
12 |
_LLM = os.environ.get('QUESTION_LLM', 'chatgpt')
|
13 |
|
14 |
if _LANG == "cn":
|
15 |
-
requirement_ph = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
requirement_label = "游戏须知"
|
17 |
question_ph = "你对大语言模型的提问"
|
18 |
question_label = "提问栏"
|
@@ -30,7 +38,15 @@ if _LANG == "cn":
|
|
30 |
api_error_info = "请在提交问题之前先输入你的 API Key"
|
31 |
try_again_label = "再玩一次"
|
32 |
elif _LANG == "en":
|
33 |
-
requirement_ph =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
requirement_label = "Requirements"
|
35 |
question_ph = "Your Question for LLM"
|
36 |
question_label = "Question"
|
@@ -69,7 +85,7 @@ if __name__ == '__main__':
|
|
69 |
gr_requirement = gr.TextArea(placeholder=requirement_ph, label=requirement_label)
|
70 |
gr_question = gr.TextArea(placeholder=question_ph, label=question_label)
|
71 |
gr_answer = gr.TextArea(placeholder=answer_ph, label=answer_label)
|
72 |
-
gr_submit = gr.Button(submit_label, interactive=
|
73 |
|
74 |
with gr.Column():
|
75 |
gr_api_key = gr.Text(placeholder=api_ph, label=api_label, type='password',
|
@@ -91,7 +107,7 @@ if __name__ == '__main__':
|
|
91 |
if _qid >= len(_QUESTIONS):
|
92 |
del _QUESTION_IDS[uuid_]
|
93 |
return game_cleared_label, '', '', {}, '', \
|
94 |
-
gr.Button(submit_label, interactive=
|
95 |
gr.Button(try_again_label, interactive=True), \
|
96 |
''
|
97 |
else:
|
|
|
12 |
_LLM = os.environ.get('QUESTION_LLM', 'chatgpt')
|
13 |
|
14 |
if _LANG == "cn":
|
15 |
+
requirement_ph = """
|
16 |
+
欢迎来玩LLM Riddles!
|
17 |
+
|
18 |
+
你将通过本游戏对语言大模型产生更深刻的理解。
|
19 |
+
|
20 |
+
在本游戏中,你需要构造一个提给一个语言大模型的问题,使得它回复的答案符合要求。
|
21 |
+
|
22 |
+
点击\"下一题\"开始游戏
|
23 |
+
"""
|
24 |
requirement_label = "游戏须知"
|
25 |
question_ph = "你对大语言模型的提问"
|
26 |
question_label = "提问栏"
|
|
|
38 |
api_error_info = "请在提交问题之前先输入你的 API Key"
|
39 |
try_again_label = "再玩一次"
|
40 |
elif _LANG == "en":
|
41 |
+
requirement_ph = """
|
42 |
+
Welcome to LLM Riddles!
|
43 |
+
|
44 |
+
In this game, you'll gain a deeper understanding of language models.
|
45 |
+
|
46 |
+
Your challenge is to create a question to ask a language model in a way that the answer it provides meets specific criteria.
|
47 |
+
|
48 |
+
Click \'Next\' to Start
|
49 |
+
"""
|
50 |
requirement_label = "Requirements"
|
51 |
question_ph = "Your Question for LLM"
|
52 |
question_label = "Question"
|
|
|
85 |
gr_requirement = gr.TextArea(placeholder=requirement_ph, label=requirement_label)
|
86 |
gr_question = gr.TextArea(placeholder=question_ph, label=question_label)
|
87 |
gr_answer = gr.TextArea(placeholder=answer_ph, label=answer_label)
|
88 |
+
gr_submit = gr.Button(submit_label, interactive=True)
|
89 |
|
90 |
with gr.Column():
|
91 |
gr_api_key = gr.Text(placeholder=api_ph, label=api_label, type='password',
|
|
|
107 |
if _qid >= len(_QUESTIONS):
|
108 |
del _QUESTION_IDS[uuid_]
|
109 |
return game_cleared_label, '', '', {}, '', \
|
110 |
+
gr.Button(submit_label, interactive=True), \
|
111 |
gr.Button(try_again_label, interactive=True), \
|
112 |
''
|
113 |
else:
|
llmriddles/questions/executor.py
CHANGED
@@ -17,11 +17,11 @@ class QuestionExecutor:
|
|
17 |
|
18 |
def check(self, qs_text: str) -> Tuple[str, bool, str]:
|
19 |
answer_text = get_llm_fn(self.llm)(qs_text, **self.llm_cfgs)
|
20 |
-
correct, explanation = self.check_answer(answer_text)
|
21 |
return answer_text, correct, explanation
|
22 |
|
23 |
-
def check_answer(self, answer_text: str) -> Tuple[bool, str]:
|
24 |
-
correct, explanation = self.question.checker(self.question_text, answer_text, self.lang)
|
25 |
if explanation is None:
|
26 |
if correct:
|
27 |
explanation = 'LLM的回答满足要求' if self.lang == 'cn' else 'Correct Answer From LLM'
|
|
|
17 |
|
18 |
def check(self, qs_text: str) -> Tuple[str, bool, str]:
|
19 |
answer_text = get_llm_fn(self.llm)(qs_text, **self.llm_cfgs)
|
20 |
+
correct, explanation = self.check_answer(qs_text, answer_text)
|
21 |
return answer_text, correct, explanation
|
22 |
|
23 |
+
def check_answer(self, user_text: str, answer_text: str) -> Tuple[bool, str]:
|
24 |
+
correct, explanation = self.question.checker(self.question_text, user_text, answer_text, self.lang)
|
25 |
if explanation is None:
|
26 |
if correct:
|
27 |
explanation = 'LLM的回答满足要求' if self.lang == 'cn' else 'Correct Answer From LLM'
|
llmriddles/questions/level1.py
CHANGED
@@ -1,40 +1,184 @@
|
|
1 |
-
from .question import register_question
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
if
|
32 |
-
return
|
33 |
-
else:
|
34 |
-
return
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .question import register_question
|
2 |
+
|
3 |
+
def count_english_words(text: str):
|
4 |
+
return len(text.split(' '))
|
5 |
+
|
6 |
+
def count_chinese_words(text: str):
|
7 |
+
return len(text)
|
8 |
+
|
9 |
+
def check_if_chinese(text: str):
|
10 |
+
return all('\u4e00' <= char <= '\u9fff' for char in text)
|
11 |
+
|
12 |
+
def count_words(text: str, contain_punctuation: bool = False):
|
13 |
+
chinese_words = []
|
14 |
+
english_words = []
|
15 |
+
other_words = []
|
16 |
+
temp_english_words = []
|
17 |
+
for char in text:
|
18 |
+
if '\u4e00' <= char <= '\u9fff':
|
19 |
+
chinese_words.append(char)
|
20 |
+
if len(temp_english_words) > 0:
|
21 |
+
english_words.append(''.join(temp_english_words))
|
22 |
+
temp_english_words = []
|
23 |
+
else:
|
24 |
+
if char.isalpha():
|
25 |
+
temp_english_words.append(char)
|
26 |
+
else:
|
27 |
+
if len(temp_english_words) > 0:
|
28 |
+
english_words.append(''.join(temp_english_words))
|
29 |
+
temp_english_words = []
|
30 |
+
other_words.append(char)
|
31 |
+
if contain_punctuation:
|
32 |
+
return len(chinese_words)+len(english_words)+len(other_words)
|
33 |
+
else:
|
34 |
+
return len(chinese_words)+len(english_words)
|
35 |
+
|
36 |
+
|
37 |
+
CN_TEXT_1 = """
|
38 |
+
第一章第一题(初来乍到),请你构造一个问题使模型的回答是一字不差的“1+1=3”(不需要引号)。
|
39 |
+
|
40 |
+
请在下面的输入框内填写你构造并点击按钮提交。
|
41 |
+
"""
|
42 |
+
|
43 |
+
EN_TEXT_1 = """
|
44 |
+
For the first question in chapter 1, please construct a query for the model that will result in the exact answer "1+1=3" (no quotes needed).
|
45 |
+
|
46 |
+
Please enter your query below and click the submit button
|
47 |
+
"""
|
48 |
+
|
49 |
+
|
50 |
+
def _checker_1(question_text: str, user_text: str, answer_text: str, lang: str):
|
51 |
+
_ = question_text, lang
|
52 |
+
answer_text = answer_text.strip()
|
53 |
+
user_text = user_text.strip()
|
54 |
+
if answer_text == '1+1=3':
|
55 |
+
return True, None
|
56 |
+
else:
|
57 |
+
return False, None
|
58 |
+
|
59 |
+
|
60 |
+
register_question({
|
61 |
+
'cn': CN_TEXT_1,
|
62 |
+
'en': EN_TEXT_1,
|
63 |
+
}, _checker_1)
|
64 |
+
|
65 |
+
|
66 |
+
CN_TEXT_2 = """
|
67 |
+
第一章第二题(小试牛刀),请你输入三个字(及)以内的问题,使模型的回答在30个字以上。
|
68 |
+
|
69 |
+
请在下面的输入框内填写你的问题并点击按钮提交。
|
70 |
+
"""
|
71 |
+
|
72 |
+
EN_TEXT_2 = """
|
73 |
+
For the second question in chapter 1, please enter a question within three words so that the model’s answer is more than 30 words.
|
74 |
+
|
75 |
+
Please enter your query below and click the submit button
|
76 |
+
"""
|
77 |
+
|
78 |
+
|
79 |
+
def _checker_2(question_text: str, user_text: str, answer_text: str, lang: str):
|
80 |
+
_ = question_text, lang
|
81 |
+
answer_text = answer_text.strip()
|
82 |
+
user_text = user_text.strip()
|
83 |
+
|
84 |
+
if count_words(user_text) > 3:
|
85 |
+
return False, "用户的问题长度应该在三个字及以内" if lang == 'cn' else 'Question should be within three words.'
|
86 |
+
elif count_words(answer_text) <= 30:
|
87 |
+
return False, "大语言模型的答案应该超过30个字" if lang == 'cn' else 'cAnswer should be more than 30 words.'
|
88 |
+
else:
|
89 |
+
return True, None
|
90 |
+
|
91 |
+
|
92 |
+
register_question({
|
93 |
+
'cn': CN_TEXT_2,
|
94 |
+
'en': EN_TEXT_2,
|
95 |
+
}, _checker_2)
|
96 |
+
|
97 |
+
|
98 |
+
CN_TEXT_3 = """
|
99 |
+
第一章第三题(短说长话),请你输入一个字的问题,使模型的回答在100个字以上。
|
100 |
+
|
101 |
+
请在下面的输入框内填写你的问题并点击按钮提交。
|
102 |
+
"""
|
103 |
+
|
104 |
+
EN_TEXT_3 = """
|
105 |
+
For the third question in chapter 1, please enter a one-word question so that the model’s answer is more than 100 words.
|
106 |
+
|
107 |
+
Please enter your query below and click the submit button
|
108 |
+
"""
|
109 |
+
|
110 |
+
def _checker_3(question_text: str, user_text: str, answer_text: str, lang: str):
|
111 |
+
_ = question_text, lang
|
112 |
+
answer_text = answer_text.strip()
|
113 |
+
user_text = user_text.strip()
|
114 |
+
|
115 |
+
if count_words(user_text) > 1:
|
116 |
+
return False, "用户的问题长度应该在一个字及以内" if lang == 'cn' else 'Question should be one word.'
|
117 |
+
elif count_words(answer_text) <= 100:
|
118 |
+
return False, "大语言模型的答案应该超过100个字" if lang == 'cn' else 'Answer should be more than 100 words.'
|
119 |
+
else:
|
120 |
+
return True, None
|
121 |
+
|
122 |
+
register_question({
|
123 |
+
'cn': CN_TEXT_3,
|
124 |
+
'en': EN_TEXT_3,
|
125 |
+
}, _checker_3)
|
126 |
+
|
127 |
+
|
128 |
+
CN_TEXT_4 = """
|
129 |
+
第一章第四题(短说短话),请输入一个字的问题,使模型的回答字数小于20个字。
|
130 |
+
|
131 |
+
请在下面的输入框内填写你的问题并点击按钮提交。
|
132 |
+
"""
|
133 |
+
|
134 |
+
EN_TEXT_4 = """
|
135 |
+
For the fourth question in chapter 1, please enter a one-word question so that the model’s answer is less than 20 words.
|
136 |
+
|
137 |
+
Please enter your query below and click the submit button
|
138 |
+
"""
|
139 |
+
|
140 |
+
def _checker_4(question_text: str, user_text: str, answer_text: str, lang: str):
|
141 |
+
_ = question_text, lang
|
142 |
+
answer_text = answer_text.strip()
|
143 |
+
user_text = user_text.strip()
|
144 |
+
|
145 |
+
if count_words(user_text) > 1:
|
146 |
+
return False, "用户的问题长度应该在一个字及以内" if lang == 'cn' else 'Question should be one word.'
|
147 |
+
elif count_words(answer_text) >= 20:
|
148 |
+
return False, "大语言模型的答案应该小于100个字" if lang == 'cn' else 'Answer should be less than 20 words.'
|
149 |
+
else:
|
150 |
+
return True, None
|
151 |
+
|
152 |
+
register_question({
|
153 |
+
'cn': CN_TEXT_4,
|
154 |
+
'en': EN_TEXT_4,
|
155 |
+
}, _checker_4)
|
156 |
+
|
157 |
+
|
158 |
+
# CN_TEXT_5 = """
|
159 |
+
# 第一章第五题(回文不变),请输入一个本身不是回文串的问题,使无论正着问还是倒着问,模型的回答是一样的。
|
160 |
+
|
161 |
+
# 请在下面的输入框内填写你的问题并点击按钮提交。
|
162 |
+
# """
|
163 |
+
|
164 |
+
# EN_TEXT_5 = """
|
165 |
+
# For the fourth question in chapter 1, please enter a question that is not a palindrome string so that the model's answer is the same whether it is asked forward or backward.
|
166 |
+
|
167 |
+
# Please enter your query below and click the submit button
|
168 |
+
# """
|
169 |
+
|
170 |
+
# def _checker_5(question_text: str, answer_text: str, lang: str):
|
171 |
+
# _ = question_text, lang
|
172 |
+
# answer_text = answer_text.strip()
|
173 |
+
|
174 |
+
# if count_words(question_text) > 0:
|
175 |
+
# return False, 'Question should be one word.'
|
176 |
+
# elif count_words(answer_text) >= 20:
|
177 |
+
# return False, 'Answer should be less than 20 words.'
|
178 |
+
# else:
|
179 |
+
# return True, None
|
180 |
+
|
181 |
+
# register_question({
|
182 |
+
# 'cn': CN_TEXT_5,
|
183 |
+
# 'en': EN_TEXT_5,
|
184 |
+
# }, _checker_5)
|