-
Notifications
You must be signed in to change notification settings - Fork 65
Explore having the LLM return content in JSON schema instead of WordPress block markup #1119
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
base: develop
Are you sure you want to change the base?
Changes from all commits
61a4f16
e0934f6
92e2707
5fc4d5a
7e7d700
9aa185d
ba814ef
95e62cd
a3e3fdf
292fc2c
1f3d110
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| use function Classifai\sanitize_number_of_responses_field; | ||
| use function Classifai\safe_wp_remote_post; | ||
| use function Classifai\get_temperature; | ||
| use function Classifai\sanitize_generated_block_tree; | ||
|
|
||
| class OpenAI extends Provider { | ||
|
|
||
|
|
@@ -954,8 +955,9 @@ public function generate_content( int $post_id = 0, array $args = array() ) { | |
| $body = apply_filters( | ||
| 'classifai_azure_openai_content_request_body', | ||
| array( | ||
| 'messages' => $messages, | ||
| 'temperature' => 0.9, | ||
| 'messages' => $messages, | ||
| 'temperature' => 0.9, | ||
| 'response_format' => array( 'type' => 'json_object' ), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document introduction of 🔢 Applies to other providers too. |
||
| ), | ||
| $post_id | ||
| ); | ||
|
|
@@ -977,17 +979,28 @@ public function generate_content( int $post_id = 0, array $args = array() ) { | |
| return $response; | ||
| } | ||
|
|
||
| // If we have a message, return it. | ||
| $return = ''; | ||
| // Pull the message content out of the response. | ||
| $content = ''; | ||
| if ( ! empty( $response['choices'] ) ) { | ||
| foreach ( $response['choices'] as $choice ) { | ||
| if ( isset( $choice['message'], $choice['message']['content'] ) ) { | ||
| $return = wp_kses_post( trim( $choice['message']['content'], ' "\'' ) ); | ||
| $content = trim( $choice['message']['content'] ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $return; | ||
| // The response should be a JSON BlockTree; validate before returning. | ||
| // Decode to objects (not arrays) so empty objects like "props":{} are | ||
| // preserved when re-encoded rather than becoming "props":[]. | ||
| $decoded = json_decode( $content ); | ||
| if ( null === $decoded || JSON_ERROR_NONE !== json_last_error() ) { | ||
| return new WP_Error( 'invalid_content_response', esc_html__( 'The generated content was not in the expected format. Please try again.', 'classifai' ) ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For developers using the filter and returning block formatted code, a check here for 🔢 Applies to other providers too. |
||
| } | ||
|
|
||
| // Sanitize the block tree's string values before they are rendered/saved. | ||
| $decoded = sanitize_generated_block_tree( $decoded ); | ||
|
|
||
| return wp_json_encode( $decoded ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.
Is it possible to ensure that blocks with specific parent requirements are enforced, eg column is a child of columns? It's in the instructions but it would be good to remove the element of trust.