[Woo AI] Add JSON response format (#46137)

* Add response_format to Woo AI package

* Add changelog

* Request JSON response format for product name generation

* Add changelog

* Add shared types
This commit is contained in:
Nima Karimi 2024-04-03 11:37:39 -07:00 committed by GitHub
parent 92c62c4d96
commit 1dc97f0801
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 37 additions and 8 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
Add response format parameter for request completion method

View File

@ -7,8 +7,7 @@ import { useRef, useState } from '@wordpress/element';
* Internal dependencies
*/
import { getCompletion, createExtendedError } from '../utils/';
type StopReason = 'abort' | 'finished' | 'error' | 'interrupted';
import { ResponseFormat, StopReason } from '../shared/types';
export type UseCompletionError = Error & { code?: string; cause?: Error };
@ -62,7 +61,8 @@ export const useCompletion = ( {
const requestCompletion = async (
prompt: string,
featureOverride?: string
featureOverride?: string,
responseFormat?: ResponseFormat
) => {
if (
! window.JP_CONNECTION_INITIAL_STATE?.connectionStatus?.isActive
@ -89,7 +89,8 @@ export const useCompletion = ( {
try {
const suggestionsSource = await getCompletion(
prompt,
completionFeature
completionFeature,
responseFormat ?? 'text'
);
setCompletionActive( true );

View File

@ -0,0 +1,9 @@
/**
* Streaming Stop Reason
*/
export type StopReason = 'abort' | 'finished' | 'error' | 'interrupted';
/**
* Response Format for text completion
*/
export type ResponseFormat = 'json_object' | 'text';

View File

@ -2,13 +2,20 @@
* Internal dependencies
*/
import { requestJetpackToken } from './requestJetpackToken';
import { ResponseFormat } from '../shared/types';
/**
* Leaving this here to make it easier to debug the streaming API calls for now
*
* @param {string} prompt - The query to send to the API
* @param {string} prompt - The query to send to the API
* @param {string} feature - The feature to use for the completion
* @param {string} responseFormat - The format of the response. Can be 'text' or 'json_object'.
*/
export async function getCompletion( prompt: string, feature: string ) {
export async function getCompletion(
prompt: string,
feature: string,
responseFormat?: ResponseFormat
) {
const { token } = await requestJetpackToken();
const url = new URL(
@ -19,5 +26,9 @@ export async function getCompletion( prompt: string, feature: string ) {
url.searchParams.append( 'token', token );
url.searchParams.append( 'feature', feature );
if ( responseFormat ) {
url.searchParams.append( 'response_format', responseFormat );
}
return new EventSource( url.toString() );
}

View File

@ -0,0 +1,4 @@
Significance: patch
Type: tweak
Request valid JSON from the API when generating AI product names

View File

@ -240,7 +240,7 @@ export const ProductNameSuggestions = () => {
'Return a short and concise reason for each suggestion in seven words in the "reason" part of your response.',
"The product's properties are:",
`${ JSON.stringify( validProductData ) }`,
'Here is an example of a valid response:',
'Write your response in valid JSON. Here is an example of a valid response:',
'{"suggestions": [{"content": "An improved alternative to the product\'s title", "reason": "Reason for the suggestion"}, {"content": "Another improved alternative to the product title", "reason": "Reason for this suggestion"}]}',
];
@ -262,7 +262,7 @@ export const ProductNameSuggestions = () => {
} );
try {
await requestCompletion( buildPrompt() );
await requestCompletion( buildPrompt(), undefined, 'json_object' );
} catch ( e ) {
setSuggestionsState( SuggestionsState.Failed );
}