[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:
parent
92c62c4d96
commit
1dc97f0801
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: dev
|
||||||
|
|
||||||
|
Add response format parameter for request completion method
|
|
@ -7,8 +7,7 @@ import { useRef, useState } from '@wordpress/element';
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import { getCompletion, createExtendedError } from '../utils/';
|
import { getCompletion, createExtendedError } from '../utils/';
|
||||||
|
import { ResponseFormat, StopReason } from '../shared/types';
|
||||||
type StopReason = 'abort' | 'finished' | 'error' | 'interrupted';
|
|
||||||
|
|
||||||
export type UseCompletionError = Error & { code?: string; cause?: Error };
|
export type UseCompletionError = Error & { code?: string; cause?: Error };
|
||||||
|
|
||||||
|
@ -62,7 +61,8 @@ export const useCompletion = ( {
|
||||||
|
|
||||||
const requestCompletion = async (
|
const requestCompletion = async (
|
||||||
prompt: string,
|
prompt: string,
|
||||||
featureOverride?: string
|
featureOverride?: string,
|
||||||
|
responseFormat?: ResponseFormat
|
||||||
) => {
|
) => {
|
||||||
if (
|
if (
|
||||||
! window.JP_CONNECTION_INITIAL_STATE?.connectionStatus?.isActive
|
! window.JP_CONNECTION_INITIAL_STATE?.connectionStatus?.isActive
|
||||||
|
@ -89,7 +89,8 @@ export const useCompletion = ( {
|
||||||
try {
|
try {
|
||||||
const suggestionsSource = await getCompletion(
|
const suggestionsSource = await getCompletion(
|
||||||
prompt,
|
prompt,
|
||||||
completionFeature
|
completionFeature,
|
||||||
|
responseFormat ?? 'text'
|
||||||
);
|
);
|
||||||
|
|
||||||
setCompletionActive( true );
|
setCompletionActive( true );
|
||||||
|
|
|
@ -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';
|
|
@ -2,13 +2,20 @@
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import { requestJetpackToken } from './requestJetpackToken';
|
import { requestJetpackToken } from './requestJetpackToken';
|
||||||
|
import { ResponseFormat } from '../shared/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Leaving this here to make it easier to debug the streaming API calls for now
|
* 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 { token } = await requestJetpackToken();
|
||||||
|
|
||||||
const url = new URL(
|
const url = new URL(
|
||||||
|
@ -19,5 +26,9 @@ export async function getCompletion( prompt: string, feature: string ) {
|
||||||
url.searchParams.append( 'token', token );
|
url.searchParams.append( 'token', token );
|
||||||
url.searchParams.append( 'feature', feature );
|
url.searchParams.append( 'feature', feature );
|
||||||
|
|
||||||
|
if ( responseFormat ) {
|
||||||
|
url.searchParams.append( 'response_format', responseFormat );
|
||||||
|
}
|
||||||
|
|
||||||
return new EventSource( url.toString() );
|
return new EventSource( url.toString() );
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: tweak
|
||||||
|
|
||||||
|
Request valid JSON from the API when generating AI product names
|
|
@ -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.',
|
'Return a short and concise reason for each suggestion in seven words in the "reason" part of your response.',
|
||||||
"The product's properties are:",
|
"The product's properties are:",
|
||||||
`${ JSON.stringify( validProductData ) }`,
|
`${ 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"}]}',
|
'{"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 {
|
try {
|
||||||
await requestCompletion( buildPrompt() );
|
await requestCompletion( buildPrompt(), undefined, 'json_object' );
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
setSuggestionsState( SuggestionsState.Failed );
|
setSuggestionsState( SuggestionsState.Failed );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue