# 校正テキストでテキストが二重挿入される

**URL:** https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248
**Category:** Bug
**Tags:** ai, ai-helper
**Created:** [2024 年 11 月 29 日午前 9:42 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248 "2024-11-29T09:42:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2024 年 11 月 29 日午前 9:42 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/1 "2024-11-29T09:42:26Z")

</div>

理由が全く分からないのですが、すべての投稿で発生するわけではなく、特定の[投稿](https://meta.discourse.org/t/emoji-fluff/339163/6?u=moin)でproofreadを使用すると、コンテンツが重複します。

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2024 年 11 月 30 日午後 4:41 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/2 "2024-11-30T16:41:35Z")

</div>

うーん、リンクされた投稿では再現できましたが、まだ他の場所では見つけられていません。本当に奇妙です 🧐 🤔

これでも試してみました

 ![Screenshot 2024-11-30 at 8.39.39 AM](https://global.discourse-cdn.com/meta/original/4X/e/2/a/e2a5f5ac2fbca6a67f567471b2569ab404490baa.png)

しかし、引用文のタイプミスがなければ、引用文を修正しようとすることに気づきました。

 ![Screenshot 2024-11-30 at 8.52.35 AM](https://global.discourse-cdn.com/meta/original/4X/e/f/1/ef1b83b6ce83b648e8358ec65b93fb300c781be4.png)  
 ![Screenshot 2024-11-30 at 8.52.51 AM](https://global.discourse-cdn.com/meta/original/4X/e/e/0/ee0ad0341f9d60d05010bf7a0fd592e9e08ef37c.png)

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [2024 年 11 月 30 日午後 5:20 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/3 "2024-11-30T17:20:07Z")

</div>

これはとても奇妙です。 😄

テキストを何も選択しておらず、引用文に修正すべき点があり、あなたが非スタッフユーザーの場合、重複します。 🤔

 ![画像は、テキストエディタのインターフェースと、「提案された編集」ポップアップウィンドウを表示しており、最初の編集は「このコンポーネントにはDiscourseの更新が必要です。」となっています。（AIによるキャプション）](https://global.discourse-cdn.com/meta/original/4X/9/9/6/996e38a3de6d9740c7a8773c9ddd8e5968f5b7a0.png)

 ![テキストドキュメントの提案された編集オプション（確認または破棄）を含むポップアップウィンドウを示すスクリーンショット。（AIによるキャプション）](https://global.discourse-cdn.com/meta/original/4X/c/4/8/c483b5c5930666b3a83616dbd70840cea9215550.png)

* * *

引用文の内容が問題ない場合、重複しません。

 ![テキストエディタのインターフェースと、テキストへの提案された編集を提供するポップアップウィンドウを示すスクリーンショット。（AIによるキャプション）](https://global.discourse-cdn.com/meta/original/4X/5/1/b/51b67ee85f067d42ff63dec587f567bce97ddaa3.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024 年 12 月 3 日午前 3:44 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/6 "2024-12-03T03:44:32Z")

</div>

これは、Qwen @Falco によってトリガーされているバグです。

```json
{
  "model": "meta-llama/Llama-3.2-11B-Vision-Instruct",
  "temperature": 0,
  "stop": [
    "\n</output>"
  ],
  "messages": [
    {
      "role": "system",
      "content": "あなたはマークダウン校正者です。ひどいタイプミスや言い回しの問題を修正しますが、ユーザーの元の声はそのまま維持します。\nコードブロックには触れません。校正するテキストを提供します。修正が必要ない場合は、テキストをそのまま返します。\nテキストは <input></input> XML タグの間にあります。\n常に修正されたテキストを <output></output> XML タグの間に返します。\n\n"
    },
    {
      "role": "user",
      "content": "<input>[quote=\"Arkshine, post:1, topic:339163\"]\n:information_source: This component requires Discourse to be current as of [date=2024-11-27 timezone=\"Europe/Paris\"]. \n[/quote]\nDid you update Discourse? You only receive a notification to update when a new beta is released, but new commits are added every day.</input>"
    }
  ]
}

```

```json
{
  "id": "chatcmpl-752c6aacdc7f496b951592e88d485eb3",
  "object": "chat.completion",
  "created": 1733196730,
  "model": "Qwen/Qwen2.5-32B-Instruct-AWQ",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "<input>[quote=\"Arkshine, post:1, topic:339163\"]\n:information_source: This component requires Discourse to be current as of [date=2024-11-27 timezone=\"Europe/Paris\"]. \n[/quote]\nDid you update Discourse? You only receive a notification to update when a new beta is released, but new commits are added daily.</input>\n\n<output>[quote=\"Arkshine, post:1, topic:339163\"]\n:information_source: This component requires Discourse to be current as of [date=2024-11-27 timezone=\"Europe/Paris\"]. \n[/quote]\nDid you update Discourse? You only receive a notification to update when a new beta is released, but new commits are added daily.</output>",
        "tool_calls": []
      },
      "logprobs": null,
      "finish_reason": "stop",
      "stop_reason": null
    }
  ],
  "usage": {
    "prompt_tokens": 184,
    "total_tokens": 358,
    "completion_tokens": 174,
    "prompt_tokens_details": null
  },
  "prompt_logprobs": null
}

```

`<input>` と `<output>` の両方のタグが返されていることに注意してください。したがって、ここにバグがあります。

> <https://github.com/discourse/discourse-ai/blob/e3f5e86dc5d1d75d0cd45c9ece385e5198888d48/lib/ai_helper/assistant.rb#L174-L183>

Sanitize regex が入力と出力の両方を保持しています。

API をより慎重に使用するか、出力を求める場合のみ、またはより優れたプロンプトエンジニアリングを行うべきだと思います。

また、興味深いことに、例を送信するのをやめました。例はありますが、@Roman

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024 年 12 月 3 日午前 4:34 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/8 "2024-12-03T04:34:53Z")

</div>

これは、リグレッションの根本的な原因を修正します。

> <https://github.com/discourse/discourse-ai/pull/993>
>
> For a while now we have not been sending the examples to AI
> helper, which can le…ad to inconsistent results.
> 
> Note: this also means that in non English we did not send
> English results, so this may end up reducing performance
> 
> That said first thing we need to do is fix the regression.

ただし、副作用があります、@Jagster。しばらくの間、英語の例の送信を停止していましたが、再び送信するようになります。これが影響するかどうかお知らせください。

とはいえ、@Roman、これは私には意味が通りません。

```plaintext
SANITIZE_REGEX_STR =
            %w[term context topic replyTo input output result]
              .map { |tag| "<#{tag}>\n?|\n?<\/#{tag}>" }
              .join("|")

```

そうではなく、こうなるべきではありませんか？

（item はタイトルの提案用ですが、別のパスを取っている可能性があります）

```plaintext
SANITIZE_REGEX_STR =
            %w[output item]
              .map { |tag| "<#{tag}>\n?|\n?<\/#{tag}>" }
              .join("|")

```

---

<div class="post-metadata">

### Author: ![Roman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/roman/32/157504_2.png) [@Roman](https://meta.discourse.org/u/Roman)
#### Post date: [2024 年 12 月 3 日午後 12:39 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/10 "2024-12-03T12:39:44Z")

</div>

コンテキストを提供するために、ヘルパープロンプトの一部でこれらのタグが使用されています。たとえば、次のようになります。

> <https://github.com/discourse/discourse-ai/blob/main/app/jobs/regular/stream_post_helper.rb#L25>

一部のモデルでは、返信にそれらを含める場合があるため、削除します。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024 年 12 月 3 日午後 7:07 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/11 "2024-12-03T19:07:54Z")

</div>

フォローされていません。完全な例で説明していただけますか？

モデルが提供するものをサニタイズする際に、なぜ入力タグ内のテキストを出力に保持したいのですか？

（ちなみに、オペレーターは現在稼働中です）

---

<div class="post-metadata">

### Author: ![Roman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/roman/32/157504_2.png) [@Roman](https://meta.discourse.org/u/Roman)
#### Post date: [2024 年 12 月 3 日午後 8:31 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/12 "2024-12-03T20:31:51Z")

</div>

「サニタイズ」という言葉はここでは少し誤解を招きます。私たちは2つの異なる問題を解決したいのです。

1. 出力のみを取得できるようにする。
2. 結果が不自然に見えるタグをすべて削除する。

問題は、(1)に対してあまりにも緩いことです。関連部分は常に``で囲み、それ以外は何も使用しないようにする必要があります。この関連部分を取得したら、結果がきれいに見えるように、他のすべてのタグを削除します (2)。

* * *

上記で提供した例を拡張し、なぜ現在これらのタグをすべて削除しているのかを説明するために、シードされた「説明」プロンプトは次のようになります。

[https://github.com/discourse/discourse-ai/blob/main/db/fixtures/ai\_helper/603\_completion\_prompts.rb#L157](https://github.com/discourse/discourse-ai/blob/main/db/fixtures/ai_helper/603_completion_prompts.rb#L157)

`<term>`、`<replyTo>`はモデルにコンテキストを提供するために使用され、`<input>`は特定のテキストに焦点を当てるように指示するために使用されます。

問題は、一部のモデルが返信で同じタグを使用していたため、テキストが不自然でユーザーにとって奇妙に見えることでした。最終的な目標は、これらのタグを削除し、「クリーンな」テキストを結果として生成することです。

たとえば、「Not following」の意味の説明を取得したい場合、次のようなものを見たくありません。

> `<term>`Not following`</term>` この文脈では、ユーザーが説明や述べられている点を理解するのに苦労していることを意味します。(…)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024 年 12 月 4 日午前 12:11 UTC](https://meta.discourse.org/t/proofread-text-inserts-the-text-twice/339248/13 "2024-12-04T00:11:40Z")

</div>


