aicodingfun commited on
Commit
8e83a33
·
verified ·
1 Parent(s): e1f9cb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -1
app.py CHANGED
@@ -21,6 +21,20 @@ output_dir = "./question_bank"
21
  # 載入題庫字典(question_bank_dict),格式為 { "年級_學期": [題庫檔名列表] }
22
  question_bank_dict = json.load(open(f"{output_dir}/question_bank_dict.json", "r"))
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def random_questions_with_limit(data, limit=20000):
25
  """
26
  隨機從 data 中挑選題目,並將總字串長度限制在 limit 字元數內(至少不小於 5000)。
@@ -147,7 +161,7 @@ def generate_math_questions(grade, term, qtype="Unspecified", num_questions=10):
147
  )
148
 
149
  response = completion.choices[0].message.content
150
-
151
  print(response)
152
 
153
  # 傳回模型產生的文本
 
21
  # 載入題庫字典(question_bank_dict),格式為 { "年級_學期": [題庫檔名列表] }
22
  question_bank_dict = json.load(open(f"{output_dir}/question_bank_dict.json", "r"))
23
 
24
+ import re
25
+
26
+ def remove_think_tags(content):
27
+ """刪除 <think>...</think> 標籤及其內容的正規表達式解法"""
28
+ # pattern = r'<think>.*?</think>'
29
+ # cleaned_content = re.sub(pattern, '', content, flags=re.DOTALL)
30
+
31
+ # 刪除所有 <think> 標籤及其內容
32
+ content = re.sub(r'<\s*think[^>]*>.*?<\s*/\s*think\s*>', '', content, flags=re.DOTALL)
33
+ # 清除殘留空行 (連續兩個以上換行符)
34
+ content = re.sub(r'\n{3,}', '\n\n', content)
35
+
36
+ return cleaned_content.strip()
37
+
38
  def random_questions_with_limit(data, limit=20000):
39
  """
40
  隨機從 data 中挑選題目,並將總字串長度限制在 limit 字元數內(至少不小於 5000)。
 
161
  )
162
 
163
  response = completion.choices[0].message.content
164
+ response = remove_think_tags(response)
165
  print(response)
166
 
167
  # 傳回模型產生的文本