コミートム合同会社

コミートム合同会社

VertexAIの「text-bison」をLambdaから呼んでみました

VertexAI AI textbison AWS GCP GCP API Lambda This article describes how to call VertexAI's AI model textbison from AWS lambda to create hashtags. The author created a service account from the GCP console, got the authentication information from the service account programmatically, and used the authentication information to call the GCP API. The author then uploaded the code to Lambda and tested it successfully.
by JanitorJun 29, 2023
VertexAIのAIモデル「text-bison」をAWSのlambdaから呼んで、ハッシュタグを作成してもらいました。色々な種類のAIモデルが群雄割拠していて、いまいちどのAIモデルを使わせて頂くのが良いのかよくわからない状況の中で、「text-bison」というAIモデルの守備範囲がかなり広そうで、Vertex AIも「これでよくないですか?」とご提案頂いているようにも感じたので、「text-bison」をAPIで呼んでみることにしました。流れとしては、GCPのコンソールからサービスアカウントを作成しておき、プログラム上でサービスアカウント経由で認証情報を取得、その認証情報を使ってGCPのAPIをコールしました。今回の場合は、そのAPIの相手がVertex AIの「text-bison」であり、呼び元がAWSのLambda、となっています。
VertexAIのAIモデル「text-bison」をAWSのlambdaから呼んで、ハッシュタグを作成してもらいました。色々な種類のAIモデルが群雄割拠していて、いまいちどのAIモデルを使わせて頂くのが良いのかよくわからない状況の中で、「text-bison」というAIモデルの守備範囲がかなり広そうで、Vertex AIも「これでよくないですか?」とご提案頂いているようにも感じたので、「text-bison」をAPIで呼んでみることにしました。流れとしては、GCPのコンソールからサービスアカウントを作成しておき、プログラム上でサービスアカウント経由で認証情報を取得、その認証情報を使ってGCPのAPIをコールしました。今回の場合は、そのAPIの相手がVertex AIの「text-bison」であり、呼び元がAWSのLambda、となっています。

Jump Links

