AI大模型在Git中的应用--探索中

28 阅读2分钟

想法

你知道Github的自动流水线吗?

我有一个想法, 我想实现一个功能。

在每次指定分支如dev分支push代码后.执行钩子,让AI给出优化后代码再由相关人员合并

具体分为如下几个大的步骤: 在每次指定分支如dev分支push代码后.执行钩子,

  1. 切换到指定的分支下,如AI-PB/deepSeek-R1分支。
  2. 获取dev分支提交的文件与其代码。
  3. 将每个文件交给指定的AI处理,调用准备好的大模型API和Prompt。
  4. 将大模型AI处理后的代码覆盖到对应的文件上。
  5. 按照指定的commit格式提交代码到当前分支。
  6. 提交PR到待审核分支,等待人工审核。
---
   title: 工作流示意图
---
flowchart TD
    A[Dev Branch Push] --> B[Trigger Workflow]
    B --> C[Checkout AI-PB/deepSeek-R1]
    C --> D[Merge Dev Changes]
    D --> E[Install Dependencies]
    E --> F{Modified Files?}
    F -->|Yes| G[Process Each File with AI]
    F -->|No| H[Skip Processing]
    G --> I[Commit AI Changes]
    I --> J[Push to AI-PB/deepSeek-R1]
    J --> K[Create PR to Dev]
    K --> L[Add Label and Reviewers]
    L --> M[Waiting for manual review]
    M --> N[End Workflow]
    H --> N
---
   title: 工作流示意图--详细版
---
flowchart TD
    A[Dev Branch Push] --> B[Trigger GitHub Action]
    
    subgraph "AI Processing Workflow"
        B --> C1[Checkout AI-PB/deepSeek-R1]
        C1 --> C2[Fetch Dev Branch]
        C2 --> C3[Merge Changes]
        
        C3 --> D1[Setup Python]
        D1 --> D2[Install Packages]
        
        D2 --> E1{Get Modified Files}
        E1 -->|Files Exist| F1[Process with AI]
        E1 -->|No Files| F2[Skip Processing]
        
        subgraph "AI Processing"
            F1 --> G1[Read File Content]
            G1 --> G2[Call AI API]
            G2 --> G3[Apply Changes]
            G3 --> G4[Save File]
        end
        
        F1 --> H1{Changes Made?}
        H1 -->|Yes| I1[Create Commit]
        H1 -->|No| I2[Skip Commit]
        
        I1 --> J1[Push Changes]
        J1 --> K1[Create PR]
        K1 --> L1[Set Labels]
    end
    
    L1 --> M[End Workflow]
    I2 --> M
    F2 --> M
---
   title: 工作时序图
---
sequenceDiagram
    participant Dev as Dev Branch
    participant GH as GitHub
    participant Runner as CI Runner
    participant AI as AI Service
    
    Dev->>GH: git push
    GH->>Runner: Trigger Workflow
    Runner->>Runner: git checkout AI-PB/deepSeek-R1
    Runner->>Runner: git merge dev
    loop For Each Modified File
        Runner->>AI: Send Code + Prompt
        AI-->>Runner: Return Processed Code
        Runner->>Runner: Overwrite File
    end
    Runner->>GH: git commit & push
    Runner->>GH: Create PR
    GH-->>Runner: PR Created Notification

Actions 编写如下:

name: AI Processing Workflow

on:
  push:
    branches:
      - dev

jobs:
  process-code:
    runs-on: ubuntu-latest

    steps:
      #      从Dev切出分支
      - name: Checkout target branch
        uses: actions/checkout@v4
        with:
          ref: 'dev'
          fetch-depth: 2  # 用于获取足够的提交历史进行diff比较
      #       配置Git选项
      - name: Configure Git
        run: |
          git config --global user.email "action@github.com"
          git config --global user.name "GitHub Action"
      # 检查AI-DP/deepSeek-R1分支是否存在
      - name: Check if AI-DP/deepSeek-R1 branch exists
        id: check-branch
        run: |
          if git ls-remote --heads origin AI-DP/deepSeek-R1; then
            echo "Branch exists=true" >> $GITHUB_OUTPUT
          else
            echo "Branch exists=false" >> $GITHUB_OUTPUT
          fi
