Skip to content

Instantly share code, notes, and snippets.

@ExcelRobot
Last active May 25, 2024 02:04
Show Gist options
  • Save ExcelRobot/4319e3c81f6fc8145aa845e908aeacb0 to your computer and use it in GitHub Desktop.
Save ExcelRobot/4319e3c81f6fc8145aa845e908aeacb0 to your computer and use it in GitHub Desktop.
RegEx Expression From Generative AI LAMBDA
/*
Name: RegEx Expression From Generative AI (RegExFromAI)
Description: Given a natural language input, uses Excel Labs' Generative AI function to return a RegEx expression. Optionally uses Excel's new REGEXEXTRACT function to check whether the RegEx generated works on a provided example and expected value.
Parameters:
input - natural language prompt like: extract email address
example - optionally provide some example data to help train the AI
expected - optionally provide the expected value to be returned given the example data
Source: Excel Robot (@ExcelRobot)
*/
RegExFromAI =LAMBDA(input, [example], [expected], LET(
\\LambdaName, "RegExFromAI",
\\CommandName, "RegEx Expression From Generative AI",
\\Description, "Given a natural language input, uses Excel Labs' Generative AI function to return a RegEx expression. Optionally uses Excel's new REGEXEXTRACT function to check whether the RegEx generated works on a provided example and expected value.",
\\Parameters, {
"input","natural language prompt like: extract email address";
"example","optionally provide some example data to help train the AI";
"expected","optionally provide the expected value to be returned given the example data"
},
\\Source, "Excel Robot (@ExcelRobot)",
_SystemPrompt, "You are a Regex robot. Given a natural language input, you will respond with ONLY a valid RegEx expression. Do not return your response in quotes. "
& IF(
ISOMITTED(example),
"",
"You will be given Example Data of the kind of data your RegEx expression will be processing. "
)
& IF(
ISOMITTED(expected),
"",
"You will be shown Expected Output of what the expected return value of your RegEx expression might look like. Use the natural language Input to determine the logic of the RegEx and only use the Expected Output to check your work."
),
_UserPrompt, "Input: " & input & CHAR(10)
& IF(ISOMITTED(example), "", "Example Data: " & example & CHAR(10))
& IF(ISOMITTED(expected), "", "Expected Output: " & expected & CHAR(10))
& "Regex Expression: ",
_RegEx, LABS.GENERATIVEAI(_SystemPrompt & CHAR(10) & _UserPrompt),
_Result, IF(
OR(ISOMITTED(example), ISOMITTED(expected)),
_RegEx,
IF(
IFERROR(REGEXEXTRACT(example, _RegEx), "") = expected,
_RegEx,
"RegEx didn't work as expected: " & _RegEx & ". Recalc to try again."
)
),
_Result
));
@ExcelRobot
Copy link
Author

Added ability to provide example data and expected output to be used to 1) train the AI, and 2) check the results to make sure the RegEx actually works.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment