Prompt_Service
Answers a question from the retrieved site 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\Better_Search\Pro\AI
Since: 4.5.0
Source: includes/pro/ai/class-prompt-service.php line 23
Methods
__construct()
Constructor.
Line: 51
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: 4.5.0
Line: 62
static public function get_default_retriever( ): Retriever_Interface;
Returns: Retriever_Interface — Retriever.
ask()
Answer a question.
Since: 4.5.0
Line: 100
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: 4.5.1
Line: 195
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: 4.5.0
Line: 295
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: 4.5.0
Line: 405
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: 4.5.0
Line: 430
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 site.
Since: 4.5.0
Line: 449
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: 4.5.0
Line: 552
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.
unanswered() static
An unanswered result.
Since: 4.5.0
Line: 619
static public function unanswered( bool $provider_called ): array;
| Type | Name | Description |
|---|---|---|
bool |
$provider_called |
Whether a provider request was made. |
Returns: array — Result.