การทำงานอัตโนมัติในการตรวจสอบโค้ดด้วย GenAI

1. ภาพรวม

ในแล็บนี้ คุณจะได้ตั้งค่าไปป์ไลน์ CICD และผสานรวมกับ Gemini เพื่อทำให้ขั้นตอนการตรวจสอบโค้ดเป็นแบบอัตโนมัติ

9dde56ad139b9553.png

สิ่งที่คุณจะได้เรียนรู้

ในแล็บนี้ คุณจะได้เรียนรู้วิธีทำสิ่งต่อไปนี้

  • วิธีเพิ่มขั้นตอนการทำงานอัตโนมัติของการตรวจสอบโค้ด GenAI ใน GitHub, GitLab และ CircleCI
  • วิธีใช้เอเจนต์และชุดเครื่องมือ LangChain ReAct เพื่อทำให้งานต่างๆ เป็นแบบอัตโนมัติ เช่น การแสดงความคิดเห็นในปัญหาของ GitLab และการเปิดคำขอใน JIRA

ข้อกำหนดเบื้องต้น

  • แล็บนี้มีสมมติฐานว่าคุณคุ้นเคยกับสภาพแวดล้อมของ Cloud Console และ Cloud Shell

2. การตั้งค่าและข้อกำหนด

การตั้งค่าโปรเจ็กต์ Cloud

  1. ลงชื่อเข้าใช้ Google Cloud Console แล้วสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี

fbef9caa1602edd0.png

a99b7ace416376c4.png

5e3ff691252acf41.png

  • ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ คุณอัปเดตได้ทุกเมื่อ
  • รหัสโปรเจ็กต์จะไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและเปลี่ยนแปลงไม่ได้ (เปลี่ยนไม่ได้หลังจากตั้งค่าแล้ว) Cloud Console จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งโดยปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นคืออะไร ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยทั่วไปจะระบุเป็น PROJECT_ID) หากไม่ชอบรหัสที่สร้างขึ้น คุณอาจสร้างรหัสแบบสุ่มอีกรหัสหนึ่งได้ หรือคุณอาจลองใช้ชื่อของคุณเองและดูว่ามีชื่อนั้นหรือไม่ คุณจะเปลี่ยนแปลงรหัสนี้หลังจากขั้นตอนนี้ไม่ได้ และรหัสจะคงอยู่ตลอดระยะเวลาของโปรเจ็กต์
  • โปรดทราบว่ายังมีค่าที่ 3 ซึ่งคือหมายเลขโปรเจ็กต์ที่ API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 นี้ได้ในเอกสารประกอบ
  1. จากนั้นคุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำตาม Codelab นี้จะไม่มีค่าใช้จ่ายมากนัก หรืออาจไม่มีค่าใช้จ่ายเลย หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่สร้างขึ้นหรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD

การตั้งค่าสภาพแวดล้อม

เปิด Gemini Chat

bc3c899ac8bcf488.png

หรือพิมพ์ "ขอความช่วยเหลือจาก Gemini" ในแถบค้นหา

e1e9ad314691368a.png

วิธีเปิดใช้ Cloud AI Companion API

66cb6e561e384bbf.png

คลิก "Start chatting" แล้วทำตามตัวอย่างคำถามหรือพิมพ์พรอมต์ของคุณเองเพื่อลองใช้

5482c153eef23126.png

พรอมต์ให้ลองใช้

  • อธิบาย Cloud Run ใน 5 ประเด็นสำคัญ
  • คุณเป็นผู้จัดการผลิตภัณฑ์ Google Cloud Run โปรดอธิบาย Cloud Run ให้แก่นักเรียน/นักศึกษาโดยใช้ประเด็นสำคัญสั้นๆ 5 ข้อ
  • คุณเป็นผู้จัดการผลิตภัณฑ์ Google Cloud Run โปรดอธิบาย Cloud Run ให้กับนักพัฒนา Kubernetes ที่ได้รับการรับรองโดยใช้ประเด็นสำคัญสั้นๆ 5 ข้อ
  • คุณเป็นผู้จัดการผลิตภัณฑ์ Google Cloud Run โปรดอธิบายว่าเมื่อใดที่คุณควรใช้ Cloud Run แทน GKE ให้กับนักพัฒนาแอปอาวุโสใน 5 ประเด็นสั้นๆ

ดูข้อมูลเพิ่มเติมเกี่ยวกับการเขียนพรอมต์ให้ดียิ่งขึ้นได้ในคู่มือการใช้พรอมต์

วิธีที่ Gemini สำหรับ Google Cloud ใช้ข้อมูลของคุณ

ความมุ่งมั่นของ Google ในการปกป้องความเป็นส่วนตัว

Google เป็นหนึ่งในบริษัทแรกๆ ในอุตสาหกรรมที่เผยแพร่ความมุ่งมั่นด้านความเป็นส่วนตัวของ AI/ML ซึ่งระบุความเชื่อของเราว่าลูกค้าควรมีการรักษาความปลอดภัยและการควบคุมข้อมูลในระดับสูงสุดที่จัดเก็บไว้ในระบบคลาวด์

ข้อมูลที่คุณส่งและรับ

คำถามที่คุณถาม Gemini รวมถึงข้อมูลอินพุตหรือโค้ดที่คุณส่งให้ Gemini วิเคราะห์หรือทำให้เสร็จสมบูรณ์เรียกว่าพรอมต์ คำตอบหรือการเติมโค้ดที่คุณได้รับจาก Gemini เรียกว่าคำตอบ Gemini จะไม่ใช้พรอมต์ของคุณหรือคำตอบของ Gemini เป็นข้อมูลในการฝึกโมเดล

การเข้ารหัสพรอมต์

