-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-ai-sdk-demo.php
More file actions
69 lines (62 loc) · 2.04 KB
/
Copy pathphp-ai-sdk-demo.php
File metadata and controls
69 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Plugin Name: PHP AI SDK Demo
* Description: A demo plugin to showcase the integration of the PHP AI SDK.
* Version: 1.0.0
* Author: Jonathan Bossenger
* Plugin URI: https://github.com/jonathanbossenger/php-ai-sdk-demo
*
* @package php-ai-sdk-demo
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Check if WordPress version is greater than or equal to 7.0, and if so, skip the autoloader.
if ( ! isset( $wp_version ) || version_compare( $wp_version, '7.0-alpha', '<' ) ) {
// Include the Composer autoloader.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
} else {
wp_die( 'Please run "composer install" in the plugin directory to install the required dependencies.' );
}
}
/**
* Generate text using the PHP AI SDK with the Anthropic provider.
*
* @param string $prompt The prompt to generate text from.
*
* @return string
*/
function php_ai_sdk_demo_generate_text( $prompt ) {
/**
* Set custom request options for the AI Client. This is optional, but demonstrates how to customize the request.
*/
$options = new \WordPress\AiClient\Providers\Http\DTO\RequestOptions();
$options->setTimeout( 60.0 );
$options->setConnectTimeout( 10.0 );
if ( function_exists( 'wp_ai_client_prompt' ) ) { // WordPress 7.0 helper function for AI Client.
$result = wp_ai_client_prompt( $prompt )
->using_request_options( $options )
->generate_text();
} else {
$result = \WordPress\AiClient\AiClient::prompt( $prompt )
->usingRequestOptions( $options )
->generateText();
}
return $result;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command(
'php-ai-sdk-demo-text',
function ( $args ) {
if ( empty( $args[0] ) ) {
WP_CLI::error( 'Please provide a prompt.' );
}
WP_CLI::line( php_ai_sdk_demo_generate_text( 'Write a short poem about WordPress plugins.' ) );
},
array(
'shortdesc' => 'Generate text using the PHP AI SDK with the Anthropic provider. Usage: wp php-ai-sdk-demo-text "Your prompt here"',
)
);
}