1313import os
1414import sys
1515from io import BytesIO
16-
1716import aiohttp
1817import PIL .Image
1918
19+ # Import LangChain components
20+ from langchain_google_genai import ChatGoogleGenerativeAI
21+ from langchain .schema .messages import HumanMessage , SystemMessage
22+ from langchain_core .messages import AIMessage
23+ from langchain_core .prompts import ChatPromptTemplate
24+
2025
2126# get channel_secret and channel_access_token from your environment variable
2227channel_secret = os .getenv ('ChannelSecret' , None )
4348line_bot_api = AsyncLineBotApi (channel_access_token , async_http_client )
4449parser = WebhookParser (channel_secret )
4550
46- # Initialize the Gemini Pro API
47- genai .configure (api_key = gemini_key )
51+ # Initialize LangChain with Gemini
52+ os .environ ["GOOGLE_API_KEY" ] = gemini_key
53+
54+ # Create LangChain Gemini model instances
55+ text_model = ChatGoogleGenerativeAI (model = "gemini-2.0-flash-lite" )
56+ vision_model = ChatGoogleGenerativeAI (model = "gemini-2.0-flash-lite" )
4857
4958
5059@app .post ("/" )
@@ -65,10 +74,10 @@ async def handle_callback(request: Request):
6574 continue
6675
6776 if (event .message .type == "text" ):
68- # Provide a default value for reply_msg
77+ # Process text message using LangChain
6978 msg = event .message .text
70- ret = generate_gemini_text_complete (f'{ msg } , reply in zh-TW:' )
71- reply_msg = TextSendMessage (text = ret . text )
79+ response = generate_text_with_langchain (f'{ msg } , reply in zh-TW:' )
80+ reply_msg = TextSendMessage (text = response )
7281 await line_bot_api .reply_message (
7382 event .reply_token ,
7483 reply_msg
@@ -81,8 +90,8 @@ async def handle_callback(request: Request):
8190 image_content += s
8291 img = PIL .Image .open (BytesIO (image_content ))
8392
84- result = generate_result_from_image (img , imgage_prompt )
85- reply_msg = TextSendMessage (text = result . text )
93+ result = generate_vision_with_langchain (img , imgage_prompt )
94+ reply_msg = TextSendMessage (text = result )
8695 await line_bot_api .reply_message (
8796 event .reply_token ,
8897 reply_msg
@@ -94,21 +103,37 @@ async def handle_callback(request: Request):
94103 return 'OK'
95104
96105
97- def generate_gemini_text_complete (prompt ):
106+ def generate_text_with_langchain (prompt ):
98107 """
99- Generate a text completion using the generative model.
108+ Generate a text completion using LangChain with Gemini model.
100109 """
101- model = genai .GenerativeModel ('gemini-2.0-flash-lite' )
102- response = model .generate_content (prompt )
103- return response
110+ # Create a chat prompt template with system instructions
111+ prompt_template = ChatPromptTemplate .from_messages ([
112+ SystemMessage (
113+ content = "You are a helpful assistant that responds in Traditional Chinese (zh-TW)." ),
114+ HumanMessage (content = prompt )
115+ ])
104116
117+ # Format the prompt and call the model
118+ formatted_prompt = prompt_template .format_messages ()
119+ response = text_model .invoke (formatted_prompt )
105120
106- def generate_result_from_image (img , prompt ):
121+ return response .content
122+
123+
124+ def generate_vision_with_langchain (img , prompt ):
107125 """
108- Generate a image vision result using the generative model.
126+ Generate a image vision result using LangChain with Gemini model.
109127 """
110-
111- model = genai .GenerativeModel ('gemini-2.0-flash-lite' )
112- response = model .generate_content ([prompt , img ], stream = True )
113- response .resolve ()
114- return response
128+ # Create a message with both text and image
129+ message = HumanMessage (
130+ content = [
131+ {"type" : "text" , "text" : prompt },
132+ {"type" : "image" , "image" : img }
133+ ]
134+ )
135+
136+ # Call the vision model
137+ response = vision_model .invoke ([message ])
138+
139+ return response .content
0 commit comments