เมื่อคุณส่งพรอมต์ไปยัง Gemini ระบบจะเข้ารหัสข้อมูลของคุณขณะรับส่งเป็นอินพุตไปยังโมเดลพื้นฐานใน Gemini

ข้อมูลโปรแกรมที่สร้างจาก Gemini

Gemini ได้รับการฝึกด้วยโค้ดของ Google Cloud ที่เป็นของบุคคลที่หนึ่ง รวมถึงโค้ดของบุคคลที่สามที่เลือก คุณเป็นผู้รับผิดชอบด้านความปลอดภัย การทดสอบ และประสิทธิภาพของโค้ด รวมถึงการเติมโค้ด การสร้าง หรือการวิเคราะห์ที่ Gemini มอบให้คุณ

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีที่ Google จัดการพรอมต์ของคุณ

3. ตัวเลือกในการทดสอบพรอมต์

หากต้องการเปลี่ยน/ขยายข้อความแจ้ง devai cli ที่มีอยู่ คุณมีตัวเลือกหลายอย่างในการดำเนินการดังกล่าว

Vertex AI Studio เป็นส่วนหนึ่งของแพลตฟอร์ม Vertex AI ของ Google Cloud ซึ่งออกแบบมาโดยเฉพาะเพื่อลดความซับซ้อนและเร่งการพัฒนาและการใช้โมเดล Generative AI

Google AI Studio เป็นเครื่องมือบนเว็บสำหรับสร้างต้นแบบและทดลองใช้การออกแบบพรอมต์และ Gemini API ลงชื่อสมัครใช้ Gemini 1.5 Pro ที่มีหน้าต่างบริบทขนาด 1 ล้านโทเค็น หรือดูข้อมูลเพิ่มเติม

เว็บแอป Google Gemini (gemini.google.com) เป็นเครื่องมือบนเว็บที่ออกแบบมาเพื่อช่วยให้คุณสำรวจและใช้ประโยชน์จากความสามารถของโมเดล AI ของ Google Gemini

4. สร้างบัญชีบริการ

เปิดใช้งาน Cloud Shell โดยคลิกไอคอนทางด้านขวาของแถบค้นหา

3e0c761ca41f315e.png

ในเทอร์มินัลที่เปิดอยู่ ให้เปิดใช้บริการที่จำเป็นเพื่อใช้ Vertex AI API และ Gemini Chat

gcloud services enable \
    aiplatform.googleapis.com \
    cloudaicompanion.googleapis.com \
    cloudresourcemanager.googleapis.com \
    secretmanager.googleapis.com

หากระบบแจ้งให้ให้สิทธิ์ ให้คลิก "ให้สิทธิ์" เพื่อดำเนินการต่อ

6356559df3eccdda.png

เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างบัญชีบริการและคีย์ใหม่

คุณจะใช้บัญชีบริการนี้เพื่อทำการเรียก API ไปยัง Vertex AI Gemini API จากไปป์ไลน์ CICD

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 projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/secretmanager.secretAccessor" --condition None

gcloud iam service-accounts keys create $KEY_FILE_NAME.json --iam-account=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com

5. Fork ที่เก็บ GitHub ไปยังที่เก็บ GitHub ส่วนตัว

ไปที่ https://github.com/GoogleCloudPlatform/genai-for-developers/fork แล้วเลือกรหัสผู้ใช้ GitHub ของคุณเป็นเจ้าของ

ยกเลิกการเลือกตัวเลือกเพื่อคัดลอกเฉพาะสาขา "หลัก"

โปรดคลิกที่ "Create fork"

6. เปิดใช้เวิร์กโฟลว์ GitHub Actions

เปิดที่เก็บ GitHub ที่แยกสาขาในเบราว์เซอร์ แล้วสลับไปที่แท็บ "Actions" เพื่อเปิดใช้เวิร์กโฟลว์

1cd04db9b37af7cf.png

7. เพิ่มข้อมูลลับของที่เก็บ

สร้างข้อมูลลับของที่เก็บในส่วน "Settings / Secrets and variables / Actions" ในที่เก็บ GitHub ที่แยก

เพิ่มข้อมูลลับของที่เก็บโดยใช้ชื่อ "GOOGLE_API_CREDENTIALS"

94cbe2778bef25eb.png

เปลี่ยนไปที่หน้าต่าง/แท็บ Google Cloud Shell แล้วเรียกใช้คำสั่งด้านล่างในเทอร์มินัล Cloud Shell

cat ~/vertex-client-key.json

คัดลอกเนื้อหาของไฟล์และวางเป็นค่าสำหรับข้อมูลลับ

915579a97f8f2ced.png

เพิ่มPROJECT_IDลับด้วยรหัสโปรเจ็กต์ Qwiklabs เป็นค่า 4fa92833ce615a36.png

8. เรียกใช้เวิร์กโฟลว์ GitHub Actions

ไปที่ที่เก็บ GitHub ในเบราว์เซอร์แล้วเรียกใช้เวิร์กโฟลว์

เวิร์กโฟลว์ได้รับการกำหนดค่าให้ทำงานเมื่อมีการพุชโค้ดหรือการดำเนินการด้วยตนเอง

เลือก "GenAI For Developers" ในเวิร์กโฟลว์ทั้งหมด แล้วคลิก "Run workflow" โดยใช้กิ่ง "main"

da11273b4b54f7b6.png

ตรวจสอบผลลัพธ์:

cf49aa41980aacc5.png

ผลลัพธ์ของคำสั่งความครอบคลุมของการทดสอบ:

devai review testcoverage -c ${{ github.workspace }}/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

3b21bd4639524763.png

ผลลัพธ์ของคำสั่งการตรวจสอบโค้ด:

