-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptAI.config.php
More file actions
224 lines (195 loc) · 8.72 KB
/
PromptAI.config.php
File metadata and controls
224 lines (195 loc) · 8.72 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
<?php namespace ProcessWire;
class PromptMatrixEntity {
public function __construct(
public ?string $mode = 'inline',
public ?array $templates = null,
public ?array $fields = null,
public ?string $prompt = null,
public ?string $label = null,
public ?bool $overwriteTarget = false,
public ?string $targetSubfield = 'description',
public ?bool $ignoreFieldContent = false,
) {}
public static function fromArray(array $config): self {
$targetSubfield = trim($config['targetSubfield'] ?? 'description');
return new self(
mode: $config['mode'] ?? 'inline',
templates: $config['templates'] ?? null,
fields: $config['fields'] ?? null,
prompt: $config['prompt'] ?? null,
label: $config['label'] ?? null,
overwriteTarget: $config['overwriteTarget'] ?? false,
targetSubfield: $targetSubfield ?: 'description',
ignoreFieldContent: $config['ignoreFieldContent'] ?? false,
);
}
public function toArray(): array {
return [
'mode' => $this->mode ?? 'inline',
'templates' => $this->templates ?? [],
'fields' => $this->fields ?? [],
'prompt' => $this->prompt ?? '',
'label' => $this->label ?? '',
'overwriteTarget' => $this->overwriteTarget ?? false,
'targetSubfield' => $this->targetSubfield ?? 'description',
'ignoreFieldContent' => $this->ignoreFieldContent ?? false,
];
}
}
class PromptAIConfig extends ModuleConfig {
// Parts of the code are adopted from the Jumplinks module, thx!
// Copyright (c) 2016-17, Mike Rockett
private array $providers = [
'anthropic' => 'Anthropic',
'openai' => 'OpenAI',
'gemini' => 'Gemini',
'deepseek' => 'DeepSeek',
];
protected function buildInputField($fieldNameId, $meta) {
$field = wire('modules')->get($fieldNameId);
foreach ($meta as $metaNames => $metaInfo) {
$metaNames = explode('+', $metaNames);
foreach ($metaNames as $metaName) {
$field->$metaName = $metaInfo;
}
}
return $field;
}
public function getDefaults() {
return [
'provider' => 'anthropic',
'model' => '',
'apiKey' => '',
'systemPrompt' => '',
'promptMatrix' => '',
'individualButtons' => false,
'enableTools' => false,
'testSettings' => 0,
];
}
public function getInputFields() {
$inputfields = parent::getInputfields();
$inputfields->add(
$this->buildInputField('InputfieldSelect', [
'name+id' => 'provider',
'label' => $this->_('AI Provider'),
'description' => $this->_('Which AI provider should be used?'),
'options' => $this->providers,
'columnWidth' => 33,
'required' => true,
])
);
$inputfields->add(
$this->buildInputField('InputfieldText', [
'name+id' => 'model',
'label' => $this->_('AI Model'),
'description' => $this->_('Which AI model should be used?'),
'notes' => $this->_("[Anthropic](https://platform.claude.com/docs/en/about-claude/models/overview), [OpenAI](https://platform.openai.com/docs/models), [Gemini](https://ai.google.dev/gemini-api/docs/models), [DeepSeek](https://api-docs.deepseek.com/quick_start/pricing)"),
'columnWidth' => 33,
])
);
$inputfields->add(
$this->buildInputField('InputfieldText', [
'name+id' => 'apiKey',
'label' => $this->_('API Key'),
'description' => $this->_('You need an API key to use this module.'),
'notes' => $this->_("[Anthropic](https://console.anthropic.com/settings/keys), [OpenAI](https://platform.openai.com/account/api-keys), [Gemini](https://aistudio.google.com/apikey), [DeepSeek](https://platform.deepseek.com/api_keys)"),
'columnWidth' => 34,
'required' => false,
])
);
$inputfields->add(
$this->buildInputField('InputfieldTextArea', [
'name+id' => 'systemPrompt',
'label' => $this->_('System prompt'),
'description' => $this->_('This text will be used as the system prompt for the AI. It will be prepended to all AI calls.'),
'notes' => $this->_('Optional'),
'columnWidth' => 66,
])
);
$configLabel = $this->_('Prompt Configuration');
$configUrl = wire('urls')->admin . 'setup/prompt-ai/';
$configLink = "[{$configLabel}]($configUrl)";
$inputfields->add(
$this->buildInputField('InputfieldMarkup', [
'name+id' => 'promptMatrixHint',
'label' => $this->_('Prompts'),
'description' => $this->_('Use the visual configuration interface to manage your prompts: ') . $configLink,
'columnWidth' => 100,
])
);
$inputfields->add(
$this->buildInputField('InputfieldHidden', [
'name+id' => 'promptMatrix',
'label' => $this->_('Prompts'),
'description' => $this->_('Prompt configurations are stored in JSON format. Use the visual configuration interface in Setup > Prompt AI to manage your prompts.'),
'notes' => $this->_('This field stores the prompt configuration data. Please use the dedicated Prompt AI configuration page to modify settings.'),
'columnWidth' => 100,
])
);
if (wire('input')->post('promptMatrix')) {
PromptAIHelper::parsePromptMatrix(wire('input')->post('promptMatrix'), true);
}
$inputfields->add(
$this->buildInputField('InputfieldCheckbox', [
'name+id' => 'individualButtons',
'label' => $this->_('Individual buttons for page mode'),
'description' => $this->_('Show separate "Save + {prompt label}" button for each page mode prompt instead of a single "Save + Send to AI" button'),
'value' => 1,
'columnWidth' => 100,
])
);
$inputfields->add(
$this->buildInputField('InputfieldCheckbox', [
'name+id' => 'enableTools',
'label' => $this->_('Enable AI tools'),
'description' => $this->_('Allow the AI to call ProcessWire API tools (getPages, getPage, getFields) for data retrieval. This lets you write prompts like "list the first 15 blog posts as links".'),
'value' => 1,
'columnWidth' => 100,
])
);
$inputfields->add(
$this->buildInputField('InputfieldCheckbox', [
'name+id' => 'testSettings',
'label' => $this->_('Test settings on save'),
'description' => $this->_('Send a test request to selected AI'),
'value' => 1,
'checked' => '',
'columnWidth' => 100,
'collapsed' => Inputfield::collapsedYes,
])
);
if (wire('input')->post('testSettings')) {
wire('session')->set('testSettings', 1);
}
if (wire('session')->get('testSettings')) {
// Run the test only once; cache result in session for the render pass
$testResult = wire('session')->get('testSettingsResult');
if ($testResult === null) {
$testResult = $this->requestTest();
wire('session')->set('testSettingsResult', $testResult);
} else {
wire('session')->remove('testSettings');
wire('session')->remove('testSettingsResult');
}
$inputfields->add(
$this->buildInputField('InputfieldMarkup', [
'name+id' => 'debug_log',
'label' => $this->_('Test results'),
'columnWidth' => 100,
'value' => $testResult,
])
);
// Uncheck testSettings to prevent testing next time the module config is shown
$moduleConfig = wire('modules')->getConfig('PromptAI');
$moduleConfig['testSettings'] = 0;
wire('modules')->saveConfig('PromptAI', $moduleConfig);
}
return $inputfields;
}
private function requestTest() {
$module = wire('modules')->get('PromptAI');
$test = $module->testConnection();
return '<pre>'.$test.'</pre>';
}
}