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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system('pip install dashscope')
|
| 3 |
+
os.system('pip install sympy')
|
| 4 |
+
os.system('pip install "gradio==3.40.1"')
|
| 5 |
+
import random
|
| 6 |
+
import re
|
| 7 |
+
from http import HTTPStatus
|
| 8 |
+
|
| 9 |
+
import dashscope
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import sympy
|
| 12 |
+
|
| 13 |
+
dashscope.api_key = os.getenv('DASHSCOPE_API_KEY')
|
| 14 |
+
|
| 15 |
+
# 定义关卡信息和验证逻辑
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# 辅助函数 - 检查是否为质数
|
| 19 |
+
def is_prime(num):
|
| 20 |
+
return sympy.isprime(num)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# 辅助函数 - 获取下一个质数
|
| 24 |
+
def next_prime(num):
|
| 25 |
+
return sympy.nextprime(num)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# 辅助函数 - 检查是否为平方数
|
| 29 |
+
def is_square(n):
|
| 30 |
+
return sympy.sqrt(n).is_integer
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# 辅助函数 - 获取平方根
|
| 34 |
+
def get_square_root(n):
|
| 35 |
+
return int(sympy.sqrt(n))
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def reverse_en(input):
|
| 39 |
+
return ' '.join(input.split()[::-1])
|
| 40 |
+
|
| 41 |
+
def validate_palindrome_invariance(origin_input, response):
|
| 42 |
+
"""
|
| 43 |
+
验证器函数,检查对于给定的问题,正着问和倒着问的模型回答是否一致。
|
| 44 |
+
|
| 45 |
+
:param origin_input: 原始问题
|
| 46 |
+
:return: True 如果回答一致,否则 False
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
# 将原始问题倒序并提交
|
| 50 |
+
reversed_question = reverse_en(origin_input)
|
| 51 |
+
reversed_response = generate_response(reversed_question)
|
| 52 |
+
|
| 53 |
+
# 比较两个回答是否一致
|
| 54 |
+
return response.strip() == reversed_response.strip()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def is_palindrome(sentence):
|
| 58 |
+
# Remove non-alphanumeric characters and convert to lowercase
|
| 59 |
+
cleaned_sentence = re.sub(r'[^A-Za-z0-9]', '', sentence.lower())
|
| 60 |
+
# Compare the cleaned sentence with its reverse
|
| 61 |
+
return cleaned_sentence == cleaned_sentence[::-1]
|
| 62 |
+
|
| 63 |
+
def validate_palindrome_inverse(origin_input, response):
|
| 64 |
+
"""
|
| 65 |
+
验证器函数,检查对于给定的问题,正着问和倒着问的模型的回答本身不回文且也是逆序的关系。
|
| 66 |
+
|
| 67 |
+
:param origin_input: 原始问题
|
| 68 |
+
:param response: 模型对原始问题的回答
|
| 69 |
+
:param model_fn: 能够返回模型回答的函数
|
| 70 |
+
:return: True 如果回答本身不回文且也是逆序关系,否则 False
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
# 检查原始输入不是回文
|
| 74 |
+
if origin_input == is_palindrome(origin_input):
|
| 75 |
+
return False
|
| 76 |
+
|
| 77 |
+
# 将原始问题倒序并获取模型回答
|
| 78 |
+
reversed_question = reverse_en(origin_input)
|
| 79 |
+
reversed_response = generate_response(reversed_question).strip()
|
| 80 |
+
|
| 81 |
+
# 检查原始回答不是回文
|
| 82 |
+
if response == is_palindrome(origin_input):
|
| 83 |
+
return False
|
| 84 |
+
|
| 85 |
+
# 比较正着问和倒着问的回答是否是逆序关系
|
| 86 |
+
return response == reverse_en(origin_input)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def validate_reciprocal_question(input, response):
|
| 90 |
+
"""
|
| 91 |
+
验证器函数,检查给定的问题A和回答B,是否能够通过以B作为新的提问得到原始问题A作为回答。
|
| 92 |
+
|
| 93 |
+
:param response: 模型对问题A的回答B
|
| 94 |
+
:param input: 原始问题A
|
| 95 |
+
:param model_fn: 模型调用函数,接收问题并返回回答
|
| 96 |
+
:return: True 如果以回答B作为新的提问能得到问题A作为回答,否则 False
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
# 确保回答B不等于原始问题A
|
| 100 |
+
if response == input:
|
| 101 |
+
return False
|
| 102 |
+
|
| 103 |
+
# 以回答B作为新的提问
|
| 104 |
+
new_response = generate_response(response)
|
| 105 |
+
|
| 106 |
+
# 检查新的回答是否与原始问题A相等
|
| 107 |
+
return new_response.strip() == input.strip()
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
challenges = [
|
| 111 |
+
{
|
| 112 |
+
'name':
|
| 113 |
+
'第一章 对话之趣',
|
| 114 |
+
'problems': [
|
| 115 |
+
{
|
| 116 |
+
'title': '第1题 初来乍到',
|
| 117 |
+
'description': '请你构造一个问题使模型的回答是一字不差的“1+1=3”(不需要引号)。',
|
| 118 |
+
'validator':
|
| 119 |
+
lambda response, input: response.strip() == '1+1=3'
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
'title':
|
| 123 |
+
'第2题 小试牛刀',
|
| 124 |
+
'description':
|
| 125 |
+
'请输入三个字以内的问题,使模型的回答在30个字以上。',
|
| 126 |
+
'validator':
|
| 127 |
+
lambda response, input: len(input) <= 3 and len(response) > 30
|
| 128 |
+
},
|
| 129 |
+
{
|
| 130 |
+
'title':
|
| 131 |
+
'第3题 短说长话',
|
| 132 |
+
'description':
|
| 133 |
+
'请输入一个字的问题,使模型的回答在100个字以上。',
|
| 134 |
+
'validator':
|
| 135 |
+
lambda response, input: len(input) == 1 and len(response) > 100
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
'title':
|
| 139 |
+
'第4题 短说短话',
|
| 140 |
+
'description':
|
| 141 |
+
'请输入一个字的问题,使模型的回答在20个字以内。',
|
| 142 |
+
'validator':
|
| 143 |
+
lambda response, input: len(input) == 1 and len(response) <= 20
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
'title':
|
| 147 |
+
'第5题 回文不变',
|
| 148 |
+
'description':
|
| 149 |
+
'请输入一个本身不是回文串的问题,使无论正着问还是倒着问,模型的回答是一样的。',
|
| 150 |
+
# 验证器将取决于模型的行为,这里只是一个占位符
|
| 151 |
+
'validator':
|
| 152 |
+
lambda response, input: validate_palindrome_invariance(
|
| 153 |
+
input, response)
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
'title':
|
| 157 |
+
'第6题 无中生狗',
|
| 158 |
+
'description':
|
| 159 |
+
'请提一个不包含“狗”这个字的问题,但是回答中至少出现3次“狗”这个字。',
|
| 160 |
+
'validator':
|
| 161 |
+
lambda response, input: '狗' not in input and response.count(
|
| 162 |
+
'狗') >= 3
|
| 163 |
+
},
|
| 164 |
+
]
|
| 165 |
+
},
|
| 166 |
+
{
|
| 167 |
+
'name':
|
| 168 |
+
'第二章 数字游戏',
|
| 169 |
+
'problems': [
|
| 170 |
+
{
|
| 171 |
+
'title':
|
| 172 |
+
'第1题 质数长度',
|
| 173 |
+
'description':
|
| 174 |
+
'你需要提出一个字数是质数的问题,使回答的长度刚好是它的下一个质数。',
|
| 175 |
+
'validator':
|
| 176 |
+
lambda response, input: is_prime(len(input)) and len(response)
|
| 177 |
+
== next_prime(len(input))
|
| 178 |
+
},
|
| 179 |
+
{
|
| 180 |
+
'title':
|
| 181 |
+
'第2题 越说越大',
|
| 182 |
+
'description':
|
| 183 |
+
'请输入一个大于一的正整数作为问题,使回答里包含至少比它大一千的数。',
|
| 184 |
+
'validator':
|
| 185 |
+
lambda response, input:
|
| 186 |
+
(input.isdigit() and int(input) > 1 and any(
|
| 187 |
+
int(num) > int(input) + 1000
|
| 188 |
+
for num in re.findall(r'\d+', response)))
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
'title':
|
| 192 |
+
'第3题 越说越小',
|
| 193 |
+
'description':
|
| 194 |
+
'请输入一个大于一的正整数作为问题,使回答里包含至少10个不同的至少比它小一千的自然数。',
|
| 195 |
+
'validator':
|
| 196 |
+
lambda response, input:
|
| 197 |
+
(input.isdigit() and int(input) > 1 and (len({
|
| 198 |
+
int(num)
|
| 199 |
+
for num in re.findall(r'\d+', response)
|
| 200 |
+
if int(num) < int(input) - 1000
|
| 201 |
+
}) >= 10))
|
| 202 |
+
},
|
| 203 |
+
{
|
| 204 |
+
'title':
|
| 205 |
+
'第4题 警世名言',
|
| 206 |
+
'description':
|
| 207 |
+
'请在不提及1,4,5这三个字符的情况下让模型回答114514,不要有多余的字符。',
|
| 208 |
+
'validator':
|
| 209 |
+
lambda response, input: response.strip() == '114514' and all(
|
| 210 |
+
c not in input for c in ['1', '4', '5'])
|
| 211 |
+
},
|
| 212 |
+
{
|
| 213 |
+
'title':
|
| 214 |
+
'第5题 开平方数',
|
| 215 |
+
'description':
|
| 216 |
+
'请输入一个大于一的平方数作为问题,使回答里包含它的平方根对应的数。',
|
| 217 |
+
'validator':
|
| 218 |
+
lambda response, input:
|
| 219 |
+
(input.isdigit() and int(input) > 1 and is_square(int(input))
|
| 220 |
+
and str(get_square_root(int(input))) in response)
|
| 221 |
+
},
|
| 222 |
+
{
|
| 223 |
+
'title':
|
| 224 |
+
'第6题 得寸进狗',
|
| 225 |
+
'description':
|
| 226 |
+
'请提一个不包含“狗”这个字的10个字以内的问题,使回答中“狗”这个字出现的次数至少是问题字数的2倍。',
|
| 227 |
+
'validator':
|
| 228 |
+
lambda response, input: '狗' not in input and len(input) <= 10
|
| 229 |
+
and response.count('狗') >= 2 * len(input)
|
| 230 |
+
},
|
| 231 |
+
]
|
| 232 |
+
},
|
| 233 |
+
{
|
| 234 |
+
'name':
|
| 235 |
+
'第三章 巅峰挑战',
|
| 236 |
+
'problems': [
|
| 237 |
+
{
|
| 238 |
+
'title':
|
| 239 |
+
'第1题 自说自话',
|
| 240 |
+
'description':
|
| 241 |
+
'请做出一个至少10个字的提问,使回答和提问完全一样。',
|
| 242 |
+
'validator':
|
| 243 |
+
lambda response, input: input == response and len(input) >= 10
|
| 244 |
+
},
|
| 245 |
+
{
|
| 246 |
+
'title':
|
| 247 |
+
'第2题 话自说自',
|
| 248 |
+
'description':
|
| 249 |
+
'请做出一个至少10个字的提问,使回答是提问的逆序。',
|
| 250 |
+
'validator':
|
| 251 |
+
lambda response, input: input[::-1] == response and len(input)
|
| 252 |
+
>= 10
|
| 253 |
+
},
|
| 254 |
+
{
|
| 255 |
+
'title':
|
| 256 |
+
'第3题 就是敢说',
|
| 257 |
+
'description':
|
| 258 |
+
'请在不提及1,3这三个字符的情况下让模型回答1+1=3,不要有多余的字符。',
|
| 259 |
+
'validator':
|
| 260 |
+
lambda response, input: response.strip() == '1+1=3' and all(
|
| 261 |
+
c not in input for c in ['1', '3'])
|
| 262 |
+
},
|
| 263 |
+
{
|
| 264 |
+
'title':
|
| 265 |
+
'第4题 回文协变',
|
| 266 |
+
'description':
|
| 267 |
+
'请输入一个本身不是回文串的问题,使正着问和倒着问的时候,模型的回答本身不回文且也是逆序的关系。',
|
| 268 |
+
'validator':
|
| 269 |
+
lambda response, input: validate_palindrome_inverse(
|
| 270 |
+
input, response)
|
| 271 |
+
},
|
| 272 |
+
]
|
| 273 |
+
},
|
| 274 |
+
{
|
| 275 |
+
'name':
|
| 276 |
+
'第四章 无人之境',
|
| 277 |
+
'problems': [
|
| 278 |
+
{
|
| 279 |
+
'title':
|
| 280 |
+
'第1题 循环往复',
|
| 281 |
+
'description':
|
| 282 |
+
'请构造一个问题A,它的回复是不同于A的B,然后以B提问能再次得到A.',
|
| 283 |
+
'validator':
|
| 284 |
+
lambda response, input: validate_reciprocal_question(
|
| 285 |
+
input, response)
|
| 286 |
+
},
|
| 287 |
+
{
|
| 288 |
+
'title':
|
| 289 |
+
'第2题 惜字如金',
|
| 290 |
+
'description':
|
| 291 |
+
'请输入一个字的问题,使模型的回答在16个字以内。',
|
| 292 |
+
'validator':
|
| 293 |
+
lambda response, input: len(input) == 1 and len(response) <= 16
|
| 294 |
+
},
|
| 295 |
+
{
|
| 296 |
+
'title':
|
| 297 |
+
'第3题 自然之密',
|
| 298 |
+
'description':
|
| 299 |
+
'请输入一个大于一的正整数作为问题,使回答里包含和它刚好相差1的数。',
|
| 300 |
+
'validator':
|
| 301 |
+
lambda response, input:
|
| 302 |
+
(input.isdigit() and int(input) > 1 and any(
|
| 303 |
+
abs(int(num) - int(input)) == 1
|
| 304 |
+
for num in re.findall(r'\d+', response)))
|
| 305 |
+
},
|
| 306 |
+
]
|
| 307 |
+
}
|
| 308 |
+
]
|
| 309 |
+
|
| 310 |
+
challenges = [
|
| 311 |
+
{
|
| 312 |
+
"name": "Chapter 1. Joy of Conversations",
|
| 313 |
+
"problems": [
|
| 314 |
+
{
|
| 315 |
+
"title": "Question 1. First Trial",
|
| 316 |
+
"description": "Construct a question that elicit the LLM to respond with answer '1+1=3' (without quotes), word for word.",
|
| 317 |
+
"validator": lambda response, input: response.strip() == "1+1=3"
|
| 318 |
+
},
|
| 319 |
+
{
|
| 320 |
+
"title": "Question 2. Let's get into it",
|
| 321 |
+
"description": "Input a question with three words or less, and have the LLM respond with over 30 words.",
|
| 322 |
+
"validator": lambda response, input: len(input.split())<= 3 and len(response.split()) > 30
|
| 323 |
+
},
|
| 324 |
+
{
|
| 325 |
+
"title": "Question 3. Long story short",
|
| 326 |
+
"description": "Input a one-word question that prompts a response from the LLM exceeding 100 words.",
|
| 327 |
+
"validator": lambda response, input: len(input.split()) == 1 and len(response.split()) > 100
|
| 328 |
+
},
|
| 329 |
+
{
|
| 330 |
+
"title": "Question 4. Short for short",
|
| 331 |
+
"description": "Input a one-word question that elicits a response from the LLM that is within 20 words.",
|
| 332 |
+
"validator": lambda response, input: len(input.split()) == 1 and len(response.split()) <= 20
|
| 333 |
+
},
|
| 334 |
+
{
|
| 335 |
+
"title": "Question 5. Not so Palindrome",
|
| 336 |
+
"description": "Input a non-palindrome question such that regardless of whether it is asked forwards or backwards, the LLM's response remains unchanged.",
|
| 337 |
+
"validator": lambda response, input: validate_palindrome_invariance(input, response)
|
| 338 |
+
},
|
| 339 |
+
{
|
| 340 |
+
"title": "Question 6.Dogs out of nothing",
|
| 341 |
+
"description": "Given a question that does not contain the word 'dog,' that elicit LLM to respond with answer that includes the word 'dog' at least three times.",
|
| 342 |
+
"validator": lambda response, input: "dog" not in input and response.count("dog") >= 3
|
| 343 |
+
},
|
| 344 |
+
]
|
| 345 |
+
},
|
| 346 |
+
|
| 347 |
+
{
|
| 348 |
+
"name": "Chapter 2. Game of Numbers",
|
| 349 |
+
"problems": [
|
| 350 |
+
{
|
| 351 |
+
"title": "Question 1. Length of Prime",
|
| 352 |
+
"description": "Craft a question with word count of a prime number, that prompts a response whose word count is exactly its succeeding prime number.",
|
| 353 |
+
"validator": lambda response, input: is_prime(len(input.split())) and len(response) == next_prime(len(input.split()))
|
| 354 |
+
},
|
| 355 |
+
{
|
| 356 |
+
"title": "Question 2. Larger as we speak",
|
| 357 |
+
"description": "Provie a positive integer greater than one as your question such that the response contains a number at least 1000 larger than the input number.",
|
| 358 |
+
"validator": lambda response, input: input.isdigit() and int(input) > 1 and any(int(num) > int(input) + 1000 for num in re.findall(r'\d+', response))
|
| 359 |
+
},
|
| 360 |
+
{
|
| 361 |
+
"title": "Question 3. Smaller as we speak",
|
| 362 |
+
"description": "Provide a positive integer greater than one as your question such that the response contains at least 10 distinct integers all smaller than the input by at least 1000.",
|
| 363 |
+
"validator": lambda response, input: input.isdigit() and int(input) > 1 and (len({int(num) for num in re.findall(r'\d+', response) if int(num) < int(input) - 1000}) >= 10)
|
| 364 |
+
},
|
| 365 |
+
{
|
| 366 |
+
"title": "Question 4. Words of Proverbs",
|
| 367 |
+
"description": "Prompt the LLM to output string '114514' excatly, without mentioning the characters or digits 1, 4, or 5.",
|
| 368 |
+
"validator": lambda response, input: response.strip() == "114514" and all(c not in input for c in ["1", "4", "5"])
|
| 369 |
+
},
|
| 370 |
+
{
|
| 371 |
+
"title": "Question 5. The square root ",
|
| 372 |
+
"description": "Enter a perfect square greater than one as prompt such that the LLM response includes the number corresponding to its square root.",
|
| 373 |
+
"validator": lambda response, input: input.isdigit() and int(input) > 1 and is_square(int(input)) and str(get_square_root(int(input))) in response
|
| 374 |
+
},
|
| 375 |
+
{
|
| 376 |
+
"title": "Question 6. Dog alert",
|
| 377 |
+
"description": "Craft a prompt less than 10-word long without the word 'dog', yet prompts a response where the word 'dog' appears at least twice the word-length of the question.",
|
| 378 |
+
"validator": lambda response, input: "dog" not in input and len(input.split()) <= 10 and response.count("dog") >= 2 * len(input.split())
|
| 379 |
+
},
|
| 380 |
+
]
|
| 381 |
+
},
|
| 382 |
+
{
|
| 383 |
+
"name": "Chapter 3. Top Challenges",
|
| 384 |
+
"problems": [
|
| 385 |
+
{
|
| 386 |
+
"title": "Question 1. Talk to me back",
|
| 387 |
+
"description": "Craft a question at least ten words long that prompts an identical response.",
|
| 388 |
+
"validator": lambda response, input: input == response and len(input.split()) >= 10
|
| 389 |
+
},
|
| 390 |
+
{
|
| 391 |
+
"title": "Question 2. Back me to talk",
|
| 392 |
+
"description": "Craft a question at least ten words long such that the response is the reverse-word-order of the question.",
|
| 393 |
+
"validator": lambda response, input: input[::-1] == response and len(input.split()) >= 10
|
| 394 |
+
},
|
| 395 |
+
{
|
| 396 |
+
"title": "Question 3. Dare to speak",
|
| 397 |
+
"description": "Prompt the LLM to output exactly '1 + 1 = 3' without mentioning the characters or/digits 1 or 3.",
|
| 398 |
+
"validator": lambda response, input: response.strip() == "1+1=3" and all(c not in input for c in ["1", "3"])
|
| 399 |
+
},
|
| 400 |
+
{
|
| 401 |
+
"title": "Question 4. Covariant Palindrome",
|
| 402 |
+
"description": "Craft a non-palindrome question such that when asked forward and backward, the model's response is not a palindrome, but is in reverse order.",
|
| 403 |
+
"validator": lambda response, input: validate_palindrome_inverse(input, response)
|
| 404 |
+
},
|
| 405 |
+
]
|
| 406 |
+
},
|
| 407 |
+
{
|
| 408 |
+
"name": "Chapter 4. No man's land",
|
| 409 |
+
"problems": [
|
| 410 |
+
{
|
| 411 |
+
"title": "Question 1. The reciprocal",
|
| 412 |
+
"description": "Construct a prompt A for which the response B differs from A, and such that posing B as prompt shall elicit A as response",
|
| 413 |
+
"validator": lambda response, input: validate_reciprocal_question(input, response)
|
| 414 |
+
},
|
| 415 |
+
{
|
| 416 |
+
"title": "Question 2. Word precious as gold ",
|
| 417 |
+
"description": "Craft a one-word question prompting a response of 16 words or less.",
|
| 418 |
+
"validator": lambda response, input: len(input.split()) == 1 and len(response.split()) <= 16
|
| 419 |
+
},
|
| 420 |
+
{
|
| 421 |
+
"title": "Question 2. Nature's myth ",
|
| 422 |
+
"description": "Enter a positive integer greater than one as prompt such that the response contains a number differing from it by exactly one.",
|
| 423 |
+
"validator": lambda response, input: input.isdigit() and int(input) > 1 and any(abs(int(num) - int(input)) == 1 for num in re.findall(r'\d+', response))
|
| 424 |
+
},
|
| 425 |
+
]
|
| 426 |
+
}
|
| 427 |
+
]
|
| 428 |
+
|
| 429 |
+
def test_valid():
|
| 430 |
+
for challenge in challenges:
|
| 431 |
+
for p in challenge['problems']:
|
| 432 |
+
val_fn = p['validator']
|
| 433 |
+
try:
|
| 434 |
+
val_fn('response', 'input')
|
| 435 |
+
except:
|
| 436 |
+
import traceback
|
| 437 |
+
traceback.print_exc()
|
| 438 |
+
print(p, 'failed')
|
| 439 |
+
|
| 440 |
+
def get_problem(challenge_idx, problem_idx):
|
| 441 |
+
problems = challenges[challenge_idx]['problems']
|
| 442 |
+
return problems[problem_idx]
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def update_challenge_info(current_chapter_index, current_challenge_index):
|
| 446 |
+
return get_problem(current_chapter_index,
|
| 447 |
+
current_challenge_index)['description']
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
def update_question_info(current_chapter_index, current_challenge_index):
|
| 451 |
+
global challenges
|
| 452 |
+
current_chapter = challenges[current_chapter_index]
|
| 453 |
+
challenge = get_problem(current_chapter_index, current_challenge_index)
|
| 454 |
+
question_info = f"""\n<center><font size=4>{current_chapter["name"]}""" \
|
| 455 |
+
f"""</center>\n\n <center><font size=3>{challenge["title"]}</center>"""
|
| 456 |
+
return question_info
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
def validate_challenge(response, input, state):
|
| 460 |
+
print('in validate_challenge')
|
| 461 |
+
assert 'current_chapter_index' in state, 'current_chapter_index not found in state'
|
| 462 |
+
assert 'current_challenge_index' in state, 'current_challenge_index not found in state'
|
| 463 |
+
current_chapter_index = state['current_chapter_index']
|
| 464 |
+
current_challenge_index = state['current_challenge_index']
|
| 465 |
+
# 获取当前章节
|
| 466 |
+
current_chapter = challenges[current_chapter_index]
|
| 467 |
+
# 获取当前挑战
|
| 468 |
+
challenge = current_chapter['problems'][current_challenge_index]
|
| 469 |
+
|
| 470 |
+
if challenge['validator'](response, input):
|
| 471 |
+
challenge_result = 'Challenge successful! Proceed to the next level.'
|
| 472 |
+
# 检查是否还有更多挑战在当前章节
|
| 473 |
+
if current_challenge_index < len(current_chapter['problems']) - 1:
|
| 474 |
+
# 移动到当前章节的下一个挑战
|
| 475 |
+
current_challenge_index += 1
|
| 476 |
+
else:
|
| 477 |
+
# 如果当前章节的挑战已经完成,移动到下一个章节
|
| 478 |
+
current_challenge_index = 0
|
| 479 |
+
if current_chapter_index < len(challenges) - 1:
|
| 480 |
+
current_chapter_index += 1
|
| 481 |
+
else:
|
| 482 |
+
challenge_result = 'All Challenges Completed!'
|
| 483 |
+
else:
|
| 484 |
+
challenge_result = 'challenge failed,please retry'
|
| 485 |
+
state['current_chapter_index'] = current_chapter_index
|
| 486 |
+
state['current_challenge_index'] = current_challenge_index
|
| 487 |
+
print('update state: ', state)
|
| 488 |
+
|
| 489 |
+
return challenge_result, \
|
| 490 |
+
update_question_info(current_chapter_index, current_challenge_index), \
|
| 491 |
+
update_challenge_info(current_chapter_index, current_challenge_index)
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
def generate_response(input):
|
| 495 |
+
messages = [{
|
| 496 |
+
'role': 'system',
|
| 497 |
+
'content': """You are a helpful assistant."""
|
| 498 |
+
}, {
|
| 499 |
+
'role': 'user',
|
| 500 |
+
'content': input
|
| 501 |
+
}]
|
| 502 |
+
response = dashscope.Generation.call(
|
| 503 |
+
model='qwen-max',
|
| 504 |
+
messages=messages,
|
| 505 |
+
# set the random seed, optional, default to 1234 if not set
|
| 506 |
+
seed=random.randint(1, 10000),
|
| 507 |
+
result_format='message', # set the result to be "message" format.
|
| 508 |
+
top_p=0.8)
|
| 509 |
+
if response.status_code == HTTPStatus.OK:
|
| 510 |
+
return response.output.choices[0].message.content
|
| 511 |
+
else:
|
| 512 |
+
print(response.request_id, response.message)
|
| 513 |
+
print('Network error, please retry')
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
def on_submit(input, state):
|
| 517 |
+
response = generate_response(input)
|
| 518 |
+
history = [(input, response)]
|
| 519 |
+
print(history)
|
| 520 |
+
challenge_result, question_info, challenge_info = validate_challenge(
|
| 521 |
+
response, input, state)
|
| 522 |
+
print('validate_challenge done')
|
| 523 |
+
return challenge_result, history, question_info, challenge_info
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
# Gradio界面构建
|
| 527 |
+
block = gr.Blocks()
|
| 528 |
+
|
| 529 |
+
with block as demo:
|
| 530 |
+
state = gr.State(dict(current_challenge_index=0, current_chapter_index=0))
|
| 531 |
+
current_chapter_index = 0
|
| 532 |
+
current_challenge_index = 0
|
| 533 |
+
gr.Markdown("""<center><font size=6>Darn! Ambushed by LLMs!</center>""")
|
| 534 |
+
gr.Markdown("""<font size=3>Welcome to the LLM Riddles Replica Edition: Oh no! I'm surrounded by LLMs!
|
| 535 |
+
|
| 536 |
+
Through this game, you will gain a deeper understanding of large language models.
|
| 537 |
+
|
| 538 |
+
In this game, you need to construct a question to ask a large language model, so that its response meets the specified requirements.""")
|
| 539 |
+
question_info = gr.Markdown(
|
| 540 |
+
update_question_info(current_chapter_index, current_challenge_index))
|
| 541 |
+
challenge_info = gr.Textbox(
|
| 542 |
+
value=update_challenge_info(current_chapter_index,
|
| 543 |
+
current_challenge_index),
|
| 544 |
+
label='Current Challenge',
|
| 545 |
+
disabled=True)
|
| 546 |
+
challenge_result = gr.Textbox(label='Challenge Result', disabled=True)
|
| 547 |
+
chatbot = gr.Chatbot(
|
| 548 |
+
lines=8, label='Qwen-max', elem_classes='control-height')
|
| 549 |
+
message = gr.Textbox(lines=2, label='Input')
|
| 550 |
+
|
| 551 |
+
with gr.Row():
|
| 552 |
+
submit = gr.Button('🚀 Send')
|
| 553 |
+
|
| 554 |
+
submit.click(
|
| 555 |
+
on_submit,
|
| 556 |
+
inputs=[message, state],
|
| 557 |
+
outputs=[challenge_result, chatbot, question_info, challenge_info])
|
| 558 |
+
|
| 559 |
+
demo.queue(concurrency_count=10).launch(height=800, share=True)
|