Try   HackMD
tags: AFS

AFS Coder - GitCoder

Draft Version

GitCoder - CLI-Coder

* 若文件或圖片與產品有不一致之處,請以實際產品體驗為準。

簡介

使用 JavaScript 語言開發的命令行界面(CLI),可使用 LLama AI(Code LLama, Llama 2)自動撰寫 Git 提交訊息或生成簡潔的程式碼審查摘要,亦可輕鬆設定 Git Hooks 以準備提交。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

系統需求

  • 本機運行:Node.js v16 或更新版本。

安裝

目前提供使用 npm 或執行檔案安裝的方式:

  1. 使用 npm 安裝
npm install -g @twsc/coder
  1. 設定您的 LLM API 金鑰和服務的 URL請勿複製以下的範例)。
coder config set LLM_URL https://{DOMAIN_URL}/text-generation coder config set LLM_TOKEN oooooooo-1af4-49cb-8ccf-xxxxxxxxxxxx coder config set MODEL <your model name>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
提示:

  • GitCoder - CLI-Coder 安裝成功後,可於 Linux/Mac/Windows 作業系統預設的 Terminal 執行,但不能於 PowerShell 上執行。
  • npm 安裝後若要更新請使用:npm install @twsc/coder@latest
  • 移除安裝請使用:npm uninstall -g @twsc/coder

開始使用

方式一:在 GitLab CI/CD 中使用

您可以使用 prepare-commit-msg hook 將 GitCoder - CLI-Coder 整合到 Git 中,這可讓您正常使用 Git,並在提交之前編輯提交訊息。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
提示: git commit 會觸發 Coder 將訊息摘要發送給您的編輯器,無論使用 q!:wq,都會強制提交訊息。如果您想中止訊息,請使用 git reset HEAD^

  • 安裝

    ​​​​coder hook install
  • 解除安裝

    ​​​​coder hook uninstall

方式二:在命令列介面中使用

  • 您可以使用 GitCoder - CLI-Coder 為變更產生程式碼審查訊息。
coder review
  • 您可以直接呼叫 GitCoder - CLI-Coder 來為您的變更生成一條提交訊息。

    • --preview:預覽 AI 模型中生成的摘要
    ​​​​git add <file...> ​​​​coder commit --preview
    • --diffUnifed:生與 <n> 行的差異
    ​​​​coder commit --preview --diffUnifed 5
    • --excludeList:可以忽略暫存文件
    ​​​​coder commit --preview --excludeList README.md .env
    • --promptOnly:顯示提示,不發送請求給 GPT
    ​​​​coder commit --preview --promptOnly
    • --amend:替換當前提交
    ​​​​coder commit --amend
    • --maxTokens:設定生成的最大 tokens 數(預設為 1000)
    ​​​​coder commit --preview --maxTokens 2000
    • --templateFolder:設定 Git 提交訊息的自定義格式
    ​​​​# make sure your format file name is 'commit.message.hbs' ​​​​coder commit --preview --templateFolder /Users/custom/folder
    • --promptFolder:設定自定義系統提示
    ​​​​# summarize.title.hbs: to summary your commit title ​​​​# summarize.file.diff.hbs: to summary your commit change ​​​​# conventional.commit.hbs: to summary your commit types (feat, modify...) ​​​​coder commit --preview --promptFolder /Users/custom/folder
    • --language:更改提交訊息語言(en, tw, cn)
    ​​​​coder commit --language tw
    • --files: 指定變更的檔案
    ​coder commit --preview --files src/index.js helm/templates
    • --parse: coder 切割 commit changes,以每一筆 change 為單位,請 llm 總結(預設為 false)。
    ​coder commit --preview --parse
    • --tokenLimit: llms 的 input tokens + max tokens 限制(預設為 8192)。
    ​coder commit --tokenLimit 4096
    • --modelTokenizer: 指定自訂的 model tokenzier.json 的位置,coder 會依照設定來計算輸入的 token (預設會用 llama2 的方式去計算 tokens)。
    ​# 34b 底下必須包含 tokenizer.json, tokenizer_config.json
    ​code commit --tokenLimit .config/coder/34b
    
  • 可以在命令列介面設定配置。

coder config set LLM_URL https://{DOMAIN_URL}/text-generation coder config set TEMPERATURE 0.1
  • 檢查設定檔:/Users/user/.config/coder/.env
