-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathContentGeneration.php
More file actions
338 lines (295 loc) · 10.3 KB
/
ContentGeneration.php
File metadata and controls
338 lines (295 loc) · 10.3 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace Classifai\Features;
use Classifai\Providers\Azure\OpenAI;
use Classifai\Providers\GoogleAI\GeminiAPI;
use Classifai\Providers\OpenAI\ChatGPT;
use Classifai\Providers\Browser\ChromeAI;
use Classifai\Providers\XAI\Grok;
use Classifai\Providers\Localhost\Ollama;
use Classifai\Services\LanguageProcessing;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
use function Classifai\sanitize_prompts;
use function Classifai\get_asset_info;
/**
* Class ContentGeneration
*/
class ContentGeneration extends Feature {
/**
* ID of the feature.
*
* @var string
*/
const ID = 'feature_content_generation';
/**
* Instruction file name for the feature.
*
* @var string
*/
public $instruction_file = '04.content-generation.md';
/**
* Prompt for creating content.
*
* @var string
*/
public $prompt = 'Act as an experienced SEO copywriter tasked with writing an article based off of a given summary and an optionally provided title. Your goal is to craft a compelling, informative piece that adheres to SEO best practices, is well-researched, engaging to the target audience, and structured in a way that enhances readability. Incorporate relevant keywords naturally throughout the text, without compromising the flow or quality of the content. Ensure that the article provides value to the reader. Only return the contents of the article, not the title or other commentary.';
// phpcs:disable Squiz.PHP.Heredoc.NotAllowed
/**
* The format of how we'd like content to be returned.
*
* @var string
*/
public $return_format = <<<EOD
The content returned should be valid WordPress block markup as described below, using elements like paragraphs and headings where appropriate. Be selective on the elements you use, defaulting to paragraphs. Please check the content before returning to ensure each element has proper opening and closing block markup and HTML tags and any required block attributes. Ensure elements don't nest inside each other, i.e. don't put a paragraph inside another paragraph or a list within a paragraph. Don't start the content with a heading, start with a paragraph.
Markup available to use; don't use any other blocks, even if requested:
<!-- wp:paragraph -->
<p>CONTENT</p>
<!-- /wp:paragraph -->
<!-- wp:heading -->
<h2 class="wp-block-heading">CONTENT</h2>
<!-- /wp:heading -->
<!-- wp:table -->
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>CONTENT</td></tr><tr><td>CONTENT</td></tr></tbody></table></figure>
<!-- /wp:table -->
<!-- wp:quote -->
<blockquote class="wp-block-quote">
<p>CONTENT</p>
</blockquote>
<!-- /wp:quote -->
<!-- wp:pullquote -->
<figure class="wp-block-pullquote"><blockquote><p>QUOTE</p><cite>AUTHOR</cite></blockquote></figure>
<!-- /wp:pullquote -->
<!-- wp:list -->
<ul class="wp-block-list">
<li>CONTENT</li>
</ul>
<!-- /wp:list -->
<!-- wp:list {"ordered":true} -->
<ol class="wp-block-list">
<li>CONTENT</li>
</ol>
<!-- /wp:list -->
EOD;
// phpcs:enable Squiz.PHP.Heredoc.NotAllowed
/**
* Constructor.
*/
public function __construct() {
$this->label = __( 'Content Generation', 'classifai' );
// Contains all providers that are registered to the service.
$this->provider_instances = $this->get_provider_instances( LanguageProcessing::get_service_providers() );
// Contains just the providers this feature supports.
$this->supported_providers = [
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
OpenAI::ID => __( 'Azure OpenAI', 'classifai' ),
Ollama::ID => __( 'Ollama', 'classifai' ),
];
// Get readme content.
$readme_content = $this->get_instruction_content();
// Contains supported providers data.
$this->supported_providers_data = [
'instruction' => [
ChatGPT::ID => preg_match( '/## Set Up via OpenAI ChatGPT(.*?)(?:\n## |\z)/s', $readme_content, $matches ) ? $matches[1] : '',
OpenAI::ID => preg_match( '/## Set Up via Azure OpenAI(.*?)(?:\n## |\z)/s', $readme_content, $matches ) ? $matches[1] : '',
Ollama::ID => $this->get_locally_hosted_llm_instruction(),
],
];
}
/**
* Set up necessary hooks.
*
* We utilize this so we can register the REST route.
*/
public function setup() {
parent::setup();
add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
}
/**
* Set up necessary hooks.
*/
public function feature_setup() {
add_action( 'enqueue_block_assets', [ $this, 'enqueue_editor_assets' ] );
}
/**
* Register any needed endpoints.
*/
public function register_endpoints() {
register_rest_route(
'classifai/v1',
'create-content',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'rest_endpoint_callback' ],
'permission_callback' => [ $this, 'create_content_permissions_check' ],
'args' => [
'id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
'description' => esc_html__( 'Post ID where content should be stored.', 'classifai' ),
],
'summary' => [
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'The summary that will be used to generate the full article.', 'classifai' ),
],
'title' => [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'The title of the article.', 'classifai' ),
],
'conversation' => [
'type' => 'object',
'properties' => [
'prompt' => [
'type' => 'string',
'sanitize_callback' => 'wp_kses_post',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'The prompt a user sent.', 'classifai' ),
],
'response' => [
'type' => 'string',
'sanitize_callback' => 'wp_kses_post',
'validate_callback' => 'rest_validate_request_arg',
'description' => esc_html__( 'The response from the assistant to the prompt.', 'classifai' ),
],
],
'description' => esc_html__( 'Any previous conversation between a user and assistant.', 'classifai' ),
],
],
]
);
}
/**
* Check if a given request has access to create content.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function create_content_permissions_check( WP_REST_Request $request ) {
$post_id = $request->get_param( 'id' );
// Ensure we have a logged in user that can edit the item.
if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
return false;
}
$post_type = get_post_type( $post_id );
$post_type_obj = get_post_type_object( $post_type );
// Ensure the post type is allowed in REST endpoints.
if ( ! $post_type || empty( $post_type_obj ) || empty( $post_type_obj->show_in_rest ) ) {
return false;
}
// Ensure the feature is enabled. Also runs a user check.
if ( ! $this->is_feature_enabled() ) {
return new WP_Error( 'not_enabled', esc_html__( 'Content Generation is not currently enabled.', 'classifai' ) );
}
return true;
}
/**
* Generic request handler for all our custom routes.
*
* @param WP_REST_Request $request The full request object.
* @return \WP_REST_Response
*/
public function rest_endpoint_callback( WP_REST_Request $request ) {
$route = $request->get_route();
if ( strpos( $route, '/classifai/v1/create-content' ) === 0 ) {
return rest_ensure_response(
$this->run(
$request->get_param( 'id' ),
'create_content',
[
'title' => $request->get_param( 'title' ),
'summary' => $request->get_param( 'summary' ),
'conversation' => $request->get_param( 'conversation' ),
]
)
);
}
return parent::rest_endpoint_callback( $request );
}
/**
* Enqueue the editor scripts.
*/
public function enqueue_editor_assets() {
global $post;
if ( empty( $post ) || ! is_admin() ) {
return;
}
wp_enqueue_script(
'classifai-plugin-content-generation-js',
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-content-generation.js',
get_asset_info( 'classifai-plugin-content-generation', 'dependencies' ),
get_asset_info( 'classifai-plugin-content-generation', 'version' ),
true
);
}
/**
* Get the description for the enable field.
*
* @return string
*/
public function get_enable_description(): string {
return esc_html__( 'A sparkle icon will show in the bottom right corner in the block editor. Click on this to start the generation process.', 'classifai' );
}
/**
* Returns the default settings for the feature.
*
* @return array
*/
public function get_feature_default_settings(): array {
return [
'prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->prompt,
'original' => 1,
],
],
'post_types' => [
'post' => 'post',
],
'provider' => ChatGPT::ID,
];
}
/**
* Returns the settings for the feature.
*
* @param string $index The index of the setting to return.
* @return array|mixed
*/
public function get_settings( $index = false ) {
$settings = parent::get_settings( $index );
// Keep using the original prompt from the codebase to allow updates.
if ( $settings && ! empty( $settings['prompt'] ) ) {
foreach ( $settings['prompt'] as $key => $prompt ) {
if ( 1 === intval( $prompt['original'] ) ) {
$settings['prompt'][ $key ]['prompt'] = $this->prompt;
break;
}
}
}
return $settings;
}
/**
* Sanitizes the default feature settings.
*
* @param array $new_settings Settings being saved.
* @return array
*/
public function sanitize_default_feature_settings( array $new_settings ): array {
$post_types = \Classifai\get_post_types_for_language_settings();
$new_settings['prompt'] = sanitize_prompts( 'prompt', $new_settings );
foreach ( $post_types as $post_type ) {
if ( ! isset( $new_settings['post_types'][ $post_type->name ] ) ) {
$new_settings['post_types'][ $post_type->name ] = '';
} else {
$new_settings['post_types'][ $post_type->name ] = sanitize_text_field( $new_settings['post_types'][ $post_type->name ] );
}
}
return $new_settings;
}
}