Prompt_Service
Answers a question from the retrieved knowledge base articles.
The model only ever returns data: whether it answered, the answer as plain text, and the IDs of the articles used. Source links are built here from validated IDs; links or HTML produced by the model are never output.
Namespace: WebberZone\Knowledge_Base\Pro\AI
Since: 3.2.0
Source: includes/pro/ai/class-prompt-service.php line 25
Methods
__construct()
Constructor.
Line: 53
public function __construct( ?Retriever_Interface $retriever = ... );
| Type | Name | Description |
|---|---|---|
Retriever_Interface|null |
$retriever |
Retriever. Defaults to the filtered search retriever. |
get_default_retriever() static
Get the retriever, allowing it to be replaced.
Since: 3.2.0
Line: 64
static public function get_default_retriever( ): Retriever_Interface;
Returns: Retriever_Interface — Retriever.
ask()
Answer a question.
Since: 3.2.0
Line: 102
public function ask( string $question, ?callable $before_request = ... ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string |
$question |
Visitor question, already validated. |
callable|null |
$before_request |
Optional. Called just before the provider request. Return false to |
Returns: array|\WP_Error — {
ask_with()
Answer a question with one provider and model, ignoring the provider chain and pauses.
Used to compare models from the settings. Provider errors do not pause the provider.
Since: 3.2.0
Line: 183
public function ask_with( string $question, string $provider, string $model, ?callable $before_request = ... ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string |
$question |
Question, already validated. |
string |
$provider |
Provider ID. |
string |
$model |
Model ID, or an empty string for the provider’s default. |
callable|null |
$before_request |
Optional. See ask(). |
Returns: array|\WP_Error — See ask().
get_system_instruction() static
System instruction sent with every request.
Since: 3.2.0
Line: 287
static public function get_system_instruction( string $answer_length = ... ): string;
| Type | Name | Description |
|---|---|---|
string |
$answer_length |
short or medium. |
Returns: string — System instruction.
build_prompt() static
Build the prompt with the articles in delimited blocks.
Since: 3.2.0
Line: 329
static public function build_prompt( string $question, array $articles ): string;
| Type | Name | Description |
|---|---|---|
string |
$question |
Visitor question. |
array[] |
$articles |
Articles. |
Returns: string — Prompt.
neutralise_delimiters() static
Remove anything that looks like the prompt’s own delimiters.
Stops article text or a question from closing a block early and posing as instructions or another article.
Since: 3.2.0
Line: 354
static public function neutralise_delimiters( string $text ): string;
| Type | Name | Description |
|---|---|---|
string |
$text |
Text. |
Returns: string — Text without delimiter tags.
parse_response() static
Parse and validate the model’s output.
Invalid output is treated as unanswered. Source IDs not in the retrieved set are discarded, and an answer with no valid sources is treated as unanswered because it cannot be shown to come from the knowledge base.
Since: 3.2.0
Line: 373
static public function parse_response( string $text, array $allowed_ids ): array;
| Type | Name | Description |
|---|---|---|
string |
$text |
Raw model output. |
int[] |
$allowed_ids |
IDs of the articles sent as context. |
Returns: array — Array with answered (bool), answer (string), sources (int[]) and valid (bool).
build_sources() static
Build source links from validated IDs.
Titles and URLs come from the retrieved articles, never from the model.
Since: 3.2.0
Line: 476
static public function build_sources( array $ids, array $articles ): array;
| Type | Name | Description |
|---|---|---|
int[] |
$ids |
Validated article IDs. |
array[] |
$articles |
Articles sent as context. |
Returns: array — Sources with id, title and url.
get_related() static
Articles related to the main source, from the same section or tags.
Since: 3.2.0
Line: 504
static public function get_related( array $source_ids ): array;
| Type | Name | Description |
|---|---|---|
int[] |
$source_ids |
Source article IDs, main source first. |
Returns: array — Up to three articles with id, title and url.
unanswered() static
An unanswered result.
Since: 3.2.0
Line: 583
static public function unanswered( bool $provider_called ): array;
| Type | Name | Description |
|---|---|---|
bool |
$provider_called |
Whether a provider request was made. |
Returns: array — Result.