cat /Users/user/.config/coder/.env LLM_URL=https://{DOMAIN_URL}/text-generation TEMPERATURE=0.1 echo $LLM_URL https://{DOMAIN_URL}/text-generation echo $TEMPERATURE 0.1
  • 您也可以直接修改設定檔 /Users/<user>/.config/coder/.env。如果將配置設定在 .env 檔案,編碼器將優先使用 .env 檔。
coder config set MAXTOKENS 500 coder commit --maxTokens 1000 # the max tokens will be 500
  • 如果想從 .env 中刪除 env 變數,有兩個選擇:

    1. 從設定檔移除 .env
    2. 從命令列介面移除 coder config unset TEMPERATURE
  • 可用的 ENV 變數

    • DIFFUNIFIED
    • MAXTOKENS
    • LANGUAGE
    • MODEL
    • TEMPLATEFOLDER
    • PROMPTFOLDER
    • TEMPERATURE
    • TOP_K
    • TOP_P
    • LLM_URL
    • LLM_TOKEN

客製化範本

如果您想自定義系統提示,可以在 /Users/<user>/.config/coder/ 中創建範本,或設置自己的範本路徑 coder config set PROMPTFOLDER <your path>

:Warning:注意: 檔案名稱必須正確,範本中的 {{ key }} 請勿更改。

預設範本

commit.message.hbs
{{ summarizePrefix }}: {{ summarizeTitle }}

{{ summarizeMessage }}
summarize.title.hbs
You are an expert programmer, and you are trying to title a pull request.
You went over every file that was changed in it.
For some of these files changes were too big and were omitted in the files diff summary.
Please summarize the pull request into a single specific theme.
Write your response using the imperative tense following the kernel git commit style guide.
Write a high level title.
Do not repeat the commit summaries or the file summaries.
Do not list individual changes in the title.

EXAMPLE SUMMARY COMMENTS:

Raise the amount of returned recordings
Switch to internal API for completions
Lower numeric tolerance for test files


THE FILE SUMMARIES:
###
{{ summaryPoints }}
###

To write only one line, no more than 50 characters.
summarize.file.diff.hbs
You are an expert programmer, and you are trying to summarize a git diff.
Reminders about the git diff format:
For every file, there are a few metadata lines, like (for example):

diff --git a/lib/index.js b/lib/index.js
index aadf691..bfef603 100644
--- a/lib/index.js
+++ b/lib/index.js

This means that `lib/index.js` was modified in this commit. Note that this is only an example.
Then there is a specifier of the lines that were modified.
A line starting with `+` means it was added.
A line that starting with `-` means that line was deleted.
A line that starts with neither `+` nor `-` is code given for context and better understanding.
It is not part of the diff.
After the git diff of the first file, there will be an empty line, and then the git diff of the next file.

Do not include the file name as another part of the comment.
Do not use the characters `[` or `]` in the summary.
Write every summary comment in a new line.
Comments should be in a bullet point list, each line starting with a `-`.
The summary should not include comments copied from the code.
The output should be easily readable. When in doubt, write less comments and not more. Do not output comments that
simply repeat the contents of the file.
Readability is top priority. Write only the most important comments about the diff.

THE GIT DIFF TO BE SUMMARIZED:
###
{{ fileDiffs }}
###
conventional.commit.hbs
You are an expert programmer, and you are trying to summarize a code change.
You went over every file that was changed in it.
For some of these files changes where too big and were omitted in the files diff summary.
Determine the best label for the commit.
Only label please.

Here are the labels you can choose from:

- build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
- chore: Updating libraries, copyrights or other repo setting, includes updating dependencies.
- ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, GitHub Actions)
- docs: Non-code changes, such as fixing typos or adding new documentation (example scopes: Markdown file)
- feat: a commit of the type feat introduces a new feature to the codebase
- fix: A commit of the type fix patches a bug in your codebase
- perf: A code change that improves performance
- refactor: A code change that neither fixes a bug nor adds a feature
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- test: Adding missing tests or correcting existing tests


THE FILE SUMMARIES:
###
{{ summaryPoints }}
###

Based on the changes described in the file summaries, What's the best label for the commit? Your answer must be one of
the labels above. Don't describe the changes, just write the label.

GitCoder - Auto-Reviewer

簡介

