Support translating through chat AI.

Created on 5 February 2025, 2 months ago

I wanted to translate texts without the need for a specific translation provider like Deepl.
The AI module supports this use case through a "chat AI" + a template text that instructs to translate.
The AI ckeditor submodule implements this and it works fine for us in ckeditor text fields.
Is there a specific reason that this module does not support this use case?
Would it be possible to implement?

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇧🇪Belgium weseze

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

Merge Requests

Comments & Activities

  • Issue created by @weseze
  • 🇫🇮Finland jhuhta

    How about this one? Support chat operation type AI providers Active

    Just install the latest dev version. I can make a new release if someone could tell that it seems to work. (It works on our project pretty well though.)

  • 🇧🇪Belgium weseze

    Tried with a dev checkout from #3499977 but didn't work straight away.
    First It wasn't obvious that I needed to enable on a per-field bases in the "Manage form display". In retrospect this does seem kind of logic :)
    I also had to change the below code in (\Drupal\ai_translate_textfield\AiTranslateTexfieldCallbacks)

      private static function getProvider() {
        /** @var \Drupal\ai\AiProviderPluginManager $aiProviderManager */
        $aiProviderManager = \Drupal::service('ai.provider');
        $config = \Drupal::config('ai_translate_textfield.settings');
        $provider_id = $config->get('provider');
        $model_id = $config->get('provider') ?? '';
        if (!empty($provider_id)) {
          $provider = $aiProviderManager->loadProviderFromSimpleOption($provider_id . '__' . $model_id);
        }
        else {
          // Get the default provider.
          $default_provider = $aiProviderManager->getDefaultProviderForOperationType('translate_text');
          if (empty($default_provider['provider_id'])) {
            return NULL;
          }
          $provider = $aiProviderManager->createInstance($default_provider['provider_id']);
        }
        return $provider;
      }
    

    To:

      private static function getProvider() {
        /** @var \Drupal\ai\AiProviderPluginManager $aiProviderManager */
        $aiProviderManager = \Drupal::service('ai.provider');
        $config = \Drupal::config('ai_translate_textfield.settings');
        $provider_id = $config->get('provider');
        $model_id = $config->get('model') ?? '';
        if (!empty($provider_id)) {
          $provider = $aiProviderManager->loadProviderFromSimpleOption($provider_id . '__' . $model_id);
        }
        if (empty($provider)) {
          // Get the default provider.
          $default_provider = $aiProviderManager->getDefaultProviderForOperationType('translate_text');
          if (empty($default_provider['provider_id'])) {
            return NULL;
          }
          $provider = $aiProviderManager->createInstance($default_provider['provider_id']);
        }
        return $provider;
      }
    

    It is no fetching the correct ai provider at all for me... I went to all the configuration and it all seems to be OK. Also translation is working fine in other places.
    Obviously, something is still going wrong in trying to fetch the configured provider, but loading the default one in case of issues at least makes it work.

  • 🇫🇮Finland jhuhta

    Thanks for the feedback! Would you be so kind and provide the fix as a MR? I don't have much time to work for this right now, so that would make it easier for me and faster for you to get a working solution (+ credit).

    Just wondering if the code should try the default chat provider too, if none is configured.

    The instructions for configuring the module are stated on the module front page.

  • First commit to issue fork.
  • 🇧🇪Belgium indymeermans

    indymeermans changed the visibility of the branch 3504447-use-the-default to hidden.

Production build 0.71.5 2024