Update main.py
Browse files
main.py
CHANGED
|
@@ -17,7 +17,7 @@ genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
| 17 |
generation_config = genai.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
| 18 |
|
| 19 |
# 使用 Gemini-1.5-flash 模型
|
| 20 |
-
model = genai.GenerativeModel('gemini-1.5-flash', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題")
|
| 21 |
|
| 22 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
| 23 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
@@ -96,7 +96,6 @@ def analyze_with_gemini(image_path, user_text):
|
|
| 96 |
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 97 |
|
| 98 |
organ = PIL.Image.open(image_path)
|
| 99 |
-
chat = model.start_chat()
|
| 100 |
response = chat.send_message([user_text, organ])
|
| 101 |
|
| 102 |
# 提取回應內容
|
|
@@ -138,11 +137,11 @@ def handle_image_message(event):
|
|
| 138 |
|
| 139 |
# 儲存當前文字訊息
|
| 140 |
user_text = event.message.text
|
| 141 |
-
|
| 142 |
|
| 143 |
# 結合圖片與文字分析
|
| 144 |
out = analyze_with_gemini(image_path, user_text)
|
| 145 |
-
|
| 146 |
else:
|
| 147 |
global working_status
|
| 148 |
# 檢查事件類型和訊息類型
|
|
@@ -164,16 +163,19 @@ def handle_image_message(event):
|
|
| 164 |
try:
|
| 165 |
# 取得使用者輸入的文字
|
| 166 |
prompt = event.message.text
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
# 使用 Gemini 模型生成文字
|
| 170 |
-
chat = model.start_chat(history
|
| 171 |
-
completion = chat.send_message(prompt
|
| 172 |
# 檢查生成結果是否為空
|
| 173 |
if (completion.parts[0].text != None):
|
| 174 |
# 取得生成結果
|
| 175 |
out = completion.parts[0].text
|
| 176 |
-
GeneAI = store_user_message("AI", "text", out)
|
| 177 |
else:
|
| 178 |
# 回覆 "Gemini沒答案!請換個說法!"
|
| 179 |
out = "我不太懂什麼意思也~"
|
|
|
|
| 17 |
generation_config = genai.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
| 18 |
|
| 19 |
# 使用 Gemini-1.5-flash 模型
|
| 20 |
+
model = genai.GenerativeModel('gemini-1.5-flash', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
| 21 |
|
| 22 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
| 23 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
|
|
| 96 |
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 97 |
|
| 98 |
organ = PIL.Image.open(image_path)
|
|
|
|
| 99 |
response = chat.send_message([user_text, organ])
|
| 100 |
|
| 101 |
# 提取回應內容
|
|
|
|
| 137 |
|
| 138 |
# 儲存當前文字訊息
|
| 139 |
user_text = event.message.text
|
| 140 |
+
store_user_message(user_id, "text", user_text)
|
| 141 |
|
| 142 |
# 結合圖片與文字分析
|
| 143 |
out = analyze_with_gemini(image_path, user_text)
|
| 144 |
+
|
| 145 |
else:
|
| 146 |
global working_status
|
| 147 |
# 檢查事件類型和訊息類型
|
|
|
|
| 163 |
try:
|
| 164 |
# 取得使用者輸入的文字
|
| 165 |
prompt = event.message.text
|
| 166 |
+
store_user_message(user_id, "text", prompt)
|
| 167 |
+
|
| 168 |
+
history = [{"role": "user", "parts": [store_user_message]}]
|
| 169 |
+
if GeneAI:
|
| 170 |
+
history.append({"role": "model", "parts": [out]})
|
| 171 |
+
|
| 172 |
# 使用 Gemini 模型生成文字
|
| 173 |
+
chat = model.start_chat(history=history)
|
| 174 |
+
completion = chat.send_message(prompt)
|
| 175 |
# 檢查生成結果是否為空
|
| 176 |
if (completion.parts[0].text != None):
|
| 177 |
# 取得生成結果
|
| 178 |
out = completion.parts[0].text
|
|
|
|
| 179 |
else:
|
| 180 |
# 回覆 "Gemini沒答案!請換個說法!"
|
| 181 |
out = "我不太懂什麼意思也~"
|