Parsing natural language to structure output
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?
Last updated
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?
Last updated
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.
We need a new ChatGPT node to extract structured data from the user’s input before passing it to the Mediastack Node.
Let's remove the connection of input and output node, instead connect it to another ChatOpenAI Node
Connect the Input Node’s message
output to the new ChatGPT Node’s message
input.
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.
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.
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).
Modify the JavaScript Code
Inside the Function Code of the JavaScript Node, enter the following:
This ensures that ChatGPT receives the prompt in a structured format:
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.
Test the Setup
Type: "latest news in Malaysia today"
ChatGPT should now able to extract the information
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.
We generated the JSON Schema for the Mediastack Live News API using ChatGPT and the official JSON Schema standards (json-schema.org).
Here is an example JSON Schema that enforces the correct format:
Add a "Const Data" Node to store the JSON Schema.
Double-click an empty space in the editor, search Data, and add it.
Paste the JSON Schema into the Const Data Node.
Modify the System Prompt
Change the prompt in the Text Area Node to:
Connect the Schema to the ChatGPT Node
Link the Const Data (JSON Schema Node) to the Schema input of the ChatGPT Node.
Now, ChatGPT will refer to the schema before generating the extracted response.
Next, we’ll connect the structured data to the Mediastack API and dynamically fetch news based on user input!
Since ChatGPT outputs JSON as a string instead of an actual JSON object. We need to parse the stringified JSON before passing it to Mediastack
Create a JavaScript Node to Parse JSON
Since ChatGPT outputs JSON as a string instead of an actual JSON object. We need to parse the stringified JSON before passing it to Mediastack
Double-click an empty space in the editor and search for JavaScript.
Rename it to "Parse JSON" for clarity.
Set Code, Inputs and Outputs accordingly.
So now we have successfully create a agent that reply with news based on user's prompt.
Here is the exported JSON file containing the full workflow setup. You can import this into Editor to instantly recreate the agent.