diff --git a/README.md b/README.md index 80d4f86..94011e4 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,7 @@ firebase emulators:start --only functions ```bash firebase deploy --only functions -``` \ No newline at end of file +``` + +## Reference +- https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/develop/langchain#vertex-ai-extension diff --git a/functions/agent.py b/functions/agent.py new file mode 100644 index 0000000..27115a7 --- /dev/null +++ b/functions/agent.py @@ -0,0 +1,44 @@ +from vertexai import agent_engines, init +from langchain_google_vertexai import HarmBlockThreshold, HarmCategory + +init(project="ai-agent-hackathon-3-471422", location="us-central1") + +safety_settings = { + HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE, + HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, + HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH, + HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, + HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE, +} + +model_kwargs = { + # temperature (float): The sampling temperature controls the degree of + # randomness in token selection. + "temperature": 0.28, + # max_output_tokens (int): The token limit determines the maximum amount of + # text output from one prompt. + "max_output_tokens": 1000, + # top_p (float): Tokens are selected from most probable to least until + # the sum of their probabilities equals the top-p value. + "top_p": 0.95, + # top_k (int): The next token is selected from among the top-k most + # probable tokens. This is not supported by all model versions. See + # https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/image-understanding#valid_parameter_values + # for details. + "top_k": None, + # safety_settings (Dict[HarmCategory, HarmBlockThreshold]): The safety + # settings to use for generating content. + # (you must create your safety settings using the previous step first). + "safety_settings": safety_settings, +} + +model = "gemini-2.0-flash" + +agent = agent_engines.LangchainAgent( + model=model, # Required. + model_kwargs=model_kwargs, # Optional. +) + +if __name__ == "__main__": + response = agent.query(input="What is the exchange rate from US dollars to SEK today?") + print(response) \ No newline at end of file diff --git a/functions/gee_ndvi.py b/functions/gee_ndvi.py new file mode 100644 index 0000000..f6a88e7 --- /dev/null +++ b/functions/gee_ndvi.py @@ -0,0 +1,54 @@ +import ee +import requests + +def initialize_gee(): + """認証と初期化。ローカル認証が必要な場合はee.Authenticate()を使う。""" + try: + ee.Initialize() + except Exception: + ee.Authenticate() + ee.Initialize() + +def get_ndvi_image(lat, lon, start_date, end_date, out_path): + """ + 指定座標・期間でNDVI画像を生成し、ファイル保存する。 + Args: + lat (float): 緯度 + lon (float): 経度 + start_date (str): 取得開始日(YYYY-MM-DD) + end_date (str): 取得終了日(YYYY-MM-DD) + out_path (str): 保存先ファイルパス + Returns: + out_path (str): 保存した画像ファイルパス + """ + # Sentinel-2画像コレクション取得 + point = ee.Geometry.Point([lon, lat]) + collection = ee.ImageCollection('COPERNICUS/S2') \ + .filterBounds(point) \ + .filterDate(start_date, end_date) \ + .sort('CLOUD_COVER') + image = collection.first() + # NDVI計算 + ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI') + # 可視化パラメータ + vis_params = {'min': 0, 'max': 1, 'palette': ['blue', 'white', 'green']} + # 画像をエクスポート + url = ndvi.getThumbURL({'region': point.buffer(10000).bounds().getInfo(), 'dimensions': 512, 'format': 'png', **vis_params}) + # 画像ダウンロード + r = requests.get(url) + with open(out_path, 'wb') as f: + f.write(r.content) + return out_path + +if __name__ == "__main__": + # サンプル座標(東京駅付近) + lat = 35.681236 + lon = 139.767125 + start_date = "2023-08-01" + end_date = "2023-08-31" + out_path = "ndvi_sample.png" + print("GEE認証・初期化中...") + initialize_gee() + print("NDVI画像生成中...") + result = get_ndvi_image(lat, lon, start_date, end_date, out_path) + print(f"画像保存先: {result}") \ No newline at end of file diff --git a/functions/requirements.txt b/functions/requirements.txt index bcf4c12..4a4c649 100644 --- a/functions/requirements.txt +++ b/functions/requirements.txt @@ -1 +1,2 @@ -firebase_functions~=0.1.0 \ No newline at end of file +firebase_functions~=0.1.0 +earthengine-api \ No newline at end of file