Update src/streamlit_app.py
Browse files- src/streamlit_app.py +32 -8
src/streamlit_app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
|
|
@@ -66,7 +67,8 @@ if not api_key:
|
|
| 66 |
st.error("請確認已經在 Space 的 Settings 設定了 GEMINI_API_KEY")
|
| 67 |
st.stop()
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
# 1. 基礎設定與連結
|
| 72 |
# 抓取 Deep Learning 101 的 Logo (你也可以換成 SHIELD 專屬 Logo)
|
|
@@ -138,7 +140,7 @@ for col, ex in zip(example_cols, examples):
|
|
| 138 |
if col.button(ex):
|
| 139 |
st.session_state.example_prompt = ex
|
| 140 |
|
| 141 |
-
# 5. 模型回覆邏輯(
|
| 142 |
def get_gemini_response(user_input):
|
| 143 |
system_instruction = f"""
|
| 144 |
你現在是 Deep Learning 101 開發的『S.H.I.E.L.D. 戰情小助手』。
|
|
@@ -148,18 +150,40 @@ def get_gemini_response(user_input):
|
|
| 148 |
---
|
| 149 |
{st.session_state.knowledge}
|
| 150 |
---
|
|
|
|
|
|
|
| 151 |
請嚴格基於上述提供的資訊來回答使用者的問題。
|
| 152 |
如果使用者問了超出 S.H.I.E.L.D. 白皮書範圍的問題,請禮貌地告知:「目前的戰情資料庫尚未收錄此資訊,建議您查閱 Deep Learning 101 的其他專案或直接聯繫維護團隊。」
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
"""
|
|
|
|
| 154 |
try:
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
)
|
| 160 |
-
|
| 161 |
response = chat.send_message(user_input)
|
| 162 |
return response.text
|
|
|
|
| 163 |
except Exception as e:
|
| 164 |
error_msg = str(e)
|
| 165 |
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 requests
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 67 |
st.error("請確認已經在 Space 的 Settings 設定了 GEMINI_API_KEY")
|
| 68 |
st.stop()
|
| 69 |
|
| 70 |
+
# 【更新】使用新版 SDK 建立 Client
|
| 71 |
+
client = genai.Client(api_key=api_key)
|
| 72 |
|
| 73 |
# 1. 基礎設定與連結
|
| 74 |
# 抓取 Deep Learning 101 的 Logo (你也可以換成 SHIELD 專屬 Logo)
|
|
|
|
| 140 |
if col.button(ex):
|
| 141 |
st.session_state.example_prompt = ex
|
| 142 |
|
| 143 |
+
# 5. 【更新】模型回覆邏輯(新版 SDK + 記憶功能 + 防止草稿輸出)
|
| 144 |
def get_gemini_response(user_input):
|
| 145 |
system_instruction = f"""
|
| 146 |
你現在是 Deep Learning 101 開發的『S.H.I.E.L.D. 戰情小助手』。
|
|
|
|
| 150 |
---
|
| 151 |
{st.session_state.knowledge}
|
| 152 |
---
|
| 153 |
+
|
| 154 |
+
【核心任務】:
|
| 155 |
請嚴格基於上述提供的資訊來回答使用者的問題。
|
| 156 |
如果使用者問了超出 S.H.I.E.L.D. 白皮書範圍的問題,請禮貌地告知:「目前的戰情資料庫尚未收錄此資訊,建議您查閱 Deep Learning 101 的其他專案或直接聯繫維護團隊。」
|
| 157 |
+
|
| 158 |
+
【嚴格輸出限制】:
|
| 159 |
+
1. 請直接給出專業、精確的繁體中文回覆。
|
| 160 |
+
2. 絕對禁止輸出任何你的內部思考過程、計畫草稿、英文標籤、或是角色設定分析。
|
| 161 |
+
3. 所有的輸出內容必須 100% 都是直接面對使用者溝通的對話內容。
|
| 162 |
"""
|
| 163 |
+
|
| 164 |
try:
|
| 165 |
+
# 將 Streamlit 儲存的歷史紀錄,轉換為新版 SDK 規定的格式
|
| 166 |
+
history_format = []
|
| 167 |
+
for msg in st.session_state.messages:
|
| 168 |
+
role = "user" if msg["role"] == "user" else "model"
|
| 169 |
+
history_format.append(
|
| 170 |
+
types.Content(role=role, parts=[types.Part.from_text(text=msg["content"])])
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# 使用新版 client 建立對話,並帶入歷史紀錄
|
| 174 |
+
chat = client.chats.create(
|
| 175 |
+
# 升級為 gemini-2.5-flash,反應速度最快且對長文本技術文件的理解力極佳
|
| 176 |
+
model="gemma-4-31b-it",
|
| 177 |
+
config=types.GenerateContentConfig(
|
| 178 |
+
system_instruction=system_instruction,
|
| 179 |
+
temperature=0.3, # 針對技術戰情,調低溫度讓回答精準客觀,減少幻覺
|
| 180 |
+
),
|
| 181 |
+
history=history_format
|
| 182 |
)
|
| 183 |
+
|
| 184 |
response = chat.send_message(user_input)
|
| 185 |
return response.text
|
| 186 |
+
|
| 187 |
except Exception as e:
|
| 188 |
error_msg = str(e)
|
| 189 |
if "429" in error_msg or "quota" in error_msg.lower():
|