devai review code -c ${{ github.workspace }}/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

4876dbc2e0042943.png

ผลลัพธ์ของคำสั่งตรวจสอบประสิทธิภาพ

devai review performance -c ${{ github.workspace }}/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

98dd2472b2e819bf.png

ผลลัพธ์ของคำสั่งตรวจสอบความปลอดภัย:

devai review security -c ${{ github.workspace }}/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

7d180a763db92d56.png

Blockers review command's results:

devai review blockers -c ${{ github.workspace }}/sample-app/pom.xml

726175e874aefcf.png

9. โคลนที่เก็บ

กลับไปที่เทอร์มินัล Cloud Shell แล้วโคลนที่เก็บ

สร้างโฟลเดอร์สำหรับที่เก็บ GitHub

mkdir github
cd github

เปลี่ยน YOUR-GITHUB-USERID เป็นรหัสผู้ใช้ GitHub ก่อนเรียกใช้คำสั่ง

ตั้งค่าชื่อผู้ใช้และอีเมล Git ในเทอร์มินัล

อัปเดตค่าก่อนเรียกใช้คำสั่ง

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
git clone https://github.com/YOUR-GITHUB-USERID/genai-for-developers.git 

เปลี่ยนโฟลเดอร์และเปิดไฟล์เวิร์กโฟลว์ใน Cloud Shell Editor

cd genai-for-developers

cloudshell edit .github/workflows/devai-review.yml 

รอจนกว่าไฟล์การกำหนดค่าจะแสดงใน IDE

9e81e5a79d421eac.png

10. เปิดใช้ Gemini Code Assist

คลิกไอคอน "Gemini" ที่มุมขวาล่าง 7c891e32c055c0e4.png

คลิก "Login to Google Cloud" และ "Select a Google Cloud Project"

f5318f22c91ecaa8.png

6b7203ffdd8485fa.png

fb8d42a6bc8a260f.png

เลือกโปรเจ็กต์ Qwiklabs จากหน้าต่างป๊อปอัป

f661047956a6d6f9.png

11. อธิบายโค้ดด้วย Gemini Code Assist

คลิกขวาที่ใดก็ได้ในdevai-review.ymlไฟล์ แล้วเลือกGemini Code Assist > Explainนี้

41fb7da002bdf43.png

คำอธิบายการตรวจสอบ:

7724d52e54918c00.png

12. เรียกใช้ DEVAI CLI ในเครื่อง

กลับไปที่ Cloud Shell Editor แล้วเปิดเทอร์มินัลใหม่

149218baaf30865f.png

กลับไปที่เทอร์มินัล Cloud Shell แล้วเรียกใช้คำสั่งด้านล่างเพื่อติดตั้ง devai ในเครื่อง

pip3 install devai-cli

ติดตั้ง CLI แล้ว แต่ไม่ได้อยู่ใน PATH

WARNING: The script devai is installed in '/home/student_00_478dfeb8df15/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

เรียกใช้คำสั่งด้านล่างเพื่ออัปเดตตัวแปรสภาพแวดล้อม PATH แทนที่ด้วยชื่อโฟลเดอร์หลักของผู้ใช้ เช่น student_00_478dfeb8df15

export PATH=$PATH:/home/YOUR-USER-HOME-FOLDER/.local/bin

เรียกใช้คำสั่ง devai cli เพื่อตรวจสอบโค้ดในเครื่อง ตรวจสอบเอาต์พุตของ CLI

export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1

cd ~/github/genai-for-developers

devai review code -c ./sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

เปิดสคริปต์การตรวจสอบโดยเรียกใช้คำสั่งด้านล่าง

cloudshell edit devai-cli/src/devai/commands/review.py

คลิกขวาที่ใดก็ได้ในreview.pyไฟล์ แล้วเลือกGemini Code Assist > Explainนี้

ดูคำอธิบาย

30e3baf4c272c8ab.png

13. การพัฒนา DevAI CLI

ในส่วนนี้ คุณจะทำการเปลี่ยนแปลงกับ devai cli

ในการเริ่มต้น ให้ตั้งค่าสภาพแวดล้อมเสมือนของ Python ติดตั้งข้อกำหนด และเรียกใช้คำสั่งตัวอย่าง

cd ~/github/genai-for-developers/devai-cli
python3 -m venv venv
. venv/bin/activate
pip3 install -r src/requirements.txt
pip3 install --editable ./src
devai echo

เรียกใช้คำสั่งตรวจสอบความครอบคลุมของการทดสอบเพื่อให้แน่ใจว่าทุกอย่างทำงานได้ดี

devai review testcoverage -c ~/github/genai-for-developers/sample-app/src

ตรวจสอบผลลัพธ์โดยใช้ตัวอย่างมาร์กดาวน์ใน Cloud Shell Editor

สร้างไฟล์ใหม่และวางคำตอบของ Gemini

จากนั้นใช้ Command Palette แล้วเลือก "Markdown: Open Preview"

ec6fedf4b6d3fb73.png

9999e7fbb20cf251.png

76858be03d73abd0.png

14. สำรวจคำสั่ง devai CLI

คำสั่งตรวจสอบโค้ด

devai review code -c ~/github/genai-for-developers/sample-app/src/main/java

คำสั่งการประเมินประสิทธิภาพ

devai review performance -c ~/github/genai-for-developers/sample-app/src/main/java

คำสั่งตรวจสอบความปลอดภัย

devai review security -c ~/github/genai-for-developers/sample-app/src/main/java

คำสั่งตรวจสอบความครอบคลุมของการทดสอบ

devai review testcoverage -c ~/github/genai-for-developers/sample-app/src

คำสั่งตรวจสอบรายการที่บล็อก

