Spaces:
Running
Running
Fix: api_key Setting
Browse files- app.py +12 -10
- src/tools/navigation_toolkit.py +2 -2
- src/tools/optimizer_toolkit.py +2 -2
- src/tools/scout_toolkit.py +2 -2
- src/tools/weather_toolkit.py +2 -2
app.py
CHANGED
|
@@ -141,24 +141,26 @@ class LifeFlowAI:
|
|
| 141 |
|
| 142 |
# [Security Fix] 優先使用用戶提供的 API Key (這裡以 Gemini 為例,若支援其他模型需擴充邏輯)
|
| 143 |
# 注意:實際應用中需根據選擇的 Model ID (Claude/Gemini) 來決定使用哪個 Key
|
| 144 |
-
|
| 145 |
|
|
|
|
|
|
|
| 146 |
# 初始化模型 (應用設定)
|
| 147 |
planner_model = Gemini(
|
| 148 |
id=selected_model_id,
|
| 149 |
thinking_budget=2048,
|
| 150 |
-
api_key=
|
| 151 |
)
|
| 152 |
|
| 153 |
main_model = Gemini(
|
| 154 |
id=selected_model_id,
|
| 155 |
thinking_budget=1024,
|
| 156 |
-
api_key=
|
| 157 |
)
|
| 158 |
|
| 159 |
lite_model = Gemini(
|
| 160 |
id="gemini-2.5-flash-lite", # 輕量級模型通常固定或由次要選項決定
|
| 161 |
-
api_key=
|
| 162 |
)
|
| 163 |
|
| 164 |
# 配置模型和工具
|
|
@@ -173,10 +175,10 @@ class LifeFlowAI:
|
|
| 173 |
|
| 174 |
# [Note] 如果 Toolkit 支援傳入 API Key,應在此處從 session.custom_settings 傳入
|
| 175 |
tools_dict = {
|
| 176 |
-
"scout": [ScoutToolkit()],
|
| 177 |
-
"optimizer": [OptimizationToolkit()],
|
| 178 |
-
"navigator": [NavigationToolkit()],
|
| 179 |
-
"weatherman": [WeatherToolkit()],
|
| 180 |
"presenter": [ReaderToolkit()],
|
| 181 |
}
|
| 182 |
|
|
@@ -554,13 +556,13 @@ class LifeFlowAI:
|
|
| 554 |
# ===== 輔助方法 =====
|
| 555 |
|
| 556 |
# [Security Fix] 新增:保存設定到 User Session
|
| 557 |
-
def save_settings(self, google_key, weather_key,
|
| 558 |
"""保存設定到用戶 Session"""
|
| 559 |
session = UserSession.from_dict(session_data)
|
| 560 |
|
| 561 |
session.custom_settings['google_maps_api_key'] = google_key
|
| 562 |
session.custom_settings['openweather_api_key'] = weather_key
|
| 563 |
-
session.custom_settings['
|
| 564 |
session.custom_settings['model'] = model
|
| 565 |
|
| 566 |
return "✅ Settings saved locally!", session.to_dict()
|
|
|
|
| 141 |
|
| 142 |
# [Security Fix] 優先使用用戶提供的 API Key (這裡以 Gemini 為例,若支援其他模型需擴充邏輯)
|
| 143 |
# 注意:實際應用中需根據選擇的 Model ID (Claude/Gemini) 來決定使用哪個 Key
|
| 144 |
+
gemini_api_key = session.agno_settings.gemini_api_key
|
| 145 |
|
| 146 |
+
google_maps_api_key = session.agno_settings.gemini_api_key
|
| 147 |
+
openweather_api_key = session.agno_settings.openweather_api_key
|
| 148 |
# 初始化模型 (應用設定)
|
| 149 |
planner_model = Gemini(
|
| 150 |
id=selected_model_id,
|
| 151 |
thinking_budget=2048,
|
| 152 |
+
api_key=gemini_api_key
|
| 153 |
)
|
| 154 |
|
| 155 |
main_model = Gemini(
|
| 156 |
id=selected_model_id,
|
| 157 |
thinking_budget=1024,
|
| 158 |
+
api_key=gemini_api_key
|
| 159 |
)
|
| 160 |
|
| 161 |
lite_model = Gemini(
|
| 162 |
id="gemini-2.5-flash-lite", # 輕量級模型通常固定或由次要選項決定
|
| 163 |
+
api_key=gemini_api_key
|
| 164 |
)
|
| 165 |
|
| 166 |
# 配置模型和工具
|
|
|
|
| 175 |
|
| 176 |
# [Note] 如果 Toolkit 支援傳入 API Key,應在此處從 session.custom_settings 傳入
|
| 177 |
tools_dict = {
|
| 178 |
+
"scout": [ScoutToolkit(google_maps_api_key)],
|
| 179 |
+
"optimizer": [OptimizationToolkit(google_maps_api_key)],
|
| 180 |
+
"navigator": [NavigationToolkit(google_maps_api_key)],
|
| 181 |
+
"weatherman": [WeatherToolkit(openweather_api_key)],
|
| 182 |
"presenter": [ReaderToolkit()],
|
| 183 |
}
|
| 184 |
|
|
|
|
| 556 |
# ===== 輔助方法 =====
|
| 557 |
|
| 558 |
# [Security Fix] 新增:保存設定到 User Session
|
| 559 |
+
def save_settings(self, google_key, weather_key, gemini_key, model, session_data):
|
| 560 |
"""保存設定到用戶 Session"""
|
| 561 |
session = UserSession.from_dict(session_data)
|
| 562 |
|
| 563 |
session.custom_settings['google_maps_api_key'] = google_key
|
| 564 |
session.custom_settings['openweather_api_key'] = weather_key
|
| 565 |
+
session.custom_settings['gemini_api_key'] = gemini_key
|
| 566 |
session.custom_settings['model'] = model
|
| 567 |
|
| 568 |
return "✅ Settings saved locally!", session.to_dict()
|
src/tools/navigation_toolkit.py
CHANGED
|
@@ -6,9 +6,9 @@ from src.infra.poi_repository import poi_repo
|
|
| 6 |
|
| 7 |
|
| 8 |
class NavigationToolkit(Toolkit):
|
| 9 |
-
def __init__(self,
|
| 10 |
super().__init__(name="navigation_toolkit")
|
| 11 |
-
self.gmaps = GoogleMapAPIService(api_key=
|
| 12 |
self.register(self.calculate_traffic_and_timing)
|
| 13 |
|
| 14 |
def calculate_traffic_and_timing(self, optimization_ref_id: str) -> str:
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
class NavigationToolkit(Toolkit):
|
| 9 |
+
def __init__(self, google_maps_api_key: str = None):
|
| 10 |
super().__init__(name="navigation_toolkit")
|
| 11 |
+
self.gmaps = GoogleMapAPIService(api_key=google_maps_api_key)
|
| 12 |
self.register(self.calculate_traffic_and_timing)
|
| 13 |
|
| 14 |
def calculate_traffic_and_timing(self, optimization_ref_id: str) -> str:
|
src/tools/optimizer_toolkit.py
CHANGED
|
@@ -8,9 +8,9 @@ from src.infra.poi_repository import poi_repo
|
|
| 8 |
|
| 9 |
|
| 10 |
class OptimizationToolkit(Toolkit):
|
| 11 |
-
def __init__(self,
|
| 12 |
super().__init__(name="optimization_toolkit")
|
| 13 |
-
self.solver = TSPTWSolver(api_key=
|
| 14 |
self.register(self.optimize_from_ref)
|
| 15 |
|
| 16 |
def optimize_from_ref(self, ref_id: str) -> str:
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
class OptimizationToolkit(Toolkit):
|
| 11 |
+
def __init__(self, google_maps_api_key: None):
|
| 12 |
super().__init__(name="optimization_toolkit")
|
| 13 |
+
self.solver = TSPTWSolver(api_key=google_maps_api_key)
|
| 14 |
self.register(self.optimize_from_ref)
|
| 15 |
|
| 16 |
def optimize_from_ref(self, ref_id: str) -> str:
|
src/tools/scout_toolkit.py
CHANGED
|
@@ -9,9 +9,9 @@ logger = get_logger(__name__)
|
|
| 9 |
MAX_SEARCH = 1000
|
| 10 |
|
| 11 |
class ScoutToolkit(Toolkit):
|
| 12 |
-
def __init__(self,
|
| 13 |
super().__init__(name="scout_toolkit")
|
| 14 |
-
self.gmaps = GoogleMapAPIService(api_key=
|
| 15 |
self.register(self.search_and_offload)
|
| 16 |
|
| 17 |
def _extract_first_json_object(self, text: str) -> str:
|
|
|
|
| 9 |
MAX_SEARCH = 1000
|
| 10 |
|
| 11 |
class ScoutToolkit(Toolkit):
|
| 12 |
+
def __init__(self, google_maps_api_key: str):
|
| 13 |
super().__init__(name="scout_toolkit")
|
| 14 |
+
self.gmaps = GoogleMapAPIService(api_key=google_maps_api_key)
|
| 15 |
self.register(self.search_and_offload)
|
| 16 |
|
| 17 |
def _extract_first_json_object(self, text: str) -> str:
|
src/tools/weather_toolkit.py
CHANGED
|
@@ -9,9 +9,9 @@ logger = get_logger(__name__)
|
|
| 9 |
|
| 10 |
|
| 11 |
class WeatherToolkit(Toolkit):
|
| 12 |
-
def __init__(self,
|
| 13 |
super().__init__(name="weather_toolkit")
|
| 14 |
-
self.weather_service = OpenWeatherMapService(api_key=
|
| 15 |
self.register(self.check_weather_for_timeline)
|
| 16 |
|
| 17 |
def check_weather_for_timeline(self, nav_ref_id: str) -> str:
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class WeatherToolkit(Toolkit):
|
| 12 |
+
def __init__(self, openweather_api_key: str):
|
| 13 |
super().__init__(name="weather_toolkit")
|
| 14 |
+
self.weather_service = OpenWeatherMapService(api_key=openweather_api_key)
|
| 15 |
self.register(self.check_weather_for_timeline)
|
| 16 |
|
| 17 |
def check_weather_for_timeline(self, nav_ref_id: str) -> str:
|