AI Services

AI Services

توسط

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 an AI Playground screen to explore AI capabilities as well as a settings screen to configure AI service credentials. 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.

Here’s a (non-comprehensive) feature list:

  • Abstraction layer and APIs to communicate with any AI service in a uniform way
    • APIs are available in both PHP and in JavaScript, as well as via the WordPress REST API and WP-CLI commands
    • Currently supports the following AI capabilities (with more on the way!):
      • text generation (including text streaming for more immediate feedback to users)
      • text chats with history
      • multimodal input
      • function calling
      • image generation
  • AI Playground administration screen (in the Tools menu) allows exploring the different AI capabilities
    • Explore all AI capabilities supported by the plugin via user interface
    • Select which AI service and model to use and set a few advanced configuration parameters
    • Define your own function declarations used for AI function calling
    • Generate images and save them to the WordPress media library
    • Exchange the AI service or model on the fly to continue a chat started with one model with another one
  • AI Services settings screen to configure services with API credentials
  • Built-in AI service implementations
  • Additional AI service integrations can be registered and will then be available in the same way as built-in ones

Disclaimer: The AI Services plugin is still in its early stages, with a limited feature set and more being added. A crucial part of refining the plugin is shaping the APIs to make them easy to use and cover the different generative AI capabilities that the AI services offer in a uniform way. That’s why your feedback is much appreciated!

Why?

  • A centralized AI infrastructure facilitates user choice. Users may prefer certain AI services over other ones, and for many common tasks, either of the popular AI services is suitable. Having a common API regardless of the AI service allows leaving the choice to the user, rather than the plugin author.
  • Since the centralized AI infrastructure comes with a common API that works the same for every AI service, it means plugin developers don’t have to spend as much time familiarizing themselves with different services, at least when it comes to simple tasks. For tasks where certain services may have advantages over others, there is still flexibility to focus on a specific AI service.
  • It also means no more reinventing the wheel: Since most AI services do not provide PHP SDKs for their APIs, many times this means WordPress plugins that want to leverage AI have to implement their own layer around the service’s API. Not only is that time consuming, it also distracts from working on the actual (AI driven) features that the plugin should offer to its users. In fact this directly facilitates the user choice aspect mentioned, as having APIs for various AI services already provided means you can simply make those available to your plugin users.
  • Having central AI infrastructure available unlocks AI capabilities for smaller plugins or features: It may not be worth the investment to implement a whole AI API layer for a simple AI driven feature, but when you already have it available, it can lead to more plugins (and thus more users) benefitting from AI capabilities.
  • Last but not least, a central AI infrastructure means users will only have to configure the AI API once, e.g. paste their API keys only in a single WordPress administration screen. Without central AI infrastructure, every plugin has to provide its own UI for pasting API keys, making the process more tedious for site owners the more AI capabilities their site uses.

Integration with third party services

While the plugin APIs allow registering custom AI services, the plugin comes with a few popular AI services built-in. These AI services rely on the respective third party API. Their use is optional and it is up to you to choose which third party service you would like to use or whether you would like to use multiple.

The use of the third party AI services is subject to the respective terms of service. The following third party services are supported out of the box:

Code examples for using the API

Generate the answer to a prompt in PHP code:

use Felix_ArntzAI_ServicesServicesAPIEnumsAI_Capability;
use Felix_ArntzAI_ServicesServicesAPIHelpers;

if ( ai_services()->has_available_services() ) {
    $service = ai_services()->get_available_service();
    try {
        $candidates = $service
            ->get_model(
                array(
                    'feature'      => 'my-test-feature',
                    'capabilities' => array( AI_Capability::TEXT_GENERATION ),
                )
            )
            ->generate_text( 'What can I do with WordPress?' );

        $text = Helpers::get_text_from_contents(
            Helpers::get_candidate_contents( $candidates )
        );

        echo $text;
    } catch ( Exception $e ) {
        // Handle the exception.
    }
}

