管理员 - 自定义 - 文本表现不佳

在尝试过滤我的聊天机器人提示时,遇到了许多类似的问题。

最奇怪的是,一个安装可以正常运行,而另一个则不行。

以管理员 → 自定义 → 文本中的 chatbot.prompt 的非常简单的过滤器为例:

工作安装:

失败安装:

NoMethodError (undefined method `scan' for {:basic=>"You are a helpful assistant.", :agent=>"You are a helpful assistant. You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with. The current date and time is %{current_date_time}. Only respond to the last question, using the prior information as context, if appropriate."}:Hash)
lib/i18n/i18n_interpolation_keys_finder.rb:6:in `find'
app/controllers/admin/site_texts_controller.rb:183:in `record_for'
app/controllers/admin/site_texts_controller.rb:249:in `block in find_translations'
app/controllers/admin/site_texts_controller.rb:234:in `each'
app/controllers/admin/site_texts_controller.rb:234:in `find_translations'
app/controllers/admin/site_texts_controller.rb:34:in `index'
app/controllers/application_controller.rb:422:in `block in with_resolved_locale'
app/controllers/application_controller.rb:422:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:64:in `call'
lib/middleware/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:389:in `call'
lib/middleware/gtm_script_nonce_injector.rb:10:in `call'
config/initializers/100-quiet_logger.rb:20:in `call'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/enforce_hostname.rb:24:in `call'
lib/middleware/request_tracker.rb:233:in `call'

相关的语言环境文件是:

我是否做了什么不合规的操作?

3 个赞

好的

看起来有问题的方法期望接收文本:

但不知何故收到了一个 Hash:

irb(main):001:0> item = {:basic=>"You are a helpful assistant.", :agent=>"You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}
=>
{:basic=>"You are a helpful assistant.", :agent=>
  "You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}
irb(main):002:0> item
=>
{:basic=>"You are a helpful assistant.",
 :agent=>
  "You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}
irb(main):003:0> item.scan
(irb):3:in `<main>': undefined method `scan' for {:basic=>"You are a helpful assistant.", :agent=>"You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}:Hash (NoMethodError)
        from /home/robert/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from /home/robert/.rbenv/versions/3.2.1/bin/irb:25:in `load'
        from /home/robert/.rbenv/versions/3.2.1/bin/irb:25:in `<main>'
irb(main):004:0

导致它崩溃……我将继续调查

我认为它是从这里调用的:

我可以用以下方法重现问题:

I18nInterpolationKeysFinder.find(I18n.overrides_disabled { I18n.t("chatbot.prompt.system.", locale: :en) })

来自 rails 控制台。

我认为这一行本应阻止该方法接收 Hash,但它不起作用,为什么呢?:

1 个赞

将此移至 Bug,因为我认为此行为不正确且不可靠。

根据代码,如果值为 Hash,则不应将其发送到 record_for 方法(否则将导致异常,因为它期望的是文本),但在某些情况下,它却被发送了。

如果我错了,我很乐意被纠正。

1 个赞

当一个键的翻译被覆盖,而该键以前是可翻译字符串,后来被转换为 Hash 时,就会发生这种情况。这正是 chatbot.prompt.system 所发生的情况。

这似乎是一个回归。我很确定过去这样做是有效的。

我可以用一个像这样的 locale 文件轻松重现它:

en:
  foo: "this is foo"

转到 Customize –> Text 并用不同的值覆盖“foo”。

然后,将 locale 文件更改为如下所示:

en:
  foo:
    basic: "Basic Foo"
    advanced: "Advanced Foo"

再次访问 Customize –> Text 并搜索“foo”。Discourse 将记录以下错误。

NoMethodError (undefined method `scan' for {:basic=>"Basic Foo", :advanced=>"Advanced Foo"}:Hash)
lib/i18n/i18n_interpolation_keys_finder.rb:6:in `find'
app/controllers/admin/site_texts_controller.rb:183:in `record_for'
app/controllers/admin/site_texts_controller.rb:249:in `block in find_translations'
app/controllers/admin/site_texts_controller.rb:234:in `each'
app/controllers/admin/site_texts_controller.rb:234:in `find_translations'
app/controllers/admin/site_texts_controller.rb:34:in `index'

如果不存在相应的英文字符串,Discourse 就不应该加载翻译覆盖。

4 个赞

感谢 @gerhard

这正是我所发生的情况:我在同一个存储桶中添加了另一个最终密钥。

……而且它不影响新安装。

有没有什么 Rails 控制台的变通方法?

1 个赞
TranslationOverride.where(translation_key: "chatbot.prompt.system").delete_all
TranslationOverride.send(:reload_locale!)
5 个赞

太棒了。是的,我正打算尝试一下,但不是在 :laptop: 上。

猜猜我可以在迁移中添加这个?!

2 个赞

我相信这个 bug 也已经和这个一起解决了::tada:

2 个赞