Update main.py
Browse files
main.py
CHANGED
@@ -18,6 +18,7 @@ generation_config = genai.types.GenerationConfig(max_output_tokens=2048, tempera
|
|
18 |
|
19 |
# 使用 Gemini-1.5-flash 模型
|
20 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
|
|
21 |
|
22 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
23 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
@@ -77,7 +78,6 @@ def get_image_url(message_id):
|
|
77 |
|
78 |
# 使用字典模擬用戶訊息歷史存儲
|
79 |
user_message_history = defaultdict(list)
|
80 |
-
history=[]
|
81 |
def store_user_message(user_id, message_type, message_content):
|
82 |
"""
|
83 |
儲存用戶的訊息
|
@@ -98,8 +98,6 @@ def analyze_with_gemini(image_path, user_text):
|
|
98 |
organ = PIL.Image.open(image_path)
|
99 |
chat = model.start_chat(history=history)
|
100 |
response = chat.send_message([user_text, organ])
|
101 |
-
history.append({"role": "user", "parts": [user_text]})
|
102 |
-
history.append({"role": "model", "parts": [response.parts[0].text]})
|
103 |
|
104 |
# 提取回應內容
|
105 |
return response.parts[0].text
|
@@ -120,7 +118,6 @@ def get_previous_message(user_id):
|
|
120 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
121 |
def handle_image_message(event):
|
122 |
user_id = event.source.user_id
|
123 |
-
GeneAI = ""
|
124 |
# 從 LINE API 獲取圖片數據
|
125 |
image_path = get_image_url(event.message.id)
|
126 |
if image_path:
|
@@ -170,19 +167,14 @@ def handle_image_message(event):
|
|
170 |
# 取得使用者輸入的文字
|
171 |
prompt = event.message.text
|
172 |
store_user_message(user_id, "text", prompt)
|
173 |
-
|
174 |
-
history.append({"role": "user", "parts": [prompt]})
|
175 |
-
if GeneAI:
|
176 |
-
history.append({"role": "model", "parts": [GeneAI]})
|
177 |
|
178 |
# 使用 Gemini 模型生成文字
|
179 |
chat = model.start_chat(history=history)
|
180 |
completion = chat.send_message(prompt)
|
181 |
# 檢查生成結果是否為空
|
182 |
-
if (completion.
|
183 |
# 取得生成結果
|
184 |
-
out = completion.
|
185 |
-
GeneAI = out
|
186 |
else:
|
187 |
# 回覆 "Gemini沒答案!請換個說法!"
|
188 |
out = "我不太懂什麼意思也~"
|
|
|
18 |
|
19 |
# 使用 Gemini-1.5-flash 模型
|
20 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
21 |
+
chat = model.start_chat(history=[])
|
22 |
|
23 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
24 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
|
78 |
|
79 |
# 使用字典模擬用戶訊息歷史存儲
|
80 |
user_message_history = defaultdict(list)
|
|
|
81 |
def store_user_message(user_id, message_type, message_content):
|
82 |
"""
|
83 |
儲存用戶的訊息
|
|
|
98 |
organ = PIL.Image.open(image_path)
|
99 |
chat = model.start_chat(history=history)
|
100 |
response = chat.send_message([user_text, organ])
|
|
|
|
|
101 |
|
102 |
# 提取回應內容
|
103 |
return response.parts[0].text
|
|
|
118 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
119 |
def handle_image_message(event):
|
120 |
user_id = event.source.user_id
|
|
|
121 |
# 從 LINE API 獲取圖片數據
|
122 |
image_path = get_image_url(event.message.id)
|
123 |
if image_path:
|
|
|
167 |
# 取得使用者輸入的文字
|
168 |
prompt = event.message.text
|
169 |
store_user_message(user_id, "text", prompt)
|
|
|
|
|
|
|
|
|
170 |
|
171 |
# 使用 Gemini 模型生成文字
|
172 |
chat = model.start_chat(history=history)
|
173 |
completion = chat.send_message(prompt)
|
174 |
# 檢查生成結果是否為空
|
175 |
+
if (completion.text != None):
|
176 |
# 取得生成結果
|
177 |
+
out = completion.text
|
|
|
178 |
else:
|
179 |
# 回覆 "Gemini沒答案!請換個說法!"
|
180 |
out = "我不太懂什麼意思也~"
|