please use drupal AI module for AI providers

Created on 10 July 2024, 4 months ago

Problem/Motivation

we're working at the Drupal ai module
https://www.drupal.org/project/ai
to provide a number of AI services.
Translations can be done with the LLMs.

The ai module provides you LLM;s via code so you as a module developer dont have to worry about api keys aond configuration.
It would be great if you would connect your module to the AI module.

an example implementation:

$prompt_text = "You are helpful translator.
Translate the following from $lang_from to $lang_to
------------
" . $input_text;

    try {
      $type = "chat";
      $ai_config = \Drupal::service('config.factory')->get('ai.settings');
      $default_providers = $ai_config->get('default_providers') ?? [];
      $service = \Drupal::service('ai.provider');
      $provider = $service->createInstance($default_providers[$type]['provider_id']);
      $messages = new ChatInput([
        new chatMessage('system', 'You are helpful translator. '),
        new chatMessage('user', $prompt_text),
      ]);
      /** @var /Drupal\ai\OperationType\Chat\ChatOutput $message */
      $message = $provider->chat($messages, $default_providers[$type]['model_id'])->getNormalized();
      // Calling ai
    } catch (GuzzleException $exception) {
      // Error handling for the API call.
      return $exception->getMessage();
    }
    $cleaned = trim($message->getText(), ' ');
    return trim($cleaned, '"');

Which you can find here: https://git.drupalcode.org/project/ai/-/merge_requests/10/diffs

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

1.0

Component

Code

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

Production build 0.71.5 2024