devai review blockers -c ~/github/genai-for-developers/sample-app/pom.xml
devai review blockers -c ~/github/genai-for-developers/sample-app/setup.md

การตรวจสอบและสรุปรูปภาพ/ไดอะแกรม:

แผนภาพอินพุต[~/github/genai-for-developers/images/extension-diagram.png]:

4b109a74e1aa3fb6.png

คำสั่งตรวจสอบ

devai review image \
  -f ~/github/genai-for-developers/images/extension-diagram.png \
  -p "Review and summarize this diagram"

เอาต์พุต:

The diagram outlines a process for conducting local code reviews using a VS Code extension or CLI, leveraging Google Cloud's Vertex AI (Gemini Pro) for generating review prompts. 

**Process Flow:**

1. **Code Style Check:** Developers initiate the process by checking their code for adherence to pre-defined style guidelines.
2. **Prompt Generation:** The VS Code extension/CLI sends the code to Vertex AI (Gemini Pro) on Google Cloud. 
3. **Vertex AI Review:** Vertex AI analyzes the code and generates relevant review prompts.
4. **Local Review:** The prompts are sent back to the developer's IDE for their consideration.
5. **Optional Actions:** Developers can optionally: 
    - Create new JIRA issues directly from the IDE based on the review prompts.
    - Generate new issues in a GitLab repository.

**Key Components:**

* **VS Code Extension/CLI:** Tools facilitating the interaction with Vertex AI and potential integrations with JIRA and GitLab.
* **Vertex AI (Gemini Pro):** Google Cloud's generative AI service responsible for understanding the code and generating meaningful review prompts.
* **Google Cloud Secret Manager:** Securely stores API keys and access tokens required to authenticate and interact with Google Cloud services.
* **JIRA/GitLab (Optional):** Issue tracking and project management tools that can be integrated for a streamlined workflow.

**Benefits:**

* **Automated Review Assistance:** Leveraging AI to generate review prompts saves time and improves the consistency and quality of code reviews. 
* **Local Development:** The process empowers developers to conduct reviews locally within their familiar IDE.
* **Integration Options:** The flexibility to integrate with project management tools like JIRA and GitLab streamlines workflow and issue tracking.

การวิเคราะห์ความแตกต่างของรูปภาพ

devai review imgdiff \
  -c ~/github/genai-for-developers/images/devai-api.png \
  -t ~/github/genai-for-developers/images/devai-api-slack.png

เอาต์พุต:

The following UI elements are missing in the "AFTER UPGRADE STATE" image compared to the "BEFORE UPGRADE STATE" image:

1. **Slack:** The entire Slack element, including the icon, "Team channel" label, and the arrow indicating interaction, is absent in the AFTER UPGRADE image. 
2. **Storage Bucket:** The "Storage Bucket" element with its icon and "PDFs" label is missing in the AFTER UPGRADE image. 
3. **"GenAI Agents" label in Vertex AI block:** The BEFORE UPGRADE image has "Vertex AI Agents" and "GenAI Agent" labels within the Vertex AI block, while the AFTER UPGRADE image only has "Vertex AI."
4. **"Open JIRA Issue" and "Team Project" labels:** In the BEFORE UPGRADE image, these labels are connected to the JIRA block with an arrow. These are missing in the AFTER UPGRADE image.

**Decision Explanation:**

The analysis is based on a direct visual comparison of the two provided images, noting the presence and absence of specific UI elements and their associated labels. The elements listed above are present in the BEFORE UPGRADE image but absent in the AFTER UPGRADE image.

คำสั่งสร้างเอกสาร

devai document readme -c ~/github/genai-for-developers/sample-app/src/main/

เอาต์พุต:

# Bank of Anthos - Balance Reader Service

