搜索和(法学硕士)转换
收藏
欢迎各位小伙伴来到米云,相聚于此都是缘哈哈哈!今天我给大家带来《搜索和(法学硕士)转换》,这篇文章主要讲到等等知识,如果你对文章相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

本文展示了文本编辑器中“搜索和替换”功能的演变,
其中“替换”步骤已被 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'
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注米云公众号,一起学习编程~
版本声明 本文转载于:dev.to 如有侵犯,请联系删除
- WGCLOUD如何监测服务器业务应用运行状态?
