Skip to content

Commit

Permalink
Provide DebugService for convenient testing
Browse files Browse the repository at this point in the history
Because OpenAI service has already been tested and works,
and because part of $instructions won't be shown to the user,
there is no point in using the real service during development.

Instead, DebugService will print both $prompt and $instructions
as response to any query.
  • Loading branch information
edwardspec committed Dec 7, 2024
1 parent f34af75 commit dce5d2f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ which allows user to make AI queries about content of articles in this wiki.
## Example configuration

```php
$wgAskAIApiKey = 'something';
$wgAskAIServiceOptionsOpenAI['apiKey'] = 'some-api-key-to-ChatGPT';
```

## Debug configuration

```php
// This setting won't use the real AI service,
// instead it will respond with "what did we ask from AI" information.
$wgAskAIServiceClass = 'MediaWiki\\AskAI\\Service\\DebugService';
```
1 change: 1 addition & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"AutoloadClasses": {
"MediaWiki\\AskAI\\SpecialAI": "includes/SpecialAI.php",
"MediaWiki\\AskAI\\Service\\IExternalService": "includes/Service/IExternalService.php",
"MediaWiki\\AskAI\\Service\\DebugService": "includes/Service/DebugService.php",
"MediaWiki\\AskAI\\Service\\OpenAI": "includes/Service/OpenAI.php",
"MediaWiki\\AskAI\\Service\\ServiceFactory": "includes/Service/ServiceFactory.php"
},
Expand Down
5 changes: 5 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

"action-askai": "use [[Special:AI]]",
"askai": "AI",
"askai-debug-header": "Request was received by DebugService (see $wgAskAIServiceClass), not a real AI service.",
"askai-debug-instructions": "AI has received the following instructions:",
"askai-debug-prompt": "User has asked the following question:",
"askai-desc": "Provides [[Special:AI]], which allows user to make AI queries about content of articles in this wiki.",
"askai-field-pages": "List of wiki pages (and paragraphs in them) to be analyzed by the AI:",
"askai-field-prompt": "Question to ask:",
"askai-field-response": "Response from the AI:",
"askai-openai-not-configured": "Error: OpenAI not configured: apiKey, apiUrl or model are not set.",
"askai-unknown-service": "Not configured: incorrect value of $wgAskAIServiceClass.",
"right-askai": "Send queries to Special:AI."
}
5 changes: 5 additions & 0 deletions i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

"action-askai": "{{doc-action|askai}}",
"askai": "Title of the page Special:AI.",
"askai-debug-header": "Header shown before results of Special:AI if the response was provided by DebugService, not real AI.",
"askai-debug-instructions": "Label shown by DebugService before the instructions received by the AI.",
"askai-debug-prompt": "Label shown by DebugService before the question received by the AI.",
"askai-desc": "{{desc|name=AskAI|url=https://www.mediawiki.org/wiki/Extension:AskAI}}",
"askai-field-pages": "Label of field for the list of pages/paragraphs.",
"askai-field-prompt": "Label of field for a question to the AI.:",
"askai-field-response": "Label of readonly field that will show the response from the AI.",
"askai-openai-not-configured": "Error message if OpenAI API key, etc. are not configured.",
"askai-unknown-service": "Error message if failed to create an AI service.",
"right-askai": "{{doc-right|askai}}\nAllows to use Special:AI.",
}
46 changes: 46 additions & 0 deletions includes/Service/DebugService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Implements AskAI extension for MediaWiki.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/

namespace MediaWiki\AskAI\Service;

/**
* Fake service that responds to any prompts with "What did the user request?" information.
*/
class DebugService implements IExternalService {
/**
* Send an arbitrary question to ChatGPT and return the response.
* @param string $prompt Question to ask.
* @param string $instructions Preferences on how to respond, e.g. "You are a research assistant".
* @return string
*/
public function query( $prompt, $instructions = '' ) {
$delim = str_repeat( '-', 80 );
$response = wfMessage( 'askai-debug-header' )->plain() . "\n\n" .
wfMessage( 'askai-debug-prompt' )->plain() .
"\n$delim\n$prompt\n$delim\n\n" .
wfMessage( 'askai-debug-instructions' )->plain() .
"\n$delim\n$instructions\n$delim";

return $response;
}
}
2 changes: 1 addition & 1 deletion includes/Service/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct( Config $config ) {
*/
public function query( $prompt, $instructions = '' ) {
if ( !$this->isConfigured ) {
return 'Error: not configured: apiKey, apiUrl or model are not set.';
return wfMessage( 'askai-openai-not-configured' )->plain();
}

$postData = FormatJson::encode( [
Expand Down
8 changes: 7 additions & 1 deletion includes/SpecialAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ protected function alterForm( HTMLForm $form ) {
/** @inheritDoc */
public function onSubmit( array $data ) {
$ai = ServiceFactory::getAI();
if ( !$ai ) {
return Status::newFatal( 'askai-unknown-service' );
}

$response = $ai->query( $data['prompt'], $data['pagesAndParagraphs'] );

$this->getOutput()->addHTML( Xml::element( 'div', [], $response ) );
$this->getOutput()->addHTML( Xml::element( 'div', [
'style' => 'white-space: pre-wrap'
], $response ) );

return Status::newGood();
}
Expand Down

0 comments on commit dce5d2f

Please sign in to comment.