Update main.py
Browse files
main.py
CHANGED
@@ -2,7 +2,7 @@ import json, os
|
|
2 |
import gradio as gr
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
5 |
-
import google.generativeai as genai
|
6 |
import base64
|
7 |
from collections import defaultdict
|
8 |
from linebot import LineBotApi, WebhookHandler
|
@@ -10,15 +10,22 @@ from linebot.exceptions import InvalidSignatureError
|
|
10 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
11 |
import PIL.Image
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# 設定 Google AI API 金鑰
|
14 |
-
|
15 |
|
16 |
# 設定生成文字的參數
|
17 |
-
generation_config =
|
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"])
|
@@ -97,6 +104,8 @@ def analyze_with_gemini(image_path, user_text):
|
|
97 |
|
98 |
organ = PIL.Image.open(image_path)
|
99 |
response = chat.send_message([user_text, organ])
|
|
|
|
|
100 |
|
101 |
# 提取回應內容
|
102 |
return response.text
|
@@ -114,6 +123,14 @@ def get_previous_message(user_id):
|
|
114 |
return user_message_history[user_id][-1]
|
115 |
return None
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
118 |
def handle_image_message(event):
|
119 |
user_id = event.source.user_id
|
@@ -167,7 +184,9 @@ def handle_image_message(event):
|
|
167 |
store_user_message(user_id, "text", prompt)
|
168 |
|
169 |
# 使用 Gemini 模型生成文字
|
170 |
-
completion = chat.send_message(prompt)
|
|
|
|
|
171 |
# 檢查生成結果是否為空
|
172 |
if (completion.text != None):
|
173 |
# 取得生成結果
|
|
|
2 |
import gradio as gr
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
5 |
+
#import google.generativeai as genai
|
6 |
import base64
|
7 |
from collections import defaultdict
|
8 |
from linebot import LineBotApi, WebhookHandler
|
|
|
10 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
11 |
import PIL.Image
|
12 |
|
13 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
14 |
+
from langchain.memory import ConversationBufferMemory
|
15 |
+
from langchain.chains import ConversationChain
|
16 |
+
|
17 |
# 設定 Google AI API 金鑰
|
18 |
+
ChatGoogleGenerativeAI.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
19 |
|
20 |
# 設定生成文字的參數
|
21 |
+
generation_config = ChatGoogleGenerativeAI.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
22 |
|
23 |
# 使用 Gemini-1.5-flash 模型
|
24 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
25 |
chat = model.start_chat(history=[])
|
26 |
+
# 2. 初始化 ConversationChain 和 Memory
|
27 |
+
memory = ConversationBufferMemory()
|
28 |
+
conversation = ConversationChain(llm=model, memory=memory)
|
29 |
|
30 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
31 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
|
104 |
|
105 |
organ = PIL.Image.open(image_path)
|
106 |
response = chat.send_message([user_text, organ])
|
107 |
+
#chain = get_or_create_chain(user_id)
|
108 |
+
#completion = chain.run(prompt)
|
109 |
|
110 |
# 提取回應內容
|
111 |
return response.text
|
|
|
123 |
return user_message_history[user_id][-1]
|
124 |
return None
|
125 |
|
126 |
+
# 分開不同ID的歷史
|
127 |
+
chat_memories = {}
|
128 |
+
def get_or_create_chain(user_id):
|
129 |
+
if user_id not in chat_memories:
|
130 |
+
memory = ConversationBufferMemory()
|
131 |
+
chat_memories[user_id] = ConversationChain(llm=model, memory=memory)
|
132 |
+
return chat_memories[user_id]
|
133 |
+
|
134 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
135 |
def handle_image_message(event):
|
136 |
user_id = event.source.user_id
|
|
|
184 |
store_user_message(user_id, "text", prompt)
|
185 |
|
186 |
# 使用 Gemini 模型生成文字
|
187 |
+
#completion = chat.send_message(prompt)
|
188 |
+
chain = get_or_create_chain(user_id)
|
189 |
+
completion = chain.run(prompt) # 使用帶對話歷史的方法
|
190 |
# 檢查生成結果是否為空
|
191 |
if (completion.text != None):
|
192 |
# 取得生成結果
|