Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
搜索和(法学硕士)转换_创想鸟

搜索和(法学硕士)转换

搜索和(法学硕士)转换

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

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

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

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

到

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

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

host.execs*([^,]+,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.varsconst patternrx = new regexp(pattern, "g")const { files } = await workspace.grep(patternrx, glob)

计算变换

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

const { transform } = env.vars...const patches = {} // map of match -> transformedfor (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.execs*([^,]+,s*[[^]]+]s*)' 'transform=Convert the call to a single string command shell in TypeScript'

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1491590.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用 create-next-breeze 简化您的 Nextjs 开发
上一篇 2025年12月19日 14:05:51
执行上下文和调用堆栈
下一篇 2025年12月19日 14:06:00

相关推荐

发表回复

登录后才能评论
关注微信