Generate the answer to a prompt in JavaScript code:

const helpers = aiServices.ai.helpers;
const { hasAvailableServices, getAvailableService } = wp.data.select( 'ai-services/ai' );
if ( hasAvailableServices() ) {
    const service = getAvailableService();
    try {
        const candidates = await service.generateText(
            'What can I do with WordPress?',
            { feature: 'my-test-feature' }
        );

        const text = helpers.getTextFromContents(
                helpers.getCandidateContents( candidates )
            );

        console.log( text );
    } 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:

use Felix_ArntzAI_ServicesServicesAPIEnumsAI_Capability;
use Felix_ArntzAI_ServicesServicesAPIHelpers;

if ( ai_services()->is_service_available( 'google' ) ) {
    $service = ai_services()->get_available_service( 'google' );
    try {
        $candidates = $service
            ->get_model(
                array(
                    'feature'      => 'my-test-feature',
                    'capabilities' => array( AI_Capability::TEXT_GENERATION ),
                )
            )
            ->generate_text( 'What can I do with WordPress?' );

        $text = Helpers::get_text_from_contents(
            Helpers::get_candidate_contents( $candidates )
        );

        echo $text;
    } catch ( Exception $e ) {
        // Handle the exception.
    }
}

Refer to the plugin documentation for granular examples including explainers.

For complete examples such as entire plugins built on top of the AI Services infrastructure, please see the examples directory on GitHub.

Installation from within WordPress

  1. Visit Plugins > Add New.
  2. Search for AI Services.
  3. Install and activate the AI Services plugin.

Manual installation

  1. Upload the entire ai-services folder to the /wp-content/plugins/ directory.
  2. Visit Plugins.
  3. Activate the AI Services plugin.

Usage

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. After that, you can use the Tools > AI Playground screen to explore the available AI capabilities of the different connected services.

If you have enabled the WordPress assistant chatbot via filter, you should see a small “Need help?” button in the lower right throughout WP Admin after you have configured at least one (valid) API key.

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

سوالات متداول

How can I customize AI Services model parameters?

You can use the ai_services_model_params filter in PHP to customize the model parameters before they are used to retrieve a given AI service model.

This filter is run consistently in any context, regardless of whether the AI model is used via PHP, JavaScript, or WP-CLI.

This can be helpful, for example, if you need to inject custom model configuration parameters or a custom system instruction for a specific feature in a way that it happens dynamically on the server.

Here is an example code snippet which injects a custom system instruction whenever the feature my-movie-expert is used with any google model:

add_filter(
    'ai_services_model_params',
    function ( $params, $service ) {
        if ( 'my-movie-expert' === $params['feature'] && 'google' === $service ) {
            $params['systemInstruction']  = 'You are a movie expert. You can answer questions about movies, actors, directors, and movie references.';
            $params['systemInstruction'] .= ' If the user asks you about anything unrelated to movies, you should politely deny the request.';
            $params['systemInstruction'] .= ' You may use famous movie quotes in your responses to make the conversation more engaging.';
        }
        return $params;
    },
    10,
    2
);

Note that this filter does not allow you to change the feature parameter, as that needs to be controlled by the caller.

How can I enable the WordPress Assistant chatbot feature?

There is a simple WordPress Assistant chatbot available as an experimental feature of the plugin, effectively acting as a proof of concept. Since the plugin is purely an infrastructure plugin that other plugins can use to access AI capabilities in WordPress, that chatbot feature is disabled by default.

If you want to test or use the chatbot, you can easily enable it via filter:

add_filter( 'ai_services_chatbot_enabled', '__return_true' );

How can I tweak the WP-CLI commands’ behavior?

The wp ai-services generate-text command streams text responses by default. This can help provide more immediate feedback to the user, since chunks with partial response candidates will be available iteratively while the model still processes the remainder of the response.

An exception where it does not stream the response, but returns it all at once is if any function declarations are present.

If you prefer to show the complete text response in one go instead, you can disable streaming in WP-CLI by using the ai_services_wp_cli_use_streaming filter.

add_filter( 'ai_services_wp_cli_use_streaming', '__return_false' );

How can I programmatically provide service API keys?

If you prefer to not expose the sensitive controls over the AI service API keys to the site’s end users, you can programmatically specify the keys by filtering the relevant service’s option value.

For example, to enforce an API key to use for the Google AI service, you could use a code snippet like the following:

add_filter(
    'pre_option_ais_google_api_key',
    function () {
        return 'my-google-api-key';
    }
);

The same approach works for any other services too. Simply use the correct service slug, e.g. openai for the OpenAI integration and anthropic for the Anthropic integration.

Which user capabilities are available and how can I customize them?

Please see the documentation article on customizing the available plugin capabilities.

Should this be in WordPress Core?

Most certainly at some point, in some form. But maybe not yet. While generative AI has been around for a few years, in the grand scheme of things we are still only scratching the surface of what’s possible. But most importantly, the lack of standardization makes it difficult to consider built-in AI support in WordPress Core.

WordPress Core rarely adds support for features that rely on third party services. An exception is oEmbed support for many popular services, however via the common oEmbed endpoint that each service implements there is a standard way to have it work correctly without having to individually maintain each integration. Doing so would be a maintenance burden and it would make it almost impossible to stay on top of everything: Imagine one of the services makes a change – not only would this require to manually update the WordPress Core integration, but it would also require to quickly ship a new release ASAP because otherwise the WordPress sites using the service would break. Unfortunately, there is no such standard for how generative AI APIs provided by third party services should work. In other words, if you implement support for a generative AI API in your plugin, that implementation is subject to the same concern, and it applies to the AI Services plugin too. However, by centralizing the implementation in one plugin, the problem surface is greatly reduced. And differently from WordPress Core, it’s more straightforward and more reasonable to ship a quick hotfix for this plugin.

The other reason that integrating generative AI in WordPress Core would be difficult is because (almost all) the services that make those APIs available require paid subscriptions. This is not well aligned with WordPress’s FOSS philosophy. A promising development that may change that situation is the increasing availability of on-device models including browser built-in AI models, such as Chrome built-in AI (which is also supported by the AI Services plugin).

Until the time is right for built-in AI capabilities in WordPress Core, you can use a plugin like this one. While it is for now not a WordPress Core feature plugin, it is built to potentially become one and could be considered a canonical AI plugin for WordPress:

  • It is free, and always will be.
  • It follows the WordPress Core philosophies.
  • It uses WordPress UI components as much as possible.
  • It is neutral and does not favor one AI service over another.
Where should I submit my support request?

For regular support requests, please use the wordpress.org support forums. If you have a technical issue with the plugin where you already have more insight on how to fix it, you can also open an issue on GitHub instead.

How can I contribute to the plugin?

If you have ideas to improve the plugin or to solve a bug, feel free to raise an issue or submit a pull request in the GitHub repository for the plugin. Please stick to the contributing guidelines.

You can also contribute to the plugin by translating it. Simply visit translate.wordpress.org to get started.

×
نظری برای این آیتم موجود نیست.
0 0 رای ها
امتیازدهی
اشتراک در
اطلاع از
0 نظرات
قدیمی‌ترین
تازه‌ترین بیشترین رأی
نسخه حجم فایل SHA256 تغییرات دانلود
0.7.4 893 کیلوبایت -
دانلود
×
★★★★★
★★★★★
5.0 /5 (2 نظر)

قیمت:

رایگان

نگارش

0.7.4

آخرین انتشار

17 اردیبهشت 1405

آخرین بروزرسانی

3 ماه پیش

نصب های فعال

90+

نگارش وردپرس

وردپرس 6.0+

تست شده از نسخه

وردپرس 6.9.4

نگارش PHP

PHP 7.2+

نسخه ها

1 نسخه