99 AsyncLineBotApi , WebhookParser
1010)
1111from fastapi import Request , FastAPI , HTTPException
12- import google .generativeai as genai
1312import os
1413import sys
1514from io import BytesIO
1615import aiohttp
1716import PIL .Image
1817
19- # Import LangChain components
20- from langchain_google_genai import ChatGoogleGenerativeAI
18+ # Import LangChain components with Vertex AI
19+ from langchain_google_vertexai import ChatVertexAI
2120from langchain .schema .messages import HumanMessage , SystemMessage
22- from langchain_core .messages import AIMessage
2321from langchain_core .prompts import ChatPromptTemplate
2422
2523
2624# get channel_secret and channel_access_token from your environment variable
2725channel_secret = os .getenv ('ChannelSecret' , None )
2826channel_access_token = os .getenv ('ChannelAccessToken' , None )
29- gemini_key = os .getenv ('GEMINI_API_KEY' )
3027imgage_prompt = '''
3128Describe this image with scientific detail, reply in zh-TW:
3229'''
3330
31+ # Vertex AI needs a project ID and possibly authentication
32+ google_project_id = os .getenv ('GOOGLE_PROJECT_ID' )
33+ # Location for Vertex AI resources, e.g., "us-central1"
34+ google_location = os .getenv ('GOOGLE_LOCATION' , 'us-central1' )
35+
3436if channel_secret is None :
3537 print ('Specify ChannelSecret as environment variable.' )
3638 sys .exit (1 )
3739if channel_access_token is None :
3840 print ('Specify ChannelAccessToken as environment variable.' )
3941 sys .exit (1 )
40- if gemini_key is None :
41- print ('Specify GEMINI_API_KEY as environment variable.' )
42+ if google_project_id is None :
43+ print ('Specify GOOGLE_PROJECT_ID as environment variable.' )
4244 sys .exit (1 )
4345
4446# Initialize the FastAPI app for LINEBot
4850line_bot_api = AsyncLineBotApi (channel_access_token , async_http_client )
4951parser = WebhookParser (channel_secret )
5052
51- # Initialize LangChain with Gemini
52- os .environ ["GOOGLE_API_KEY" ] = gemini_key
53+ # Create LangChain Vertex AI model instances
54+ # For Vertex AI, we use "gemini-2.0-flash" instead of "gemini-2.0-flash-lite"
55+ text_model = ChatVertexAI (
56+ model_name = "gemini-2.0-flash" ,
57+ project = google_project_id ,
58+ location = google_location ,
59+ max_output_tokens = 1024
60+ )
5361
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" )
62+ vision_model = ChatVertexAI (
63+ model_name = "gemini-2.0-flash" ,
64+ project = google_project_id ,
65+ location = google_location ,
66+ max_output_tokens = 1024
67+ )
5768
5869
5970@app .post ("/" )
@@ -74,7 +85,7 @@ async def handle_callback(request: Request):
7485 continue
7586
7687 if (event .message .type == "text" ):
77- # Process text message using LangChain
88+ # Process text message using LangChain with Vertex AI
7889 msg = event .message .text
7990 response = generate_text_with_langchain (f'{ msg } , reply in zh-TW:' )
8091 reply_msg = TextSendMessage (text = response )
@@ -105,7 +116,7 @@ async def handle_callback(request: Request):
105116
106117def generate_text_with_langchain (prompt ):
107118 """
108- Generate a text completion using LangChain with Gemini model.
119+ Generate a text completion using LangChain with Vertex AI model.
109120 """
110121 # Create a chat prompt template with system instructions
111122 prompt_template = ChatPromptTemplate .from_messages ([
@@ -123,13 +134,13 @@ def generate_text_with_langchain(prompt):
123134
124135def generate_vision_with_langchain (img , prompt ):
125136 """
126- Generate a image vision result using LangChain with Gemini model.
137+ Generate a image vision result using LangChain with Vertex AI model.
127138 """
128139 # Create a message with both text and image
129140 message = HumanMessage (
130141 content = [
131142 {"type" : "text" , "text" : prompt },
132- {"type" : "image " , "image " : img }
143+ {"type" : "image_url " , "image_url " : { "url" : img } }
133144 ]
134145 )
135146
0 commit comments