Last active
July 4, 2024 11:57
-
-
Save jordymeow/7c9e31eff694cffba957eaffdfac5442 to your computer and use it in GitHub Desktop.
AI Engine: Function Calling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#region Functions Definitions + Callable Functions | |
function define_userInfo() { | |
return Meow_MWAI_Query_Function::fromJson( [ | |
'id' => 'userInfo', // Anything you like! | |
'type' => 'manual', // You can set it to your add-on name (if null, it will be 'manual') | |
'name' => 'getCurrentUserInfo', // Important: What's the name of the function? | |
'desc' => 'Get the current user information.' // Important: What the function does? | |
] ); | |
} | |
function call_userInfo() { | |
$current_user = wp_get_current_user(); | |
if ( $current_user->exists() ) { | |
return json_encode( [ | |
'user_url' => $current_user->user_url, | |
'user_login' => $current_user->user_login, | |
'user_email' => $current_user->user_email, | |
'display_name' => $current_user->display_name, | |
] ); | |
} | |
return null; | |
} | |
function define_sendEmail() { | |
return Meow_MWAI_Query_Function::fromJson( [ | |
'id' => 'sendEmail', // Anything you like! | |
'type' => 'manual', // You can set it to your add-on name (if null, it will be 'manual') | |
'name' => 'sendEmail', // Important: What's the name of the function? | |
'desc' => 'Send an email to the admin.', // Important: What the function does? | |
'args' => [ | |
[ | |
'name' => 'subject', | |
'desc' => 'The subject of the email.', | |
'type' => 'string', | |
'required' => true | |
], | |
[ | |
'name' => 'message', | |
'desc' => 'The message of the email.', | |
'type' => 'string', | |
'required' => true | |
] | |
] | |
] ); | |
} | |
function call_sendEmail( $subject, $message ) { | |
$admin_email = get_option( 'admin_email' ); | |
if ( empty( $admin_email ) ) { | |
return 'The admin email is not set.'; | |
} | |
$headers = 'From: ' . get_bloginfo( 'name' ) . ' <' . $admin_email . '>'; | |
$result = wp_mail( $admin_email, $subject, $message, $headers ); | |
return $result ? 'The email has been sent.' : 'The email could not be sent.'; | |
} | |
#endregion | |
// DEF #1: Register functions naturally. They will then be selectable in Settings. | |
add_filter( 'mwai_functions_list', function ( $functions ) { | |
$functions[] = define_userInfo(); | |
$functions[] = define_sendEmail(); | |
return $functions; | |
}, 10, 1 ); | |
// DEF #2: Force-add functions into the query. | |
// This will not work with OpenAI Assistants. | |
add_filter( 'mwai_ai_query', function ( $query ) { | |
$query->add_function( define_userInfo() ); | |
$query->add_function( define_sendEmail() ); | |
return $query; | |
}, 10, 1 ); | |
// CALL #1: Give the value as a feecback to the AI model. | |
// The AI model will prepare the final answer or ask for more feedback. | |
add_filter( 'mwai_ai_feedback', function ( $value, $needFeedback ) { | |
$function = $needFeedback['function']; | |
if ( $function->id === 'userInfo' ) { | |
return call_userInfo(); | |
} | |
else if ( $function->id === 'sendEmail' ) { | |
$subject = $needFeedback['arguments']['subject']; | |
$message = $needFeedback['arguments']['message']; | |
return call_sendEmail( $subject, $message ); | |
} | |
return $value; | |
}, 10, 2 ); | |
// CALL #2: Override the reply with our own, based on the feedback value. | |
// The AI model will have no chance to process the feedback. | |
add_filter( 'mwai_ai_reply', function ( $reply, $query ) { | |
foreach ( $reply->needFeedbacks as $index => $needFeedback ) { | |
$function = $needFeedback['function']; | |
if ( $function->id === 'userInfo' ) { | |
$value = call_userInfo(); | |
if ( !empty( $value ) ) { | |
$reply->result = "Here is your data: " . $value; | |
unset( $reply->needFeedbacks[$index] ); | |
return $reply; | |
} | |
} | |
} | |
return $reply; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment