Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app_bayesian.py +29 -11
- bayesian_llm_assistant.py +38 -13
app_bayesian.py
CHANGED
|
@@ -117,16 +117,32 @@ st.markdown("---")
|
|
| 117 |
with st.sidebar:
|
| 118 |
st.header("⚙️ 配置設定")
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
"
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
if api_key:
|
| 128 |
st.session_state.api_key = api_key
|
| 129 |
-
st.
|
|
|
|
| 130 |
|
| 131 |
st.markdown("---")
|
| 132 |
|
|
@@ -526,11 +542,13 @@ with tab2:
|
|
| 526 |
st.info("ℹ️ 請先在「貝氏分析」頁面執行分析")
|
| 527 |
else:
|
| 528 |
# 初始化 LLM 助手
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
|
|
|
|
|
|
| 534 |
|
| 535 |
# 聊天容器
|
| 536 |
chat_container = st.container()
|
|
|
|
| 117 |
with st.sidebar:
|
| 118 |
st.header("⚙️ 配置設定")
|
| 119 |
|
| 120 |
+
# API 選擇
|
| 121 |
+
api_choice = st.radio(
|
| 122 |
+
"選擇 LLM API",
|
| 123 |
+
options=["Google Gemini", "Anthropic Claude"],
|
| 124 |
+
index=0,
|
| 125 |
+
help="選擇要使用的 AI 助手"
|
| 126 |
)
|
| 127 |
|
| 128 |
+
# API Key 輸入
|
| 129 |
+
if api_choice == "Google Gemini":
|
| 130 |
+
api_key = st.text_input(
|
| 131 |
+
"Google Gemini API Key",
|
| 132 |
+
type="password",
|
| 133 |
+
help="輸入您的 Google Gemini API Key"
|
| 134 |
+
)
|
| 135 |
+
else: # Claude
|
| 136 |
+
api_key = st.text_input(
|
| 137 |
+
"Anthropic Claude API Key",
|
| 138 |
+
type="password",
|
| 139 |
+
help="輸入您的 Anthropic API Key (https://console.anthropic.com)"
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
if api_key:
|
| 143 |
st.session_state.api_key = api_key
|
| 144 |
+
st.session_state.api_choice = api_choice # 新增:儲存 API 選擇
|
| 145 |
+
st.success(f"✅ {api_choice} API Key 已載入")
|
| 146 |
|
| 147 |
st.markdown("---")
|
| 148 |
|
|
|
|
| 542 |
st.info("ℹ️ 請先在「貝氏分析」頁面執行分析")
|
| 543 |
else:
|
| 544 |
# 初始化 LLM 助手
|
| 545 |
+
if 'llm_assistant' not in st.session_state:
|
| 546 |
+
api_choice = st.session_state.get('api_choice', 'Google Gemini')
|
| 547 |
+
st.session_state.llm_assistant = BayesianLLMAssistant(
|
| 548 |
+
api_key=st.session_state.api_key,
|
| 549 |
+
session_id=st.session_state.session_id,
|
| 550 |
+
api_provider=api_choice # 新增:傳遞 API 選擇
|
| 551 |
+
)
|
| 552 |
|
| 553 |
# 聊天容器
|
| 554 |
chat_container = st.container()
|
bayesian_llm_assistant.py
CHANGED
|
@@ -10,20 +10,32 @@ class BayesianLLMAssistant:
|
|
| 10 |
貝氏階層模型 LLM 問答助手(支援動態 DAG 生成)
|
| 11 |
協助用戶理解貝氏分析結果,並可根據描述生成客製化 DAG 圖
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
def __init__(self, api_key, session_id):
|
| 15 |
"""
|
| 16 |
初始化 LLM 助手
|
| 17 |
|
| 18 |
Args:
|
| 19 |
-
api_key:
|
| 20 |
session_id: 唯一的 session 識別碼
|
|
|
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 24 |
self.session_id = session_id
|
| 25 |
self.conversation_history = []
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# 系統提示詞(加入 DAG 生成能力)
|
| 28 |
self.system_prompt = """You are an expert Bayesian statistician specializing in hierarchical models and meta-analysis, particularly in the context of Pokémon battle statistics.
|
| 29 |
|
|
@@ -192,16 +204,29 @@ Format responses with proper markdown for better readability.
|
|
| 192 |
# 組合最終提示詞
|
| 193 |
final_prompt = full_prompt + conversation_text + f"\nUser: {user_message}\n\nAssistant:"
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
temperature=0.7,
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 201 |
)
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
assistant_message = response.text
|
| 205 |
|
| 206 |
# 檢查是否包含 Graphviz 代碼
|
| 207 |
dag_image = self._extract_and_render_dag(assistant_message)
|
|
|
|
| 10 |
貝氏階層模型 LLM 問答助手(支援動態 DAG 生成)
|
| 11 |
協助用戶理解貝氏分析結果,並可根據描述生成客製化 DAG 圖
|
| 12 |
"""
|
| 13 |
+
|
| 14 |
+
def __init__(self, api_key, session_id, api_provider="Google Gemini"):
|
| 15 |
"""
|
| 16 |
初始化 LLM 助手
|
| 17 |
|
| 18 |
Args:
|
| 19 |
+
api_key: API key (Gemini 或 Claude)
|
| 20 |
session_id: 唯一的 session 識別碼
|
| 21 |
+
api_provider: API 提供商 ("Google Gemini" 或 "Anthropic Claude")
|
| 22 |
"""
|
| 23 |
+
self.api_provider = api_provider
|
|
|
|
| 24 |
self.session_id = session_id
|
| 25 |
self.conversation_history = []
|
| 26 |
|
| 27 |
+
if api_provider == "Google Gemini":
|
| 28 |
+
import google.generativeai as genai
|
| 29 |
+
genai.configure(api_key=api_key)
|
| 30 |
+
self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 31 |
+
self.client = None
|
| 32 |
+
else: # Anthropic Claude
|
| 33 |
+
import anthropic
|
| 34 |
+
self.client = anthropic.Anthropic(api_key=api_key)
|
| 35 |
+
self.model_name = "claude-3-5-sonnet-20241022"
|
| 36 |
+
self.model = None
|
| 37 |
+
|
| 38 |
+
|
| 39 |
# 系統提示詞(加入 DAG 生成能力)
|
| 40 |
self.system_prompt = """You are an expert Bayesian statistician specializing in hierarchical models and meta-analysis, particularly in the context of Pokémon battle statistics.
|
| 41 |
|
|
|
|
| 204 |
# 組合最終提示詞
|
| 205 |
final_prompt = full_prompt + conversation_text + f"\nUser: {user_message}\n\nAssistant:"
|
| 206 |
|
| 207 |
+
|
| 208 |
+
# 調用對應的 API
|
| 209 |
+
if self.api_provider == "Google Gemini":
|
| 210 |
+
response = self.model.generate_content(
|
| 211 |
+
final_prompt,
|
| 212 |
+
generation_config=genai.types.GenerationConfig(
|
| 213 |
+
temperature=0.7,
|
| 214 |
+
max_output_tokens=4000,
|
| 215 |
+
)
|
| 216 |
+
)
|
| 217 |
+
assistant_message = response.text
|
| 218 |
+
|
| 219 |
+
else: # Anthropic Claude
|
| 220 |
+
response = self.client.messages.create(
|
| 221 |
+
model=self.model_name,
|
| 222 |
+
max_tokens=4000,
|
| 223 |
temperature=0.7,
|
| 224 |
+
system=self.system_prompt,
|
| 225 |
+
messages=[
|
| 226 |
+
{"role": "user", "content": final_prompt}
|
| 227 |
+
]
|
| 228 |
)
|
| 229 |
+
assistant_message = response.content[0].text
|
|
|
|
|
|
|
| 230 |
|
| 231 |
# 檢查是否包含 Graphviz 代碼
|
| 232 |
dag_image = self._extract_and_render_dag(assistant_message)
|