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

1. ภาพรวม

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

9dde56ad139b9553.png

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

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

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

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

  • กิจกรรมนี้ถือว่าคุณคุ้นเคยกับสภาพแวดล้อม Cloud Console และ Cloud Shell

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

การตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์

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

fbef9caa1602edd0.png

a99b7ace416376c4.png

5e3ff691252acf41.png

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

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

เปิดแชท Gemini

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 ได้รับการฝึกด้วยโค้ด 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 ของ Gemini ของ Google

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

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

3e0c761ca41f315e.png

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

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. แยกที่เก็บ 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

ผลลัพธ์ของคำสั่งตรวจสอบตัวบล็อก

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

คลิกไอคอน "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 แล้ว แต่ 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

ในการเริ่มต้น ให้ตั้งค่า virtualenv ของ 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

ตรวจสอบผลลัพธ์โดยใช้การแสดงตัวอย่าง Markdown ในเครื่องมือแก้ไข Cloud Shell

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

จากนั้นใช้พาเล็ตคำสั่งและเลือก "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 และสร้างคีย์ Service API ในส่วนการตั้งค่า https://docs.smith.langchain.com/

ตั้งค่าตัวแปรสภาพแวดล้อมที่จําเป็นสําหรับการผสานรวม LangSmith แทนที่คีย์ Service 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 หากยังไม่มี

สร้างโทเค็น JIRA API สำหรับโปรเจ็กต์ 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: ฟังก์ชัน __call__ ถูกเลิกใช้งานใน LangChain 0.1.0 และจะนําออกใน 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

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 คุณจะต้องทําตามขั้นตอนการตั้งค่าด้านล่างอีกครั้ง

ในเทอร์มินัล ให้ตั้งค่า virtualenv ของ 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

กลับไปที่เครื่องมือแก้ไข Cloud Shell ของ Google

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

แบ่งกลุ่ม คอมมิต และพุชการเปลี่ยนแปลงที่คุณทําเพื่ออัปเดตไฟล์ 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

กลับไปที่เครื่องมือแก้ไข Cloud Shell ของ Google ยกเลิกการคอมเมนต์บรรทัดดังกล่าวเพื่อปิดใช้การดำเนินการเวิร์กโฟลว์ 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 และทําการเปลี่ยนแปลงในไฟล์ 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 เพื่อทำงานต่างๆ ให้เป็นแบบอัตโนมัติ เช่น การเขียนความคิดเห็นในปัญหา GitLab และการเปิดคำขอแจ้งปัญหา JIRA

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

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

ล้างข้อมูล

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

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

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

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