Skip to content
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

[BUG] Reuse IfElseFunction returnValue with getVariable - Unexpected Token XY in ChatPromptTemplates promptValues #1887

Closed
sirsimonson opened this issue Mar 6, 2024 · 2 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@sirsimonson
Copy link
Contributor

sirsimonson commented Mar 6, 2024

Describe the bug
I was using a Custom Function at the start of my flow to retrieve a variable injected via chatflowConfig named "context".
Inside this variable there was some text (markdown formatted) and some stringified objects (schema-org) that got extracted from webpage dynamically. This has worked great.

In order to black list some values I decided to replace this functionality with an IfElse Function. This hasn't worked.

When looking into the code I found that "handleEscapeCharacters" is not called for an IfElse Function's returnValue. I've seen that some weeks ago something similar has been fixed on Custom Function so I wonder why this wasn't applied to IfElse Function. I prepared a Pull-Request to fix this issue. I've tested it inside my IfElse Function, too.

@HenryHengZJ Do you want me to create a PR? The solution would be to call handleEscapeCharacters if the IfElse Response output is type of string

See:

image

To Reproduce
Steps to reproduce the behavior:

  1. This has worked:
    image
var context = $vars.context;
return String(context || 'N/A');
  1. This hasn't worked: Unexpected Token XY in ChatPromptTemplates promptValues
    image
var isNotBlacklisted = true;
var NO_CONTEXT = 'N/A';
var context = $vars.context || NO_CONTEXT;
var isValid = isNotBlacklisted && context !== NO_CONTEXT;

if (isValid) {
  console.log(context);
  return String(context); 
}
  1. This fixed it:
var NO_CONTEXT = 'N/A'
var context = $vars.context || NO_CONTEXT;
var isValid = isNotBlacklisted && context !== NO_CONTEXT;

const jsonEscapeCharacters = [
    { escape: '"', value: 'FLOWISE_DOUBLE_QUOTE' },
    { escape: '\n', value: 'FLOWISE_NEWLINE' },
    { escape: '\b', value: 'FLOWISE_BACKSPACE' },
    { escape: '\f', value: 'FLOWISE_FORM_FEED' },
    { escape: '\r', value: 'FLOWISE_CARRIAGE_RETURN' },
    { escape: '\t', value: 'FLOWISE_TAB' },
    { escape: '\\', value: 'FLOWISE_BACKSLASH' }
]

function handleEscapesJSONParse(input, reverse) {
    for (const element of jsonEscapeCharacters) {
        input = reverse ? input.replaceAll(element.value, element.escape) : input.replaceAll(element.escape, element.value)
    }
    return input
}

function iterateEscapesJSONParse(input, reverse) {
    for (const element in input) {
        const type = typeof input[element]
        if (type === 'string') input[element] = handleEscapesJSONParse(input[element], reverse)
        else if (type === 'object') input[element] = iterateEscapesJSONParse(input[element], reverse)
    }
    return input
}

function handleEscapeCharacters(input, reverse) {
    const type = typeof input
    if (type === 'string') return handleEscapesJSONParse(input, reverse)
    else if (type === 'object') return iterateEscapesJSONParse(input, reverse)
    return input
}

if (isValid) {
  console.log(context);
  return handleEscapeCharacters(String(context), false); 
}
@HenryHengZJ
Copy link
Contributor

@sirsimonson sounds good! go ahead with the PR. Yes in CustomFunction we have this

@sirsimonson
Copy link
Contributor Author

If it's not missing for a reason then I'm pretty sure this got overseen. :) Appreciate to contribute that tiny piece. Using Flowise for some months now and really appreciate those valuable constant improvements that arrive, great work!

HenryHengZJ added a commit that referenced this issue Mar 11, 2024
…ters-in-ifelse

fix(utilities): Handle escape characters in ifelse-function node's return value, resolves #1887
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants