Prerequisites
- Ensure you have a properly set up and functioning Drupal environment.
- Familiarize yourself with Composer as it's needed for managing dependencies in Drupal.
- Create an account with OpenAI to obtain your API key.
Install OpenAI PHP Client
- Navigate to your Drupal project's root directory in your terminal.
- Use Composer to require the OpenAI PHP client.
composer require openai-php/client
Module Creation
- Navigate to the modules/custom directory in your Drupal installation.
- Create a new directory for your custom module, e.g., "openai\_integration".
- Inside this directory, create the necessary files for your module.
Create Module Info File
- Create a file named openai\_integration.info.yml.
- Add the following contents to define your module.
name: 'OpenAI Integration'
type: module
description: 'Integrates OpenAI API with Drupal.'
core_version_requirement: ^8 || ^9
package: Custom
dependencies:
- drupal:system (>=8.8)
Create Module Service
- Create a file named openai\_integration.services.yml.
- Define the service that will be responsible for interacting with OpenAI.
services:
openai_integration.client:
class: OpenAI
arguments: ['@http_client', '%openai.api_key%']
Environment API Key Configuration
- In your settings.php file, add your OpenAI API key.
- Ensure this is done securely and not directly in your module code.
$settings['openai.api_key'] = 'your_openai_api_key_here';
Create a PHP Class for the Integration
- Create a src/ directory within your module's directory if it doesn't exist.
- Inside src/, create a directory named Plugin/OpenAI and add a file named OpenAIClient.php.
- Define the OpenAIClient class to interact with the OpenAI API.
namespace Drupal\openai_integration\Plugin\OpenAI;
use OpenAI;
class OpenAIClient {
protected $client;
public function __construct($http_client, $api_key) {
$this->client = OpenAI::client($api_key);
}
public function generateText($prompt) {
return $this->client->completions->create([
'model' => 'text-davinci-003',
'prompt' => $prompt,
'max_tokens' => 150,
]);
}
}
Utilizing the Service in a Drupal Controller
- Create a Controller to use the OpenAI service and process requests.
- Add routes in a routing file if needed to connect with this Controller.
namespace Drupal\openai_integration\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OpenAIController extends ControllerBase {
protected $openaiClient;
public function __construct($openai_integration_client) {
$this->openaiClient = $openai_integration_client;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('openai_integration.client')
);
}
public function renderGeneratedText() {
$response = $this->openaiClient->generateText('Sample Prompt');
return [
'#type' => 'markup',
'#markup' => $this->t('Generated Text: @text', ['@text' => $response['choices'][0]['text']]),
];
}
}
Clear Cache and Enable the Module
- After making changes to Drupal files, clear the cache to apply the updates.
- Enable your new module either through the Drupal admin interface or Drush.
drush en openai_integration
drush cr
Testing the Integration
- Navigate to the route associated with your Controller to test the OpenAI integration.
- Ensure all data returns as expected and troubleshoot if errors are encountered.
Security Considerations
- Always ensure your API key is stored securely and not hard-coded in source files.
- Review and update permissions to restrict access to the integration features as needed.
This guide provides a detailed process to integrate OpenAI with Drupal, allowing for further customization and expansion based on specific use cases.