GitCoder - Auto-Reviewer 是一個結合 CodeLlama 和 GitLab 的 DevOps 工具,可在 GitLab CI/CD 流程中協助開發人員進行程式碼審查,簡化審查工作量,提升開發效率。同時,它還提供詳細的中文審查建議,並將這些建議自動添加到 GitLab 的評論中,讓開發人員快速瞭解審查內容或進行必要的修改。

圖片來源:GitLab

設定與使用

  1. 在 GitLab 要使用該工具的專案設定 CI/CD 環境變數,Project > Settings > CI/CD > Variables。

    • GITLAB_TOKEN
    • LLM_TOKEN
    • CODE_LLAMA_URL
    • CODE_LLAMA_MODEL

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    提示: GITLAB_TOKEN 為使用工具的 GitLab 用戶(請額外開一個專案用戶 Code-Reviewer)的 Access Token,可以在 User Settings > Access Tokens 獲取。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. 將該 GitLab User 加入到對應專案的 Members。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  3. 修改專案的 gitlab-ci.yml

    ​​​​stages: ​​​​ - auto-code-review ​​​​auto-code-review:codellama: ​​​​ stage: auto-code-review ​​​​ image: node:18-alpine ​​​​ script: ​​​​ - npm install -g @twsc/code-review ​​​​ - code-review start --automode 1 --llm codellama --mr ${CI_PROJECT_URL}/-/merge_requests/${CI_MERGE_REQUEST_IID} ​​​​ tags: ​​​​ - normal # 要使用的 runner tag ​​​​ rules: ​​​​ - if: $CI_MERGE_REQUEST_ID ​​​​ when: manual

    :Warning:注意: 該專案需安裝 GitLab Runners。

  4. 設定啟動方式

    • 發 Merged Request 後可以在 Pipeline 的 Stage 手動啟動(若想要全自動可以拿掉 when: manual)。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

範例

以下列舉審查範例及說明。

  • 範例一:理解程式碼的邏輯風險,提醒除以零是不合法的運算。

    Review Example

  • 範例二:理解程式碼變更,並且提出風險提醒;改成同步複製等待可能會導致效能問題。

    Review Example

  • 範例三:理解程式碼變更,提出遞迴呼叫可能帶來的風險,並給予相關改進建議。

    Review Example

GitCoder - Chatbot

簡介

GitCoder - Chatbot 可讓開發人員在 GitLab MR 流程中透過聊天的方式與 Code Llama 進行互動,目前提供 Code Review、Code Change Explanation 以及 Free Chat 功能,另外還可以設定客製化指令,進而優化 GitCoder - Chatbot。

framework

GitCoder - Chatbot 會在 init 的時候根據提供的 data 來建構 Vector DB,當使用者在 GitLab MR 留下 Comment 時,GitCoder - Chatbot 會接收來自 GitLab Comment Webhook 使用者 Comment,依據使用者的 Comment 是否匹配 Vector DB 的 Document 而有不同行為;如果符合 code review 或是 explanationdocument,則帶入特定的 Prompt 來加強對話的有效度,如果不匹配則直接將 Comment 給 code-llama 回應,與 code-llama 溝通的時候使用 Conversation API 將包含 diffprompt 以及 chat log 包成 Conversation 傳送給 code-llama,當 code-llama 完成 Inference 後再將結果回覆到使用者的討論中。

設定說明

:Warning:注意
以下 GitLab 截圖皆以 v.16.6 版為參考,不同版本的 GitLab 介面可能有所不同,請依實際版本為準。

1. 建立 GitCoder - Chatbot 在 GitLab 上的代理人

  • 進入 GitLab Admin Area 頁面,點擊左側邊欄 Overview > Users,接著選擇 New user

    new user

  • 填寫必要資料,Name、Username & Email 請自行設定(本文件以 Git-Coder 為例),Can create top level group 可以取消勾選(預設為勾選),Access level 維持預設 Regular 即可。

    user setting

    :Warning:注意

    • 這裡設定的 Username 會影響之後如何在 Merge Request 中呼叫 Git-Coder,如果不確定如何呼叫可以在所有設定完成後使用 !help 指令來確認。
    • 另外 Access level 請勿設定成 admin 以免影響後續使用。
  • 設定完成後按下 Create user

    create user