1.GCPの「IAMと管理」-> 「サービスアカウント」からサービスアカウントを作成し、「鍵」ファイルをダウンロードする
2.認証情報を取得するスクリプトを作成
3.スクリプトの続きを書く
4.Lambdaにアップロード
5.テスト実行
6.おわりに
1.GCPの「IAMと管理」-> 「サービスアカウント」からサービスアカウントを作成し、「鍵」ファイルをダウンロードする
1.GCPの「IAMと管理」-> 「サービスアカウント」からサービスアカウントを作成し、「鍵」ファイルをダウンロードする
下図のように「サービスアカウントを作成」をクリックします。 サービスアカウントを作成したら、作成したサービスアカウントをクリックし、「キー」タブに移動して、「鍵を追加」を押して鍵を作成し、「鍵」ファイルをダウンロードします。権限やロールの設定はここでは割愛します。
下図のように「サービスアカウントを作成」をクリックします。 サービスアカウントを作成したら、作成したサービスアカウントをクリックし、「キー」タブに移動して、「鍵を追加」を押して鍵を作成し、「鍵」ファイルをダウンロードします。権限やロールの設定はここでは割愛します。
2.認証情報を取得するスクリプトを作成
2.認証情報を取得するスクリプトを作成
とりあえず、作成したサービスアカウント経由で認証情報が取得できるか確認します。pythonで作成したため、あらかじめ仮想環境作成して、「google-auth」をフォルダにインストールしています。コマンドを実行した時点で、フォルダ(下の例の場合tmpDir)に大量にフォルダが出てきていれば問題ないです。 サービスアカウントのスコープは「Google API の OAuth 2.0 スコープ」(下記URL)に記載されている内容を設定します。 https://developers.google.com/identity/protocols/oauth2/scopes?hl=ja 今回の場合は、「AI Platform Training & Prediction API」に記載された2つのスコープを指定しています。プロジェクトIDとエンドポイントは下記の画像の箇所で確認できます。pythonスクリプトを実行して文字が大量に出てきたら、ちゃんと認証情報が取得できています。
とりあえず、作成したサービスアカウント経由で認証情報が取得できるか確認します。pythonで作成したため、あらかじめ仮想環境作成して、「google-auth」をフォルダにインストールしています。コマンドを実行した時点で、フォルダ(下の例の場合tmpDir)に大量にフォルダが出てきていれば問題ないです。 サービスアカウントのスコープは「Google API の OAuth 2.0 スコープ」(下記URL)に記載されている内容を設定します。 https://developers.google.com/identity/protocols/oauth2/scopes?hl=ja 今回の場合は、「AI Platform Training & Prediction API」に記載された2つのスコープを指定しています。プロジェクトIDとエンドポイントは下記の画像の箇所で確認できます。pythonスクリプトを実行して文字が大量に出てきたら、ちゃんと認証情報が取得できています。
コマンドプロンプト python -m venv tmpEnv tmpEnv\Scripts\activate mkdir tmpDir cd tmpDir pip install -U google-auth -t .
コマンドプロンプト python -m venv tmpEnv tmpEnv\Scripts\activate mkdir tmpDir cd tmpDir pip install -U google-auth -t .
# pythonスクリプト # tmpDir内でlambda_function.pyというファイル名で作成 from google.oauth2 import service_account import google.auth.transport.requests import requests import json project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) print( credentials.token )
# pythonスクリプト # tmpDir内でlambda_function.pyというファイル名で作成 from google.oauth2 import service_account import google.auth.transport.requests import requests import json project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) print( credentials.token )
3.スクリプトの続きを書く
3.スクリプトの続きを書く
上の画像のCurlの例のように、「Examples」と「Test」をhttpリクエストに載せてポストするだけです。大量にスクリプトを記載しているように見えますが、ほとんどが「Examples」であり、文字列です。5つの例を与えて、じゃあテストします、これは?と聞くと答えてくれる、ということですね。
上の画像のCurlの例のように、「Examples」と「Test」をhttpリクエストに載せてポストするだけです。大量にスクリプトを記載しているように見えますが、ほとんどが「Examples」であり、文字列です。5つの例を与えて、じゃあテストします、これは?と聞くと答えてくれる、ということですね。
from google.oauth2 import service_account import google.auth.transport.requests import requests import json def lambda_handler(event, context): project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) # print( credentials.token ) MODEL_ID="text-bison@001" headers = { "Content-Type": "application/json", "Authorization": "Bearer " + credentials.token } endpoint = f"https://{endpoint_id}/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{MODEL_ID}:predict" parameters = { "temperature": 0.2, "maxOutputTokens": 256, "topP": 0.95, "topK": 40 } examples = """Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 4h Google Cloud 🤝 @MongoDB : helping startups scale quicker, more safely, and more successfully. Learn how we\'re supporting high-potential startups via our dedicated startup programs—the Google for Startups Cloud Program and MongoDB for Startups Program ↓ The hashtags in this tweet are: #GoogleCloud #googlecloud #MongoDB #mongodb #startups #startup #scale #quicker #moresafely #moresuccessfully #GoogleforStartupsCloudProgram #MongoDBforStartupsProgram#mongodb Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 10h Women deliver impact across Google Cloud—from keeping the internet running to driving AI innovation to transforming our customers\' businesses. For IWD2023, check out their advice for the next generation of innovators, change-makers, and leaders ↓ The hashtags in this tweet are: #IWD2023 #WomenInTech #GoogleCloud Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 7 The E-learning platform, @schooliocorp , started with an act of kindness, a cold DM, and a belief that schools must be reimagined for the modern world. Learn more about this women-led startup and how it reimagines e-learning with Google Cloud ↓ The hashtags in this tweet are: #GoogleCloud #schooliocorp Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 6 A good migration plan has 4️⃣ phases: assessment, planning, execution, and optimization. A great migration plan has 5️⃣ phases: discussion (i.e., join our Twitter Space on March 8), assessment, planning, execution, and optimization. Set a reminder ↓ The hashtags in this tweet are: #googlecloud #migration #assessment #planning #execution #optimization #discussion #twitterspace #march8 """ test = """ Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Feb 25 We heard it through the grapevine: - Document AI Workbench is GA and ready for production use through APIs and the Google Cloud Console - Deliver higher model accuracy with Workbench - Build production-ready models faster with Workbench Read more ↓ The hashtags in this tweet are: """ content = examples + test postBody = { "instances": [ { "content": content } ], "parameters": parameters } x = requests.post( endpoint, data = json.dumps( postBody ), headers = headers ) print( x.text )
from google.oauth2 import service_account import google.auth.transport.requests import requests import json def lambda_handler(event, context): project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) # print( credentials.token ) MODEL_ID="text-bison@001" headers = { "Content-Type": "application/json", "Authorization": "Bearer " + credentials.token } endpoint = f"https://{endpoint_id}/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{MODEL_ID}:predict" parameters = { "temperature": 0.2, "maxOutputTokens": 256, "topP": 0.95, "topK": 40 } examples = """Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 4h Google Cloud 🤝 @MongoDB : helping startups scale quicker, more safely, and more successfully. Learn how we\'re supporting high-potential startups via our dedicated startup programs—the Google for Startups Cloud Program and MongoDB for Startups Program ↓ The hashtags in this tweet are: #GoogleCloud #googlecloud #MongoDB #mongodb #startups #startup #scale #quicker #moresafely #moresuccessfully #GoogleforStartupsCloudProgram #MongoDBforStartupsProgram#mongodb Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 10h Women deliver impact across Google Cloud—from keeping the internet running to driving AI innovation to transforming our customers\' businesses. For IWD2023, check out their advice for the next generation of innovators, change-makers, and leaders ↓ The hashtags in this tweet are: #IWD2023 #WomenInTech #GoogleCloud Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 7 The E-learning platform, @schooliocorp , started with an act of kindness, a cold DM, and a belief that schools must be reimagined for the modern world. Learn more about this women-led startup and how it reimagines e-learning with Google Cloud ↓ The hashtags in this tweet are: #GoogleCloud #schooliocorp Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 6 A good migration plan has 4️⃣ phases: assessment, planning, execution, and optimization. A great migration plan has 5️⃣ phases: discussion (i.e., join our Twitter Space on March 8), assessment, planning, execution, and optimization. Set a reminder ↓ The hashtags in this tweet are: #googlecloud #migration #assessment #planning #execution #optimization #discussion #twitterspace #march8 """ test = """ Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Feb 25 We heard it through the grapevine: - Document AI Workbench is GA and ready for production use through APIs and the Google Cloud Console - Deliver higher model accuracy with Workbench - Build production-ready models faster with Workbench Read more ↓ The hashtags in this tweet are: """ content = examples + test postBody = { "instances": [ { "content": content } ], "parameters": parameters } x = requests.post( endpoint, data = json.dumps( postBody ), headers = headers ) print( x.text )
4.Lambdaにアップロード
4.Lambdaにアップロード
作成したtmpDirをzipファイルにしてlambdaにアップロードします。 Lambdaのコンソールから新しい関数をpythonで作成し、アップロード元からzipファイルを選択してアップロードします。 2MByteしかないので、あとでブラウザ上から修正できます。Lambdaの親フォルダの一つ下の階層に作成したlambda_function.pyが無い状態になっていれば、tmpDir内のすべてのフォルダとファイルを選択して移動します。
作成したtmpDirをzipファイルにしてlambdaにアップロードします。 Lambdaのコンソールから新しい関数をpythonで作成し、アップロード元からzipファイルを選択してアップロードします。 2MByteしかないので、あとでブラウザ上から修正できます。Lambdaの親フォルダの一つ下の階層に作成したlambda_function.pyが無い状態になっていれば、tmpDir内のすべてのフォルダとファイルを選択して移動します。
5.テスト実行
5.テスト実行
テストから実行して、ログ出力に「"content": "#GoogleCloud\n #DocumentAI\n #GA\n #production\n #APIs\n #GoogleCloudConsole\n #higher\n #modelaccuracy\n #Workbench\n #build\n #productionready\n #models\n #faster",」のような文字列が出てきました。
テストから実行して、ログ出力に「"content": "#GoogleCloud\n #DocumentAI\n #GA\n #production\n #APIs\n #GoogleCloudConsole\n #higher\n #modelaccuracy\n #Workbench\n #build\n #productionready\n #models\n #faster",」のような文字列が出てきました。
6.おわりに
6.おわりに
今回はハッシュタグを生成してもらいましたが、要約もtext-bisonに例を与えて、じゃあこれは?と聞くと答えてくれました。こんな便利なAIモデルが一般に公開され、使わせてもらえるということは素晴らしいですね。
今回はハッシュタグを生成してもらいましたが、要約もtext-bisonに例を与えて、じゃあこれは?と聞くと答えてくれました。こんな便利なAIモデルが一般に公開され、使わせてもらえるということは素晴らしいですね。

