拉取请求描述器
拉取请求是协作软件开发中不可或缺的一部分。 它们允许开发人员在将代码变更合并到主代码库之前进行审查。 创建信息丰富且简洁的拉取请求描述可能是一项耗时的任务,特别是在处理大型或复杂的变更时。 这正是GenAI发挥作用的地方,它通过智能脚本自动生成拉取请求描述,从而简化了这一流程。🚀
脚本元数据
script({ title: "Pull Request Descriptor", description: "Generate a pull request description from the git diff", temperature: 0.5, system: [ "system", "system.safety_harmful_content", "system.safety_protected_material", ],})script 函数用于设置脚本的元数据。这是您首先会注意到的内容,以下是每个属性的含义:
title: 这是脚本的名称,即"Pull Request Descriptor"。description: 对该脚本功能的简要说明。temperature: 设置AI模型的创意水平。较低的温度意味着较低的创意度,0.5是一个平衡的选择。system.safety...将安全规则注入系统消息中,以确保AI模型不会生成有害或受保护的内容。
使用Git收集变更
该脚本捕获当前分支与defaultBranch之间的差异。
// compute diffconst defaultBranch = await git.defaultBranch()const changes = await git.diff({ base: defaultBranch,})console.log(changes)定义Git Diff输出
def("GIT_DIFF", changes, { language: "diff", maxTokens: 20000,})这里,def 用于定义一个名为 GIT_DIFF 的变量,该变量保存来自 git diff 命令的更改内容。它指定内容采用 diff 格式,并允许最多 20000 个 tokens(这是对AI模型内容长度的一种度量单位)。
生成拉取请求描述
$`You are an expert software developer and architect.
## Task
- Describe a high level summary of the changes in GIT_DIFF in a way that a software engineer will understand.
## Instructions
- do NOT explain that GIT_DIFF displays changes in the codebase- try to extract the intent of the changes, don't focus on the details- use bullet points to list the changes- use emojis to make the description more engaging- focus on the most important changes- ignore comments about imports (like added, remove, changed, etc.)`模板字面量,用$表示,是给AI模型提供提示以生成拉取请求描述的地方。指令清晰明了:总结变更内容而无需深入细节,并通过使用项目符号和表情符号使描述易于理解。
运行脚本
要使用此脚本,您需要安装GenAIScript CLI。如果尚未安装,请参考安装指南。
设置好CLI后,运行以下命令:
npx genaiscript run prd添加 -prd 标志将自动更新 GitHub 上的拉取请求描述。
npx genaiscript run prd -prd完整源代码 (GitHub)
script({ title: "Pull Request Descriptor", description: "Generate a pull request description from the git diff", temperature: 0.5, systemSafety: true,})const { safety } = env.vars
const defaultBranch = await git.defaultBranch()const branch = await git.branch()if (branch === defaultBranch) cancel("you are already on the default branch")
// compute diffconst changes = await git.diff({ base: defaultBranch,})console.log(changes)
def("GIT_DIFF", changes, { maxTokens: 14000, detectPromptInjection: "available",})
// task$`## Task
You are an expert code reviewer with great English technical writing skills.
Your task is to generate a high level summary of the changes in <GIT_DIFF> for a pull request in a way that a software engineer will understand.This description will be used as the pull request description.
## Instructions
- do NOT explain that GIT_DIFF displays changes in the codebase- try to extract the intent of the changes, don't focus on the details- use bullet points to list the changes- use emojis to make the description more engaging- focus on the most important changes- do not try to fix issues, only describe the changes- ignore comments about imports (like added, remove, changed, etc.)`内容安全
为确保生成内容的安全性,采取了以下措施。
- This script includes system prompts to prevent prompt injection and harmful content generation.
- 生成的描述会保存到指定路径的文件中,以便在提交更改前进行手动审查。
进一步增强安全性的额外措施包括运行带有安全过滤器的模型或使用内容安全服务验证消息。
有关内容安全的更多信息,请参阅透明度说明。