Skip to content

Commit

Permalink
Merge pull request #65 from GoogleCloudPlatform/gemini-pro
Browse files Browse the repository at this point in the history
Add Gemini Pro Slack bot
  • Loading branch information
rosmo authored Mar 27, 2024
2 parents 904e700 + 08563d0 commit 1f9ec13
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 47 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Out of the box, you'll have the following functionality available as examples:

| Title | Example use cases | Samples |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Vertex AI | A Slack bot using Vertex AI Generative AI models. | [Vertex AI Slack bot](examples/vertexgenai-slack-bot) (also see [blog post](https://taneli-leppa.medium.com/building-an-ai-slack-bot-using-vertex-generative-ai-d5f2c9e5e0b0)) |
| Vertex AI | A Slack bot using Vertex AI Generative AI models. | [Vertex AI Slack bot](examples/vertexgenai-slack-bot) (also see [blog post](https://taneli-leppa.medium.com/building-an-ai-slack-bot-using-vertex-generative-ai-d5f2c9e5e0b0))<br/>[Multi-modal Gemini Slack bot](examples/gemini-pro-slack-bot/) |
| Budget alerts | Get email if a project's budget exceeds certain limit. For more information, see [How to set up programmatic notifications from billing budgets](https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications). | [Budget alerts](examples/budget-config.yaml) |
| Cloud Security Command Center | Send emails when a new security finding is made (see [how to set up finding notifications from SCC](https://cloud.google.com/security-command-center/docs/how-to-notifications)), or create new findings from any Pub/Sub message. | [Email notifications of findings](examples/scc-config.yaml)<br />[Create findings from Cloud IDS](examples/scc-cloud-ids.yaml)<br />[Create custom findings](examples/scc-finding-config.yaml) |
| Containers | Synchronize container images from Artifactory to Artifact Registry. | [Artifactory to Artifact Registry.](examples/artifactory-to-artifact-registry) |
Expand Down Expand Up @@ -138,6 +138,7 @@ The YAML file is structured of the following top level keys:
- `pipeline`: a list of processors and/or outputs to run in sequence.
- `type`: what processor or output to run (eg. `processor.genericjson` or `output.logger`)
- `variables`: Additional variables to set before invoking this processor/output.
- `description`: A description that gets printed in the logs.
- `config`: configuration of the processor or output
- `runIf`: if this evaluates to empty, the processor/output is not run
- `stopIf`: if this evalues to non-empty, the processing is stopped immediately (before the processor/output is run)
Expand Down
3 changes: 3 additions & 0 deletions docs/build/docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,9 @@
* [`SlackProcessor.call_slack()`](processors.md#processors.slack.SlackProcessor.call_slack)


* [`SlackProcessor.download_slack()`](processors.md#processors.slack.SlackProcessor.download_slack)


* [`SlackProcessor.get_default_config_key()`](processors.md#processors.slack.SlackProcessor.get_default_config_key)


Expand Down
28 changes: 27 additions & 1 deletion docs/build/docs/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,25 @@ Slack processor for fetching messages.
* **token** (*str*) – A Slack Bot User OAuth Token.


* **api** (*str*) – One of: conversations.list, conversations.history, conversations.replies,
* **api** (*str*) – One of: conversations.list, conversations.history, conversations.replies


* **(****str** (*mode*) – api or processMessages (default api)


* **optional** – api or processMessages (default api)


* **multimodal** (*bool**, **optional*) – Use multi-modal processing in processMessages.


* **messages** (*list**, **optional*) – List of messages to process.


* **appId** (*str**, **optional*) – The app ID to detect bot messages.


* **prompt** (*str**, **optional*) – Initial message to append to the beginning of the conversation.


* **request** (*dict*) – The API call body.
Expand All @@ -787,6 +805,8 @@ Slack processor for fetching messages.

#### call_slack(api, token, request, urlencoded=False)

#### download_slack(url, token)

#### get_default_config_key()

#### process(output_var='slack')
Expand Down Expand Up @@ -854,6 +874,12 @@ Vertex AI Generative AI processor.
* **project** (*str**, **optional*) – Google Cloud project ID.


* **method** (*str**, **optional*) – Method to call, by default: “predict”


* **returnErrors** (*bool**, **optional*) – Set to true to return errors


* **request** (*dict*) – Request.


Expand Down
31 changes: 31 additions & 0 deletions examples/gemini-pro-slack-bot/README.md
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)
119 changes: 119 additions & 0 deletions examples/gemini-pro-slack-bot/main.tf
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)
}

20 changes: 20 additions & 0 deletions examples/gemini-pro-slack-bot/outputs.tf
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
}
Loading

0 comments on commit 1f9ec13

Please sign in to comment.