VertexAIの「text-bison」をLambdaから呼んでみました

VertexAI AI textbison AWS GCP GCP API Lambda This article describes how to call VertexAI's AI model textbison from AWS lambda to create hashtags. The author created a service account from the GCP console, got the authentication information from the service account programmatically, and used the authentication information to call the GCP API. The author then uploaded the code to Lambda and tested it successfully.
by JanitorJun 29, 2023
VertexAIのAIモデル「text-bison」をAWSのlambdaから呼んで、ハッシュタグを作成してもらいました。色々な種類のAIモデルが群雄割拠していて、いまいちどのAIモデルを使わせて頂くのが良いのかよくわからない状況の中で、「text-bison」というAIモデルの守備範囲がかなり広そうで、Vertex AIも「これでよくないですか?」とご提案頂いているようにも感じたので、「text-bison」をAPIで呼んでみることにしました。流れとしては、GCPのコンソールからサービスアカウントを作成しておき、プログラム上でサービスアカウント経由で認証情報を取得、その認証情報を使ってGCPのAPIをコールしました。今回の場合は、そのAPIの相手がVertex AIの「text-bison」であり、呼び元がAWSのLambda、となっています。
VertexAIのAIモデル「text-bison」をAWSのlambdaから呼んで、ハッシュタグを作成してもらいました。色々な種類のAIモデルが群雄割拠していて、いまいちどのAIモデルを使わせて頂くのが良いのかよくわからない状況の中で、「text-bison」というAIモデルの守備範囲がかなり広そうで、Vertex AIも「これでよくないですか?」とご提案頂いているようにも感じたので、「text-bison」をAPIで呼んでみることにしました。流れとしては、GCPのコンソールからサービスアカウントを作成しておき、プログラム上でサービスアカウント経由で認証情報を取得、その認証情報を使ってGCPのAPIをコールしました。今回の場合は、そのAPIの相手がVertex AIの「text-bison」であり、呼び元がAWSのLambda、となっています。

