Skip to content

Commit

Permalink
Use newer chatGPT supporting function calling
Browse files Browse the repository at this point in the history
Using the function calling GPT allows to further improve on the answers given. Now we receive structured data that can easily be used.

Also introducing Logging by DokuWiki for Debug and error reasons.
  • Loading branch information
gamma committed Jun 19, 2023
1 parent 4023162 commit dcdb221
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
74 changes: 62 additions & 12 deletions action/keywords.php
Expand Up @@ -6,6 +6,7 @@
* @author Gerry Weißbach <tools@inetsoftware.de>
*/
use dokuwiki\HTTP\DokuHTTPClient;
use \dokuwiki\Logger;

class action_plugin_keywords_keywords extends \dokuwiki\Extension\ActionPlugin
{
Expand Down Expand Up @@ -65,7 +66,12 @@ public function handleCommonWikipageSave(Doku_Event $event, $param) {
}

$requestContent = $event->data['newContent'];
$requestContent = trim( preg_replace( '/\{\{keywords>.*?\}\}/i' , '', $requestContent ) );
$requestContent = trim( preg_replace( '/\{\{keywords>.*?\}\}/is' , '', $requestContent ) );
if ( empty( $requestContent ) ) {
$event->data['contentChanged'] = 1;
$event->data['newContent'] = '';
return;
}

//* // Remove one slash to have a sample of keywords saved instead of checking with chatGPT
$httpClient = new DokuHTTPClient();
Expand All @@ -78,30 +84,74 @@ public function handleCommonWikipageSave(Doku_Event $event, $param) {
'model' => $this->getConf('APIModel'),
'messages' => [
[ "role" => "system", "content" => $this->getConf('APIRole') ],
[ "role" => "user", "content" => $requestContent ],
] ] ), 'POST');
[ "role" => "user", "content" => $requestContent ]
],
"functions" => [[
"name" => "keywords_for_text",
"description" => "Use this function to sent the list of keywords back to the dokuwiki. The Input is the array of keywords",
"parameters" => [
"type" => "object",
"properties" => [
"keywords" => [
"type" => "array",
"items" => [
"type" => "string"
],
"description" => "This is the list of keywords generated from the input. Each keyword is given as string."
]
]
]
],[
"name" => "no_content",
"description" => "Use this function to indicate that no kexwords could be generated.",
"parameters" => [
"type" => "object",
"properties" => [
"empty" => [
"type" => "string",
"description" => "This parameter must always be NULL."
]
]
]
]]
] ), 'POST');

$data = json_decode( $httpClient->resp_body );

if ( $status === false ) {
$error = $httpClient->error;
Logger::error( "An error occurred during the Chat GPT call", $httpClient->error, __FILE__, __LINE__ );
return;
} else if ( $data->error ) {
$error = $data->error->message;
Logger::error( "An error occurred during the Chat GPT call", $data->error->message, __FILE__, __LINE__ );
return;
}

if ( !empty( $error ) ) {
print "Error:<br>\n";
print_r( $error );
exit;
if ( $data->choices[0]->message->function_call->name == "keywords_for_text" ) {
$arguments = json_decode( $data->choices[0]->message->function_call->arguments );
$keywords = $arguments->keywords;
Logger::debug( "Chat GPT response", $keywords, __FILE__, __LINE__ );
} else {
Logger::debug( "INVALID Chat GPT response", $data->choices[0]->message, __FILE__, __LINE__ );
}

$keywords = $data->choices[0]->message->content;
if ( empty( $keywords ) || !is_array( $keywords ) ) {
// there is no content herre.
if ( $requestContent == $event->data['newContent'] ) {
// Nothing has changed, return
return;
}

// The keywords are empty now.
$event->data['contentChanged'] = 1;
$event->data['newContent'] = $requestContent;
return;
}
/*/
$keywords = 'word1, word2, word3, word4';
$keywords = [ "word1", "word2", "word3", "word4" ];
//*/

$event->data['contentChanged'] = 1;
$event->data['newContent'] = '{{keywords>' . $keywords . '}}' . "\n\n" . $requestContent;
$event->data['newContent'] = '{{keywords>' . implode( ", ", $keywords ) . '}}' . "\n\n" . $requestContent;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions conf/default.php
Expand Up @@ -5,5 +5,5 @@
* @author Gerry Weißbach <tools@inetsoftware.de>
*/

$conf['APIModel'] = "gpt-3.5-turbo";
$conf['APIRole'] = "As a keyword generator, your task is to generate a list of FIVE (5) semantically related keywords not directly stated in the provided text. These keywords should be in the same language as the provided text and should reflect the information a user might search for when seeking the content in the text. Your response should consist only of keywords of potential search queries a user of our product might enter into our help system, each separated by a comma ','. Avoid using non-alphanumeric characters. Keep your response concise. ONLY RETURN 5 to 10 KEYWORDS. NO MORE.";
$conf['APIModel'] = "gpt-3.5-turbo-0613";
$conf['APIRole'] = "Generate a list of five (5) semantically related keywords in the same language as the provided text. These keywords should reflect potential search queries a user might enter into our help system to find content related to the provided text. RETURN 5 TO 10 KEYWORDS, NO MORE.";

0 comments on commit dcdb221

Please sign in to comment.