Update main.py
Browse files
main.py
CHANGED
|
@@ -82,7 +82,7 @@ def handle_message(event):
|
|
| 82 |
|
| 83 |
# 檢查是否正在與使用者交談
|
| 84 |
elif working_status:
|
| 85 |
-
|
| 86 |
# 取得使用者輸入的文字
|
| 87 |
question = event.message.text
|
| 88 |
doc_url = "https://www.twse.com.tw/pdf/ch/"+question+"_ch.pdf"
|
|
@@ -94,8 +94,7 @@ def handle_message(event):
|
|
| 94 |
temp_file.write(doc_data.content)
|
| 95 |
temp_file_path = temp_file.name
|
| 96 |
sample_doc = client.files.upload(file=temp_file_path)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
# gemini-2.5-flash
|
| 100 |
completion = client.models.generate_content(
|
| 101 |
model="gemini-2.5-flash",
|
|
@@ -103,18 +102,14 @@ def handle_message(event):
|
|
| 103 |
config=generation_config).text
|
| 104 |
# 取得生成結果
|
| 105 |
out = completion
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
line_bot_api.reply_message(
|
| 116 |
-
event.reply_token,
|
| 117 |
-
TextSendMessage(text=out))
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
# 啟動 FastAPI 應用程式
|
|
|
|
| 82 |
|
| 83 |
# 檢查是否正在與使用者交談
|
| 84 |
elif working_status:
|
| 85 |
+
try:
|
| 86 |
# 取得使用者輸入的文字
|
| 87 |
question = event.message.text
|
| 88 |
doc_url = "https://www.twse.com.tw/pdf/ch/"+question+"_ch.pdf"
|
|
|
|
| 94 |
temp_file.write(doc_data.content)
|
| 95 |
temp_file_path = temp_file.name
|
| 96 |
sample_doc = client.files.upload(file=temp_file_path)
|
| 97 |
+
|
|
|
|
| 98 |
# gemini-2.5-flash
|
| 99 |
completion = client.models.generate_content(
|
| 100 |
model="gemini-2.5-flash",
|
|
|
|
| 102 |
config=generation_config).text
|
| 103 |
# 取得生成結果
|
| 104 |
out = completion
|
| 105 |
+
except:
|
| 106 |
+
# 處理錯誤
|
| 107 |
+
out = "Gemini執行出錯!請換個說法!"
|
| 108 |
|
| 109 |
+
# 回覆生成結果
|
| 110 |
+
line_bot_api.reply_message(
|
| 111 |
+
event.reply_token,
|
| 112 |
+
TextSendMessage(text=out))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
# 啟動 FastAPI 應用程式
|