搜索和(法学硕士)转换

搜索和(法学硕士)转换

本文展示了文本编辑器中“搜索和替换”功能的演变,
其中“替换”步骤已被 llm 转换替换。
该示例使用 genaiscript。

批量应用使用
不容易完成的文本转换可能很有用 正则表达式。

例如,当我们在
中添加使用字符串命令的功能时 对于 exec 命令,我们需要将使用参数数组的所有调用转换为以下新语法:

host.exec("cmd", ["arg0", "arg1", "arg2"])


host.exec(`cmd arg0 arg1 arg2`)`

虽然可以将此函数调用与正则表达式匹配

host\.exec\s*\([^,]+,\s*\[[^\]]+\]\s*\)

制定替换字符串并不容易......除非你能用自然语言描述它:

convert the call to a single string command shell in typescript

以下是 llm 正确处理变量的一些转换示例。

  • 将函数调用的参数连接成单个字符串
- const { stdout } = await host.exec("git", ["diff"])
+ const { stdout } = await host.exec(`git diff`)
  • 连接参数并使用 ${} 语法来插入变量
- const { stdout: commits } = await host.exec("git", [
-     "log",
-     "--author",
-     author,
-     "--until",
-     until,
-     "--format=oneline",
- ])
+ const { stdout: commits } = 
+   await host.exec(`git log --author ${author} --until ${until} --format=oneline`)

搜索

搜索步骤是通过workspace.grep
完成的 允许有效地搜索文件中的模式(这是同一个搜索引擎
visual studio code 搜索提供支持)。

const { pattern, glob } = env.vars
const patternrx = new regexp(pattern, "g")
const { files } = await workspace.grep(patternrx, glob)

计算变换

第二步是将正则表达式应用到文件内容
并使用内联提示预先计算每场比赛的 llm 转换。

const { transform } = env.vars
...
const patches = {} // map of match -> transformed
for (const file of files) {
    const { content } = await workspace.readtext(file.filename)
    for (const match of content.matchall(patternrx)) {
        const res = await runprompt(
            (ctx) => {
                ctx.$`
            ## task

            your task is to transform the match with the following transform.
            return the transformed text.
            - do not add enclosing quotes.

            ## context
            `
                ctx.def("matched", match[0])
                ctx.def("transform", transform)
            },
            { label: match[0], system: [], cache: "search-and-transform" }
        )
        ...

由于 llm 有时决定将答案用引号引起来,因此我们需要将其删除。

    ...
    const transformed = res.fences?.[0].content ?? res.text
    patches[match[0]] = transformed

转换

最后,通过预先计算的转换,我们将最终的正则表达式替换应用于
使用转换后的字符串修补旧文件内容。

    const newcontent = content.replace(
        patternrx,
        (match) => patches[match] ?? match
    )
    await workspace.writetext(file.filename, newcontent)
}

参数

该脚本采用三个参数:文件 glob、要搜索的模式以及要应用的 llm 转换。
我们在脚本元数据中声明这些参数,并从 env.vars 对象中提取它们。

script({ ...,
    parameters: {
        glob: {
            type: "string",
            description: "the glob pattern to filter files",
            default: "*",
        },
        pattern: {
            type: "string",
            description: "the text pattern (regular expression) to search for",
        },
        transform: {
            type: "string",
            description: "the llm transformation to apply to the match",
        },
    },
})
const { pattern, glob, transform } = env.vars

跑步

要运行此脚本,您可以使用 --vars 选项来传递模式和转换。

genaiscript run st --vars 'pattern=host\.exec\s*\([^,]+,\s*\[[^\]]+\]\s*\)' 'transform=Convert the call to a single string command shell in TypeScript'

以上就是搜索和(法学硕士)转换的详细内容,更多请关注www.sxiaw.com其它相关文章!