Add allow_empty_str prop so react-json-form-schema treats empty text area as empty string, not undefined

This commit is contained in:
Ian Arawjo 2023-07-07 14:57:13 -04:00
parent c735008d88
commit 9cf9673b27
3 changed files with 20 additions and 7 deletions

View File

@ -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",

View File

@ -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);

View File

@ -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;