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
Comments
Comment #2
wouters_f commentedin this sample you see that you dont have to wrry about api calls or api keys.
The ai module arranges that for you, it just provides you withAI's you can use.
It would be great if your module uses this system too.
Comment #3
jhuhta commentedThe suggestion sounds very reasonable. By the time of writing this module, the need was to make it as simple as possible and match the requirements from the client. Writing the LLM client appeared to be so simple that adding a dependency to the AI module ecosystem felt a bit overkill, but I'm ready to reconsider that. A working MR would make the change easier. An upgrade path would also help.
But now this current implementation provides also a possibility to include service provider specific processing, which I'm not sure would be possible.
Comment #6
jhuhta commentedCrediting @wouters_f for the idea and support to make this happen.
It also required writing a new operation type for the AI module and a provider module for DeepL.
Fixed in 2.0.x branch.