In the previous section, we hardcoded Malaysia into the Mediastack Node’s input. But what if we want users to specify their own news filters dynamically using natural language?
Goal
We will modify our workflow so that:
The user inputs a query like “latest news in Japan today”.
ChatGPT extracts structured details from the input (e.g., country, category, keyword).
The extracted details are passed to the Mediastack Node, dynamically filtering news.
Step 1: Add a New ChatGPT Node for Information Extraction
We need a new ChatGPT node to extract structured data from the user’s input before passing it to the Mediastack Node.
New workflow setup with only ChatOpenAI node
1
Let's remove the connection of input and output node, instead connect it to another ChatOpenAI Node
2
Connect the Input Node’smessage output to the new ChatGPT Node’smessage input.
3
Test it by sending the message:
“latest news in Malaysia today”
Right now, ChatGPT doesn’t understand what to do, because we haven’t given it proper instructions.
Step 2: Instruct ChatGPT to Extract Information
To make ChatGPT extract information, we need to provide a system instruction. However, the ChatGPT Node only has one input, but we need to send both a system instruction and user query. In this case, we use JavaScript Node combines them into a single string, ensuring ChatGPT processes the request properly.
1
Add a JavaScript Node
Double-click an empty space in the editor and search for JavaScript to quickly add it.
Name it "System Prompt" for clarity.
Define two inputs:
User Input (user - string) → This will hold the user’s message.
System Prompt (system - string) → This will contain the instruction:
"Extract the country, category, and keyword from the following user request"
Define one output (output - string).
2
Modify the JavaScript Code
Configuring Javascript Node
Inside the Function Code of the JavaScript Node, enter the following:
This ensures that ChatGPT receives the prompt in a structured format:
3
Link the Inputs & Outputs
Connect:
The user’s message from the Input Node to the User Input of the JavaScript Node.
A Text Area Node containing the system instruction to the System Input of the JavaScript Node.
The JavaScript Node's output to the message input of the ChatGPT Node.
4
Test the Setup
Workflow integrated with Javascript Node
Type: "latest news in Malaysia today"
ChatGPT should now able to extract the information
Step 3: Using a JSON Schema for ChatGPT Extraction
In the previous step, we saw that the extracted data was incorrect
This happens because ChatGPT doesn’t know exactly how to format its response based on the Mediastack API requirements.
To fix this, we will provide a JSON Schema to standardize the extracted data format.
system: Extract the country, category, and keyword from the following user request
user: latest news in Malaysia today
country: Malaysia
category: news
keyword: latest news today
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"sources": {
"type": "string",
"description": "Use this parameter to include or exclude one or multiple comma-separated news sources. Example: 'cnn,-bbc' to include CNN and exclude BBC.",
"pattern": "^[a-zA-Z0-9,-]+$"
},
"categories": {
"type": "string",
"description": "Use this parameter to include or exclude one or multiple comma-separated news categories. Example: 'business,-sports' to include business and exclude sports.",
"pattern": "^[a-zA-Z,-]+$"
},
"countries": {
"type": "string",
"description": "Use this parameter to include or exclude one or multiple comma-separated countries. Example: 'au,-us' to include Australia and exclude the US.",
"pattern": "^[a-zA-Z,-]+$"
},
"languages": {
"type": "string",
"description": "Use this parameter to include or exclude one or multiple comma-separated languages. Example: 'en,-de' to include English and exclude German.",
"pattern": "^[a-zA-Z,-]+$"
},
"keywords": {
"type": "string",
"description": "Use this parameter to search for sentences or exclude words. Example: 'new movies 2021 -matrix' to search for 'New movies 2021' but exclude 'Matrix'."
},
"date": {
"type": "string",
"description": "Use this parameter to specify a date or date range. Examples: '2020-01-01' for a specific date or '2020-12-24,2020-12-31' for a date range.",
"pattern": "^\\d{4}-\\d{2}-\\d{2}(,\\d{4}-\\d{2}-\\d{2})?$"
},
"sort": {
"type": "string",
"description": "Use this parameter to specify a sorting order. Available values: 'published_desc' (default), 'published_asc', 'popularity'.",
"enum": [
"published_desc",
"published_asc",
"popularity"
],
"default": "published_desc"
},
"limit": {
"type": "integer",
"description": "Use this parameter to specify a pagination limit (number of results per page). Default is 25, maximum allowed is 100.",
"minimum": 1,
"maximum": 100,
"default": 25
},
"offset": {
"type": "integer",
"description": "Use this parameter to specify a pagination offset value. Default is 0, starting with the first available result.",
"minimum": 0,
"default": 0
}
},
"additionalProperties": false
}
Extract the mediastack data from the following user request