Jump Links

1.GCPの「IAMと管理」-> 「サービスアカウント」からサービスアカウントを作成し、「鍵」ファイルをダウンロードする
2.認証情報を取得するスクリプトを作成
3.スクリプトの続きを書く
4.Lambdaにアップロード
5.テスト実行
6.おわりに
1.GCPの「IAMと管理」-> 「サービスアカウント」からサービスアカウントを作成し、「鍵」ファイルをダウンロードする
1.GCPの「IAMと管理」-> 「サービスアカウント」からサービスアカウントを作成し、「鍵」ファイルをダウンロードする
下図のように「サービスアカウントを作成」をクリックします。 サービスアカウントを作成したら、作成したサービスアカウントをクリックし、「キー」タブに移動して、「鍵を追加」を押して鍵を作成し、「鍵」ファイルをダウンロードします。権限やロールの設定はここでは割愛します。
下図のように「サービスアカウントを作成」をクリックします。 サービスアカウントを作成したら、作成したサービスアカウントをクリックし、「キー」タブに移動して、「鍵を追加」を押して鍵を作成し、「鍵」ファイルをダウンロードします。権限やロールの設定はここでは割愛します。
2.認証情報を取得するスクリプトを作成
2.認証情報を取得するスクリプトを作成
とりあえず、作成したサービスアカウント経由で認証情報が取得できるか確認します。pythonで作成したため、あらかじめ仮想環境作成して、「google-auth」をフォルダにインストールしています。コマンドを実行した時点で、フォルダ(下の例の場合tmpDir)に大量にフォルダが出てきていれば問題ないです。 サービスアカウントのスコープは「Google API の OAuth 2.0 スコープ」(下記URL)に記載されている内容を設定します。 https://developers.google.com/identity/protocols/oauth2/scopes?hl=ja 今回の場合は、「AI Platform Training & Prediction API」に記載された2つのスコープを指定しています。プロジェクトIDとエンドポイントは下記の画像の箇所で確認できます。pythonスクリプトを実行して文字が大量に出てきたら、ちゃんと認証情報が取得できています。
とりあえず、作成したサービスアカウント経由で認証情報が取得できるか確認します。pythonで作成したため、あらかじめ仮想環境作成して、「google-auth」をフォルダにインストールしています。コマンドを実行した時点で、フォルダ(下の例の場合tmpDir)に大量にフォルダが出てきていれば問題ないです。 サービスアカウントのスコープは「Google API の OAuth 2.0 スコープ」(下記URL)に記載されている内容を設定します。 https://developers.google.com/identity/protocols/oauth2/scopes?hl=ja 今回の場合は、「AI Platform Training & Prediction API」に記載された2つのスコープを指定しています。プロジェクトIDとエンドポイントは下記の画像の箇所で確認できます。pythonスクリプトを実行して文字が大量に出てきたら、ちゃんと認証情報が取得できています。
コマンドプロンプト python -m venv tmpEnv tmpEnv\Scripts\activate mkdir tmpDir cd tmpDir pip install -U google-auth -t .
コマンドプロンプト python -m venv tmpEnv tmpEnv\Scripts\activate mkdir tmpDir cd tmpDir pip install -U google-auth -t .
# pythonスクリプト # tmpDir内でlambda_function.pyというファイル名で作成 from google.oauth2 import service_account import google.auth.transport.requests import requests import json project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) print( credentials.token )
# pythonスクリプト # tmpDir内でlambda_function.pyというファイル名で作成 from google.oauth2 import service_account import google.auth.transport.requests import requests import json project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) print( credentials.token )
3.スクリプトの続きを書く
3.スクリプトの続きを書く
上の画像のCurlの例のように、「Examples」と「Test」をhttpリクエストに載せてポストするだけです。大量にスクリプトを記載しているように見えますが、ほとんどが「Examples」であり、文字列です。5つの例を与えて、じゃあテストします、これは?と聞くと答えてくれる、ということですね。
上の画像のCurlの例のように、「Examples」と「Test」をhttpリクエストに載せてポストするだけです。大量にスクリプトを記載しているように見えますが、ほとんどが「Examples」であり、文字列です。5つの例を与えて、じゃあテストします、これは?と聞くと答えてくれる、ということですね。
from google.oauth2 import service_account import google.auth.transport.requests import requests import json def lambda_handler(event, context): project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) # print( credentials.token ) MODEL_ID="text-bison@001" headers = { "Content-Type": "application/json", "Authorization": "Bearer " + credentials.token } endpoint = f"https://{endpoint_id}/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{MODEL_ID}:predict" parameters = { "temperature": 0.2, "maxOutputTokens": 256, "topP": 0.95, "topK": 40 } examples = """Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 4h Google Cloud 🤝 @MongoDB : helping startups scale quicker, more safely, and more successfully. Learn how we\'re supporting high-potential startups via our dedicated startup programs—the Google for Startups Cloud Program and MongoDB for Startups Program ↓ The hashtags in this tweet are: #GoogleCloud #googlecloud #MongoDB #mongodb #startups #startup #scale #quicker #moresafely #moresuccessfully #GoogleforStartupsCloudProgram #MongoDBforStartupsProgram#mongodb Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 10h Women deliver impact across Google Cloud—from keeping the internet running to driving AI innovation to transforming our customers\' businesses. For IWD2023, check out their advice for the next generation of innovators, change-makers, and leaders ↓ The hashtags in this tweet are: #IWD2023 #WomenInTech #GoogleCloud Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 7 The E-learning platform, @schooliocorp , started with an act of kindness, a cold DM, and a belief that schools must be reimagined for the modern world. Learn more about this women-led startup and how it reimagines e-learning with Google Cloud ↓ The hashtags in this tweet are: #GoogleCloud #schooliocorp Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 6 A good migration plan has 4️⃣ phases: assessment, planning, execution, and optimization. A great migration plan has 5️⃣ phases: discussion (i.e., join our Twitter Space on March 8), assessment, planning, execution, and optimization. Set a reminder ↓ The hashtags in this tweet are: #googlecloud #migration #assessment #planning #execution #optimization #discussion #twitterspace #march8 """ test = """ Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Feb 25 We heard it through the grapevine: - Document AI Workbench is GA and ready for production use through APIs and the Google Cloud Console - Deliver higher model accuracy with Workbench - Build production-ready models faster with Workbench Read more ↓ The hashtags in this tweet are: """ content = examples + test postBody = { "instances": [ { "content": content } ], "parameters": parameters } x = requests.post( endpoint, data = json.dumps( postBody ), headers = headers ) print( x.text )
from google.oauth2 import service_account import google.auth.transport.requests import requests import json def lambda_handler(event, context): project_id = "プロジェクトID" endpoint_id = "エンドポイント" scopeURL0 = "https://www.googleapis.com/auth/cloud-platform" scopeURL1 = "https://www.googleapis.com/auth/cloud-platform.read-only" credentials = service_account.Credentials.from_service_account_file( '鍵ファイルへのパス', scopes=[ scopeURL0, scopeURL1 ] ) request = google.auth.transport.requests.Request() credentials.refresh(request) # print( credentials.token ) MODEL_ID="text-bison@001" headers = { "Content-Type": "application/json", "Authorization": "Bearer " + credentials.token } endpoint = f"https://{endpoint_id}/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{MODEL_ID}:predict" parameters = { "temperature": 0.2, "maxOutputTokens": 256, "topP": 0.95, "topK": 40 } examples = """Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 4h Google Cloud 🤝 @MongoDB : helping startups scale quicker, more safely, and more successfully. Learn how we\'re supporting high-potential startups via our dedicated startup programs—the Google for Startups Cloud Program and MongoDB for Startups Program ↓ The hashtags in this tweet are: #GoogleCloud #googlecloud #MongoDB #mongodb #startups #startup #scale #quicker #moresafely #moresuccessfully #GoogleforStartupsCloudProgram #MongoDBforStartupsProgram#mongodb Tokenize the hashtags of this tweet: Google Cloud @googlecloud · 10h Women deliver impact across Google Cloud—from keeping the internet running to driving AI innovation to transforming our customers\' businesses. For IWD2023, check out their advice for the next generation of innovators, change-makers, and leaders ↓ The hashtags in this tweet are: #IWD2023 #WomenInTech #GoogleCloud Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 7 The E-learning platform, @schooliocorp , started with an act of kindness, a cold DM, and a belief that schools must be reimagined for the modern world. Learn more about this women-led startup and how it reimagines e-learning with Google Cloud ↓ The hashtags in this tweet are: #GoogleCloud #schooliocorp Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Mar 6 A good migration plan has 4️⃣ phases: assessment, planning, execution, and optimization. A great migration plan has 5️⃣ phases: discussion (i.e., join our Twitter Space on March 8), assessment, planning, execution, and optimization. Set a reminder ↓ The hashtags in this tweet are: #googlecloud #migration #assessment #planning #execution #optimization #discussion #twitterspace #march8 """ test = """ Tokenize the hashtags of this tweet: Google Cloud @googlecloud · Feb 25 We heard it through the grapevine: - Document AI Workbench is GA and ready for production use through APIs and the Google Cloud Console - Deliver higher model accuracy with Workbench - Build production-ready models faster with Workbench Read more ↓ The hashtags in this tweet are: """ content = examples + test postBody = { "instances": [ { "content": content } ], "parameters": parameters } x = requests.post( endpoint, data = json.dumps( postBody ), headers = headers ) print( x.text )
4.Lambdaにアップロード
4.Lambdaにアップロード
作成したtmpDirをzipファイルにしてlambdaにアップロードします。 Lambdaのコンソールから新しい関数をpythonで作成し、アップロード元からzipファイルを選択してアップロードします。 2MByteしかないので、あとでブラウザ上から修正できます。Lambdaの親フォルダの一つ下の階層に作成したlambda_function.pyが無い状態になっていれば、tmpDir内のすべてのフォルダとファイルを選択して移動します。
作成したtmpDirをzipファイルにしてlambdaにアップロードします。 Lambdaのコンソールから新しい関数をpythonで作成し、アップロード元からzipファイルを選択してアップロードします。 2MByteしかないので、あとでブラウザ上から修正できます。Lambdaの親フォルダの一つ下の階層に作成したlambda_function.pyが無い状態になっていれば、tmpDir内のすべてのフォルダとファイルを選択して移動します。
5.テスト実行
5.テスト実行
テストから実行して、ログ出力に「"content": "#GoogleCloud\n #DocumentAI\n #GA\n #production\n #APIs\n #GoogleCloudConsole\n #higher\n #modelaccuracy\n #Workbench\n #build\n #productionready\n #models\n #faster",」のような文字列が出てきました。
テストから実行して、ログ出力に「"content": "#GoogleCloud\n #DocumentAI\n #GA\n #production\n #APIs\n #GoogleCloudConsole\n #higher\n #modelaccuracy\n #Workbench\n #build\n #productionready\n #models\n #faster",」のような文字列が出てきました。
6.おわりに
6.おわりに
今回はハッシュタグを生成してもらいましたが、要約もtext-bisonに例を与えて、じゃあこれは?と聞くと答えてくれました。こんな便利なAIモデルが一般に公開され、使わせてもらえるということは素晴らしいですね。
今回はハッシュタグを生成してもらいましたが、要約もtext-bisonに例を与えて、じゃあこれは?と聞くと答えてくれました。こんな便利なAIモデルが一般に公開され、使わせてもらえるということは素晴らしいですね。
© 2023 - Comytom LLC