-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-output-preset.php
More file actions
139 lines (123 loc) · 3.71 KB
/
Copy pathjson-output-preset.php
File metadata and controls
139 lines (123 loc) · 3.71 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
<?php
/**
* Example: JSON Output Preset
*
* A preset that returns structured data (JSON) instead of plain text.
* Uses outputSchema() to enforce a JSON Schema on the AI response
* and automatically decodes the result into a PHP array.
*
* @package WordPress\InfomaniakAiToolkit\Examples
*/
declare(strict_types=1);
namespace YourPlugin\Presets;
use WordPress\InfomaniakAiToolkit\Presets\BasePreset;
/**
* Generates SEO metadata from content as a structured JSON object.
*/
class SeoMetaPreset extends BasePreset
{
public function name(): string
{
return 'seo-meta';
}
public function label(): string
{
return __('Generate SEO Metadata', 'your-plugin');
}
public function description(): string
{
return __('Generates SEO-optimized meta title, description, and keywords.', 'your-plugin');
}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'content' => [
'type' => 'string',
'description' => 'The text content to analyze.',
],
'language' => [
'type' => 'string',
'description' => 'Target language for the metadata.',
'default' => 'English',
],
],
'required' => ['content'],
];
}
protected function templateName(): string
{
return 'seo-meta';
}
protected function systemTemplateName(): ?string
{
return 'seo-expert';
}
protected function templateData(array $input): array
{
return [
'content' => $input['content'] ?? '',
'language' => $input['language'] ?? 'English',
];
}
/**
* Define a JSON Schema for the AI output.
*
* When this returns a non-null array, BasePreset will:
* 1. Call as_json_response($schema) on the prompt builder
* 2. Automatically json_decode() the result
* 3. Return a PHP array instead of a string
*/
protected function outputSchema(): ?array
{
return [
'type' => 'object',
'properties' => [
'title' => [
'type' => 'string',
'description' => 'SEO meta title (max 60 characters).',
],
'description' => [
'type' => 'string',
'description' => 'SEO meta description (max 160 characters).',
],
'keywords' => [
'type' => 'array',
'items' => ['type' => 'string'],
'description' => 'List of relevant SEO keywords.',
],
],
'required' => ['title', 'description', 'keywords'],
];
}
/**
* For ability registration, expose the same schema as output.
*/
protected function outputAbilitySchema(): array
{
return $this->outputSchema();
}
protected function temperature(): float
{
return 0.5;
}
protected function maxTokens(): int
{
return 500;
}
}
/*
* --------------------------------------------------------------------------
* Usage -- the result is a PHP array, not a string:
* --------------------------------------------------------------------------
*
* $preset = new \YourPlugin\Presets\SeoMetaPreset();
* $meta = $preset->execute(['content' => 'Your article...']);
*
* if (!is_wp_error($meta)) {
* echo $meta['title']; // "Best Swiss Hosting in 2026"
* echo $meta['description']; // "Discover the top hosting..."
* print_r($meta['keywords']); // ["hosting", "swiss", ...]
* }
*/