ai translate (ai_translate) submodule

Created on 26 June 2024, 7 months ago
Updated 12 August 2024, 5 months ago

Problem/Motivation

I think AI powered translations could be a core (for editors) feature.
So it makes sense to have this as a submodule I think (and I can port this from the https://www.drupal.org/project/chatgpt_plugin → module)

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

1.0

Component

Miscellaneous

Created by

🇧🇪Belgium wouters_f Leuven

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @wouters_f
  • 🇱🇹Lithuania mindaugasd

    Related issue where this was discussed in the past: ✨ Add support for language translation, and non-English languages Active

  • 🇧🇪Belgium wouters_f Leuven

    So it works now.

    This is the translation screen if translations is enabled for the Content type.

    This is how it looks after the Translation.

    Remarks

    tested with

    • Openai: Works
    • Mistral: Does not work
  • 🇧🇪Belgium wouters_f Leuven

    @Marcus: I'm not sure why the Mistral integration throws this error

    PHP message: Uncaught PHP Exception TypeError: "array_map(): Argument #2 ($array) must be of type array, null given" at /var/www/html/vendor/openai-php/client/src/Responses/Chat/CreateResponse.php line 49
    
  • Status changed to Needs review 7 months ago
  • 🇧🇪Belgium wouters_f Leuven
  • 🇧🇪Belgium wouters_f Leuven
  • 🇧🇪Belgium wouters_f Leuven
  • 🇧🇪Belgium wouters_f Leuven

    The Mistral API errors were not caught by the underlying library.
    It did not have to do with the translations.

  • 🇬🇧United Kingdom yautja_cetanu

    Very cool! I wonder if it should be under an examples or just a submodule

  • 🇧🇬Bulgaria valthebald Sofia

    I would vote for submodule (and agree with @wouters_f that this should be the core feature for editors)

  • 🇧🇪Belgium wouters_f Leuven

    I created it as a submodule.
    In my opinion this is a very valid separate module :D.
    I organised my code also like this.

  • 🇧🇬Bulgaria valthebald Sofia

    Hey @wouters_f,
    First of, thanks for the submodule, that would help a lot of people!
    Some notes/questions though:

    1. Passing both lang_code and lang_name in ai_translate.translate_content route seems redundant. Can be only lang_code?
    2. Instead of lang_name, I would pass entity_type parameter, allowing to translate not only nodes, but other content entities as well. Logic can be similar to core's Drupal\field_ui\Routing\RouteSubscriber::alterRoutes(), that basically attaches to most content entity types.
    3. Another parameter that I would add is source_lang. For longer texts this may be less relevant, since it's easier to guess from the context, but for shorter ones would be less easier to prompt "as a helpful translator, translate from ... to ..."
    4. Permission "access chatgpt translation" should have more generic name, i.e. "access ai translation" or (following core pattern) "create ai content translations". There are some other mentions of chatgpt in the code, but given AiTranslateController uses default provider, I assume the code is provider-agnostic

    What do you think?
    If you agree with any point, I'd be glad to help (then I'll need permissions on the issue branch)

  • 🇩🇪Germany marcus_johansson

    To add to @valthebald great comments - if we add source language we could also add a generic operation type for translation. Since deepl, Grammarly and some other services offers none-LLM, but AI powered translations these would be very simple to add support for these services for any module to use.

    This makes this module more flexible and the AI module more comprehensive.

    So you would do:

    $input = new \Drupal\ai\OperationType\Translate\TranslateInput('How do you do?', 'de', 'en');
    $translation = \Drupal::service('ai.provider')->createInstance('deepl')->translate($input)->getNormalized();
    // $translation would be a string "Wie geht es dir?".
    

    This would take 1 hour to add and maybe 1-2 hours to add those two plugins.

    Another comment on this in general is that we should perhaps notice which LLM's actually are ok with translating and to which languages. If you do gemma-2b for instance it can't really translate almost anything, while OpenAI is generally even better than Deepl at certrain languages.

  • 🇬🇧United Kingdom yautja_cetanu

    I wonder if it's worth putting in a setting for "Context" for specific languages so that the pre-prompt can be edited at all for a specific language. So when you are translating from American English to Swedish, the site owner can choose to make it translate the tone or make it a word for word translation?

    This could be optional settings to make it a little more useful?

    I would eventually like any modules like this in core to work with the future evaluations module.

  • Status changed to Needs work 7 months ago
  • 🇧🇬Bulgaria valthebald Sofia

    Moving to Needs work because of comments

  • 🇱🇹Lithuania mindaugasd
  • 🇧🇪Belgium wouters_f Leuven

    I agree those would be nice things.
    I just wanted to provide a lift and shift from the chatgpt_plugin to this module to provide them an easy switch with similar behavior.
    Personally I think supporting paragraphs ( https://www.drupal.org/project/chatgpt_plugin/issues/3456725 ✨ Add support for paragraphs in translations Needs work ) which is a feature they request is more important for the adoption (and even adding layout builder to that too).

    From what I see the 141 sites that use chatgpt_plugin have no issues with the detection of language, there's no bugs regarding this (yet?).
    I would log these things as separate improvements to the translation module which we could work on later (if found of importance).
    Also I personally don't know (yet) how to add these plugins (coding is rusty sorry).

    So I think if we lift and shift the as is, we're already doing a great job and we could pull in the 141 sites using chatgpt_plugin.
    Anything extra is nice, but in my opinion extra.

  • 🇧🇪Belgium wouters_f Leuven
  • Status changed to Needs review 7 months ago
  • 🇧🇬Bulgaria valthebald Sofia

    As I said, I'd be glad to help when it comes to the coding part :)
    Suggested changes are not that big to postpone them

  • Status changed to Needs work 7 months ago
  • 🇧🇬Bulgaria valthebald Sofia
  • 🇧🇬Bulgaria valthebald Sofia

    I will submit some changes later tonight

  • Pipeline finished with Success
    7 months ago
    Total: 142s
    #221158
  • Pipeline finished with Success
    7 months ago
    Total: 164s
    #221262
  • Pipeline finished with Success
    7 months ago
    #221269
  • Status changed to Needs review 7 months ago
  • 🇧🇬Bulgaria valthebald Sofia

    I've submitted my suggestions from #12:

    • Removed node as hard-coded entity type, expecting ContentEntityInterface now. AiTranslateRouteSubscriber::alterRoutes() was already adding "Translate with AI" links to all supported entity types.
    • Changed signature of AiTranslateController::translate() to AiTranslateController::translate($entity_type, $entity_id, $lang_from, $lang_to)
    • Changed permission name to "create ai content translation" and added module's .permissions.yml
    • Enabled translation from original to default language (entity is not necessarily created in site default language, but it would be good to have an option to translate from original language to default one)

    Performed several tests locally, module worked for me for both nodes and taxonomy terms

    Things that are not working yet:

    • The only supported base field is label
    • All fields are treated as text_format (value + format), including plain text

    Both issues do not look to me as deal breakers to include the module to HEAD, but up to you to decide.

  • Pipeline finished with Success
    7 months ago
    Total: 163s
    #221468
  • Pipeline finished with Success
    7 months ago
    Total: 147s
    #221480
  • Pipeline finished with Success
    7 months ago
    #221485
  • Pipeline finished with Success
    7 months ago
    Total: 169s
    #221494
  • Pipeline finished with Success
    7 months ago
    Total: 139s
    #222835
  • Pipeline finished with Success
    7 months ago
    Total: 172s
    #222875
  • Pipeline finished with Success
    7 months ago
    Total: 245s
    #222896
  • Status changed to RTBC 7 months ago
  • 🇩🇪Germany marcus_johansson

    Thank @valthebald and wouters_f - the code in the module looks great now and I have tested it on combinations of Swedish, English, German and Japanese and with OpenAI GPT-4o it works great.

    I would say we merge it with dev, unless someone is opposed to it being part of AI and should be a contrib module?

  • 🇧🇪Belgium wouters_f Leuven

    Lets do it!

  • Pipeline finished with Success
    6 months ago
    Total: 143s
    #223361
  • Status changed to Needs work 6 months ago
  • 🇧🇬Bulgaria valthebald Sofia
  • Status changed to RTBC 6 months ago
  • 🇧🇪Belgium wouters_f Leuven

    ok so I added the requirements hook and now propose to close/merge the ticket now.

  • Pipeline finished with Canceled
    6 months ago
    Total: 131s
    #225278
  • Pipeline finished with Failed
    6 months ago
    #225279
  • Status changed to Needs work 6 months ago
  • 🇩🇪Germany marcus_johansson

    I posted two minor change requests, after that hopefully all is done.

  • 🇧🇪Belgium wouters_f Leuven
  • Pipeline finished with Canceled
    6 months ago
    Total: 130s
    #225630
  • Pipeline finished with Failed
    6 months ago
    Total: 186s
    #225632
  • Status changed to Needs review 6 months ago
  • Pipeline finished with Failed
    6 months ago
    Total: 178s
    #225655
  • Pipeline finished with Failed
    6 months ago
    Total: 184s
    #225660
  • Pipeline finished with Failed
    6 months ago
    Total: 192s
    #225694
  • 🇧🇬Bulgaria valthebald Sofia
  • Pipeline finished with Failed
    6 months ago
    Total: 212s
    #226300
  • Status changed to RTBC 6 months ago
  • 🇩🇪Germany marcus_johansson

    Tested and rewritten certain parts. Unless someone opposes it, it will be merged tonight CET.

  • Status changed to Fixed 6 months ago
  • 🇩🇪Germany marcus_johansson

    Merged into 1.0.x

  • 🇮🇳India anup.sinha Bengaluru

    Hi @wouters_f, thanks for porting the translation component from my Drupal OpenAI plugin - https://www.drupal.org/project/chatgpt_plugin → . Let us all work on the patch to provide support for Paragraphs module.

  • 🇳🇱Netherlands jurriaanroelofs

    Have you considered integrating with the TMGMT framework? It's a very powerful suite for both human and AI translation and it is very extensible, too.

  • 🇧🇪Belgium wouters_f Leuven

    Hi JuriaanRoelofs,

    This module is a simple translation integration. The goal was here to be as independant as possible as many multilingual sites are not using tmgmt yet (in our company).

    But I fully agree there should be a version of https://www.drupal.org/project/tmgmt_openai → that does this.
    I think it would make perfect sense for them to also use the AI module under the hood.
    It could even be a separate ai_tmgmt module where this is abstracted away from openai.

  • 🇳🇱Netherlands jurriaanroelofs

    Got it, yeah I agree on all of that too :) We all should be making modules that abstract away from any single LLM service provider.

  • 🇧🇬Bulgaria valthebald Sofia

    @anup.sinha that was the first thing on my mind after seeing initial patch :)
    I believe we should have a separate issue for that

  • 🇧🇬Bulgaria valthebald Sofia

    ...and it exists, actually! ✨ (ai_translate) One click translations for Paragraphs Needs review

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024