-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from GoogleCloudPlatform/gemini-pro
Add Gemini Pro Slack bot
- Loading branch information
Showing
14 changed files
with
687 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Vertex AI Gemini Pro Slack bot example | ||
|
||
A Slack chat bot that uses the Gemini 1.0 Pro Vision model. It can accept images and text from Slack. | ||
|
||
## Deploying | ||
|
||
1. Navigate to Slack settings, `Tools & Settings > Manage apps`. | ||
2. Click on `Build` in the top right corner. | ||
3. Click on `Create new app` and select `From manifest`. | ||
4. Copy the contents [slack-manifest.yaml](slack-manifest.yaml) into the text box. | ||
5. Proceed with creation of the Slack application and install it to your workspace. | ||
6. You should now be able to view application credentials. Take the `App ID` and `Signing Secret` and place them in `terraform.tfvars` (`slack_app_id` and `slack_signing_key`). | ||
7. Next click on `OAuth & Permissions` and take the `Bot User OAuth Token` and place it in `terraform.tfvars` (`slack_token`). | ||
8. You can now run `terraform apply` to deploy the bot. After deployment, you should see the Cloud Run Json2Pubsub URL in the output. | ||
9. In Slack app settings, click on `Event Subscriptions` and replace the `Request URL` with the URL from the previous step. | ||
10. In Slack, create a channel and add the bot to it (channel settings, `Integrations > Add an app`). | ||
11. You can now discuss with the bot via private messages or tagging it in a channel! | ||
|
||
### Example Terraform connfiguraiton | ||
|
||
Example `terraform.tfvars` file: | ||
|
||
```hcl | ||
project_id = "<your-project-id>" | ||
region = "europe-west1" | ||
slack_token = "xoxb-...." # Slack OAuth token for workspace | ||
slack_signing_key = "8e................02f" # Slack signing key | ||
slack_app_id = "A........L" # App ID | ||
``` | ||
|
||
See the previous article: [Building an AI Slack Bot using Vertex Generative AI](https://taneli-leppa.medium.com/building-an-ai-slack-bot-using-vertex-generative-ai-d5f2c9e5e0b0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
terraform { | ||
required_version = ">= 1.4.4" | ||
required_providers { | ||
google = { | ||
source = "hashicorp/google" | ||
version = ">= 5.20.0" | ||
} | ||
google-beta = { | ||
source = "hashicorp/google-beta" | ||
version = ">= 5.20.0" | ||
} | ||
} | ||
} | ||
|
||
module "project" { | ||
source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric//modules/project?ref=daily-2024.03.27" | ||
name = var.project_id | ||
project_create = false | ||
services = [ | ||
"aiplatform.googleapis.com", | ||
"cloudfunctions.googleapis.com", | ||
"run.googleapis.com", | ||
"artifactregistry.googleapis.com" | ||
] | ||
} | ||
|
||
|
||
module "pubsub-topic" { | ||
source = "github.com/GoogleCloudPlatform/cloud-foundation-fabric//modules/pubsub?ref=daily-2024.03.27" | ||
project_id = module.project.project_id | ||
name = var.pubsub_topic | ||
iam = {} | ||
} | ||
|
||
resource "random_string" "random" { | ||
length = 8 | ||
special = false | ||
upper = false | ||
} | ||
|
||
module "function" { | ||
source = "../.." | ||
|
||
project_id = module.project.project_id | ||
region = var.region | ||
cloud_functions_v2 = true | ||
|
||
function_name = "gemini-pro-bot" | ||
|
||
service_account = "gemini-pro-bot" | ||
create_service_account = true | ||
|
||
pubsub_topic = module.pubsub-topic.id | ||
|
||
config = templatefile("${path.module}/slack-bot.yaml", { | ||
slack_token = var.slack_token | ||
slack_app_id = var.slack_app_id | ||
vertex_region = var.vertex_region | ||
vertex_model = var.vertex_model | ||
}) | ||
use_local_files = true | ||
local_files_path = "../.." | ||
|
||
bucket_name = format("gemini-pro-bot-build-%s", random_string.random.result) | ||
bucket_location = var.region | ||
|
||
function_roles = var.create_custom_role ? [] : ["vertexai-user"] | ||
|
||
deploy_json2pubsub = { | ||
enabled = true | ||
suffix = "-json2pubsub" | ||
control_cel = format(<<-EOT | ||
'x-slack-signature' in request.headers && | ||
'x-slack-request-timestamp' in request.headers && | ||
(request.unixtime - int(request.headers['x-slack-request-timestamp'])) < 300 && | ||
('v0='+hmacSHA256('%s', 'v0:'+request.headers['x-slack-request-timestamp']+':'+request.body)) == request.headers['x-slack-signature'] | ||
EOT | ||
, var.slack_signing_key) | ||
message_cel = "request.json" | ||
response_cel = "'challenge' in request.json ? request.json.challenge : 'OK'" | ||
public_access = true | ||
container_image = null | ||
min_instances = 0 | ||
max_instances = 10 | ||
grant_sa_user = null | ||
} | ||
} | ||
|
||
resource "google_project_iam_custom_role" "custom-role" { | ||
count = var.create_custom_role ? 1 : 0 | ||
project = module.project.project_id | ||
role_id = "vertexAiPredict" | ||
title = "Vertex AI predict only" | ||
description = "Only allow to perform online predictions" | ||
permissions = ["aiplatform.endpoints.predict"] | ||
stage = "BETA" | ||
} | ||
|
||
resource "google_project_iam_member" "custom-role-binding" { | ||
count = var.create_custom_role ? 1 : 0 | ||
project = module.project.project_id | ||
role = google_project_iam_custom_role.custom-role[0].id | ||
member = format("serviceAccount:%s", module.function.service_account) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
output "instructions" { | ||
value = <<-EOT | ||
Please use the following webhook URL for Slack event webhook: | ||
${module.function.json2pubsub_url} | ||
EOT | ||
} |
Oops, something went wrong.