## Table of Contents
- [Description](#description)
- [Features](#features)
- [Technologies Used](#technologies-used)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Health Checks](#health-checks)
- [Metrics and Tracing](#metrics-and-tracing)
- [Contributing](#contributing)
- [License](#license)

## Description

The Balance Reader service is a component of the Bank of Anthos sample application. It provides a REST endpoint for retrieving the current balance of a user account. This service demonstrates key concepts for building microservices with Spring Boot and deploying them to a Kubernetes cluster.

## Features

- Securely retrieves account balances using JWT authentication.
- Leverages a local cache for fast balance retrieval.
- Asynchronously processes transactions from a central ledger.
- Provides health check endpoints for Kubernetes liveness and readiness probes.
- Exposes metrics to Stackdriver for monitoring and observability.
- Supports distributed tracing with Zipkin.

## Technologies Used

- Java
- Spring Boot
- Spring Data JPA
- Hibernate
- Google Cloud SQL (PostgreSQL)
- JWT (JSON Web Token)
- Guava Cache
- Micrometer
- Stackdriver
- Zipkin

## Installation

1. **Prerequisites:**
   - Java 17 or later
   - Maven 3.5 or later
   - Docker (for containerization)
   - Kubernetes cluster (for deployment)
   - Google Cloud account (for Stackdriver and other GCP services)

ตรวจสอบคำสั่ง devai CLI ที่ใช้ได้ในเครื่องมือแก้ไข Cloud Shell

cloudshell edit ~/github/genai-for-developers/devai-cli/README.md

หรืออ่าน README.md ในที่เก็บ GitHub

15. ติดตามตัวแปรสภาพแวดล้อมทั้งหมดในไฟล์

เริ่มไฟล์ใหม่เพื่อติดตามตัวแปรสภาพแวดล้อมทั้งหมด (เช่น คีย์ API, โทเค็น API ฯลฯ) ที่คุณจะสร้าง

คุณจะต้องใช้รหัสผ่านเหล่านี้กับระบบต่างๆ หลายครั้งขณะที่ทำแล็บ ดังนั้นการเก็บรหัสผ่านไว้ในที่เดียวจะช่วยให้คุณอ้างอิงได้ง่ายขึ้น

16. การกำหนดค่าการติดตาม LLM ของ LangSmith

สร้างบัญชี LangSmith และสร้างคีย์ API ของบริการในส่วนการตั้งค่า https://docs.smith.langchain.com/

ตั้งค่าตัวแปรสภาพแวดล้อมที่จำเป็นสำหรับการผสานรวม LangSmith แทนที่คีย์ API ของบริการก่อนเรียกใช้คำสั่ง

export LANGCHAIN_API_KEY=langsmith-service-api-key

export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"

แนวทางปฏิบัติแนะนำคือการใช้ read -s ซึ่งเป็นวิธีที่ปลอดภัยในการตั้งค่าตัวแปรสภาพแวดล้อมโดยไม่ให้ค่าปรากฏในประวัติคำสั่งของคอนโซล เพื่อหลีกเลี่ยงการเปิดเผยข้อมูลที่ละเอียดอ่อนในเทอร์มินัล หลังจากเรียกใช้แล้ว คุณต้องวางค่าและกด Enter

17. การกำหนดค่าคำสั่ง JIRA

สร้างบัญชี JIRA หากยังไม่มี

สร้างโทเค็น API ของ JIRA สำหรับโปรเจ็กต์ https://id.atlassian.com/manage-profile/security/api-tokens

ตั้งค่าตัวแปรสภาพแวดล้อมที่จำเป็นสำหรับการผสานรวม JIRA (แทนที่ค่าก่อนเรียกใช้คำสั่ง)

export JIRA_API_TOKEN=your-token-value
export JIRA_USERNAME="email that you used to register with JIRA"
export JIRA_INSTANCE_URL="https://YOUR-PROJECT.atlassian.net"
export JIRA_PROJECT_KEY="JIRA project key"
export JIRA_CLOUD=true

เปิดไฟล์ review.py

cloudshell edit ~/github/genai-for-developers/devai-cli/src/devai/commands/review.py

ตรวจสอบไฟล์ review.py

    source=source.format(format_files_as_string(context))

    code_chat_model = GenerativeModel(model_name)
    code_chat = code_chat_model.start_chat()
    code_chat.send_message(qry)
    response = code_chat.send_message(source)
    ...
    else:
        click.echo(response.text) 

ค้นหาและยกเลิกการแสดงความคิดเห็นในบรรทัดด้านล่างนี้

# Uncomment after configuring JIRA and GitLab env variables - see README.md for details

นำเข้าคำสั่ง JIRA ที่ด้านบนของไฟล์

# from devai.commands.jira import create_jira_issue

วิธีสร้างปัญหา JIRA ในcodeวิธี

#create_jira_issue("Code Review Results", response.text)

เรียกใช้คำสั่งตรวจสอบโค้ดอีกครั้งและตรวจสอบเอาต์พุตของเอเจนต์

export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1

devai review code -c ~/github/genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

ตัวอย่างเอาต์พุต

(venv) student_00_19a997c157f8@cloudshell:~/genai-for-developers/devai-cli (qwiklabs-gcp-02-71a9948ae110)$ devai review code -c ../sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
/home/student_00_19a997c157f8/genai-for-developers/devai-cli/venv/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.
  warn_deprecated(
Response from Model: ```java
// Class: TransactionRepository
// Method: findBalance

// Efficiency
- Consider using a native SQL query to improve performance for complex database operations.
- Use prepared statements to avoid SQL injection vulnerabilities.

// Best Practices
- Return a Optional<Long> instead of null to handle the case when no balance is found for the given account.

/home/student_00_19a997c157f8/genai-for-developers/devai-cli/venv/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function __call__ was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. ใช้ invoke แทน warn_deprecated(

ป้อนเชน AgentExecutor ใหม่... ความคิด: คำอธิบายมีอยู่ในคำถามแล้ว จึงไม่ต้องคิดอะไร การดำเนินการ:

{
  "action": "create_issue",
  "action_input": {
    "description": "Class: TransactionRepository\nMethod: findBalance\n\nEfficiency\n- Consider using a native SQL query to improve performance for complex database operations.\n- Use prepared statements to avoid SQL injection vulnerabilities.\n\nBest Practices\n- Return a Optional<Long> instead of null to handle the case when no balance is found for the given account."
  }
}

สร้างปัญหาใหม่ด้วยคีย์ CYMEATS-117

การสังเกต: สร้างปัญหาใหม่ด้วยคีย์ CYMEATS-117 ความคิด:คำตอบสุดท้าย: CYMEATS-117

เชนที่เสร็จสมบูรณ์

Open your JIRA project in the browser and review the created issue.

Sample JIRA issue view.

<img src="img/9a93a958c30f0b51.png" alt="9a93a958c30f0b51.png"  width="624.00" />

Open  [LangSmith portal](https://smith.langchain.com/) and review LLM trace for JIRA issue creation call.

Sample LangSmith LLM trace.

<img src="img/6222ee1653a5ea54.png" alt="6222ee1653a5ea54.png"  width="624.00" />


## Import GitHub repo to GitLab repo



Go to  [https://gitlab.com/projects/new](https://gitlab.com/projects/new) and select "`Import project`" / "`Repository by URL`" option:

Git repository url:

https://github.com/GoogleCloudPlatform/genai-for-developers.git

Or

Your personal GitHub project that you created earlier in this lab.

Under Project URL - select your GitLab userid

Set Visibility to `Public`.

Click - "`Create Project`" to start the import process.

If you see an error about invalid GitHub Repository URL,  [create a new GitHub token](https://github.com/settings/tokens)(fine-grained) with Public repositories read-only access, and retry import again providing your GitHub userid and token.


## Clone GitLab repo and setup SSH key



Return to Google Cloud Shell terminal and set up a new SSH key. 

Update your email before running the commands. Hit enter multiple times to accept defaults.

ssh-keygen -t ed25519 -C "your-email-address"

eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519

cat ~/.ssh/id_ed25519.pub

Add a public key to your GitLab account.

Open  [https://gitlab.com/-/profile/keys](https://gitlab.com/-/profile/keys) and click "Add new key".

For the key value copy/paste the output of the last command.

Go back to the terminal and clone the repository. 

cd ~ mkdir gitlab cd gitlab

Replace with your GitLab userid and repository url that was just created.

```console
git clone git@gitlab.com:YOUR_GITLAB_USERID/genai-for-developers.git

เปลี่ยนไดเรกทอรีและเปิดไฟล์ .gitlab-ci.yml

cd genai-for-developers

cloudshell edit .gitlab-ci.yml

หากก่อนหน้านี้คุณไม่ได้ดำเนินการ ให้เปิดใช้ Gemini ใน Cloud Shell Editor

4a7f4640f66037f.png

คลิกขวาที่ใดก็ได้ในไฟล์ .gitlab-ci.yml แล้วเลือก "Gemini Code Assist > Explain this"

154838a0100389a9.png

18. การกำหนดค่าคำสั่ง GitLab

เปิด GitLab แล้วสร้างโทเค็นการเข้าถึงโปรเจ็กต์ในส่วน "Settings / Access Tokens" ในที่เก็บ GitLab ที่สร้างขึ้นในขั้นตอนก่อนหน้า

คัดลอกและจัดเก็บค่า Access Token เพื่อใช้ในขั้นตอนถัดไป

ใช้รายละเอียดต่อไปนี้

  • ชื่อโทเค็น: devai-cli-qwiklabs
  • บทบาท: Maintainer
  • ขอบเขต: api

1865de233277f11c.png

ตั้งค่าตัวแปรสภาพแวดล้อมที่จำเป็นสำหรับการผสานรวม GitLab

คำสั่งนี้กำหนดให้คุณต้องอัปเดตโทเค็นการเข้าถึง GitLab

export GITLAB_PERSONAL_ACCESS_TOKEN=gitlab-access-token

คำสั่งนี้กำหนดให้คุณต้องอัปเดตรหัสผู้ใช้ GitLab และชื่อที่เก็บ

export GITLAB_REPOSITORY="USERID/REPOSITORY"

ตั้งค่าตัวแปรสภาพแวดล้อมที่เหลือ

export GITLAB_URL="https://gitlab.com"
export GITLAB_BRANCH="devai"
export GITLAB_BASE_BRANCH="main"

เปิดเว็บไซต์ GitLab แล้วสร้างปัญหาใหม่ใน GitLab ในโปรเจ็กต์ของคุณโดยตั้งชื่อว่า "CICD AI Insights"

63a13948f6864074.png

อีกทางเลือกหนึ่งคือการใช้คำสั่ง curl ด้านล่าง คุณจะต้องมีรหัสโปรเจ็กต์ GitLab ซึ่งดูได้ในส่วน "Settings / General"

export GITLAB_PROJECT_ID=56390153 # replace

curl --request POST \
  --header "PRIVATE-TOKEN: $GITLAB_PERSONAL_ACCESS_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"title":"CICD AI Insights"}' \
  https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/issues

กลับไปที่ Cloud Shell แล้วเปิดไฟล์ review.py

cloudshell edit ~/gitlab/genai-for-developers/devai-cli/src/devai/commands/review.py

ค้นหาและยกเลิกการแสดงความคิดเห็นของโค้ดด้านล่าง

บรรทัดคำสั่งเพื่อนำเข้า GitLab

# from devai.commands.gitlab import create_gitlab_issue_comment

วิธีแสดงความคิดเห็นในปัญหาของ GitLab

# create_gitlab_issue_comment(response.text)

19. การพัฒนา DevAI CLI

เนื่องจากคุณเปลี่ยนไปใช้ที่เก็บ/ไดเรกทอรี GitLab คุณจะต้องเรียกใช้ขั้นตอนการตั้งค่าด้านล่างอีกครั้ง

ในเทอร์มินัล ให้ตั้งค่าสภาพแวดล้อมเสมือนของ Python ติดตั้งข้อกำหนด และเรียกใช้คำสั่งตัวอย่าง

export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1

cd ~/gitlab/genai-for-developers/devai-cli
python3 -m venv venv
. venv/bin/activate
pip3 install -r src/requirements.txt
pip3 install --editable ./src
devai echo

คุณสามารถยืนยันตำแหน่งของ CLI ได้ ซึ่งคราวนี้ควรอยู่ในโฟลเดอร์ GitLab

 which devai

เรียกใช้คำสั่งการตรวจสอบโค้ดอีกครั้งในเทอร์มินัล

devai review code -c ~/gitlab/genai-for-developers/sample-app/src/main/java/anthos/samples/bankofanthos/balancereader

ตัวอย่างเอาต์พุต - โดยย่อบางส่วน

(venv) student_00_19a997c157f8@cloudshell:~/genai-for-developers/devai-cli (qwiklabs-gcp-02-71a9948ae110)$ devai review code -c ../sample-app/src/main/java/anthos/samples/bankofanthos/balancereader
.
.
Response from Model: **Class: Transaction**

**Method: toString**

**Maintainability:**

* The formatting of the string representation could be more clear and concise. Consider using a dedicated method for formatting the amount, e.g., `formatAmount()`.
.
.

> Entering new AgentExecutor chain...
Thought: I need to first get the issue ID using the Get Issues tool, then I can comment on the issue using the Comment on Issue tool.
Action: Get Issues
Action Input: 
Observation: Found 1 issues:
[{'title': 'CICD AI Insights', 'number': 1}]
Thought:Thought: I found the issue ID, so now I can add the comment to the issue.
Action: Comment on Issue
Action Input: 1


Action: Get Issue
Action Input: 1
Observation: {"title": "CICD AI Insights", "body": "", "comments": "[{'body': '**Transaction.java**\\n\\n\\n**Class:** Transaction\\n\\n\\n* **Security:** Consider using a custom date format like \\\\\"yyyy-MM-dd HH:mm:ss.SSS\\\\\" to handle timestamps more robustly.\\n\\n\\n**JWTVerifierGenerator.java**\\n\\n\\n* .
.
Thought:Now I can use the Comment on Issue tool to add the comment to the issue.

Action: Comment on Issue
Action Input: 
1

**Class: Transaction**

**Method: toString**

**Maintainability:**
.
.
.
Observation: Commented on issue 1
Thought:I have now completed the necessary actions and added the comment to the issue 'CICD AI Insights'.

Final Answer: Comment added to issue 'CICD AI Insights'

> Finished chain.

เปิดเว็บไซต์ GitLab แล้วตรวจสอบปัญหาที่อัปเดต

9da39bf6070d9447.png

ตรวจสอบการติดตาม LLM ใน LangSmith

การติดตาม LLM ตัวอย่าง

f32eed34f3a3b040.png

20. พุชการเปลี่ยนแปลงไปยังที่เก็บ GitLab

กลับไปที่ Google Cloud Shell Editor

เปลี่ยนไปใช้แท็บ "Source Control"

Staging, คอมมิต และพุชการเปลี่ยนแปลงที่คุณทำเพื่ออัปเดตไฟล์ review.py

b838a11f362454ce.png

21. การกำหนดค่า CICD ของ GitLab

จากนั้นคุณจะเปิดใช้ไปป์ไลน์ CICD ของ GitLab เพื่อเรียกใช้การตรวจสอบโค้ดเมื่อมีการพุชการเปลี่ยนแปลงไปยังที่เก็บ

เปิดเว็บไซต์ GitLab แล้วไปที่ส่วน "Settings / CICD"

ขยายส่วน Variables แล้วคลิก "Add variable"

อย่าลืมยกเลิกการเลือกช่องทําเครื่องหมายทั้งหมดเมื่อเพิ่มตัวแปร ตัวอย่าง

68b3ed732b6a8fe9.png

ใช้บันทึกที่คุณเก็บตัวแปรสภาพแวดล้อมทั้งหมด แล้วเพิ่มตัวแปรสภาพแวดล้อมสำหรับ JIRA, GitLab และ LangSmith

PROJECT_ID=qwiklabs-project-id
LOCATION=us-central1
GOOGLE_CLOUD_CREDENTIALS - cat ~/vertex-client-key.json

LANGCHAIN_TRACING_V2=true
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
LANGCHAIN_API_KEY=your-service-api-key

JIRA_API_TOKEN=your-token
JIRA_USERNAME="email that you used to register with JIRA"
JIRA_INSTANCE_URL="https://YOUR-PROJECT.atlassian.net"
JIRA_PROJECT_KEY="JIRA project key"
JIRA_CLOUD=true

GITLAB_PERSONAL_ACCESS_TOKEN=your-gitlab-token
GITLAB_URL="https://gitlab.com"
GITLAB_REPOSITORY="USERID/REPOSITORY"
GITLAB_BRANCH="devai"
GITLAB_BASE_BRANCH="main"

สำหรับค่าตัวแปร GOOGLE_CLOUD_CREDENTIALS ให้ใช้คีย์บัญชีบริการที่สร้างไว้ในส่วนด้านบน

cat ~/vertex-client-key.json

มุมมองตัวแปร CI/CD

2f4594ce72be4834.png

22. เรียกใช้ไปป์ไลน์ CICD ของ GitLab

เปิด "Build / Pipelines" ใน UI ของ GitLab แล้วคลิก "Run Pipeline"

a7518e37dde42366.png

23. ตรวจสอบเอาต์พุตของไปป์ไลน์ GitLab

เปิด "Build / Jobs" ใน UI ของ GitLab และตรวจสอบเอาต์พุตของไปป์ไลน์

985e4b322fb73b1c.png

เปิดเว็บไซต์ GitLab และตรวจสอบความคิดเห็นที่อัปเดตแล้วในปัญหา "CICD Insights"

ปิดใช้การดำเนินการเวิร์กโฟลว์ GitLab

กลับไปที่ Google Cloud Shell Editor ยกเลิกการแสดงความคิดเห็นในบรรทัดเพื่อปิดใช้การดำเนินการเวิร์กโฟลว์ GitLab ในเหตุการณ์การพุชโค้ด คุณยังคงเรียกใช้เวิร์กโฟลว์จาก UI ได้ตามต้องการ

# workflow:
#   rules:
#     - if: $CI_PIPELINE_SOURCE == "web"

เปิด .gitlab-ci.yml ในรูทของโปรเจ็กต์และยกเลิกการแสดงความคิดเห็นในบรรทัดต่อไปนี้

cloudshell edit ~/gitlab/genai-for-developers/.gitlab-ci.yml

เปลี่ยนไปที่แท็บ "Source Control" - จัดเตรียมไฟล์ คอมมิต และพุชการเปลี่ยนแปลงนี้

a9d52beb8c642982.png

24. การผสานรวม CircleCI

CircleCI คืออะไร

CircleCI เป็นแพลตฟอร์ม CI/CD บนระบบคลาวด์ที่ช่วยให้ทีมทำให้กระบวนการพัฒนาและการติดตั้งใช้งานซอฟต์แวร์เป็นแบบอัตโนมัติได้ โดยจะผสานรวมกับระบบควบคุมเวอร์ชัน เช่น GitHub, Bitbucket และ GitLab ซึ่งช่วยให้ทีมตรวจสอบการเปลี่ยนแปลงโค้ดได้แบบเรียลไทม์ด้วยการเรียกใช้การทดสอบและการบิลด์อัตโนมัติ สำหรับการนำส่งต่อเนื่อง CircleCI สามารถทำให้การติดตั้งใช้งานซอฟต์แวร์ในสภาพแวดล้อมระบบคลาวด์ต่างๆ เช่น AWS, Google Cloud และ Azure เป็นไปโดยอัตโนมัติ

ตั้งค่า

เปิดเว็บไซต์ CircleCI แล้วสร้างโปรเจ็กต์ใหม่ เลือก "GitLab" / "Cloud" สำหรับที่เก็บ

ให้สิทธิ์ CircleCI เข้าถึงบัญชี GitLab

เลือกmain สาขาในส่วนตัวเลือกที่เร็วที่สุด CircleCI อาจตรวจพบไฟล์การกำหนดค่าที่มีอยู่และข้ามขั้นตอนนี้

4e9b7cef458d5fba.png

หลังจากสร้างโปรเจ็กต์แล้ว ให้คลิกส่วน "Project Settings" / "Environment Variables"

1499b5f96ac0fe5e.png

เพิ่มตัวแปรสภาพแวดล้อมทั้งหมดที่คุณใช้ไปแล้ว

f15b7a3e02c649e8.png

ตัวอย่างรายการตัวแปรสภาพแวดล้อมที่จะเพิ่มมีดังนี้

PROJECT_ID=qwiklabs-project-id
LOCATION=us-central1
GOOGLE_CLOUD_CREDENTIALS - cat ~/vertex-client-key.json

LANGCHAIN_TRACING_V2=true
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
LANGCHAIN_API_KEY=your-service-api-key

JIRA_API_TOKEN=your-token
JIRA_USERNAME="email that you used to register with JIRA"
JIRA_INSTANCE_URL="https://YOUR-PROJECT.atlassian.net"
JIRA_PROJECT_KEY="JIRA project key"
JIRA_CLOUD=true

GITLAB_PERSONAL_ACCESS_TOKEN=your-gitlab-token
GITLAB_URL="https://gitlab.com"
GITLAB_REPOSITORY="USERID/REPOSITORY"
GITLAB_BRANCH="devai"
GITLAB_BASE_BRANCH="main"

25. เปิดใช้เมธอด JIRA และ GitLab

เปิด Google Cloud Shell Editor แล้วทำการเปลี่ยนแปลงในไฟล์ review.py

ค้นหาและยกเลิกการแสดงความคิดเห็นในบรรทัดด้านล่าง

# from devai.commands.jira import create_jira_issue
create_jira_issue("Performance Review Results", response.text)
create_gitlab_issue_comment(response.text)
.
.
.
create_jira_issue("Security Review Results", response.text)
create_gitlab_issue_comment(response.text)

เปลี่ยนไปที่แท็บ "Source Control" - จัดเตรียมไฟล์ คอมมิต และพุชการเปลี่ยนแปลงนี้

เปิดเว็บไซต์ GitLab แล้วไปที่ "Build" / "Pipelines"

d196ad631be17b88.png

ไปที่ลิงก์ไปยัง CircleCI เพื่อตรวจสอบเวิร์กโฟลว์

d4ff287694b82445.png

ตรวจสอบความคิดเห็นเกี่ยวกับปัญหาใน GitLab ในที่เก็บ

e77ee826488d5299.png

9a51daa2960994e3.png

ตรวจสอบปัญหาใหม่ที่สร้างในโปรเจ็กต์ JIRA

1e6305a32aaef6a2.png

26. ยินดีด้วย

ยินดีด้วย คุณทำแล็บเสร็จแล้ว

สิ่งที่เราได้พูดถึงไปแล้ว

  • การเพิ่มขั้นตอนการทำงานอัตโนมัติของการตรวจสอบโค้ด GenAI ใน GitHub, GitLab และ CircleCI
  • LangChain ReAct Agent เพื่อทำงานอัตโนมัติ เช่น แสดงความคิดเห็นในปัญหาของ GitLab และเปิดตั๋ว JIRA

ขั้นตอนต่อไปที่ทำได้

  • เราจะจัดเซสชันแบบลงมือปฏิบัติเพิ่มเติมในเร็วๆ นี้

ล้างข้อมูล

โปรดลบโปรเจ็กต์ที่มีทรัพยากรหรือเก็บโปรเจ็กต์ไว้และลบทรัพยากรแต่ละรายการเพื่อหลีกเลี่ยงการเรียกเก็บเงินจากบัญชี Google Cloud สำหรับทรัพยากรที่ใช้ในบทแนะนำนี้

การลบโปรเจ็กต์

วิธีที่ง่ายที่สุดในการยกเลิกการเรียกเก็บเงินคือการลบโปรเจ็กต์ที่คุณสร้างขึ้นสำหรับบทแนะนำ

©2024 Google LLC สงวนลิขสิทธิ์ Google และโลโก้ของ Google เป็นเครื่องหมายการค้าของ Google LLC ชื่อบริษัทและผลิตภัณฑ์อื่นๆ ทั้งหมดอาจเป็นเครื่องหมายการค้าของบริษัทที่เป็นเจ้าของ