DeepLearning101 commited on
Commit
dbd890d
·
verified ·
1 Parent(s): 3ebd78c

Update src/streamlit_app.py

Browse files

升級為新版 SDK (google.genai)、修復了記憶功能問題,且包含了嚴格指令以防止模型印出內部草稿

Files changed (1) hide show
  1. src/streamlit_app.py +32 -9
src/streamlit_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
- import google.generativeai as genai
 
3
  import os
4
 
5
  # 0. 頁面配置與 CSS 注入(隱藏側邊欄捲軸)
@@ -64,7 +65,8 @@ if not api_key:
64
  st.error("請確認已經在 Space 的 Settings 設定了 GEMINI_API_KEY")
65
  st.stop()
66
 
67
- genai.configure(api_key=api_key)
 
68
 
69
  # 1. 基礎設定與連結
70
  HOME_URL = "https://kidaid.org.tw/"
@@ -155,29 +157,50 @@ for col, ex in zip(example_cols, examples):
155
  if col.button(ex):
156
  st.session_state.example_prompt = ex
157
 
158
- # 5. 模型回覆邏輯(設定專業衛教師人設
159
  def get_gemini_response(user_input):
160
  system_instruction = f"""
161
  你現在是『育兒成 | 全方位兒童發展整合照護平臺』的專業線上客服與育兒小助手。
162
  你的說話風格溫柔、有耐心、具備同理心,且充滿專業知識,就像一位溫暖的早期療育專家或兒童發展衛教師。
163
-
164
  以下是本平台最新的衛教資訊(包含認知、語言、粗大動作、精細動作、社會情緒、飲食、ASD早療對策等):
165
  ---
166
  {st.session_state.knowledge}
167
  ---
 
 
168
  請嚴格基於上述提供的資訊來回答家長的問題。將複雜的衛教知識轉化為容易理解的實作建議。
169
  如果家長問了超出了上述資訊範圍的醫療診斷問題,請溫和地告知:「小助手目前手邊沒有相關資訊。每個孩子的發展狀況不同,建議您可以諮詢專業的小兒科醫師或尋求早期療育評估喔!」
 
 
 
 
 
170
  """
171
 
172
  try:
173
- model = genai.GenerativeModel(
174
- # model_name="gemini-flash-lite-latest",
175
- model_name="gemma-4-31b-it",
176
- system_instruction=system_instruction
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  )
178
- chat = model.start_chat(history=[])
179
  response = chat.send_message(user_input)
180
  return response.text
 
181
  except Exception as e:
182
  error_msg = str(e)
183
  if "429" in error_msg or "quota" in error_msg.lower():
 
1
  import streamlit as st
2
+ from google import genai
3
+ from google.genai import types # 引入 types 用來設定對話歷史與系統指令
4
  import os
5
 
6
  # 0. 頁面配置與 CSS 注入(隱藏側邊欄捲軸)
 
65
  st.error("請確認已經在 Space 的 Settings 設定了 GEMINI_API_KEY")
66
  st.stop()
67
 
68
+ # 【更新】使用新版 SDK 建立 Client
69
+ client = genai.Client(api_key=api_key)
70
 
71
  # 1. 基礎設定與連結
72
  HOME_URL = "https://kidaid.org.tw/"
 
157
  if col.button(ex):
158
  st.session_state.example_prompt = ex
159
 
160
+ # 5. 【更新】模型回覆邏輯(新版 SDK + 記憶功能 + 防止草稿輸出
161
  def get_gemini_response(user_input):
162
  system_instruction = f"""
163
  你現在是『育兒成 | 全方位兒童發展整合照護平臺』的專業線上客服與育兒小助手。
164
  你的說話風格溫柔、有耐心、具備同理心,且充滿專業知識,就像一位溫暖的早期療育專家或兒童發展衛教師。
165
+
166
  以下是本平台最新的衛教資訊(包含認知、語言、粗大動作、精細動作、社會情緒、飲食、ASD早療對策等):
167
  ---
168
  {st.session_state.knowledge}
169
  ---
170
+
171
+ 【核心任務】:
172
  請嚴格基於上述提供的資訊來回答家長的問題。將複雜的衛教知識轉化為容易理解的實作建議。
173
  如果家長問了超出了上述資訊範圍的醫療診斷問題,請溫和地告知:「小助手目前手邊沒有相關資訊。每個孩子的發展狀況不同,建議您可以諮詢專業的小兒科醫師或尋求早期療育評估喔!」
174
+
175
+ 【嚴格輸出限制(非常重要)】:
176
+ 1. 請「直接」給出你要對家長說的回覆,開頭直接打招呼(例如:您好!...)。
177
+ 2. 絕對禁止輸出任何你的內部思考過程、計畫草稿、英文標籤、或是角色設定分析(例如絕對禁止輸出 User:, Question:, Persona:, Constraints:, Core Development Points: 等內容)。
178
+ 3. 所有的輸出內容必須 100% 都是直接面對家長溝通的繁體中文對話。
179
  """
180
 
181
  try:
182
+ # Streamlit 儲存的歷史紀錄,轉換為新版 SDK 規定的格式
183
+ history_format = []
184
+ for msg in st.session_state.messages:
185
+ role = "user" if msg["role"] == "user" else "model"
186
+ history_format.append(
187
+ types.Content(role=role, parts=[types.Part.from_text(text=msg["content"])])
188
+ )
189
+
190
+ # 使用新版 client 建立對話
191
+ chat = client.chats.create(
192
+ # 建議使用 gemini-2.5-flash,對話效果更好且不易出錯
193
+ model="gemini-2.5-flash",
194
+ config=types.GenerateContentConfig(
195
+ system_instruction=system_instruction,
196
+ temperature=0.7, # 稍微調低溫度,讓回答更穩定、不易產生幻覺
197
+ ),
198
+ history=history_format
199
  )
200
+
201
  response = chat.send_message(user_input)
202
  return response.text
203
+
204
  except Exception as e:
205
  error_msg = str(e)
206
  if "429" in error_msg or "quota" in error_msg.lower():