felixarntz / ai-services

Makes AI centrally available in WordPress, whether via PHP, REST API, JavaScript, or WP-CLI - for any provider.
https://wordpress.org/plugins/ai-services/
GNU General Public License v2.0
18 stars 1 forks source link

AI Services

Makes AI centrally available in WordPress, whether via PHP, REST API, JavaScript, or WP-CLI - for any provider.

AI Services settings screen showing text input fields for API credentials

Disclaimer: The AI Services plugin is still in its very early stages, with a limited feature set. As long as it is in a 0.x.y version, expect occasional breaking changes. Consider the plugin early access at this point, as there are lots of enhancements to add and polishing to do. A crucial part of that is shaping the APIs to make them easy to use and cover the different generative AI capabilities that the third party services offer in a uniform way. That's why your feedback is much appreciated!

What?

This WordPress plugin introduces central infrastructure which allows other plugins to make use of AI capabilities. It exposes APIs that can be used in various contexts, whether you need to use AI capabilities in server-side or client-side code. Furthermore, the APIs are agnostic of the AI service - whether that's Anthropic, Google, or OpenAI, to only name a few, you can use any of them in the same way. You can also register your own implementation of another service, if it is not supported out of the box.

The plugin does intentionally not come with specific AI driven features built-in, except for a simple WordPress support assistant chatbot that can be disabled if not needed. The purpose of this plugin is to facilitate use of AI by other plugins. As such, it is a perfect use-case for plugin dependencies.

Why?

Examples

// Generate the answer to a prompt in PHP code.
if ( ai_services()->has_available_services() ) {
    $service = ai_services()->get_available_service();
    try {
        $result = $service
      ->get_model(
        array(
          'feature'      => 'my-test-feature',
          'capabilities' => array( 'text_generation' ),
        )
      )
      ->generate_text( 'What can I do with WordPress?' );
    } catch ( Exception $e ) {
        // Handle the exception.
    }
}
// Generate the answer to a prompt in JavaScript code.
const { hasAvailableServices, getAvailableService } = wp.data.select( 'ai-services/ai' );
if ( hasAvailableServices() ) {
    const service = getAvailableService();
    try {
        const result = await service.generateText(
      'What can I do with WordPress?',
      { feature: 'my-test-feature' }
    );
    } catch ( error ) {
        // Handle the error.
    }
}
// Generate the answer to a prompt using WP-CLI.
wp ai-services generate-text 'What can I do with WordPress?' --feature=my-test-feature

You can also use a specific AI service, if you have a preference, for example the google service:

// Generate the answer to a prompt using a specific AI service, in PHP code.
if ( ai_services()->is_service_available( 'google' ) ) {
    $service = ai_services()->get_available_service( 'google' );
    try {
        $result = $service
      ->get_model(
        array(
          'feature'      => 'my-test-feature',
          'capabilities' => array( 'text_generation' ),
        )
      )
      ->generate_text( 'What can I do with WordPress?' );
    } catch ( Exception $e ) {
        // Handle the exception.
    }
}
# Generate the answer to a prompt using a specific AI service, using the REST API via cURL.
curl 'https://example.com/wp-json/ai-services/v1/services/google:generate-text' \
  -H 'Content-Type: application/json' \
  --data-raw '{"content":"What can I do with WordPress?"}'

Installation and usage

You can install the latest built release from the WordPress plugin directory, which in the long term will be the recommended way to use the plugin. Keep in mind that any 0.x.y releases are considered early access and may contain breaking changes.

Alternatively, especially in this early development stage of the plugin, feel free to test the plugin by cloning the GitHub repository. Afterwards, please run the following commands to make sure the dependencies are installed and the plugin build is complete:

git clone https://github.com/felixarntz/ai-services.git wp-content/plugins/ai-services
cd wp-content/plugins/ai-services
composer install
composer prefix-dependencies
npm install
npm run build

If you want to test the plugin in its own built-in development environment, please follow the instructions in the code contributing guidelines.

Once the AI Services plugin is installed and activated, you can configure the plugin with your AI service credentials using the Settings > AI Services screen in the WP Admin menu.

Using the plugin

Once the plugin is active, you will find a new Settings > AI Services submenu in the WordPress administration menu. In there, you can configure your AI service API keys. Once you have configured at least one (valid) API key, you should see a small "Need help?" button in the lower right throughout WP Admin, which exposes the built-in WordPress assistant chatbot. This is the only user-facing feature of the plugin, effectively as a simple proof of concept for the APIs that the plugin infrastructure provides.

Please refer to the documentation for instructions on how you can actually use the AI capabilities of the plugin in your own projects.

License

This plugin is free software, and is released under the terms of the GNU General Public License version 2 or (at your option) any later version. See LICENSE for complete license.