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

Installing Excel Labs and Configuring the Generative AI Feature

Step 1: Installing Excel Labs

  1. Open Excel:
    Launch Microsoft Excel on your computer.

  2. Access the Add-ins Store:

    • Click on the Insert tab in the ribbon.
    • Click on Get Add-ins.
  3. Search for Excel Labs:

    • In the Office Add-ins store, use the search bar to look for Excel Labs.
    • Click on the Add button next to Excel Labs to install it.
  4. Launch Excel Labs:

    • After installation, you will find Excel Labs in the Insert tab.
    • Click on Excel Labs to open it.

Step 2: Configuring the Generative AI Feature

Getting an OpenAI API Key

  1. Sign Up or Log In:

    • Go to the OpenAI website and sign up for an account or log in if you already have one.
  2. Access the API Key:

    • Navigate to the API section in your OpenAI account.
    • Create a new API key if you don’t have one. Make sure to copy this key; you will need it for the next steps.

Configuring the API Key in Excel Labs

  1. Open Excel Labs:

    • Go to the Insert tab and click on Excel Labs.
  2. Navigate to Settings:

    • In the Excel Labs pane, click on the Settings gear icon.
  3. Enter the API Key:

    • Find the Generative AI settings section.
    • Paste your OpenAI API key into the designated field.
  4. Save the Configuration:

    • Click on Save or Apply to store your API key configuration.

Step 3: Using the LABS.GENERATIVEAI Function

  1. Insert a Formula:

    • Click on a cell where you want to use the generative AI function.
    • Enter the formula =LABS.GENERATIVEAI("Your prompt here").
  2. Run the Function:

    • Press Enter. Excel Labs will use the configured API key to process your prompt and return the generated output.

Additional Tips

  • API Usage:
    Be aware of the usage limits and costs associated with the OpenAI API. Monitor your usage in the OpenAI dashboard.

  • Experiment with Prompts:
    Try different prompts to see how the generative AI responds and tailor it to suit your needs.

By following these steps, you should be able to install Excel Labs, configure the Generative AI feature, and start using it effectively within Excel.

@ExcelRobot
Copy link
Author

image

@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