AIサマライザーの多言語化カスタマイズ

こんにちは。最新の Discourse でローカルに google/gemma-3-4b を使用しています。このモデルはいくつかの言語でうまく機能します。API や LM Studio を使用してテストすると、要求した言語で要約が提供されます。

現在、Discourse は常に英語で要約を提供しています。以下の手順は、要約の言語(英語以外)をハードコーディングする方法を示しています。


重要! 次回の再構築中に変更は失われます。

ハードコーディングされた行は、2 つのファイルにあります。ai_personas テーブルのデータベース値は使用されません(2025 年 7 月)。非本番環境で作業している場合は、母国語をハードコーディングできます。

  1. サーバーに SSH します。

  2. ハードコーディングされたファイル summarize.rb をコンテナからホストファイルシステムにコピーします。

    sudo docker cp app:/var/www/discourse/plugins/discourse-ai/lib/personas/tools/summarize.rb ./summarize.rb
    
  3. ファイルを編集し、英語のシステムプロンプトを目的の言語に置き換えます。

    要約
           system_prompt = <<~TEXT
           You are a summarization bot.
           You effectively summarise any text.
           You condense it into a shorter version.
           You understand and generate Discourse forum markdown.
           Try generating links as well the format is #{topic.url}/POST_NUMBER. eg: [ref](#{topic.url}/77)
           TEXT
    
           user_prompt = <<~TEXT
             Guidance: #{guidance}
             You are summarizing the topic: #{topic.title}
             Summarize the following in 400 words:
    
             #{text}
           TEXT
    

    結果の例:

           system_prompt = <<~TEXT
           Вы — бот, выполняющий суммаризацию текста.
           Вы умеете эффективно сокращать текст до ключевых мыслей.
           Вы понимаете и умеете генерировать разметку Markdown в Discourse.
           При необходимости добавляйте ссылки в формате: #{topic.url}/POST_NUMBER, например: [ссылка](#{topic.url}/77)
           TEXT
    
           user_prompt = <<~TEXT
             Руководство: #{guidance}
             Вы суммаризуете топик: #{topic.title}
             Пожалуйста, предоставь ответ на русском языке.
             В ответе используй 400 слов:
    
             #{text}
           TEXT
    
  4. 次に、2 番目のファイルでも同様の操作を行います。

    sudo docker cp app:/var/www/discourse/plugins/discourse-ai/lib/personas/summarizer.rb ./summarizer.rb
    

    編集:

    注意:元のテキストの言語を上書きできます。

    - Используйте русский язык, несмотря на язык оригинала исходного текста.
    
    要約
         <<~PROMPT.strip
           You are an advanced summarization bot that generates concise, coherent summaries of provided text.
           You are also capable of enhancing an existing summaries by incorporating additional posts if asked to.
    
           - Only include the summary, without any additional commentary.
           - You understand and generate Discourse forum Markdown; including links, _italics_, **bold**.
           - Maintain the original language of the text being summarized.
           - Aim for summaries to be 400 words or less.
           - Each post is formatted as "<POST_NUMBER> <USERNAME> <MESSAGE>"
           - Cite specific noteworthy posts using the format [DESCRIPTION]({resource_url}/POST_NUMBER)
           - Example: links to the 3rd and 6th posts by sam: sam ([#3]({resource_url}/3), [#6]({resource_url}/6))
           - Example: link to the 6th post by jane: [agreed with]({resource_url}/6)
           - Example: link to the 13th post by joe: [joe]({resource_url}/13)
           - When formatting usernames use [USERNAME]({resource_url}/POST_NUMBER)
    
           Format your response as a JSON object with a single key named "summary", which has the summary as the value.
           Your output should be in the following format:
             <output>
               {"summary": "xx"}
             </output>
    
           Where "xx" is replaced by the summary.
         PROMPT
       end
    
    ...
           [
             "Here are the posts inside <input></input> XML tags:\n\n<input>1) user1 said: I love Mondays 2) user2 said: I hate Mondays</input>\n\nGenerate a concise, coherent summary of the text above maintaining the original language.",
             {
               summary:
                 "Two users are sharing their feelings toward Mondays. [user1]({resource_url}/1) hates them, while [user2]({resource_url}/2) loves them.",
             }.to_json,
           ],
    

    結果:

            <<~PROMPT.strip
           Вы являетесь продвинутым ботом для составления краткого содержания, который генерирует краткие, связные выдержки из предоставленного текста.
           Вы также можете дополнить существующее резюме, добавив дополнительные сообщения, если вас попросят.
    
           - Включайте только краткую сводку, без каких-либо дополнительных комментариев.
           - Вы понимаете и создаете разметку Markdown на форуме Discourse, включая ссылки, _курсив_, **жирный_текст**.
           - Используйте русский язык, несмотря на язык оригинала исходного текста.
           - Старайтесь, чтобы объем резюме не превышал 400 слов.
           - Каждая запись оформляется как "<POST_NUMBER>) <USERNAME> <MESSAGE>"
           - Цитируйте конкретные заслуживающие внимания публикации, используя формат [DESCRIPTION]({resource_url}/POST_NUMBER)
           - Пример: ссылки на 3-й и 6-й посты пользователя sam: sam ([#3]({resource_url}/3), [#6]({resource_url}/6))
           - Пример: ссылка на 6-е сообщение пользователя jane: [согласовано с]({resource_url}/6)
           - Пример: ссылка на 13-е сообщение Джо: [Джо]({resource_url}/13)
           - При форматировании имен пользователей используйте [USERNAME]({resource_url}/POST_NUMBER)
    
           Отформатируйте свой ответ в виде объекта JSON с помощью единственного ключа с именем "summary", который имеет значение "summary".
           Ваши выходные данные должны быть в следующем формате:
             <output>
               {"summary": "xx"}
             </output>
    
           Где "xx" заменяется на текст краткой сводки.
         PROMPT
       end
    
       def response_format
         [{ "key" => "summary", "type" => "string" }]
       end
    
       def examples
         [
           [
             "Вот записи внутри XML-тегов <input></input>:\n\n<input>1) user1 сказал: Я люблю понедельники 2) user2 сказал: А я ненавижу понедельники</input>\n\nСформулируйте краткое, связное изложение текста выше, сохранив язык оригинала.",
             {
               summary:
                 "Два пользователя делятся своими чувствами к понедельникам. [user1]({resource_url}/1) ненавидит их, тогда как [user2]({resource_url}/2) любит их.",
             }.to_json,
           ],
    
  5. 変更されたファイルをコンテナにコピーします。

    sudo docker cp summarize.rb app:/var/www/discourse/plugins/discourse-ai/lib/personas/tools/summarize.rb
    sudo docker cp summarizer.rb app:/var/www/discourse/plugins/discourse-ai/lib/personas/summarizer.rb
    
  6. 次に、コンテナをコミットして再起動します。

    sudo docker commit app
    sudo /var/discourse/launcher restart app
    
  7. 結果を確認します(新しいトピックの場合)。

これらすべてを行う必要はありません。管理設定で要約を行うペルソナを変更できます。

既存のペルソナの設定に従って新しいペルソナを作成し、システムプロンプトを好きなように変更して、要約機能を /admin/plugins/discourse-ai/ai-features/1/edit で使用するように設定してください。

「いいね!」 2

さて…言語サポートに関する最新の情報は、このトピックで見つかりました。返信ありがとうございます。

既存のボットのクローンとして、適切な要約ボットを作成する最初の試みは失敗しました。まだ英語を生成しています。おそらく私が何か間違っているのでしょう。

このモデルでどれほどうまくいくか分かりませんが、それほど強力ではありません。

「いいね!」 1

非英語のAI要約に対する皆さんの回避策やアプローチは何ですか?

例えば、ロケールがドイツ語に設定されているチャットの要約で、依然として英語の言語の要約が表示され、個々のチャットメッセージへの明確なテキストのマークダウンリンクが正しくリンクとしてフォーマットされていません。

Gemini 2.5 Liteでテストしました

「いいね!」 1

こんにちは。上記で説明した回避策をまだ使用しています。カスタムペルソナを使ったトリックを試しましたが、うまくいきませんでした。おそらく私が何か間違っているのでしょうが、私にとってはそれが最も痛みが少ない方法です。

簡単な概要として、GitHub からダウンロードしたテンプレートを準備し、必要に応じて変更を加え、再構築のたびに適用します。2〜3か月に一度はこれらのファイルの新しいバージョンを確認することを忘れないでください。

ホームディレクトリに実行可能スクリプト do_it_after_rebuild.sh を作成します。

#/bin/bash
#
# https://github.com/discourse/discourse/tree/main/plugins/discourse-ai/lib/personas
docker cp app:/var/www/discourse/plugins/discourse-ai/lib/personas/tools/summarize.rb orig_summarize.rb
docker cp app:/var/www/discourse/plugins/discourse-ai/lib/personas/summarizer.rb orig_summarizer.rb
docker cp app:/var/www/discourse/plugins/discourse-ai/lib/personas/short_summarizer.rb orig_short_summarizer.rb
docker cp app:/var/www/discourse/plugins/discourse-ai/lib/personas/discover.rb orig_discover.rb

rm GeoLite2*
wget https://raw.githubusercontent.com/8bitsaver/maxmind-geoip/release/GeoLite2-City.mmdb
wget https://raw.githubusercontent.com/8bitsaver/maxmind-geoip/release/GeoLite2-ASN.mmdb

docker cp GeoLite2-City.mmdb    app:/var/www/discourse/vendor/data/
docker cp GeoLite2-ASN.mmdb     app:/var/www/discourse/vendor/data/
docker cp summarize.rb          app:/var/www/discourse/plugins/discourse-ai/lib/personas/tools/summarize.rb
docker cp summarizer.rb         app:/var/www/discourse/plugins/discourse-ai/lib/personas/summarizer.rb
docker cp short_summarizer.rb   app:/var/www/discourse/plugins/discourse-ai/lib/personas/short_summarizer.rb
docker cp discover.rb           app:/var/www/discourse/plugins/discourse-ai/lib/personas/discover.rb
docker commit app
sudo /var/discourse/launcher restart app

再構築後に実行します。

./do_it_after_rebuild.sh
ここにファイルがあります

これらの変更を、定期的にこちらからダウンロードする必要があるファイルに対して行います(差分のみを示します。これらの行は手動でファイルに追加する必要があります)。

diff discover.rb orig_discover.rb
35d34
<         * Use always German language.
80d78
<
└─# diff short_summarizer.rb orig_short_summarizer.rb
12c12,13
< Du bist ein fortgeschrittener Bot, um den Text zusammenzufassen. Sie analysieren den bereitgestellten Text und erzeugen eine kurze Zusammenfassung aus einem einzigen Satz, in dem das Hauptthema und die aktuellen Ereignisse dem Gesprächspartner ohne vorläufigen Kontext verständlich sind.
---
> You are an advanced summarization bot. Analyze a given conversation and produce a concise,
> single-sentence summary that conveys the main topic and current developments to someone with no prior context.
14c15
< ### Anweisungen:
---
> ### Guidelines:
16,28c17,23
< - Unterstreiche die neuesten Updates aufgrund ihrer Bedeutung im ursprünglichen Beitrag.
< - Konzentriere dich auf das betreffende Hauptthema oder -problem und behalte einen objektiven und neutralen Ton bei.
< - Schließen Sie fremde Details oder subjektive Meinungen aus.
< - Benutze immer nur die russische Sprache, ignoriere die Sprache des Originaltextes.
<
---
> - Underline the latest updates due to their importance in the original post.
> - Focus on the main topic or issue at hand, maintaining an objective and neutral tone.
> - Exclude extraneous details or subjective opinions.
> - Always use only the Russian language, ignore the language of the original text.
>
└─# diff summarizer.rb orig_summarizer.rb
12,13c12,13
< Sie sind ein fortgeschrittener Bot, um kurze Inhalte zu erstellen, die kurze, zusammenhängende Auszüge aus dem bereitgestellten Text erzeugen.
< Sie können einen vorhandenen Lebenslauf auch ergänzen, indem Sie zusätzliche Beiträge hinzufügen, wenn Sie dazu aufgefordert werden.
---
> You are an advanced summarization bot that generates concise, coherent summaries of provided text.
> You are also capable of enhancing an existing summaries by incorporating additional posts if asked to.
15,24c15,23
< - Fügen Sie nur eine kurze Zusammenfassung hinzu, ohne weitere Kommentare.
< - Sie verstehen und erstellen Markdown im Discourse-Forum, einschließlich Links, _kursiv_, **Fetttext**.
< - Verwenden Sie die russische Sprache trotz der Sprache des ursprünglichen Quelltextes.
< - Versuchen Sie, den Lebenslauf auf 400 Wörter zu beschränken.
< - Jeder Eintrag wird als "<POST_NUMBER>) <USERNAME> <MESSAGE>" ausgegeben
< - Zitieren Sie bestimmte bemerkenswerte Publikationen mit dem Format [BESCHREIBUNG]({resource_url}/POST_NUMBER)
< - Beispiel: Links zu den 3. und 6. Posts von sam: sam ([#3]({resource_url}/3), [#6]({ resource_url}/6))
< - Beispiel: Verweis auf die 6. Nachricht von jane: [konsistent mit]({resource_url}/6)
< - Beispiel: Verweis auf Joes 13. Beitrag: [Jo]({resource_url}/13)
< - Verwenden Sie beim Formatieren von Benutzernamen [USERNAME]({resource_url}/POST_NUMBER)
---
> - Only include the summary, without any additional commentary.
> - You understand and generate Discourse forum Markdown; including links, _italics_, **bold**.
> - Maintain the original language of the text being summarized.
> - Aim for summaries to be 400 words or less.
> - Каждый пост форматируется как "<POST_NUMBER>) <USERNAME> <MESSAGE>"
> - Цитируйте определенные примечательные сообщения в формате [ОПИСАНИЕ]({resource_url}/POST_NUMBER)
> - Пример: Ссылки на 3-й и 6-й посты от sam: sam ([#3]({resource_url}/3), [#6]({ resource_url}/6))
> - Пример: Ссылка на 6-е сообщение от jane: [согласен с]({resource_url}/6)
> - Пример: Ссылка на 13-й пост от joe: [joe]({resource_url}/13)
> - Используйте [USERNAME]({resource_url}/POST_NUMBER) при форматировании имен пользователей
Nr.26,30c25,28
< Отформатируйте свой ответ в виде объекта JSON с помощью единственного ключа с именем "Zusammenfassung", который имеет значение "Zusammenfassung".
< Ваши выходные данные должны быть в следующем формате:
< 
< {"zusammenfassung": "xx"}
< 
---
> Formatieren Sie Ihre Antwort als JSON-Objekt mit einem einzelnen Schlüssel namens "summary", der die Zusammenfassung als Wert enthält.
> Ihre Ausgabe sollte im folgenden Format vorliegen:
> 
> {"Zusammenfassung": "xx"}
32c30,31
< Wobei "xx" durch den Text der Zusammenfassung ersetzt wird.
---
> Where "xx" is replaced by the summary.
> reply with valid JSON only
43c42
< "Hier sind die Einträge in den XML-Tags <input></input>:\n\n<input>1) user1 sagte: Ich liebe Montags 2) user2 sagte: Und ich hasse Montags</input>\n\nformulieren Sie die kurze, zusammenhängende Darstellung des Textes oben, während Sie die ursprüngliche Sprache beibehalten.",
---
> "Here are the posts inside <input></input> XML tags:\n\n<input>1) user1 said: I love Mondays 2) user2 said: I hate Mondays</input>\n\nGenerate a concise, coherent summary of the text above maintaining the original language.",
46c45
< "Zwei Benutzer teilen ihre Gefühle für Montag. [user1]({resource_url}/1) hasst sie, während [user2]({resource_url}/2) sie liebt.",
---
> "Two users are sharing their feelings toward Mondays. [user1]({resource_url}/1) hates them, while [user2]({resource_url}/2) loves them.",
└─# diff summarize.rb orig_summarize.rb
159c159
< max_tokens: 4096,
---
> max_tokens: 500,
170,173c170,174
< Sie sind ein Bot, der den Text zusammenfasst.
< Sie sind in der Lage, Text effektiv auf wichtige Gedanken zu reduzieren.
< Sie verstehen und können Markdown-Markdown in Discourse generieren.
< Fügen Sie bei Bedarf Links im Format #{topic.url}/POST_NUMBER hinzu, zum Beispiel: [link](#{topic.url}/77)
---
> You are a summarization bot.
> You effectively summarise any text.
> You condense it into a shorter version.
> You understand and generate Discourse forum markdown.
> Try generating links as well the format is #{topic.url}/POST_NUMBER. eg: [ref](#{topic.url}/77)
177,180c178,180
< Handbuch: #{guidance}
< Sie fassen das Thema zusammen: #{topic.title}
< Bitte gib eine Antwort auf Russisch an.
< Benutze 400 Wörter in deiner Antwort:
---
> 
> Guidance: #{guidance}
> Summarize the topic: #{topic.title}
> Please reply in Russian.
> Use 400 words in your reply: