This example demonstrates ways to integrate LLM models into a custom command line utility for use by developers both locally and in automation processes such as CICD pipelines.
This directory contains a sample cli implementation called devai, as well as a tutorial describing how to use it.
Execute commands in your terminal.
git clone https://github.com/GoogleCloudPlatform/genai-for-developers.git
cd genai-for-developers/devai-cliSet GCP project and location.
export PROJECT_ID=YOUR-GCP-PROJECT
export LOCATION=us-central1
gcloud config set project $PROJECT_IDObtain GCP user access credentials via a web flow. CLI will use these credentials to authenticate and make API calls.
gcloud auth application-default loginEnable Vertex AI and Secrets Manager APIs in your GCP project.
gcloud services enable \
aiplatform.googleapis.com \
cloudaicompanion.googleapis.com \
cloudresourcemanager.googleapis.com \
secretmanager.googleapis.comTo start, setup your virtualenv, install requirements and run the sample command
python3 -m venv venv
. venv/bin/activate
pip install -r src/requirements.txtTo create an installable CLI from the source, use setuptools to create the DEVAI cli with the following command from the project base folder.
pip install --editable ./srcOnce installed you can use the CLI with its short name devai as follows
devai review code -c ../sample-app/src/main/java
devai review performance -c ../sample-app/src/main/java
devai review security -c ../sample-app/src/main/java
devai review code -c ../sample-app/src/main/java/anthos/samples/bankofanthos/balancereader/BalanceReaderController.java
devai review testcoverage -c ../sample-app/src
devai document readme -c ../sample-app/src/main/
# Sample command to update README.md file and open GitHub PR
devai document readme -c ../sample-app/src/main/ -f "sample-app/README.md" -b "feature/docs-update"
devai document update-readme -f ../sample-app/README.md -c ../sample-app/src/main/java/
devai document releasenotes -c ../sample-app/src/main/java
devai document update-releasenotes -f ../sample-app/releasenotes.md -c ../sample-app/src/main/java/ -t "v1.2.3"
devai review blockers -c ../sample-app/pom.xml
devai review blockers -c ../sample-app/setup.md
devai review impact \
--current ~/github/repo/service-v1.0.0/modules/login \
--target ~/github/repo/service-v2.0.0/modules/login
devai review imgdiff \
-c /ui/main-page-after-upgrade.png \
-t /ui/main-page-before-upgrade.png
devai review image \
-f "/tmp/diagram.png" \
-p "Review and summarize this diagram"
devai review video \
-f "/tmp/video.mp4" \
-p "Review and summarize this video"
devai release notes_user_tag -t "v5.0.0"
devai release notes_user -s "main" -e "feature-branch-name"
devai rag load -r "https://github.com/GoogleCloudPlatform/genai-for-developers"
devai rag query -q "What does devai do"
devai prompt with_context
devai prompt with_msg
devai prompt with_msg_streamingTo uninstall the package run the following command
python setup.py develop -uTo deactivate virtual env run the following command
deactivateRun commands below to create service account and keys.
PROJECT_ID=$(gcloud config get-value project)
SERVICE_ACCOUNT_NAME='vertex-client'
DISPLAY_NAME='Vertex Client'
KEY_FILE_NAME='vertex-client-key'
gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME --display-name "$DISPLAY_NAME"
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/aiplatform.admin" --condition None
gcloud iam service-accounts keys create $KEY_FILE_NAME.json --iam-account=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/secretmanager.secretAccessor" --condition NoneAdd following environment variables/secrets to your CICD pipeline.
If you have JIRA, GitHub, GitLab and LangSmith integrations enabled, add additional env variables for respective systems, see details in sections below. See additional documentation for more details.
- GOOGLE_CLOUD_CREDENTIALS
- PROJECT_ID
- LOCATION
For GOOGLE_CLOUD_CREDENTIALS variable value, use service account key created in section above.
cat $KEY_FILE_NAME.jsonDevAI CLI can be added in any build pipeline following the examples below:
- name: Code Review
run: echo '## Code Review Results 🚀' >> $GITHUB_STEP_SUMMARY
- run: echo "$(devai review code -c ${{ github.workspace }}/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader)" >> $GITHUB_STEP_SUMMARY
shell: bashbuild-job:
stage: build
script:
.
.
- devai review code -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
- devai review performance -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
- devai review security -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereaderpipeline {
agent any
stages {
stage('build') {
steps {
dir("${env.WORKSPACE}/devai-cli") {
sh '''
python3 -m venv ./venv
. ./venv/bin/activate
./venv/bin/pip install -r src/requirements.txt
./venv/bin/pip install --editable ./src
'''
withCredentials([
file(credentialsId: 'GOOGLE_APPLICATION_CREDENTIALS', variable: 'GOOGLE_APPLICATION_CREDENTIALS'),
string(credentialsId: 'PROJECT_ID', variable: 'PROJECT_ID'),
string(credentialsId: 'LOCATION', variable: 'LOCATION'),
]) {
sh '''
./venv/bin/devai review code -c /bitnami/jenkins/home/workspace/genai-cicd_genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
./venv/bin/devai review performance -c /bitnami/jenkins/home/workspace/genai-cicd_genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
./venv/bin/devai review security -c /bitnami/jenkins/home/workspace/genai-cicd_genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
'''
}
}
}
}
}
} image: python:3.11-slim
pipelines:
default:
- step:
name: DevAI CLI
caches:
- pip
script:
- apt-get update && apt-get install -y git
.
.
- devai review code -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
- devai review performance -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
- devai review security -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
version: 2.1
jobs:
ai-insights-code-review:
docker:
- image: python:3.11-slim
steps:
- checkout
- run:
command: |
.
.
devai review code -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader There are multiple cloudbuild files included in order to facilitate local builds and tests as well as automated CICD for this repo.
First ensure you have an AR repo created to hold your image
gcloud artifacts repositories describe app-image-repo --location=$LOCATIONTo trigger a build in Cloud Build manually run the following command. This build file does not use the ${SHORT_SHA} tag as seen in the standard webhook model
gcloud builds submit . --config=build/cloudbuild-local.yaml \
--substitutions=_ARTIFACT_REGISTRY_REPO=app-image-repoTo test the CLI as it would be used in a typical pipeline, run the following command.
gcloud builds submit . --config=build/cloudbuild-pipeline-test.yaml
To work with the CLI inside the container, build it locally and run it with your .config folder mounted to provide access to gcloud credentials
docker build -t devai-img .
docker run -it -v ~/.config:/root/.config devai-imgOnce in the container run commands against the cli
devai echoTo publish manually
- Update version number in setup.py
- Follow semantic versioning
- versioned in order as follows (
.devN, aN, bN, rcN, <no suffix>, .postN)
- Retrieve a personal API key from https://pypi.org/manage/account/token/ to publish
- You will need rights to publish to the pypi project
- Run the commands below an supply your API key when prompted
pip install build twinerm -rf src/dist
rm -rf src/devai_cli.egg-info
python3 -m build src/
python3 -m twine upload src/dist/* --verboseThe cli is provided as a package on PyPi for demonstration purposes only. It is not intended for production use as is. To install the package for use locally or in CICD systems run the following command
Set environment variables in your local environment or in CICD pipeline environment variables.
export PROJECT_ID=YOUR_GCP_PROJECT_ID
export LOCATION=us-central1Install cli:
pip install devai-cliInstall specific version:
pip install devai-cli==0.0.0a1Test cli:
devai echoSetup information.
Setup information.
Setup information.
Setup information.
The prompts command provides a powerful template system for managing and executing AI prompts. It allows you to create, manage, and reuse prompt templates for different types of code analysis, documentation, and review tasks. Templates are stored in YAML format and can be organized by categories, making them easy to find and maintain.
# List all available prompts
devai prompts list
# Show details of a specific prompt
devai prompts show security/web-security.yaml
# Configure prompts directory
devai prompts config --set-path /path/to/prompts
# Initialize a prompts directory with sample templates
devai prompts init
# Create a new prompt template
devai prompts create
Each prompt template is a YAML file with the following structure:
metadata:
name: "Web Security Review"
description: "Review web application security best practices"
version: "1.0"
category: "security"
subcategory: "web-security"
author: "DevAI"
last_updated: "2024-03-25"
tags: ["security", "web", "review"]
configuration:
temperature: 0.7
max_tokens: 1024
output_format: "markdown"
prompt:
system_context: "You are a security expert reviewing web applications."
instruction: "Review the following web application code for security vulnerabilities and best practices."
examples:
- input: "Check this login form for security issues"
output: |
1. Missing CSRF token
2. Password field lacks minimum length requirement
3. No rate limiting on login attempts
validation:
required_sections: ["vulnerabilities", "recommendations"]
output_schema: {}
quality_checks: []-
Initializing Your Prompts Directory
# Create a new prompts directory with sample templates devai prompts init # Force initialization even if directory exists devai prompts init --force
-
Listing Available Prompts
# List all prompts devai prompts list # Filter by category devai prompts list --category security # Filter by subcategory devai prompts list --subcategory web-security # Filter by tags devai prompts list --tag security --tag web
-
Creating Custom Prompts
# Interactive prompt creation devai prompts create # Follow the prompts to enter: # - Prompt name # - Category # - Subcategory # - Description # - Tags
-
Managing Prompt Directories
# Show current prompts directory devai prompts config --show # Set custom prompts directory devai prompts config --set-path /path/to/prompts # Reset to use package prompts devai prompts config --reset
The CLI supports a hierarchical prompt system:
- Package Prompts: Default prompts included with the CLI
- User Prompts: Custom prompts in your prompts directory
- Overrides: User prompts with the same name as package prompts will override the package version
Example of overriding a package prompt:
# 1. Initialize your prompts directory
devai prompts init
# 2. Edit the web-security.yaml file to customize it
vim ~/.devai/prompts/security/web-security.yaml
# 3. Your version will now be used instead of the package version
devai prompts show security/web-security.yaml-
Organization
- Use categories and subcategories to organize prompts
- Add descriptive tags for better filtering
- Keep prompt names clear and consistent
-
Content
- Write clear, specific instructions
- Include relevant examples
- Use markdown formatting for better readability
- Keep system context focused and relevant
-
Version Control
- Consider versioning your prompts
- Document changes in the
last_updatedfield - Use semantic versioning for major changes
-
Validation
- Define required sections for consistent output
- Add quality checks where appropriate
- Use output schemas for structured responses
-
Security Review Template
metadata: name: "API Security Review" category: "security" subcategory: "api-security" tags: ["security", "api", "review"] prompt: system_context: "You are an API security expert." instruction: | Review the API endpoints for: 1. Authentication mechanisms 2. Authorization controls 3. Input validation 4. Rate limiting 5. Error handling
-
Performance Optimization Template
metadata: name: "Database Query Optimization" category: "performance" subcategory: "database" tags: ["performance", "database", "optimization"] prompt: system_context: "You are a database optimization expert." instruction: | Analyze the database queries for: 1. Index usage 2. Query patterns 3. Connection management 4. N+1 query issues
-
Prompt Not Found
- Check if the prompt exists in the correct directory
- Verify the path is correct
- Ensure the file has a
.yamlextension
-
Override Not Working
- Confirm the file name matches exactly
- Check file permissions
- Verify the prompts directory is correctly configured
-
Configuration Issues
- Use
devai prompts config --showto verify settings - Check directory permissions
- Ensure YAML syntax is valid
- Use
