Files
Introduction
OpenAI is the artificial intelligence company responsible for services such as ChatGPT. In FME, you can connect to the OpenAI Completions API using the OpenAICompletionsConnector transformer. Just input a text prompt and the transformer will provide the output in JSON format.
This tutorial walks through how to create an FME Workspace that uses the OpenAICompletionsConnector to generate insights from existing customer data. We will bulk-process prompts for each customer and automatically populate a spreadsheet with the results.
Requirements
- To use the OpenAICompletionsConnector, sign up for the OpenAI API, then generate an API key.
- In FME, the OpenAICompletionsConnector is available via FME Hub
Step-by-Step Instructions
This scenario walks through how to use the OpenAICompletionsConnector to update customer data with AI-generated insights. We have basic customer data in a spreadsheet and want to use AI to augment it with more information about customers’ organizations.
The response is returned as JSON, which must be parsed and processed. For this tutorial, we’ll send the output to a new spreadsheet, but you can also create an HTML report or display it in any number of ways. The workspace can also be run by end users as an FME Flow App.
1. Get an OpenAI API Key
If you haven’t already, sign up for the OpenAI API, then generate an API key.
2. Create a New FME Workspace
Open FME Workbench and create a blank workspace.
3. Add a Reader
To kick off the workspace, we’ll need to read the source data and set up the AI prompt. Click Add Reader and set the following parameters:
- Format: Microsoft Excel
- Dataset: <Tutorial download>/customerdata.xlsx
You can click Parameters to preview the source dataset.
4. Add a Decelerator
Add a Decelerator transformer and connect it after the reader feature type. Open the parameters.
OpenAI limits queries to 60 per minute to prevent excessive API requests. Since each of our queries will ask 3 questions, we’ll set the delay to 3 seconds per feature.
Note that this transformer may or may not be necessary, depending on the workspace.
The workspace should look as follows:
5. Add OpenAICompletionsConnector Transformer
Type "OpenAI" on the canvas and select the OpenAICompletionsConnector. Connect it after the Decelerator. Open the parameters. This is where you specify the question you want to ask the AI and select the model you want to use, e.g., davinci. There are many models to choose from, each specialized for different tasks – see the OpenAI API documentation for details.
Set the following parameters:
- API key: <your OpenAI API key generated in step 1>
- Model: text-davinci-003
- Max Tokens: 128
- Prompt:
As a number, how many employees does @Value(Company) have? As a year, what year was @Value(Company) founded? What awards has @Value(Company) won? numemployees|yearfounded|awards
“Max Tokens” defines the size of the answer and prompt – see this Tokenizer tool for an explanation and a helpful guide.
Best practices for the prompt:
- Put each question on its own line, and in the last line, specify how to return the answers (e.g., as a | delimited list)
- Be specific on how you want the answers returned, e.g., “as a year”, “as a two-character month abbreviation”, etc.
Leave the remaining parameters at their default values. Consult the transformer documentation for more information about the parameters. For example, “Temperature” indicates how factual or creative the answers are.
The workspace should look as follows:
6. Add an AttributeSplitter
Add an AttributeSplitter and connect it after the OpenAI transformer. This will split the output into a list, as defined above, delimited by |.
Open the parameters and set the following:
- Attribute to Split: choices{0}.text
- Delimiter or Format String: |
The workspace should look as follows:
7. Add an AttributeManager
Add an AttributeManager to define which parts of the output should be assigned to which attributes/columns in the output spreadsheet.
The previous transformer will return a list called “_list” containing our three answers. Given that, open the parameters and set the following Input Attributes and Output Attributes:
The workspace should look as follows:
8. Add a Writer
Now it’s time to output the data to a new spreadsheet. Add a new writer and set the following parameters:
- Format: Microsoft Excel
- Dataset: <Tutorial download>/customerdata-new.xlsx
9. Run the Workspace
Run the workspace and view the output by either opening the spreadsheet in Excel or previewing it in the FME Data Inspector.
If you want to ask the end user for input before prompting the AI, configure a user parameter. In the Navigator pane, right-click “User Parameters”, choose “Manage User Parameters…”, and add a new one. Then use this value in the OpenAICompletionsConnector to prompt the AI.