Skip to content

Commit 7c94026

Browse files
author
Evan Lin
committed
🔧 Update integration to use LangChain with Vertex AI for improved text and image processing capabilities
1 parent a746ad1 commit 7c94026

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

‎main.py‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,38 @@
99
AsyncLineBotApi, WebhookParser
1010
)
1111
from fastapi import Request, FastAPI, HTTPException
12-
import google.generativeai as genai
1312
import os
1413
import sys
1514
from io import BytesIO
1615
import aiohttp
1716
import 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
2120
from langchain.schema.messages import HumanMessage, SystemMessage
22-
from langchain_core.messages import AIMessage
2321
from langchain_core.prompts import ChatPromptTemplate
2422

2523

2624
# get channel_secret and channel_access_token from your environment variable
2725
channel_secret = os.getenv('ChannelSecret', None)
2826
channel_access_token = os.getenv('ChannelAccessToken', None)
29-
gemini_key = os.getenv('GEMINI_API_KEY')
3027
imgage_prompt = '''
3128
Describe 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+
3436
if channel_secret is None:
3537
print('Specify ChannelSecret as environment variable.')
3638
sys.exit(1)
3739
if 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
@@ -48,12 +50,21 @@
4850
line_bot_api = AsyncLineBotApi(channel_access_token, async_http_client)
4951
parser = 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

106117
def 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

124135
def 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

‎requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ fastapi
33
uvicorn[standard]
44
google.generativeai
55
langchain
6-
langchain-google-genai
6+
langchain-google-vertexai
7+
google-cloud-aiplatform
78
yfinance
89
pydantic
910
tiktoken

0 commit comments

Comments
 (0)