broccoli0510 Claude Opus 5.5 commited on
Commit
42b4bc7
·
1 Parent(s): 8204a67

Force Traditional Chinese output with OpenCC and stricter prompt

Browse files
Files changed (2) hide show
  1. app.py +17 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -18,6 +18,16 @@ except ImportError:
18
  pass
19
 
20
 
 
 
 
 
 
 
 
 
 
 
21
  def _env(name, default=""):
22
  """讀環境變數並去掉前後空白;變數存在但為空值時,也改用預設值。"""
23
  return (os.getenv(name) or "").strip() or default
@@ -37,7 +47,12 @@ DEFAULT_MODEL = _env("LLM_MODEL", "Qwen/Qwen2.5-Coder-7B-Instruct-AWQ")
37
 
38
  client = OpenAI(base_url=BASE_URL, api_key=API_KEY or "missing-key", timeout=120)
39
 
40
- SYSTEM_PROMPT = "你是一位樂於助人的程式設計助理。請一律使用繁體中文回答,清楚說明概念與錯誤原因。"
 
 
 
 
 
41
 
42
  # 模型上下文上限為 4096 tokens:保留 512 給回答,對話歷史超過約 6000 字元時從最舊的開始捨棄
43
  MAX_TOKENS = 512
@@ -100,7 +115,7 @@ def predict(message, history):
100
  for chunk in response:
101
  if chunk.choices and chunk.choices[0].delta.content:
102
  partial_message += chunk.choices[0].delta.content
103
- yield partial_message
104
  if not partial_message:
105
  yield "(模型沒有回傳內容,請再試一次)"
106
  except Exception as e:
 
18
  pass
19
 
20
 
21
+ # 7B 模型偶爾會混入簡體字,顯示前自動轉成繁體(台灣用語);套件不存在時直接顯示原文
22
+ try:
23
+ from opencc import OpenCC
24
+
25
+ _to_tw = OpenCC("s2twp").convert
26
+ except Exception:
27
+ def _to_tw(text):
28
+ return text
29
+
30
+
31
  def _env(name, default=""):
32
  """讀環境變數並去掉前後空白;變數存在但為空值時,也改用預設值。"""
33
  return (os.getenv(name) or "").strip() or default
 
47
 
48
  client = OpenAI(base_url=BASE_URL, api_key=API_KEY or "missing-key", timeout=120)
49
 
50
+ SYSTEM_PROMPT = (
51
+ "你是一位樂於助人的程式設計助理,對象是台灣的大學生。"
52
+ "請一律使用「繁體中文(台灣用語)」回答,絕對不要使用簡體字"
53
+ "(例如要寫「從、對於、程式、這個、輸出」,不要寫「从、对于、程序、这个、输出」)。"
54
+ "清楚說明概念與錯誤原因;程式碼與關鍵字保持英文,並放在 Markdown 程式碼區塊中。"
55
+ )
56
 
57
  # 模型上下文上限為 4096 tokens:保留 512 給回答,對話歷史超過約 6000 字元時從最舊的開始捨棄
58
  MAX_TOKENS = 512
 
115
  for chunk in response:
116
  if chunk.choices and chunk.choices[0].delta.content:
117
  partial_message += chunk.choices[0].delta.content
118
+ yield _to_tw(partial_message)
119
  if not partial_message:
120
  yield "(模型沒有回傳內容,請再試一次)"
121
  except Exception as e:
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio>=4.0.0
2
  openai>=1.0.0
3
- httpx<0.28.0
 
 
1
  gradio>=4.0.0
2
  openai>=1.0.0
3
+ httpx<0.28.0
4
+ opencc-python-reimplemented