2. 建立代理人的 Access Token

  • Create user 完成後會跳轉至 Users 的 Account 頁面,接著點擊右上方的 Impersonate

    Impersonate

  • 切換成剛創建的使用者(本文件以 Git-Coder 為例)登入頁面後,點擊上方的使用者頭像圖示,接著選擇 Edit profile

    Edit profile

  • 從左側邊欄點選 Access Tokens

    Access Tokens

  • 點選 Add new token

    Add new token

  • 填入必要資訊,Name 可自由設定不影響使用,Token 時長可自行決定但過期時需要手動更新。

    user token setting

  • 點選 Create personal access token

    create user token

  • Token 創建之後請點擊右側的複製按鈕,並妥善保存此 Token。

    copy user token

3. 透過 Docker Compose 啟動 GitCoder - Chatbot Backend

  • 建立 .env,如果需要啟用 auth module 可以將 ENABLE_AUTH 改為 true,屆時可以透過 admin key(可自行設定,須符合 UUIDv4)來發行其他 api key。

    ​​​​LOG_LEVEL=info ​​​​# gitlab settings ​​​​GITLAB_HOST={your gitlab host}/api/v4 ​​​​GITLAB_TOKEN={token you just created} ​​​​# DB ​​​​DATABASE_STORE_PATH={path where your RAG is stored} ​​​​POSTGRES_PASSWORD={your postgres password} ​​​​# auth ​​​​ENABLE_AUTH=false ​​​​ADMIN_AUTH_KEY={your admin auth api key} ​​​​# LLM ​​​​EMBEDDINGS_URL=https://{DOMAIN_URL}/embeddings ​​​​EMBEDDINGS_API_KEY={your embeddings api key} ​​​​CODE_LLAMA_URL=https://{DOMAIN_URL}/text-generation ​​​​CODE_LLAMA_API_KEY={your code llama api key}
  • 建立 docker-compose.yaml

    ​​​​services: ​​​​ postgres: ​​​​ image: postgres ​​​​ restart: unless-stopped ​​​​ environment: ​​​​ POSTGRES_USER: git-coder ​​​​ POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} ​​​​ POSTGRES_DB: git-coder-chatbot ​​​​ volumes: ​​​​ - ${DATABASE_STORE_PATH}:/var/lib/postgresql/data ​​​​ expose: ​​​​ - 5432 ​​​​ chatbot: ​​​​ image: ociscloud/git-coder-chatbot:v1.0.3 ​​​​ restart: unless-stopped ​​​​ environment: ​​​​ LOG_LEVEL: ${LOG_LEVEL:-info} ​​​​ NODE_ENV: production ​​​​ NODE_TLS_REJECT_UNAUTHORIZED: 0 ​​​​ # gitlab settings ​​​​ GITLAB_HOST: ${GITLAB_HOST} ​​​​ GITLAB_TOKEN: ${GITLAB_TOKEN} ​​​​ # code llama settings ​​​​ CODE_LLAMA_URL: ${CODE_LLAMA_URL} ​​​​ CODE_LLAMA_API_KEY: ${CODE_LLAMA_API_KEY} ​​​​ LLM_SCORE_FACTOR: 0.89 ​​​​ DEFAULT_LLM_TEMPERATURE: 0.01 ​​​​ DEFAULT_LLM_TOP_K: 50 ​​​​ DEFAULT_LLM_TOP_P: 1 ​​​​ DEFAULT_LLM_MAX_TOKENS: 2000 ​​​​ # embeddings setting ​​​​ EMBEDDINGS_URL: ${EMBEDDINGS_URL} ​​​​ EMBEDDINGS_API_KEY: ${EMBEDDINGS_API_KEY} ​​​​ LLM_REVIEW_DATA: /data/review.data.json ​​​​ LLM_EXPLAIN_DATA: /data/explain.data.json ​​​​ # auth setting ​​​​ ENABLE_AUTH: ${ENABLE_AUTH:-false} ​​​​ ADMIN_AUTH_KEY: ${ADMIN_AUTH_KEY:-541e0bde-0057-4f64-ba8a-12b49126027e} ​​​​ # db setting ​​​​ DATABASE_HOST: postgres ​​​​ DATABASE_PORT: 5432 ​​​​ DATABASE_USER: git-coder ​​​​ DATABASE_PASSWORD: ${POSTGRES_PASSWORD} ​​​​ ports: ​​​​ - "8889:8889"
  • 執行以下指令

    ​​​​# run service ​​​​docker compose --env-file .env up --detach ​​​​# stop service ​​​​docker compose --env-file .env stop

