From 9cf9673b2750d86ed9015223076dadd85652205a Mon Sep 17 00:00:00 2001 From: Ian Arawjo Date: Fri, 7 Jul 2023 14:57:13 -0400 Subject: [PATCH] Add allow_empty_str prop so react-json-form-schema treats empty text area as empty string, not undefined --- .../react-server/src/ModelSettingSchemas.js | 3 ++- .../react-server/src/ModelSettingsModal.js | 18 +++++++++++++++--- chainforge/react-server/src/backend/utils.ts | 6 +++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/chainforge/react-server/src/ModelSettingSchemas.js b/chainforge/react-server/src/ModelSettingSchemas.js index b170838..f632cc6 100644 --- a/chainforge/react-server/src/ModelSettingSchemas.js +++ b/chainforge/react-server/src/ModelSettingSchemas.js @@ -58,7 +58,8 @@ const ChatGPTSettings = { "type": "string", "title": "System Message (chat models only)", "description": "Many conversations begin with a system message to gently instruct the assistant. By default, ChainForge includes the suggested 'You are a helpful assistant.'", - "default": "You are a helpful assistant." + "default": "You are a helpful assistant.", + "allow_empty_str": true, }, "temperature": { "type": "number", diff --git a/chainforge/react-server/src/ModelSettingsModal.js b/chainforge/react-server/src/ModelSettingsModal.js index a19d720..b9709b8 100644 --- a/chainforge/react-server/src/ModelSettingsModal.js +++ b/chainforge/react-server/src/ModelSettingsModal.js @@ -59,13 +59,25 @@ const ModelSettingsModal = forwardRef((props, ref) => { }, [props.model]); const saveFormState = useCallback((fdata) => { - setFormData(fdata); + + // For some reason react-json-form-schema returns 'undefined' on empty strings. + // We need to (1) detect undefined values for keys in formData and (2) if they are of type string, replace with "", + // if that property is marked with a special "allow_empty_str" property. + let patched_fdata = {}; + Object.entries(fdata).forEach(([key, val]) => { + if (val === undefined && key in schema.properties && schema.properties[key].allow_empty_str === true) + patched_fdata[key] = ""; + else + patched_fdata[key] = val; + }); + + setFormData(patched_fdata); if (onSettingsSubmit) { props.model.emoji = modelEmoji; - onSettingsSubmit(props.model, fdata, postprocess(fdata)); + onSettingsSubmit(props.model, patched_fdata, postprocess(patched_fdata)); } - }, [props, modelEmoji, setFormData, onSettingsSubmit, postprocess]); + }, [props, modelEmoji, schema, setFormData, onSettingsSubmit, postprocess]); const onSubmit = useCallback((submitInfo) => { saveFormState(submitInfo.formData); diff --git a/chainforge/react-server/src/backend/utils.ts b/chainforge/react-server/src/backend/utils.ts index 4f15427..a538465 100644 --- a/chainforge/react-server/src/backend/utils.ts +++ b/chainforge/react-server/src/backend/utils.ts @@ -145,7 +145,7 @@ export async function call_chatgpt(prompt: string, model: LLM, n: number = 1, te } console.log(`Querying OpenAI model '${model}' with prompt '${prompt}'...`); - const system_msg: string = params?.system_msg || "You are a helpful assistant."; + const system_msg: string = params?.system_msg !== undefined ? params.system_msg : "You are a helpful assistant."; delete params?.system_msg; let query: Dict = { @@ -204,7 +204,7 @@ export async function call_azure_openai(prompt: string, model: LLM, n: number = throw Error("Could not find an Azure OpenAPI deployment name. Double-check that your deployment name is set in Settings or in your local environment."); if (!model_type) throw Error("Could not find a model type specified for an Azure OpenAI model. Double-check that your deployment name is set in Settings or in your local environment."); - + const client = new AzureOpenAIClient(AZURE_OPENAI_ENDPOINT, new AzureKeyCredential(AZURE_OPENAI_KEY)); if (params?.stop !== undefined && (!Array.isArray(params.stop) || params.stop.length === 0)) @@ -215,7 +215,7 @@ export async function call_azure_openai(prompt: string, model: LLM, n: number = delete params.function_call; console.log(`Querying Azure OpenAI deployed model '${deployment_name}' at endpoint '${AZURE_OPENAI_ENDPOINT}' with prompt '${prompt}'...`) - const system_msg = params?.system_msg || "You are a helpful assistant."; + const system_msg = params?.system_msg !== undefined ? params.system_msg : "You are a helpful assistant."; delete params?.system_msg; delete params?.model_type;