-
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
Automatic excerpt generation using OpenAI's ChatGPT API #405
Conversation
…t. Trim content to be within the ChatGPT limits. Modify the settings helper function to work for multiple services. Add custom excerpt panel, replacing the core one
…on. Small style and text tweaks
…sable. Shorten test prompt. Fix display of last response
Note that E2E tests are failing here but they seem to have been failing for a bit (all recent PRs are failing as well). I'm going to look to see what needs fixed on those and tackle that separately from this PR |
@fabiankaegy per Darin's comment of:
...are you aware of a way to add a button into that panel or is the approach here the best given the current state of the editor? |
…ate to determine when this shows. This allows us to only show the panel if an excerpt was added prior to the panel showing.
…s. Don't load our custom JS if the current user role doesn't match
…lowed roles setting
@iamdharmesh tagging you for code review here as this week you've got some OSS time, hoping to get this ready for release as expeditiously as we can (hoping to get 1-2 features released before Summit as feasible) |
…ound our prompt and update the other filters we have
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.
Thanks for adding this @dkotter. This looks amazing. 🎉
I just added 2-3 minor notes to discuss but otherwise, all looks great.
…void timeouts on slow requests.
…for handling excerpts. Minor code tweaks for consistency
Excerpt Pre-publish Check
Description of the Change
This PR adds an integration with OpenAI as a new provider in the Language Processing service, specifically integrating with ChatGPT. The specific integration being added here is utilizing ChatGPT to provide a summary of a piece of content and then storing that summary in the excerpt field.
Setup
Setup with this provider only requires an API key. There's validation done on the settings page, anytime settings are saved, to verify if the API key is valid. This can give you one of three errors:
If the API key is valid, you won't get an error message and you'll get a success message instead. You then have a few settings to change. The most important is turning on the
Generate excerpt
option. If a valid API key is added but this setting isn't on, no integration will happen. TheAllowed roles
setting allows you to control which user roles see theGenerate excerpt
button (and are allowed to access the REST API endpoint). TheExcerpt length
setting controls how many words the final excerpt will be. This defaults to the excerpt length that WordPress has set (that can be changed by the coreexcerpt_length
filter). TheTemperature value
is one of the config options the API supports. There are other options there but I decided not to bring those over for now.Excerpt integration
I debated a few different approaches on how to actually integrate with the excerpt generation. My priorities were the following:
I initially was thinking of automatically generating an excerpt on save (draft, publish, ...) but this goes against point 1 and 2. I then considered automatically generating only on publish but that goes against 3 (and possibly 4). I eventually landed on the approach of adding a
Generate excerpt
button in theExcerpt
panel that will send content to OpenAI when clicked and populate the excerpt with whatever value is returned.This solves all the points above, as you are able to choose how often the API is hit and when the excerpt is generated (only when the button is pressed). And if you don't want to generate the excerpt, you don't have to. It does make the process more manual, as you have to click the button but I think that's a fine trade-off. I am open to other ideas on the best integration here though (I had considered adding something to the pre-publish panel but I don't think that's enough by itself. May be worth adding in addition to what else we have here).
| | |
API integration
When the
Generate excerpt
button is clicked, a request is made to a new REST endpoint (wp-json/classifai/v1/generate-excerpt/POST_ID
). This endpoint verifies the current user has permission to edit the post, we are properly authenticated with OpenAI and theGenerate excerpt
setting is on.Assuming all that passes, a new
Tokenizer
class has been added that will try and determine how many tokens the content has and how many tokens the final excerpt will be. The ChatGPT API has a limit of 4096 tokens per request and this includes both the data you send and the data that is sent back. Unfortunately tokens are equivalent to words or characters (roughly 4 characters is 1 token) but we do some basic calculations, erring on the side of being too aggressive, to ensure our request doesn't go over the limit.A new
APIRequest
class has been added here as well (followed the approach in the WatsonAPIRequest
class) to make it easier to integrate with the API, not only for this feature but for any other OpenAI features we may add in the future (theTokenizer
class should also be reusable for future integrations).The request is sent and then the response is parsed and returned, whether we get a successful response including our excerpt or we get an error. If it's an error, that will be shown to the user. If success, we set the returned value as our excerpt.
Reviewer notes
get_plugin_settings
was updated to account for multiple providers instead of just always using the first provider. There are other places in the code that should be updated to account for this but I'm planning to tackle that in a followup PRHow to test the Change
A valid OpenAI API key is needed to fully test this feature. OpenAI does offer a free $5 credit for new users so if you haven't signed up before, you can sign up and get an API key (ping me in Slack if you want to use my API key for testing).
ClassifAI > Language Processing > OpenAI
and paste in your API keyGenerate excerpt
setting. The other settings can be left default. Save changes and ensure no error message is shownExcerpt
panel, ensure you can see theGenerate excerpt
button then click on thatGenerate excerpt
option is off, ensuring proper error messages are shown and functionality is removedChangelog Entry
Credits
Props @dkotter, @jeffpaul, @zamanq
Checklist: