AI 自动编程的智能体语言与框架构建?IDE 插件 AutoDev 示例
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上周微软发布了自家的 AI 编程和软件<span style="color: black;">研发</span>智能体框架:AutoDev,其与<span style="color: black;">咱们</span><span style="color: black;">研发</span>的 IDE 插件 AutoDev 有颇多的<span style="color: black;">类似</span>之处,<span style="color: black;">尤其</span>是<span style="color: black;">有些</span>设计思路,以及在<span style="color: black;">针对</span>辅助软件<span style="color: black;">研发</span>任务的智能体以及<span style="color: black;">有些</span><span style="color: black;">基本</span><span style="color: black;">设备</span>上。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">稍有<span style="color: black;">区别</span>的是:</span></p><strong style="color: blue;"><span style="color: black;">交互介质</span></strong><span style="color: black;">。<span style="color: black;">咱们</span>的 AutoDev 构建基于 IDE API 体系构建的,而微软的 AutoDev 则是构建以 CLI 为主。</span><strong style="color: blue;"><span style="color: black;">隔离环境</span></strong><span style="color: black;">。<span style="color: black;">咱们</span>设计了 DevIns 语言来构建隔离环境,而微软的 AutoDev 则是基于 Docker 隔离环境。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当危,<span style="color: black;">咱们</span>的 AutoDev <span style="color: black;">研发</span>人数比较有限,<span style="color: black;">因此</span>相比于微软的 AutoDev 成熟度上是相对比较低的。<span style="color: black;">思虑</span>到<span style="color: black;">咱们</span>的 AutoDev 是一年前开源的,而微软的 AutoDev 是<span style="color: black;">近期</span>发布的,<span style="color: black;">她们</span>这取名有点不厚道。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">AI 驱动软件<span style="color: black;">研发</span>的本质:“人类—AI—代码”的桥梁</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span> AI 驱动的自动编程<span style="color: black;">来讲</span>,无非<span style="color: black;">便是</span>让 AI 能理解好人类的<span style="color: black;">需要</span>,<span style="color: black;">而后</span>实现 AI 与代码环境的自动交互。更<span style="color: black;">仔细</span><span style="color: black;">来讲</span>,便是:</span></p><span style="color: black;">人类<span style="color: black;">经过</span>自然语言<span style="color: black;">或</span>交互描述软件<span style="color: black;">研发</span>任务,如解释代码、生成代码、运行测试等。</span><span style="color: black;">AI 结合智能体与上下文理解人类的<span style="color: black;">需要</span>,并生成对应的指令文本。</span><span style="color: black;">代码环境接收指令文本,并执行对应的操作,再返回结果给人类<span style="color: black;">或</span> AI。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">亦</span><span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span>所要构建的上是一个基于 “人类—AI—代码环境” 的沟通桥梁。实现让 AI 能理解人类的<span style="color: black;">需要</span>,并不是一件<span style="color: black;">繁杂</span>的事情,<span style="color: black;">经过</span>额外的需求澄清、展开, 就有初步得到格式化后的<span style="color: black;">需要</span>。而让 AI 与代码环境进行交互,则是一件更<span style="color: black;">繁杂</span>的事情,即<span style="color: black;">怎样</span><span style="color: black;">经过</span>指令文本来实现的。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">方式 1:基于文本的函数调用</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">函数调用(Function calling)<span style="color: black;">能够</span>让<span style="color: black;">研发</span>人员声名一系列的函数,将其与对应的说明传递给语言模型,让语言模型<span style="color: black;">按照</span>这些说明来生成格式化的结果。随后, 在对应的工具中,调用对应的 API 来实现对应的操作。诸如于 Google AI 中语言模型生成的返回结果示例:</span></p>{<span style="color: black;">"functionCall"</span>: {<span style="color: black;">"name"</span>: <span style="color: black;">"find_theaters"</span>,<span style="color: black;">"args"</span>: {<span style="color: black;">"movie"</span>: <span style="color: black;">"Barbie"</span>,<span style="color: black;">"location"</span>: <span style="color: black;">"Mountain View, CA"</span>}}}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">类似</span>的方式,还有让 AI 生成对应的代码,如 </span><span style="color: black;"><span style="color: black;">shell</span></span> 等,<span style="color: black;">而后</span>执行对应的代码。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">方式 2:语言抽象的<span style="color: black;">研发</span>环境</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">针对</span>自动化的探索是来自于 AutoDev <span style="color: black;">第1</span>个<span style="color: black;">需要</span>,针对 Spring 框架的 AutoCRUD。在这个<span style="color: black;">需要</span>中,<span style="color: black;">咱们</span><span style="color: black;">发掘</span>在<span style="color: black;">繁杂</span>的软件<span style="color: black;">研发</span>任务中,需要动态生成 高质量上下文,以让 AI 能在对应的问题域中生成对应的代码。诸如于,生成 Controller 代码,需要<span style="color: black;">晓得</span>现有 Controller,规范,以及对应的 Service、Repository 等代码。这一系列的信息,<span style="color: black;">寓意</span>着,<span style="color: black;">咱们</span>需要一个更高级别的语言来描述这些信息。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">随后,<span style="color: black;">咱们</span>在 AutoDev 中构建了一系列 Auto 功能(针对 React 的 AutoPage、针对鸿蒙操作系统的 AutoArkUI 等),以探索更合适的语言抽象来描述 “人类—AI—代码环境”,即 DevIns 语言。<span style="color: black;">经过</span>语言来<span style="color: black;">做为</span>人机接口,并<span style="color: black;">做为</span>可执行的代码,来实现对代码环境的操作。诸如于:</span></p>/patch
<span style="color: black;">``</span><span style="color: black;">`patch
// the patch to apply
`</span><span style="color: black;">``</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>形式化的方式来描述对 IDE 的操作,易于让 AI 理解,<span style="color: black;">亦</span>易于让代码环境执行。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">设计基于 IDE 的编程智能体<span style="color: black;">研发</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在设计 AutoDev 的自动编码功能时,<span style="color: black;">咱们</span>依旧是<span style="color: black;">根据</span>在 Unit Mesh 架构范式下的设计思路来设计的, 即 AI 生成的都是可验证的代码。<span style="color: black;">亦</span><span style="color: black;">因此呢</span>,在<span style="color: black;">咱们</span>设计 AutoDev 的自动测试功能时,<span style="color: black;">亦</span>是基于这个思路来设计的。当然了,在有了 DevIns 语言后,就能实现 <span style="color: black;">更加多</span>的自动化(理论上)。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">接下来,让<span style="color: black;">咱们</span>从<span style="color: black;">实质</span>的<span style="color: black;">需要</span>出发,以三个例子来<span style="color: black;">瞧瞧</span><span style="color: black;">平常</span>的编码<span style="color: black;">能够</span><span style="color: black;">怎样</span>设计:</span></p><span style="color: black;">验证生成代码<span style="color: black;">是不是</span>工作?</span><span style="color: black;">进行安全的代码信息提交?</span><span style="color: black;">探索自动化问题辅助修复?</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然了,还<span style="color: black;">能够</span>有<span style="color: black;">更加多</span>的<span style="color: black;">区别</span>示例,<span style="color: black;">这儿</span>就不一一列举了。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">示例<span style="color: black;">过程</span> 1:经验证可工作的代码</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/52d31513b83e489c930f6a6da55eee55~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728038340&x-signature=HvPmxvZeZ7AwwlIdnpgcUEIZlBM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不论是人类,还是 LLM,要验证一段代码<span style="color: black;">是不是</span>工作正确,最简单的方式<span style="color: black;">便是</span>运行它。运行它,<span style="color: black;">一般</span>有多种方式:</span></p><span style="color: black;">直接<span style="color: black;">起步</span>应用。通用 IDE <span style="color: black;">或</span> CLI 来<span style="color: black;">起步</span>应用程序,<span style="color: black;">经过</span>交互界面<span style="color: black;">或</span> API 来验证代码的正确性。</span><span style="color: black;">单元测<span style="color: black;">实验</span>证代码。即<span style="color: black;">经过</span>生成单元测试,以验证生成业务代码的正确性。</span><span style="color: black;">构建 REPL 环境。而交互环境<span style="color: black;">针对</span><span style="color: black;">繁杂</span>应用的依赖管理并非易事,<span style="color: black;">因此</span>并非<span style="color: black;">那样</span>容易。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">与<span style="color: black;">起步</span>应用的效率相比,显然<span style="color: black;">经过</span>测试驱动<span style="color: black;">研发</span>(TDD)来验证代码的正确性更加<span style="color: black;">有效</span>。<span style="color: black;">因此呢</span>,在结合 IDE 时,则需要多<span style="color: black;">思虑</span>一步:如何运行测试以验证代码。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">于是,<span style="color: black;">咱们</span>设计了一个简单的测试运行指令:</span></p>/run:src/<span style="color: black;">test</span>/java/cc/unitmesh/MathHelperTest.java<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span>当<span style="color: black;">咱们</span>生<span style="color: black;">成为了</span>代码后,便<span style="color: black;">能够</span><span style="color: black;">经过</span>运行测试来验证代码的正确性。<span style="color: black;">因为</span> Intellij IDEA 支持<span style="color: black;">区别</span>的语言,<span style="color: black;">然则</span><span style="color: black;">区别</span>的语言运行方式等是<span style="color: black;">区别</span>的。而<span style="color: black;">因为</span> JetBrains IDE <span style="color: black;">运用</span>了统一的底层抽象:</span><span style="color: black;"><span style="color: black;">RunConfiguration</span></span>,<span style="color: black;">因此呢</span><span style="color: black;">咱们</span>构建了一个 <span style="color: black;"><span style="color: black;">RunService</span></span> 来封装<span style="color: black;">区别</span>语言的运行方式:</p><span style="color: black;"><span style="color: black;">interface</span> <span style="color: black;">RunService</span> </span>{
<span style="color: black;"><span style="color: black;">fun</span> <span style="color: black;">createConfiguration</span><span style="color: black;">(project: <span style="color: black;">Project</span>, virtualFile:<span style="color: black;">VirtualFile</span>)</span></span>: RunConfiguration? = nullfun
runFile(project: Project, virtualFile: VirtualFile): String? { }
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">更<span style="color: black;">仔细</span><span style="color: black;">能够</span>参见 RunService.kt 代码。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">示例<span style="color: black;">过程</span> 2:安全的 Git 操作</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/01351d4a3dbc4872a5f280fc2057e24e~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728038340&x-signature=XENi%2FjFoNO%2FFXHqWAcV5jRVrmro%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">既然,<span style="color: black;">咱们</span>生<span style="color: black;">成为了</span>可验证的代码,<span style="color: black;">那样</span>下一步,<span style="color: black;">咱们</span>应该<span style="color: black;">思虑</span>的是结合 VCS 来进行代码提交。为了<span style="color: black;">保证</span>不执行不安全的操作,<span style="color: black;">咱们</span>不直接执行 Git 操作,而 是借助于 IDE 的 VCS API 来执行对应的操作。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">于是,<span style="color: black;">咱们</span>设计了 </span><span style="color: black;"><span style="color: black;">/commit</span></span> 指令来提交代码:</p>/<span style="color: black;">commit</span>
<span style="color: black;"><span style="color: black;">```commit</span></span><span style="color: black;"><span style="color: black;">feat: add new feature</span></span><span style="color: black;"><span style="color: black;">```</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而<span style="color: black;">针对</span>需要更<span style="color: black;">繁杂</span>的场景,诸如于远程生成的任务<span style="color: black;">来讲</span>,<span style="color: black;">咱们</span><span style="color: black;">经过</span> </span><span style="color: black;"><span style="color: black;">/patch</span></span> 指令来</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">示例<span style="color: black;">过程</span> 3:自动化问题辅助修复</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/2eaea36158d74e71a9cb9799ce91e207~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728038340&x-signature=vRV8eMMMneISd5uWBMYdMnTvFlA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">接下来,<span style="color: black;">咱们</span>的挑战<span style="color: black;">便是</span><span style="color: black;">怎样</span>在 IDE 获取运行结果,并<span style="color: black;">按照</span>结果来进行对应的操作。于是,<span style="color: black;">咱们</span>在 AutoDev 中设计了一个 </span><span style="color: black;"><span style="color: black;">DevInsProcessProcessor</span></span> 来 处理 DevIns 指令的执行结果:</p> <span style="color: black;">when</span> {
<span style="color: black;">event</span>.exitCode == <span style="color: black;">0</span> -> {
val comment = lookupFlagComment(devInFile!!).firstOrNull() ?: <span style="color: black;">return</span>
<span style="color: black;"><span style="color: black;">///</span>handle flag comments</span>
}
<span style="color: black;">event</span>.exitCode != <span style="color: black;">0</span> -> {
project.service<DevInsConversationService>().tryFixWithLlm(scriptPath)
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">即:</span></p><span style="color: black;">成功。当 exitCode 为 0 时,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span> flag comments 来决定<span style="color: black;">怎样</span>处理</span><span style="color: black;">失败。当 exitCode 不为 0 (如 -1)时,<span style="color: black;">咱们</span>则<span style="color: black;">能够</span>继续<span style="color: black;">经过</span> AI 来尝试修复对应的问题</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在失败的场景时,<span style="color: black;">咱们</span>需要构建完整的上下文:输入、编译输出、 执行结果/LLM 返回结果,以便于 AI 能更好的理解问题,再给出对应的修复<span style="color: black;">方法</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">更<span style="color: black;">仔细</span><span style="color: black;">能够</span>参见 DevInsProcessProcessor.kt 代码。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">其它</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>依旧还在设计适用于 IDE 的自动<span style="color: black;">研发</span>框架与 DevIns 语言,<span style="color: black;">倘若</span><span style="color: black;">大众</span>有兴趣,<span style="color: black;">能够</span>参与到<span style="color: black;">咱们</span>的<span style="color: black;">研发</span>中来。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">GitHub:https://github.com/unit-mesh/auto-dev</span></p>
哈哈、笑死我了、太搞笑了吧等。 “NB”(牛×的缩写,表示叹为观止)
页:
[1]