#     检查是否有未处理的PR请求
      - name: Check for open PRs on AI-DP/deepSeek-R1
        if: steps.check-branch.outputs.exists == 'true'
        id: check-pr
        run: |
          # 使用GitHub API检查是否存在未合并的PR
          PRS=$(curl -s -H "Authorization: token${{ secrets.GITHUB_TOKEN }}" \
                    "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:AI-DP/deepSeek-R1&state=open" \
                    | jq '.[] | select(.state == "open") | .number')
          if [ -n "$PRS" ]; then
            echo "Open PRs=$PRS" >>$GITHUB_OUTPUT
            echo "There are open PRs on AI-DP/deepSeek-R1 branch. Exiting to prevent data loss."
            exit 1
          else
            echo "No open PRs" >> $GITHUB_OUTPUT
          fi
#        删除AI-DP/deepSeek-R1 branch
      - name: Delete AI-DP/deepSeek-R1 branch if it exists and has no open PRs
        if: steps.check-branch.outputs.exists == 'true' && steps.check-pr.outputs.Open PRs == ''
        run: |
          git push origin --delete AI-DP/deepSeek-R1
#          Create and checkout AI-DP/deepSeek-R1 branch
      - name: Create and checkout AI-DP/deepSeek-R1 branch
        run: |
          git checkout -b AI-DP/deepSeek-R1
          git push origin AI-DP/deepSeek-R1
#     合并Dev分支的改变
      - name: Merge dev changes
        run: |
          git config --global user.email "action@github.com"
          git config --global user.name "GitHub Action"
          git fetch origin dev
          git merge origin/dev --no-edit --strategy-option theirs
#     安装Python环境
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install openai requests python-dotenv

      - name: AI Code Processing
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          # 获取差异文件列表(合并后的更改)
          FILES=$(git diff --name-only HEAD^ HEAD | grep -E '.(py|js|java|cpp)$')
          
          # Python处理脚本
          cat << 'EOF' > process_files.py
          import os
          from openai import OpenAI
          
          api_key = os.getenv("OPENAI_API_KEY")
          client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
          SYSTEM_PROMPT = """You are an experienced code optimizer. Follow these steps to process your code:
            1. Understand precisely the intent and context of the code change
            2. Strictly check the following:
              - Comply with the latest language specifications and coding standards
              - Potential performance bottlenecks and optimization points
              - Error handling with boundary condition coverage
              - Security vulnerabilities and defensive programming
              - Code is readable and maintainable
            3. Apply industry best practices to improve:
              - Optimize data structure and algorithm selection
              - Eliminate code redundancy and improve modularity
              - Enhanced type hints and documentation comments
            4. The output can directly replace the complete code of the original file while maintaining the integrity of the original function
            2. Improve code structure and readability
            3. Add necessary comments such as the function's incoming and outgoing parameters and description
            4. Return results as plain text, without any explanatory text
            5. All code and comments are written in English."""
        

          def process_file(filepath):
              try:
                  with open(filepath, 'r') as f:
                      content = f.read()
                
                  response = client.chat.completions.create(
                      model="deepseek-chat",
                      messages=[
                        {"role": "system", "content": SYSTEM_PROMPT},
                        {"role": "user", "content": content}
                      ],
                      stream=False,
                      temperature=0.0,
                      max_tokens=8000000
                  )
                                   
                  with open(filepath, 'w') as f:
                      f.write(response.choices[0].message.content)
              
              except Exception as e:
                  print(f"Error processing {filepath}: {str(e)}")
          
          
          if __name__ == "__main__":
              import sys
  
              for file in sys.argv[1:]:
                  if os.path.exists(file):
                      print(f"Processing {file}...")
                      process_file(file)
          EOF
          
          # 执行处理脚本
          python process_files.py $FILES

      - name: Commit changes
        id: commit
        run: |
          # 检查是否有文件被修改
          if git diff --quiet; then
            echo "No changes to commit"
            echo "has_changes=false" >> $GITHUB_OUTPUT
          else
            git add .
            git commit -m "🤖 AI Optimized: $(git log -1 --pretty=%B origin/dev)"
            echo "has_changes=true" >> $GITHUB_OUTPUT
          fi

      - name: Push changes
        if: steps.commit.outputs.has_changes == 'true'
        run: git push origin AI-PB/deepSeek-R1

      - name: Create PR
        uses: peter-evans/create-pull-request@v5
        if: steps.commit.outputs.has_changes == 'true'
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          title: "🤖 AI Optimized Changes from dev"
          body: "Automated AI code optimization applied to these files:\n$(git diff --name-only HEAD^ HEAD)"
          base: dev
          head: AI-PB/deepSeek-R1
          delete-branch: false
          labels: "ai-optimized"

演示

原始代码

code-snapshot-1743939033767.png

AI处理后提交的PR代码

code-snapshot-1743938942839.png