4. 將 Code-Reviewer 加入您的 GitLab Group/Project

  • 進入要使用 GitCoder - Chatbot 的 GitLab 的 Group 或 Project,先在左側邊欄選擇 Manage > Members

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    提示: 如果想要整個 Group 都使用 GitCoder - Chatbot,僅需在 Group 中加入 Member;如果只想在特定 Project 中使用,僅在特定的 Project 中加入 Member。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 點擊 Invite members

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 出現 Invite members 視窗後,在 Username 欄位輸入 Git-Coder,接著在Select a role 欄位選擇 Reporter,最後按下 Invite 即可。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    提示: Role Permission 目前至少需要開啟到 Reporter 權限,但隨著後續功能的開發可能會需要更高權限。

5. 設定 Webhooks

  • 進入要使用 GitCoder - Chatbot 的 GitLab Project,在左側邊欄下方選擇 Settings > Webhooks

    Webhooks

  • 點擊 Add new webhook 進入 Webhooks Settings 頁面。

    Add new webhook

  • URL 請輸入 http://202.5.254.102:8889/api/v1/chatbotsTrigger 僅勾擇 Comments

    Trigger

  • 設定 Secret token(Optional):如果有啟用 Auth 機制請在 Secret token 輸入 System Admin 提供的 API key。

    apikey

  • 關閉 SSL 驗證(Optional),最後按下 Add webhook

    ssl

6. 客製化參數設定 (使用預設值可跳過此步驟)

  • Webhook 的 URL 上加入 query string 來進行客製化,使用方式如下:

    Custom config

  • 目前可供設定的參數如下:

    • model:
      • meta-codellama-34b-instructmeta-codellama-13b-instructmeta-codellama-7b-instruct
      • ffm-llama2-70b-expffm-llama2-13b-expffm-llama2-7b-exp
    • temperature:預設值 0.01
    • topK:預設值 50
    • topP:預設值 1
    • maxTokens:預設值 2000

開始使用

  1. Code Review:從 MR 當中選取想要進行 Code Review 的 diff,在 Comment 中標記 Git-Coder 讓 GitCoder - Chatbot 幫您 Code Review。

    Git-Coder review

    GitCoder - Chatbot 的回覆除了 code-llama 產生的結果外,還會附上 Vector DB 找到的相似 Document。如果在GitCoder - Chatbot 的回覆中沒有看到 Document 的話,可以透過 !review 指令來加強 Vector DB 的 Document。反之,可能是相似度設定閾值過高。

    Git-Coder review reply

  2. Code Change Explanation:從 MR 當中選取想要 Code Review 的 diff,在 Comment 標記 Git-Coder 讓 GitCoder - Chatbot 解釋 Code Change。

    Git-Coder explain

    GitCoder - Chatbot 的回覆除了 code-llama 產生的結果外,還會附上 Vector DB 找到的相似 Document。如果在 GitCoder - Chatbot 的回覆中沒有看到 Document 的話,可以透過 !explain 指令來加強 Vector DB 的 Document。反之,可能是相似度設定閾值過高。

    Git-Coder explain reply

  3. Free Chat:在任意的討論中標記 Git-Coder 並加上對話的內容。

    Git-Coder free

    Free Chat 的使用沒什麼限制,可在 MR 的任意討論中使用,但是如果在討論中沒有包含 code change 的話,那麼 diff 將不會被帶入對話中;反之,則對話中會包含 diff,因此可以詢問跟 diff 內有關的問題。

    Git-Coder free reply

    如以下案例,先讓 GitCoder - Chatbot 進行 Code Explanation,接著針對 Explanation 的內容繼續提問。

    Git-Coder free reply2

指令說明

  1. !help:使用說明。

    help

  2. !score [1-10]:可以根據 GitCoder - Chatbot 的回覆給予分數(用來評估 code-llama 和 prompt 的有效程度)。

    score

  3. !review add [your review msg] 如果您留下的 code review msg 沒有在 Vector DB 中有足夠相似的 Document 的話,可以透過指令把這個 msg 加入 Vector DB。

    revew add

  4. !explain add [your explain msg]:如果您留下的 code change explanation msg 沒有在 Vector DB 中有足夠相似的 Document 的話,可以透過指令把這個 msg 加入 Vector DB。

    explain add