Drupal is a registered trademark of Dries Buytaert

openai_client

24 sites Security covered
View on drupal.org

NOW COMPATIBLE WITH IMAGES IN CHAT !!

OpenAI Client is a module for Drupal that provides integration with the OpenAI API, allowing for the use of natural language processing and artificial intelligence in your Drupal site. This module allows users to configure their OpenAI API token and view available models.

This module add the AI conversation content type to chat with the default model.

Disclaimer

This module has been created by Pedro Pelaez to connect with ChatGPT, a product of OpenAI. We have no affiliation with OpenAI and does not claim any ownership or responsibility for the ChatGPT product. We respect OpenAI's intellectual property rights and are using ChatGPT solely for the purpose of providing a useful feature to our users. Any issues or concerns related to ChatGPT should be directed to (OpenAI's support team).

Installation

To install this module, use composer:

composer require drupal/openai_client

Configuration

Go to the "OpenAI Client" configuration page (/admin/config/system/openai-client) to set your OpenAI API token. After setting the API token, available models will be displayed on the configuration page. Then, configure the default model to use in chat.

Usage

This module integrates with the OpenAI API,
adding the AI conversation content type to chat with the default model
and an example of an image creator form, in /openai-client/create-image, to try it you need openai_client create image form permission.

You can send images in the chat conversation, if the model used is compatible.
To send images you must convert it to base 64, with this steps:

  • Copy Your Image to Clipboard: You must first have the image you wish to send copied to your clipboard. This can usually be done by right-clicking the image and selecting a copy option or using keyboard shortcuts (e.g., Ctrl + C on Windows, Cmd + C on a Mac).
  • Convert to Base64: Since the form requires the image in Base64 format, you would use the provided link Clipboard to base64 online to paste your image from the clipboard and convert it into a Base64 string. The website should have a button or feature to perform the conversion.
  • Paste Base64 String: After the image has been converted to Base64, you should be able to copy the resulting text string. Come back to your Drupal form, and in the 'Image' field (which has a placeholder text 'Image in Base64 format to send to the IA.'), paste this string.

To use the features of the OpenAI API in your site, you will need to create custom functionality or integrate with other modules.

The module public the openai_client.wrapper service with the next interface:

  /**
   * Get a new client with token set.
   *
   * @param string $token Optional API token, by default use the module set token.
   *
   * @return \OpenAI\Client
   */
  public function getClient(string $token = ''): Client

  /**
   * Get available AI models.
   *
   * @return array
   */
  public function getModels(): array;

  /**
   * Has module API token set?
   *
   * @return bool
   */
  public function hasToken():bool;

  /**
   * Do a direct query to default chat model and return the response text.
   *
   * @param string $prompt
   *
   * @return string
   */
  public function fastChatQuery(string $prompt): string;

  /**
   * Get default module chat model.
   *
   * @return array|mixed|null
   */
  public function getDefaultModel();

  /**
   * Generate a new image from prompt.
   *
   * @param string $prompt
   *   Text prompt.
   * @param int $amount
   *   Images size, the service class define constants to it. Smaller sizes are faster to generate.
   * @param string $size
   *   You can request 1-10 images at a time.
   *
   * @return array|\array{created: int, data: array}
   */
  public function imageCreate(string $prompt, int $amount = 1, string $size = self::IMAGE_SIZE_256);

  /**
   * Get valid image sizes to create.
   *
   * @return string[]
   */
  public static function getImageSizes();

Requirements

  • Drupal 10+
  • An OpenAI API token

Credits

  • This module was created by Pedro Pelaez.
  • This README.md file was generated by ChatGPT, of OpenAI, and revised manually.
  • This module use the library (thephpleague/commonmark)
  • This module use the library (openai-php/client)

Maintainers

Activity

Total releases
5
First release
Feb 2025
Latest release
9 months ago
Release cadence
24 days
Stability
80% stable

Release Timeline

Releases

Version Type Release date
1.2.2 Stable May 21, 2025
1.2.x-dev Dev May 21, 2025
1.2.0 Stable May 20, 2025
1.1.5 Stable Mar 19, 2025
1.1.4 Stable Feb 14, 2025