ホームページ  >  記事  >  ウェブフロントエンド  >  検索と (LLM) 変換

検索と (LLM) 変換

DDD
DDDオリジナル
2024-09-19 06:22:32891ブラウズ

Search And (LLM) Transform

この記事では、テキスト エディターの「検索と置換」機能の進化を示します。
ここで、「置換」ステップは 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)

変換の計算

2 番目のステップは、ファイルの内容に正規表現を適用することです
インライン プロンプトを使用して、各一致の 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)
}

パラメータ

スクリプトは、ファイル グロブ、検索するパターン、および適用する LLM 変換の 3 つのパラメーターを受け取ります。
これらのパラメーターをスクリプトのメタデータで宣言し、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'

以上が検索と (LLM) 変換の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:最初の貢献次の記事:最初の貢献