การทำงานอัตโนมัติในการตรวจสอบโค้ดด้วย 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

เปิดที่เก็บ 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" Branch

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 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 แล้ว แต่ 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 Editor ดังนี้

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

หรือตรวจสอบ README.md ในที่เก็บ GitHub

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

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

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

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

สร้างบัญชี 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)

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

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 Thought:Final Answer: 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 "ที่อยู่อีเมลของคุณ"

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 และสร้าง Access Token ของโปรเจ็กต์ในส่วน "Settings / Access Tokens" ในที่เก็บ GitLab ที่สร้างขึ้นในขั้นตอนก่อนหน้า

คัดลอกและจัดเก็บค่าโทเค็นเพื่อการเข้าถึงที่จะใช้ในขั้นตอนถัดไป

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

  • ชื่อโทเค็น: 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

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

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

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

b838a11f362454ce.png

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

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

เปิดเว็บไซต์ 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" ใน GitLab UI และตรวจสอบเอาต์พุตของไปป์ไลน์

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

ต่อไปนี้คือรายการตัวอย่างของตัวแปร env ที่จะเพิ่ม

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 ชื่อบริษัทและผลิตภัณฑ์อื่นๆ ทั้งหมดอาจเป็นเครื่องหมายการค้าของบริษัทที่เป็นเจ้าของ