{"id":18258890,"url":"https://github.com/pottava/chatbot-on-googlecloud","last_synced_at":"2025-04-08T23:27:18.958Z","repository":{"id":245713288,"uuid":"818897598","full_name":"pottava/chatbot-on-googlecloud","owner":"pottava","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-24T03:32:02.000Z","size":39,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T18:20:57.656Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pottava.png","metadata":{"files":{"readme":"README-en.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-23T07:31:00.000Z","updated_at":"2024-06-24T03:32:05.000Z","dependencies_parsed_at":"2024-06-23T16:44:21.875Z","dependency_job_id":"df9b4522-1a04-46cf-8553-2da8e9830dec","html_url":"https://github.com/pottava/chatbot-on-googlecloud","commit_stats":null,"previous_names":["pottava/chatbot-on-googlecloud"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pottava%2Fchatbot-on-googlecloud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pottava%2Fchatbot-on-googlecloud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pottava%2Fchatbot-on-googlecloud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pottava%2Fchatbot-on-googlecloud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pottava","download_url":"https://codeload.github.com/pottava/chatbot-on-googlecloud/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247945040,"owners_count":21022677,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T10:35:19.476Z","updated_at":"2025-04-08T23:27:18.933Z","avatar_url":"https://github.com/pottava.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"A chatbot sample on Google Cloud\n===\n\n## Basic configurations\n\n### 1.1. Define an environment\n\nSet your project as environment variables.\n\n```bash\ngcloud config set project \"## Your project ID here ##\"\nexport PROJECT_ID=$( gcloud config get-value project )\nexport PROJECT_NUMBER=$( gcloud projects list --filter=\"${GOOGLE_CLOUD_PROJECT}\" --format=\"value(PROJECT_NUMBER)\" )\n```\n\nSpecify Tokyo as a region we use.\n\n```bash\nexport GOOGLE_CLOUD_REGION=asia-northeast1\n```\n\nIt is recommended to create resources with different environment names in this tutorial, like development and production. We will assume the development environment and prefix it with `dev`.\n\n```bash\nexport ENVIRONMENT_NAME=dev\n```\n\nIf multiple users are using one project, try using the verified email address as the username.\n\n```bash\nexport ACCOUNT_EMAIL=$( gcloud auth list --filter 'status:ACTIVE' --format 'value(account)' )\nexport ENVIRONMENT_NAME=\"dev-$( echo ${ACCOUNT_EMAIL} | cut -d \"@\" -f 1 )\"\n```\n\n### 1.2. Enable Google Cloud APIs\n\nIn Google Cloud, you can use resources by enabling **Service APIs** for each project. Now please enable the services you need.\n\n```bash\ngcloud services enable compute.googleapis.com run.googleapis.com bigquery.googleapis.com \\\n    discoveryengine.googleapis.com storage.googleapis.com artifactregistry.googleapis.com \\\n    cloudbuild.googleapis.com cloudresourcemanager.googleapis.com\n```\n\n## Basic services\n\n### 2.1. Google Cloud Storage\n\nCreate a bucket to upload the files that will be used as evidence for the RAG system.\n\n```bash\nexport BUCKET_NAME=\"${ENVIRONMENT_NAME}-rag-storage\"\ngcloud storage buckets create \"gs://${BUCKET_NAME}\" --location \"${GOOGLE_CLOUD_REGION}\" \\\n    --uniform-bucket-level-access --public-access-prevention --enable-autoclass\n```\n\nDownload a suitable file as sample.pdf and upload it to your bucket.\n\n```bash\ngcloud storage cp sample.pdf gs://${BUCKET_NAME}/rag/jp/ja/car/2024/manual/sample.pdf\n```\n\n### 2.2. Vertex AI Search\n\n1. Go to https://console.cloud.google.com/gen-app-builder/start and enable it.\n2. Select \"Search\" under \"Select app type\"\n3. Enter your app name, company or organization name as appropriate, and select \"Global\" as the app location\n4. Proceed to creating a data store, select \"Cloud Storage\" as the data source, specify the folder to import, and click \"Create\".\n5. Return to the app creation screen, select the data source and click \"Create\"\n\nThis will create a vector database and set up a search system.\n\n### 2.3. IAM service account\n\nSet the permissions required for your Cloud Run application.\n\n```sh\ngcloud iam service-accounts create \"${ENVIRONMENT_NAME}-chatbot\" \\\n    --description \"Service Account for Chatbot applications\"\ngcloud projects add-iam-policy-binding \"${PROJECT_ID}\" \\\n    --member \"serviceAccount:${ENVIRONMENT_NAME}-chatbot@${PROJECT_ID}.iam.gserviceaccount.com\" \\\n    --role \"roles/discoveryengine.editor\"\ngcloud projects add-iam-policy-binding \"${PROJECT_ID}\" \\\n    --member \"serviceAccount:${ENVIRONMENT_NAME}-chatbot@${PROJECT_ID}.iam.gserviceaccount.com\" \\\n    --role \"roles/bigquery.dataEditor\"\ngcloud storage buckets add-iam-policy-binding \"gs://${BUCKET_NAME}\" \\\n    --member \"serviceAccount:${ENVIRONMENT_NAME}-chatbot@${PROJECT_ID}.iam.gserviceaccount.com\" \\\n    --role \"roles/storage.admin\"\n```\n\n### 2.4. Cloud Run\n\nFirst, create a Cloud Run service using the sample application.\n\n```bash\ngcloud run deploy \"${ENVIRONMENT_NAME}-chatbot\" --region \"${GOOGLE_CLOUD_REGION}\" \\\n    --platform \"managed\" --cpu 1.0 --memory 512Mi --image gcr.io/cloudrun/hello \\\n    --service-account \"${ENVIRONMENT_NAME}-chatbot@${PROJECT_ID}.iam.gserviceaccount.com\" \\\n    --execution-environment gen2 --no-allow-unauthenticated\n```\n\nGrant permissions to allow communication if the request is from the service account created in 2.2.\n\n```bash\ngcloud run services add-iam-policy-binding \"${ENVIRONMENT_NAME}-chatbot\" --region \"${GOOGLE_CLOUD_REGION}\" \\\n    --member \"serviceAccount:${ENVIRONMENT_NAME}-chatbot@${PROJECT_ID}.iam.gserviceaccount.com\" \\\n    --role \"roles/run.invoker\"\n```\n\nLet's connect to the service. The following command proxies to the service.\n\n```bash\ngcloud beta run services proxy --region \"${GOOGLE_CLOUD_REGION}\" \"${ENVIRONMENT_NAME}-chatbot\"\n```\n\nIn this state, access to http://localhost:8080 will be forwarded to the service on Cloud Run.\n\n### 2.5. BigQuery\n\nCreate a dataset,\n\n```bash\nexport BQ_DATASET_ID=${ENVIRONMENT_NAME//-/_}\nbq --location \"${GOOGLE_CLOUD_REGION}\" mk --dataset \"${BQ_DATASET_ID}\"\n```\n\nAnd a table.\n\n```bash\nbq mk --table --description \"Q\u0026A table - ${ENVIRONMENT_NAME}\" \\\n    --schema 'ts:TIMESTAMP,env:STRING,ver:STRING,q:STRING,a:STRING' \\\n    --time_partitioning_field ts --time_partitioning_type DAY \\\n    \"${BQ_DATASET_ID}.qna\"\n```\n\n## Developing GenAI applications\n\n### 3.1. Start the API server\n\nCheck the data store ID on the console of Vertex AI Search, and export as a following environment variable.\n\n```bash\nexport VAIS_DATASTORE_ID=\n```\n\nAfter passing Google Cloud authentication,\n\n```bash\ngcloud auth application-default login\n```\n\nLet's try running the application locally.\n\n```bash\ncd src\npnpm install\nnpm run lint\nGOOGLE_CLOUD_PROJECT=\"${PROJECT_ID}\" PORT=9000 npm start\n```\n\nTry execute the following command from another terminal.\n\n```bash\ncurl -sXPOST -H 'Content-Type: application/json' -d '{\"q\":\"Hello :)\"}' http://localhost:9000/api/v1/chat\n```\n\n### 3.2. Docker containers\n\nLet's build it into a docker container.\n\n```bash\ndocker build -t chatbot .\n```\n\nStart it locally, and once you can confirm that it works properly as before,\n\n```bash\ndocker run --name chatbot -d --rm -p 9000:8080 \\\n    -v \"${HOME}/.config/gcloud:/gcp/config:ro\" -e CLOUDSDK_CONFIG=/gcp/config \\\n    -e GOOGLE_APPLICATION_CREDENTIALS=/gcp/config/application_default_credentials.json \\\n    -e GOOGLE_CLOUD_PROJECT=\"${PROJECT_ID}\" -e VAIS_DATASTORE_ID=\"${VAIS_DATASTORE_ID}\" \\\n    -e BQ_DATASET_ID=\"${BQ_DATASET_ID}\" -e CURRENT_VERSION=\"docker\" \\\n    chatbot\ndocker logs -f chatbot\n```\n\nRemove the container.\n\n```bash\ndocker rm -f chatbot\n```\n\n### 3.3. Artifact Registry\n\nIn the Artifact Registry, create a repository to store your application.\n\n```bash\ngcloud artifacts repositories create \"${ENVIRONMENT_NAME}-chatbot\" \\\n    --repository-format docker --location \"${GOOGLE_CLOUD_REGION}\" \\\n    --description \"${ENVIRONMENT_NAME}'s chatbot\"\n```\n\nBuild the app and push it to the repository.\n\n```sh\ngcloud builds submit \\\n    --tag \"${GOOGLE_CLOUD_REGION}-docker.pkg.dev/${PROJECT_ID}/${ENVIRONMENT_NAME}-chatbot/front:dev\" \\\n    .\n```\n\n### 3.4. Deploy to Cloud Run\n\nLet's run it on the cloud.\n\n```bash\ngcloud run deploy \"${ENVIRONMENT_NAME}-chatbot\" --region \"${GOOGLE_CLOUD_REGION}\" \\\n    --image \"${GOOGLE_CLOUD_REGION}-docker.pkg.dev/${PROJECT_ID}/${ENVIRONMENT_NAME}-chatbot/front:dev\" \\\n    --update-env-vars \"GOOGLE_CLOUD_PROJECT=${PROJECT_ID},VAIS_DATASTORE_ID=${VAIS_DATASTORE_ID},BQ_DATASET_ID=${BQ_DATASET_ID}\" \\\n    --timeout 60\n```\n\nYou can proxy the service,\n\n```bash\ngcloud beta run services proxy --region \"${GOOGLE_CLOUD_REGION}\" \"${ENVIRONMENT_NAME}-chatbot\"\n```\n\nThen execute the following command from another terminal.\n\n```bash\ncurl -sXPOST -H 'Content-Type: application/json' -d '{\"q\":\"Can I use Bluetooth?\"}' http://localhost:8080/api/v1/chat\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpottava%2Fchatbot-on-googlecloud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpottava%2Fchatbot-on-googlecloud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpottava%2Fchatbot-on-googlecloud/lists"}