-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Azure Language Services provider #786
base: develop
Are you sure you want to change the base?
Conversation
@psorensen thanks for the PR! Could you please fill out the PR template with description, changelog, and credits information so that we can properly review and merge this? |
1239c7d
to
55bca24
Compare
@dkotter How should I handle the VIP flag for |
So we've not been consistent with this so far in the codebase. We've basically done both, where we use Personally I'd prefer we go with the first approach, as this is technically a VIP approved plugin so it's nice to match their standards. We have had issues though a few times with timeouts, as some of these services take a bit to return. |
1bac627
to
b550389
Compare
sounds good @dkotter - I've included a helper function to facilitate a refactor of all the existing usage of |
@psorensen is this still WIP or can this formally go through review/merge? |
@jeffpaul Good to review - removed the WIP from the title |
$endpoint = trailingslashit( $url ) . '/text/analytics/v3.1/languages'; | ||
$endpoint = add_query_arg( 'api-version', static::API_VERSION, $endpoint ); | ||
|
||
$request = safe_wp_remote_get( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is throwing error. Should it use wp_remote_post()
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this comment and the follow-up comments for context. What error are you seeing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the neater version:
[06-Sep-2024 08:40:15 UTC] PHP Fatal error: Uncaught TypeError: http_build_query(): Argument #1 ($data) must be of type array, string given in /Users/work/Sites/classifai/app/public/wp-includes/Requests/src/Transport/Curl.php:578
Stack trace:
#0 /Users/work/Sites/classifai/app/public/wp-includes/Requests/src/Transport/Curl.php(578): http_build_query('{"documents": [...', '', '&')
#1 /Users/work/Sites/classifai/app/public/wp-includes/Requests/src/Transport/Curl.php(391): WpOrg\Requests\Transport\Curl::format_get('https://classif...', '{"documents": [...')
#2 /Users/work/Sites/classifai/app/public/wp-includes/Requests/src/Transport/Curl.php(171): WpOrg\Requests\Transport\Curl->setup_handle('https://classif...', Array, '{"documents": [...', Array)
#3 /Users/work/Sites/classifai/app/public/wp-includes/Requests/src/Requests.php(469): WpOrg\Requests\Transport\Curl->request('https://classif...', Array, '{"documents": [...', Array)
#4 /Users/work/Sites/classifai/app/public/wp-includes/class-wp-http.php(397): WpOrg\Requests\Requests::request('https://classif...', Array, '{"documents": [...', 'GET', Array)
#5 /Users/work/Sites/classifai/app/public/wp-includes/class-wp-http.php(638): WP_Http->request('https://classif...', Array)
#6 /Users/work/Sites/classifai/app/public/wp-includes/http.php(184): WP_Http->get('https://classif...', Array)
#7 /Users/work/Sites/classifai/app/public/wp-content/plugins/classifai/includes/Classifai/Helpers.php(686): wp_remote_get('https://classif...', Array)
#8 /Users/work/Sites/classifai/app/public/wp-content/plugins/classifai/includes/Classifai/Providers/Azure/Language.php(164): Classifai\safe_wp_remote_get('https://classif...', Array)
#9 /Users/work/Sites/classifai/app/public/wp-content/plugins/classifai/includes/Classifai/Providers/Azure/Language.php(122): Classifai\Providers\Azure\Language->authenticate_credentials('https://classif...', '95193e52eec647f...')
#10 /Users/work/Sites/classifai/app/public/wp-content/plugins/classifai/includes/Classifai/Features/Feature.php(258): Classifai\Providers\Azure\Language->sanitize_settings(Array)
#11 /Users/work/Sites/classifai/app/public/wp-includes/class-wp-hook.php(326): Classifai\Features\Feature->sanitize_settings(Array)
#12 /Users/work/Sites/classifai/app/public/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
#13 /Users/work/Sites/classifai/app/public/wp-includes/formatting.php(5115): apply_filters('sanitize_option...', Array, 'classifai_featu...', Array)
#14 /Users/work/Sites/classifai/app/public/wp-includes/option.php(866): sanitize_option('classifai_featu...', Array)
#15 /Users/work/Sites/classifai/app/public/wp-admin/options.php(344): update_option('classifai_featu...', Array)
#16 {main}
thrown in /Users/work/Sites/classifai/app/public/wp-includes/Requests/src/Transport/Curl.php on line 578
} | ||
|
||
while ( 'succeeded' !== $response->status ) { | ||
sleep( .5 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem here just in quickly looking at the code is that this method gets called from a REST request. The easy fix is what we're doing here, just pausing and retrying until we get a successful response.
I do wonder what happens if the response never succeeds? Would this code just be stuck in this loop?
Action Scheduler would be a nicer fix here but would be more complicated with how everything else is set up in ClassifAI. Basically when the Generate Excerpt
button is clicked, the initial request would trigger Action Scheduler and then on the javascript side, we would need to continue polling until the request is done (and show the loading indicator the entire time).
That's doable but doesn't match how we handle things elsewhere so would need to think through how best to achieve that, especially in a way that would work for other Features and Providers in the future
* @param string $summary_length The summary length. | ||
* @return string | ||
*/ | ||
'summaryLength' => apply_filters( 'classifai_azure_language_summary_length', 'oneSentence' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to format the doc block
/**
* Filter the summary length.
* Possible values are 'oneSentence', 'short', 'medium', 'long'.
* Default is 'oneSentence'.
*
* @since 3.2.0
* @hook classifai_azure_language_summary_length
* @param {string} $summary_length The summary length.
* @return {string} The summary length
*/
$response = json_decode( wp_remote_retrieve_body( $request ) ); | ||
} | ||
|
||
$summary = $response->tasks->items[0]->results->documents[0]->summaries[0]->text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just want to confirm that the array indexes at [0]
will always contain those properties, or if we should check via isset()
and count() > 0
?
@psorensen thanks for the PR! Could you please rebase your PR on top of the latest changes in the base branch? |
Description of the Change
This PR adds Azure Language Services as a provider and adds it to the Excerpt Generation feature.
Closes #159
How to test the Change
Testing Steps:
Changelog Entry
Credits
Props @psorensen @cadic @jeffpaul @Sidsector9 @dkotter
Checklist: