solnone commited on
Commit
1885646
1 Parent(s): 10d57c5
Files changed (1) hide show
  1. main.py +10 -1
main.py CHANGED
@@ -30,7 +30,15 @@ handler = WebhookHandler(os.environ.get("CHANNEL_SECRET"))
30
 
31
  genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
32
  model = genai.GenerativeModel('gemini-pro')
 
33
 
 
 
 
 
 
 
 
34
 
35
  @app.route("/")
36
  def home():
@@ -58,9 +66,10 @@ def callback():
58
 
59
  @handler.add(MessageEvent, message=TextMessageContent)
60
  def handle_message(event):
61
- response = model.generate_content(event.message.text)
62
  app.logger.info(f"UserID: {event.source.user_id}")
63
  app.logger.info(f"Q: {event.message.text}")
 
 
64
  app.logger.info(f"A: {response.text}")
65
  with ApiClient(configuration) as api_client:
66
  line_bot_api = MessagingApi(api_client)
 
30
 
31
  genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
32
  model = genai.GenerativeModel('gemini-pro')
33
+ chats: dict[str, genai.ChatSession] = {}
34
 
35
+ def get_chat(user_id: str) -> genai.ChatSession:
36
+ if user_id in chats:
37
+ return chats.get(user_id)
38
+ else:
39
+ chat = model.start_chat()
40
+ chats[user_id] = chat
41
+ return chat
42
 
43
  @app.route("/")
44
  def home():
 
66
 
67
  @handler.add(MessageEvent, message=TextMessageContent)
68
  def handle_message(event):
 
69
  app.logger.info(f"UserID: {event.source.user_id}")
70
  app.logger.info(f"Q: {event.message.text}")
71
+ chat = get_chat(event.source.user_id)
72
+ response = chat.send_message(event.message.text)
73
  app.logger.info(f"A: {response.text}")
74
  with ApiClient(configuration) as api_client:
75
  line_bot_api = MessagingApi(api_client)