FME Version
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 will walk through how to create an FME Workspace that uses the OpenAICompletionsConnector to generate insights about existing customer data. We will bulk process prompts about each customer and automatically populate a spreadsheet with the output.
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 in FME Workbench 2022.2.1.0 and later.
Step-by-Step Instructions
This scenario will walk through how to use the OpenAICompletionsConnector to update customer data with AI-generated insight. We have basic customer data in a spreadsheet, and we want to use AI to augment the data with more information about the customers’ organizations.
The response comes back in 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 Server 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 prompt for the AI. 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 restricts the number of queries to 60 per minute; this will ensure we don’t send too many queries to the API. Since each of our queries is going to ask three questions, we’ll set the delay to 3 seconds per feature.
Note 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 choose 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 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 rest of the parameters as their default values. Consult the transformer documentation for more information about the parameters. For example, “Temperature” defines how factual or creative the answers are, etc.
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, which we defined above to be a list 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 give us a list called “_list” with our three answers as the list items. 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, either by 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.
Comments
0 comments
Please sign in to leave a comment.